shubham kumar
Member level 3
- Joined
- Sep 11, 2014
- Messages
- 59
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 8
- Location
- bangalore
- Activity points
- 511
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 void wave_0() { TMR1L=0x0F; TMR1H=0x0F ; TMR1IF_bit=0; T1CON= 0x01; while(TMR1IF_bit==0); TMR1IF_bit=0; } void wave_1() { TMR1L=0; TMR1H=0 ; TMR1IF_bit=0; T1CON= 0x01; whileTMR1IF_bit==0); TMR1IF_bit=0; } void main() { AN0_bit=0; AN1_bit=0; AN2_bit=0; AN3_bit=0; // Configure AN pins as Digital I/O and turn off comparators TRISD.B7=1; TRISD.B6=0; do{ PORTD.B6 = ~ PORTD.B6; if( TRISD.B7==1) { wave_0(); } // Delay_ms(500); if( TRISD.B7==0) { wave_1(); } // Delay_ms(500); } while(1); }
Tell me if any other method exists.
I did not checked this on datasheet, but if the uC has a PWM module, you could do that with a minimal code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 main(){ --set timer values while(1) { PORTB= PORTB; --start timer while(TMR1IF_bit==0); // kind of wait statement TMR1IF_bit=0; } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 void waves(unsigned int TMR1Value) { TMR1H=(TMR1Value & 0xFF00) >> 8; TMR1L=TMR1Value & 0x00FF ; TMR1IF_bit=0; T1CON= 0x01; while(TMR1IF_bit==0); TMR1IF_bit=0; }
just as a delay by observing the timer flag
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 main(){ PORTB = 0; //set timer0 module here with 64ms or desired Interval TMR0IF = 0; // clear TMR0 flag TMR0IE = 1; // enable TMR0 // don't enable GIE bit to avoid to go to interrupt routine while(1){ if(TMR0IF){ // if timer0 flag RB0 = !RB0; TMR0IF = 0; } } // end while } // end main
Apart from possible alternatives, the OP is essentially asking how a polled timer delay can be implemented with PIC18.
The answer is, yes, it works exactly as in your code (or the similar code suggested betwixt). So if you don't get the expected result, there's apparently a problem with the timer setup. It's not shown in your code snippet, so we can't know if it's right.
The sample code posted here is for PIC16 family. He can update this code for his PIC18 family.Apart from possible alternatives, the OP is essentially asking how a polled timer delay can be implemented with PIC18.
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?