Hi Guys
If I'm using timer interrupt 4 on a PIC18F46K80 which is set to 125 Hz how do i work out how many times it will need to call the macro to give me a 1 second pulse? I worked out about 8000 but when i tried it and tried to count to 30 minutes (1800 seconds) it was not very accurate. Any help will be much appreciated
Timer Interrupt
-
- Posts: 129
- http://meble-kuchenne.info.pl
- Joined: Mon Mar 08, 2021 11:25 am
- Location: Cape Town South Africa
- Has thanked: 40 times
- Been thanked: 11 times
-
- Matrix Staff
- Posts: 1353
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 182 times
- Been thanked: 317 times
Re: Timer Interrupt
An interrupt at 125 Hz means it is being called 125 times each second. If you use a global variable initially set to zero and incremented each time within your interrupt routine, then when it reaches 125 then that should be 1 second.
Remember to keep your interrupt routine as short as possible. For example, only increment the variable within the interrupt routine itself. Then, in a convenient place in your program's main loop (i.e. somewhere that is frequently executed), you can check this variable and if it has reached 125 then you know 1 second has elapsed. As this point, reset it back to zero and process anything that should happen when this time has elapsed.
Remember to keep your interrupt routine as short as possible. For example, only increment the variable within the interrupt routine itself. Then, in a convenient place in your program's main loop (i.e. somewhere that is frequently executed), you can check this variable and if it has reached 125 then you know 1 second has elapsed. As this point, reset it back to zero and process anything that should happen when this time has elapsed.