タイマー1を使用した10msを正確にカウントするタイマーです。
※2011/11/15 カウンター値を修正しました。目的のカウント値はマイナス1したカウンター値を使用します。MPSIMで計測すると、マイナス1しないカウンター値で合っている様に見えますが、それはバグですので注意します。
#define KHz 000
#define _XTAL_FREQ 12800KHz
unsigned int True_Timer_10ms =0 ; //10millisecond counter ***1.40
~
void interrupt handler( void ){
if (CCP1IF == 1){
++ True_Timer_10ms;
CCP1IF =0;
}
return;
}
~
void main(void){
//Timer1 settings
TMR1CS = 0; //Use internal 1/4 clock (3.2MHz 312.5ns)
T1CKPS0 = 1;
T1CKPS1 = 1; //Divide per 8 (400KHz 2.5us)
T1SYNC = 0; //Synchronize
TMR1ON = 1; //Start TIMER1
TMR1L = 0; //TIMRT1 clear
TMR1H = 0; //TIMRT1 clear
~
//1ms 400counts(0x018f) 2011/11/15修正
//10ms 4000 counts (0x0f9f) 2011/11/15修正
CCP1CON = 0B00001011; //Compare clear if equal
CCPR1L = 0x9f; // マイナス1したカウンター値を使用する。MPSIMでの計測にバグがあるので注意。
CCPR1H = 0x0F; // 10ms setting
CCPR2L = 0xff; // not used
CCPR2H = 0xff; // not used
~
CCP1IF = 0;
CCP1IE = 1;
PEIE = 1; //Use Peripheral interrupt
GIE = 1; //Use General interrupt
~
while(1){
True_Timer_10ms;//これをカウントして利用できます。グローバル変数
~
}
}
16F877A timer1による10ms @12.8MHzのサンプル
- air_variable
- URL
- 2011/09/28 (Wed) 11:36:13