PIC16F877A + ultrasonic sensor + motor control

Discussion in 'General Electronics Chat' started by villkomme, May 15, 2012.

  1. villkomme

    Thread Starter New Member

    May 15, 2012
    2
    0
    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.:(

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #include <stdio.h>
    4. __CONFIG(0x3b32);
    5. #define trig RB0
    6. #define echo RB1
    7. void clrscn(void); // prototypes
    8. void setup(void);
    9. unsigned int get_srf04(void);
    10.  
    11.  
    12. void main(void)
    13. {
    14.     TRISA = 0b001111;
    15.  
    16.          TRISC = 0b00000000;
    17.     TRISE = 0b001;    
    18.     PORTA = 0b000000;    
    19.  
    20.          PORTC = 0b00000000;
    21.     PORTE = 0b000;    
    22.    
    23.         PR2 = 124;    
    24.          T2CON= 0b00000101;               //set prescale 4
    25.          CCP1CON = 0b00001100;             //set on ccp1
    26.          CCP2CON = 0b00001100;            //set on ccp2
    27.  
    28.  
    29. unsigned int range;
    30. setup(); // sets up the PIC16F877 I2C port
    31.  
    32.     if (range>75&&range<100)
    33.     {
    34.              CCPR1L = 100;            
    35.              CCPR2L = 100;
    36.              PORTC  = 0b00001000;
    37.            }
    38.  
    39.     else if (range>100&&range<300)
    40.     {
    41.              CCPR1L = 100;            
    42.              CCPR2L = 100;
    43.              PORTC  = 0b00000001;
    44.            }
    45.  
    46. TMR1H = 0; // 52mS delay - this is so that the SRF04 ranging is not too rapid
    47. TMR1L = 0; // and the previous pulse has faded away before we start the next one
    48. T1CON = 0x21; // 1:4 prescale and running
    49. TMR1IF = 0;
    50.  
    51. while(!TMR1IF); // wait for delay time
    52. TMR1ON = 0; // stop timer
    53. }
    54.  
    55.  
    56.  
    57. unsigned int get_srf04(void)
    58. {
    59. TMR1H = 0xff; // prepare timer for 10uS pulse
    60. TMR1L = -14;
    61. T1CON = 0x21; // 1:4 prescale and running
    62. TMR1IF = 0;
    63. trig = 1; // start trigger pulse
    64. while(!TMR1IF); // wait 10uS
    65. trig = 0; // end trigger pulse
    66. TMR1ON = 0; // stop timer
    67. TMR1H = 0; // prepare timer to measure echo pulse
    68. TMR1L = 0;
    69. T1CON = 0x20; // 1:4 prescale but not running yet
    70. TMR1IF = 0;
    71. while(!echo && !TMR1IF); // wait for echo pulse to start (go high)
    72. TMR1ON = 1; // start timer to measure pulse
    73. while(echo && !TMR1IF); // wait for echo pulse to stop (go low)
    74. TMR1ON = 0; // stop timer
    75. return (TMR1H<<8)+TMR1L; // TMR1H:TMR1L contains flight time of the pulse in 0.8uS units
    76. }
    77.  
    78.  
    79.  
    80. void setup(void)
    81. {
    82. unsigned long x;
    83. TRISB = 0xfe; // RB0 (trig) is output
    84. PORTB = 0xfe; // and starts low
    85. TRISC = 0xff;
    86. PORTC = 0xff;
    87. SSPSTAT = 0x80;
    88. SSPCON = 0x38;
    89. SSPCON2 = 0x00;
    90. SSPADD = 50; // SCL = 91khz with 20Mhz Osc
    91. for(x=0; x<300000L; x++); // wait for LCD03 to initialise
    92. }
    93.  
     
    Last edited by a moderator: May 15, 2012
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,365
    Where do you ever assign a value to range?
     
  3. villkomme

    Thread Starter New Member

    May 15, 2012
    2
    0
    ok my mistake, but if i do like this

    range=get_srf04()
    it still the same .no result come out
     
Loading...