PIC16F877A + ultrasonic sensor + motor control

Thread Starter

villkomme

Joined May 15, 2012
2
Hi, i need help with my c programming, i am using pic 16f877a and ultrasonic sensor HC-SRO4 module to control direction of motor.
Ultrasonic sensor is connected to port b, with trig pin to rb0 and echo pin rb1,
while motor is connected to RC1 for reverse and RC4 for forward.
RC1 is use for PWM control,
my problem is when i try to compile it it tell me like this
"local variable "_range" is used but never given a value"
why this happen, can somebody fix this to me.:(

Rich (BB code):
#include <pic.h>
#include <stdio.h>
__CONFIG(0x3b32);
#define trig RB0
#define echo RB1
void clrscn(void); // prototypes
void setup(void);
unsigned int get_srf04(void);


void main(void)
{
    TRISA = 0b001111;

         TRISC = 0b00000000;
    TRISE = 0b001;    
    PORTA = 0b000000;    

         PORTC = 0b00000000;
    PORTE = 0b000;    
    
        PR2 = 124;    
         T2CON= 0b00000101;               //set prescale 4
         CCP1CON = 0b00001100;             //set on ccp1
         CCP2CON = 0b00001100;            //set on ccp2


unsigned int range;
setup(); // sets up the PIC16F877 I2C port

    if (range>75&&range<100)
    {
             CCPR1L = 100;            
             CCPR2L = 100;
             PORTC  = 0b00001000; 
           }

    else if (range>100&&range<300)
    {
             CCPR1L = 100;            
             CCPR2L = 100;
             PORTC  = 0b00000001; 
           }

TMR1H = 0; // 52mS delay - this is so that the SRF04 ranging is not too rapid
TMR1L = 0; // and the previous pulse has faded away before we start the next one
T1CON = 0x21; // 1:4 prescale and running
TMR1IF = 0;

while(!TMR1IF); // wait for delay time
TMR1ON = 0; // stop timer
}



unsigned int get_srf04(void)
{
TMR1H = 0xff; // prepare timer for 10uS pulse
TMR1L = -14;
T1CON = 0x21; // 1:4 prescale and running
TMR1IF = 0;
trig = 1; // start trigger pulse
while(!TMR1IF); // wait 10uS
trig = 0; // end trigger pulse
TMR1ON = 0; // stop timer
TMR1H = 0; // prepare timer to measure echo pulse
TMR1L = 0;
T1CON = 0x20; // 1:4 prescale but not running yet
TMR1IF = 0;
while(!echo && !TMR1IF); // wait for echo pulse to start (go high)
TMR1ON = 1; // start timer to measure pulse
while(echo && !TMR1IF); // wait for echo pulse to stop (go low)
TMR1ON = 0; // stop timer
return (TMR1H<<8)+TMR1L; // TMR1H:TMR1L contains flight time of the pulse in 0.8uS units
}



void setup(void)
{
unsigned long x;
TRISB = 0xfe; // RB0 (trig) is output
PORTB = 0xfe; // and starts low
TRISC = 0xff;
PORTC = 0xff;
SSPSTAT = 0x80;
SSPCON = 0x38;
SSPCON2 = 0x00;
SSPADD = 50; // SCL = 91khz with 20Mhz Osc
for(x=0; x<300000L; x++); // wait for LCD03 to initialise
}
 
Last edited by a moderator:
Top