This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ADC for windshield wiper, Error C67: Sensor_value undefined identifier

Hey guys hows it going
Im working on a programming assignment where we have to write a program to control windshield wipers, and im having some trouble compiling it. The code seems fine to me, but in the main.c file, it keeps bringing up this error about sensor_value being an undefined identifier. Can you guys help me out? Here is the code:

/****************/
/* MAIN PROGRAM */
/****************/

#include <reg167.h> /* special function registers of the C167 */
#include <stdio.h> /* Standard input/output header file */
#include "adc.h" /* ADC Header File */
#include "pwm.h" /* PWM Header File */
#include "timer.h" /* Timer Header Files */

/* Main Program */
void main(void) {

/* initialize serial interface, 9600 bps, 8-N-1 */
DP3 |= 0x0400; /* PORT 3.10 DIRECTION CONTROL: OUTPUT (TXD) */
DP3 &= ~0x0800; /* PORT 3.11 DIRECTION CONTROL: INPUT (RXD) */
P3 |= 0x0400; /* PORT 3.10 DATA: HIGH (LINE IDLE) */
S0TIC = 0x80; /* SET TRANSMIT INTERRUPT FLAG (TX COMPLETE) */
S0RIC = 0x00; /* CLEAR RECEIVE INTERRUPT FLAG (NOTHING REC.) */
S0BG = 0x40; /* SET BAUDRATE TO 9600 BAUD */
S0CON = 0x8011; /* SET SERIAL MODE */

/* Initialise ADC */
ADC_init();

/* initialise PWM */
PWM_init();

/* Initialise Timer */
Timer_init();

/* Allow interrupts */
IEN=1;

/* Wait for a sweep cycle to end, to avoid starting a new cycle when one is already running */
while (T3 > 0){}

/* Control the wiper motor, depending on return ADC result */
while(1) {

/* load return ADC value into sensor voltage */
float sensor_value = ADC_read()

/* Outputs for various states */
if (sensor_value > 4.5) { PWM_set(1.0); /* Sets the Off-Phase to 100% of the pulse width */ PTR3=0; /* Resets PWM */ T3R=0; /* Sets the timer off */ printf("No Rain Detected!\n"); }

else if (sensor_value <= 4.5 && sensor_value >=1.5) { PWM_set(0.5); /* Sets the Off-Phase to half of the pulse width */ PTR3=1; /* Starts PWM */ T3=0x9860; /* Loads timer value of 2 sec (Prescale value 1024) */ T2=0x960; /* Loads auxiliary timer with 2 seconds */ T3R=1; /* Starts the timer */ printf("Few Showers Detected!\n");

while (T3>0) {} /* Wait for Sweep cycle to cmplete */ PWM_set(1); /* Sets the duty cycle to zero */ T3=(1.67*sensor_value)-2.5; /* Loads a delay timer value proportional to the sensor value */

T2=(1.67*sensor_value)-2.5; /* Loads the auxiliary timer with same value */ T3R=1; /* Starts the timer */ }
else if (sensor_value < 1.5) { PWM_set(0); /* Sets the Off-Phase of the pulse width to zero */ PTR3 =1; /* Starts PWM */ T3=0x4C30; /* timer value of 1 second (Prescale factor of 1024) */ T2=0x4C30; /* Loads the auxiliary timer with 1 second */ T3R =1; /* Starts the timer */ printf("Heavy Rain Detected!\n"); } } }

Parents
  • Do you work with a compiler that supports having new variables within blocks, and not just directly at start of functions?

    By the way - you found the markup for bold text. You didn't see the markup for code? Does your post look readable with free-flowing text instead of using the original line breaks and indentation?

Reply
  • Do you work with a compiler that supports having new variables within blocks, and not just directly at start of functions?

    By the way - you found the markup for bold text. You didn't see the markup for code? Does your post look readable with free-flowing text instead of using the original line breaks and indentation?

Children
  • Sorry didnt notice the markup for code. I'll repost in a better format. As for the compiler, not quite sure but im working with C167 on Keil uvision3.

    #include <reg167.h> /* special function registers of the C167 */
    #include <stdio.h> /* Standard input/output header file */
    #include <adc.h> /* ADC Header File */
    #include <pwm.h> /* PWM Header File */
    #include <timer.h> /* Timer Header Files */
    
    
    /* Main Program */
    void main(void) {
    unsigned int ADC_Result;
    unsigned int sensor_value;
    
    /* initialize serial interface, 9600 bps, 8-N-1 */
    DP3 |= 0x0400; /* PORT 3.10 DIRECTION CONTROL: OUTPUT (TXD) */
    DP3 &= ~0x0800; /* PORT 3.11 DIRECTION CONTROL: INPUT (RXD) */
    P3 |= 0x0400; /* PORT 3.10 DATA: HIGH (LINE IDLE) */
    S0TIC = 0x80; /* SET TRANSMIT INTERRUPT FLAG (TX COMPLETE) */
    S0RIC = 0x00; /* CLEAR RECEIVE INTERRUPT FLAG (NOTHING REC.) */
    S0BG = 0x40; /* SET BAUDRATE TO 9600 BAUD */
    S0CON = 0x8011; /* SET SERIAL MODE */
    
    /* Initialise ADC */
    ADC_init();
    
    /* initialise PWM */
    PWM_init();
    
    /* Initialise Timer */
    Timer_init();
    
    /* Control the wiper motor, depending on return ADC result */
    while(1) {
    
    /* Wait for a sweep cycle to end, to avoid starting a new cycle when one is already running */
    while (T3>0) { };
    
    /* Return result from ADC and load it into ADC_Result */
    ADC_Result=ADC_read();
    
    /* Normalise by eliminating top bits and rescale to 0-5v */
    sensor_value = (ADC_Result & 0x3FF) / 1024 * 5;
    
    /* Outputs for various states */
    if (sensor_value > 4.5) {
    PWM_set(1); /* Sets the Off-Phase to 100% of the pulse width */
    PTR3=0; /* Resets PWM */
    T3R=0; /* Sets the timer off */
    printf("No Rain Detected!\n");
    }
    
    else if (sensor_value <= 4.5 && sensor_value >=1.5) {
    PWM_set(0.5); /* Sets the Off-Phase to half of the pulse width */
    T3=0x9860; /* Loads a timer value of 2 seconds (Prescale value of 1024) */
    T2=0x960; /* Loads auxiliary timer with 2 seconds */
    PTR3=1; /* Starts PWM */
    T3R=1; /* Starts the timer */
    printf("Few Showers Detected!\n");
    
    while (T3>0) { }; /* Wait for Sweep cycle to cmplete */
    PWM_set(1); /* Sets the duty cycle to zero */
    T3=(1.67*sensor_value)-2.5; /* Loads a delay timer value proportional to the sensor value */
    T2=(1.67*sensor_value)-2.5; /* Loads the auxiliary timer with the same reload value */
    T3R=1; /* Starts the timer */
    }
    else if (sensor_value < 1.5) {
    PWM_set(0); /* Sets the Off-Phase of the pulse width to zero */
    T3=0x4C30; /* Loads a timer value of 1 second (Prescale factor of 1024) */
    T2=0x4C30; /* Loads the auxiliary timer with 1 second */
    PTR3 =1; /* Starts PWM */
    T3R =1; /* Starts the timer */
    printf("Heavy Rain Detected!\n");
    }
    }
    }
    
    
    
    
    

  • So - what was the result of moving the declaration of the variable?

  • Well seems that fixed the problem, but another problem presented itself. They all compile alright individualy, but when i try building the target, it return errors L22, L127 and L128, shown below. What do they mean, and how can i fix them?

    Cheers for you help :)

    Build target 'Target 1'
    linking...
    *** WARNING L22: CLASS RANGE NOT GIVEN IN INVOCATION LINE
        CLASS:   NCONST
    *** WARNING L22: CLASS RANGE NOT GIVEN IN INVOCATION LINE
        CLASS:   NCODE
    *** ERROR L127: UNRESOLVED EXTERNAL SYMBOL
        SYMBOL:  PWM_init
        MODULE:  main.obj (MAIN)
    *** ERROR L128: REFERENCE MADE TO UNRESOLVED EXTERNAL
        SYMBOL:  PWM_init
        MODULE:  main.obj (MAIN)
        ADDRESS: 0AC6H
    Program Size: data=520(near=518) const=208(near=208) code=3288
    Target not created
    
    
    
    
    
    

  • I don't use the C166 but google or the manual should help with the meaning of the errors.

    But can't you even guess the meaning of these two?

    *** ERROR L127: UNRESOLVED EXTERNAL SYMBOL
        SYMBOL:  PWM_init
        MODULE:  main.obj (MAIN)
    *** ERROR L128: REFERENCE MADE TO UNRESOLVED EXTERNAL
        SYMBOL:  PWM_init
        MODULE:  main.obj (MAIN)
    

    Where are your PWM_init?