📄 timer.c
字号:
//Timer.c
#include <p33FJ64GP306.h>
#include "integer.h"
//there are button timer routine here
#include "button.h"
//implements timer initialize and interrupt
//Flag in main.c
extern BYTE Timer_Flag;
void Timer_Init(void)
{
//Timer 1: main timer. about 0.226ms
//Disable Timer
T1CONbits.TON = 0;
//Select Clock = Prescaled Fcy
T1CONbits.TGATE = 0;
T1CONbits.TCS = 0;
//Initialize Timer Value
TMR1 = 0x0000;
//Prescaler for Fcy = 1/64
T1CONbits.TCKPS = 0b10;
//Comparator Value
PR1 = 100;
//Fcy 28,224,000Hz / 64 / 100 = 4410Hz
//T1IF Gate = Comparator
T1CONbits.TGATE = 0;
//Set Timer 1 interrupt Priority
IPC0bits.T1IP = 0x04;
//Clear Timer 1 Int Flag
IFS0bits.T1IF = 0;
//Enable Timer 1 interrupt
IEC0bits.T1IE = 1;
//Timer 2: Key Button Timer. 10ms.
//Disable Timer
T2CONbits.TON = 0;
//16bit mode
T2CONbits.T32 = 0;
//Select Clock = Prescaled Fcy
T2CONbits.TGATE = 0;
T2CONbits.TCS = 0;
//Initialize Timer Value
TMR2 = 0x0000;
//Prescaler for Fcy = 1/256
T2CONbits.TCKPS = 0b11;
//Comparator Value
PR2 = 1000;
//Fcy 28,224,000Hz / 256 / 1000 = 110.25 Hz Timer
//T2IF Gate = Comparator
T2CONbits.TGATE = 0;
//Set Timer 2 interrupt Priority
IPC1bits.T2IP = 0x06;
//Clear Timer 2 Int Flag
IFS0bits.T2IF = 0;
//Enable Timer 2 interrupt
IEC0bits.T2IE = 1;
//Clear External Timer Flag
Timer_Flag = 0;
//This is Test Pin, for Timer 1
PORTFbits.RF3 = 0;
TRISFbits.TRISF3 = 0;
//Enable Timer
T1CONbits.TON = 1;
T2CONbits.TON = 1;
}
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
//Set Timer Flag
Timer_Flag = 1;
//This is Test Pin, for Timer
PORTFbits.RF3 = 1;
//Clear Timer 1 interrupt Flag
IFS0bits.T1IF = 0;
}
void __attribute__((interrupt, no_auto_psv)) _T2Interrupt(void)
{
//Call Button Check routine;
Read_Buttons();
//Clear Timer 2 interrupt Flag
IFS0bits.T2IF = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -