時間遅れが必要なことが多いですが、ただ単に無駄にループで回して無駄時間を作るんでは無しに、他の仕事をしながら時間遅れを作るのって、結構面倒なんですよね。そこで汎用で使えるタイマーファンクションを作りました。

こちらをごらんください。

Time delay is often required. It is easy to make a time delay by loops doing nothing. However with this way other task cannot be performed simply CPU power is wasted. It is quite cumbersome to write such a program that does not disturb other tasks. So I made a general purpose timer function.

Look at here for sample code.

 

#include "timer.h"
#define sync_est_cnt (100) //Timer set value
volatile uint8_t Sync_Flag; //Timer request flag
volatile uint8_t TC0_int_flag; //T/C interruption flag
volatile uint8_t Sync_est;   //Time up flag
volatile uint16_t sync_t_count; //TImer counter

ISR(TIMER0_OVF_vect)
{
TC0_int_flag = 1; // Set TC0 interruption flag
.
.
.
}


void main(void)
{
.
.
Sync_est = timer(sync_est_cnt,Sync_Flag,&TC0_int_flag,&sync_t_count);
.
.

}

Turn ON a timing flag for this timer (TC0_int_flag) in a T/C interruption routine. Call the functions as shown in main routine. When start flag (Sync_Flag) is turned ON, the timer function counts number of T/C interruption. When the count reaches to the set value in (sync_est_cnt), the output of the function turns ON. Of course the main routine must run faster than T/C interruption to have this timer work properly.

T/Cの割り込みルーチンの中でフラッグ(TC0_int_flag)を立てて、メインルーチンにこのファンクションを左のように置き、Sync_FlagをONにすれば、sync_est_cntにセットした回数だけT/Cの割り込みルーチンが走った時点でタイムアップし、このファンクションの出力がONになります。もちろんメインルーチンがT/Cの割り込み間隔以内で周っていることが前提です。T/Cの割り込みの方が早い時はこのファンクションをT/Cの割り込みルーチンの中におく必要があります。


 

 
inserted by FC2 system