![]() |
timer interrupt help
Hey
what im tryin to do is set up a timer to overflow every2 ms so this code will sample an ecg wave at 500Hz. Cant get my head around how to do this and any help would be greatly appreciated. Thanks Code:
$MOD52 |
Re: timer interrupt help
Timers are usually pretty complicated, loaded with features. For the most simple of uses, you will need to calculate how many clock cycles constitute two milliseconds. Depending on the processor, this might not be period*frequency. The 18F PICs, for example, operate at 1/4th the oscillator frequency. For a 32MHz oscillator, then, the PIC will operate at 8MHz. 0.002*8MHz is 16000 cycles. Choose a 16-bit timer, and you will need to load it with 0xFFFF - 16000 = 49535. Then it will overflow in 16000 counts, or 2 ms.
Simple idea with polled interrupts: 0) make sure the interrupt for this timer is not active. It should reset this way if your program isn't doing anything else you've not mentioned. If it is doing other things, be sure you're not using it for something else, or choose some other timer. 1) stop the timer. Load 2ms_count into the timer. Clear the interrupt flag. Start the timer. (check your datasheet for the exact order of operations.) 2) wait for overflow flag to be set. If not set, goto 2. 3) start ADC conversion. This usually takes some time, maybe you can get away with a small nop-loop. 4) read the value and do whatever with it. 5) goto 1. |
| All times are GMT +5.5. The time now is 14:18. |