khangzing
Full Member level 2
- Joined
- Feb 22, 2013
- Messages
- 123
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Activity points
- 2,148
Code:while(1) { generate_square(); // in 38 khz delay_ms(120); output_low(); // IR led in Off state delay_ms(380); // 380+120 = 500ms period }
- - - Updated - - -
use this procedure to generate square wave...
Now look at the recevier output oFF time that matters with distance.....
that can be found easily
- - - Updated - - -
Code:while(1) { for( i = 0 ; recieve == 0; i++) // Count for number of ms the output is low delay_ms(1); // i is the distance ouput may be for 1m i = 75; 1.5m i = 25; while(recieve == 1); }
- - - Updated - - -
They are not meant to change the duty cycle, They are meant to set a constant square wave for some time.....
Hey are you joking, how did you got that 10 to 15 cms???
- - - Updated - - -
You are generating square wave... and after finishing whole cycle you are getting input... both things must be mixed for the operation..... You need at least one timer interrupt...
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 #include <pic.h> #define _XTAL_FREQ 20000000 #define OFFSET 80 // You can vary this and try //Timer0 //Prescaler 1:1; TMR0 Preload = 124; Actual Interrupt Time : 26.3 us void pwm_init(void); void timer_init(void); void interrupt Timer0_ISR(void); void display(void); int i,j; void main() { int i; pwm_init(); timer_init(); generateSquare(); while(1) { for(i=0;RD==0;i++) __delay_ms(1); if(i != 0) display(); else RD1 = RD0 = 1; // noting got in reception while( RD == 1 ); } } // modify timer isr with "if".. If you got compiler warning.. // // // void interrupt Timer1_ISR(void) { // check and reset interrupt flag if( ++j > 2 ) j = 0; switch ( j ) { case 0: case 1: CCPR1L =0; break case 2: CCPR1L = 65; //Set duty cycle for approx 50% break; } // Do timer Reloadings } // // // void timer_init(void) { //Configure timer 1 to interrupt every 200ms and start } // I didnt look inside this function // // void pwm_init(void){ TRISD = 0X07; TRISC = 0X00; //configure Rc2 as output for Emitter(pwm) CCP1CON = 0X0C; //select PWM mode. PR2 = 131; //Set period register for approx. 38khz. CCPR1L = 65; //Set duty cycle for approx 50% T2CON = 0X04; //Set timer2 pre/post scalers to 1 & start Timer2. } // To find the i value If you got nothing try changing OFFSET // // void display(void) { RD1 = RD2 = 0; if(i<OFFSET) RD1=1; else RD2=1; __delay_ms(400); RD1 = RD2 = 0; }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?