it will depend on your chip's hardware capabilities.
with a fast chip, you can simply use one counter and perform numerous test within the isr:
tmr_isr() {
SysTick+=1; //increment systick counter
for (i=0; i<TMR_MAX; i++) tmr[i].flag = (SysTick % tmr[i].period)?0:1; //set the flag if overflow
}
in your application, tmr.flag will be set if its period has passed.
Because of the '%' operator, this approach is not workable on a slow mcu to drive lots of software timers.
the 2nd approach would be to increment and test individual timers in the isr:
tmr_isr() {
for (i=0; i<TMR_MAX; i++) {
tmr[i].counter+=1; //increment timer;
tmr[i].flag=(tmr[i].counter == tmr[i].period)?1:0; //set the flag
}
}
if even the testing is too much, and your main loop is fast enough, you can put the testing portion of it over there.
generally, it will take you about 10 ticks to perform the various steps so if you are talking about 1ms isr period, you can do at most 100 software timers on a 1MIPS mcu, and practically 10 - 20 software timers if you want your mcu to perform other tasks.
with a fast chip, you can simply use one counter and perform numerous test within the isr:
tmr_isr() {
SysTick+=1; //increment systick counter
for (i=0; i<TMR_MAX; i++) tmr[i].flag = (SysTick % tmr[i].period)?0:1; //set the flag if overflow
}
in your application, tmr.flag will be set if its period has passed.
Because of the '%' operator, this approach is not workable on a slow mcu to drive lots of software timers.
the 2nd approach would be to increment and test individual timers in the isr:
tmr_isr() {
for (i=0; i<TMR_MAX; i++) {
tmr[i].counter+=1; //increment timer;
tmr[i].flag=(tmr[i].counter == tmr[i].period)?1:0; //set the flag
}
}
if even the testing is too much, and your main loop is fast enough, you can put the testing portion of it over there.
generally, it will take you about 10 ticks to perform the various steps so if you are talking about 1ms isr period, you can do at most 100 software timers on a 1MIPS mcu, and practically 10 - 20 software timers if you want your mcu to perform other tasks.
一周热门 更多>