Author Topic: SRF05 sensor C18 code  (Read 1968 times)

0 Members and 1 Guest are viewing this topic.

Offline daisordanTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
SRF05 sensor C18 code
« on: March 04, 2011, 04:24:36 PM »
I am currently using PIC18F4455 chip to write a C18 code with SRF05 sensor. After i finish my code, it doesn't work. If i try to run it one by one like just run the "trig_pulse", RD2 will output a pulse to the sensor. But now if i try to run them together, there is no respond on the sensor. RD2 no output signal and RD3 no feedback. What is the program with my code?Do i have to use interrupt? I have look through many example of interrupt but still dun understand how to do it . It will be very helpful if someone can give me a hand on it :(..Many Thanks!!

unsigned char distance;
void main (void){
   TRISD=0b00001000;      //set RD3(Echo)=input,RD2(Trigger)=output,RD6(servo)=output
while(1){   
   set_timer1();
   while(1){   
   trig_pulse();         //send pulse to trigger
   while(!PORTDbits.RD3);          //wait Echo goes high
   measure_pulse();         
   distance = TMR1H + TMR1L;   
   distance /= 58;         
}
}
void trig_pulse (void){
   LATDbits.LATD2 = 1;   
   Delay1TCY();      
   Delay1TCY();
   Delay1TCY();      
   LATDbits.LATD2 = 0;   
   Delay1KTCYx(10);
}
void measure_pulse (void){
   PIR1bits.TMR1IF = 0;   
   T1CONbits.TMR1ON = 1;   
   while(PORTDbits.RD3);   //wait Echo goes low
   T1CONbits.TMR1ON = 0;   
}
void set_timer1(void){   
            OpenTimer1(TIMER_INT_ON &   //interrupts enable
            T1_8BIT_RW &   
            T1_SOURCE_INT &
            T1_PS_1_1 );
            INTCONbits.GIEH = 1;            
               }

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,941
  • Helpful? 98
Re: SRF05 sensor C18 code
« Reply #1 on: March 04, 2011, 04:40:14 PM »
To learn how to use interrupts and the other feature in PIC do this tutorial:
http://www.gooligum.com.au/tutorials.html

Offline daisordanTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: SRF05 sensor C18 code
« Reply #2 on: March 06, 2011, 04:12:37 PM »
thx for ur link
i have go through all the topic and i think i did all the things he say in the interrupt topic.. Like "set_timer1" this part..
wt else should i write here?

Offline mugur

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: SRF05 sensor C18 code
« Reply #3 on: September 12, 2013, 09:51:46 AM »
Your code is a bit messed up. Here you will find a code written for the PIC16 that can be easily adapted for PIC18. I hope to be helpful.

http://rodroiddev.blogspot.ro/

 


Get Your Ad Here

data_list