Skip to content

Instantly share code, notes, and snippets.

@wbchn
Created May 22, 2016 11:55
Show Gist options
  • Save wbchn/7ee9b5a6a0a803257aeb6a39b87fc14b to your computer and use it in GitHub Desktop.
Save wbchn/7ee9b5a6a0a803257aeb6a39b87fc14b to your computer and use it in GitHub Desktop.
STC12C5A-Timer
/* define constants */
#define FOSC 11059000L // 11.059 MHz
// #define T1MS (65536-FOSC/1000) //1ms timer calculation method in 1T mode
#define T1MS_H 0xD4
#define T1MS_L 0xCC
/* define variables */
WORD count = 1000; //1000 times counter
WORD timestamp = 0;
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
TL0 = T1MS_L; //reload timer0 low byte
TH0 = T1MS_H; //reload timer0 high byte
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
++timestamp;
}
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = T1MS_L;
TH0 = T1MS_H;
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //enable timer0 interrupt
}
// enable interrupt in main()
// EA = 1; //open global interrupt switch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment