STC12C5A60S2_Timer0
综述
与常规51单片机相兼容,添加了时钟分频(1t/12t),支持时钟输出。
涉及寄存器
AUXR特殊寄存器
地址: sfr AUXR = 0x8e;
TCON
TMOD
中断
IE&IE2
中断号
WAKE_CLKO
例子
Timer0_16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <reg52.h>
sbit LED1 = P3^0;
sfr AUXR = 0x8e;
unsigned int count;
#define TIMS (65536 - (11059200L/1000)) //TIM0x12
void Timer0_16_Init() { EA = 1; // main interrupt ET0 = 1; TR0 = 1; //TCON TH0 = TIMS>>8; TL0 = TIMS; TMOD |= 0x01; //16bit AUXR =0x80; //TIM0x12 }
void main() { Timer0_16_Init(); while(1); }
void timer0_16 () interrupt 1 using 1 { TH0 = TIMS>>8; //reload TL0 = TIMS; count++; if(count>=1000) { count = 0; LED1 = !LED1; } }
|
Timer0_8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <reg52.h>
sfr AURX = 0x8e;
sbit LED1 = P3^0;
#define TIMS (256-(11059200L/10000/12)) //must be 12t otherwise can not count 1 sec
unsigned int count;
void timer0_8_Init() { EA = 1; ET0 = 1; TR0 = 1; TMOD |= 0x02; TH0 = TL0 = TIMS; AURX = 0x00; } void main() { timer0_8_Init(); while(1); }
void tiemr0_8 () interrupt 1 using 1 { count++; if(count>=10000) { LED1 = !LED1; count = 0; } }
|
Timer0_8_8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <reg52.h>
/*************** T0 Double 8 bit Timer ****************/
sfr AURX = 0x8e;
sbit LED1 = P3^0; sbit LED2 = P3^1;
unsigned int count1; unsigned int count2;
#define TIMS (256-(11059200L/10000/12)) //12t
void timer0_8_8_Init() { EA = 1; ET0 = 1; ET1 = 1; TR0 = TR1 = 1; TMOD = 0x03; // double 8bit mode TH0 = TL0 = TIMS; AURX = 0x00; //T1 T0 12t }
void main() { timer0_8_8_Init(); LED1 = 0; LED2 = 1; while(1); }
void int0() interrupt 1 using 1 { TL0 =TIMS; //reload count1++; if(count1>=10000) //1s { count1=0; LED1 = !LED1; } }
void int1() interrupt 3 using 2 { TH0 = TIMS; //reload count2++; if(count2>=10000) //1s { count2=0; LED2 = !LED2; } }
|
Timer0_8_CLKO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <reg52.h>
sfr AURX = 0x8e;
sfr WAKE_CLKO = 0x8f;
#define TIMS (256-(11059200L/10000/12)) // T = 5KHZ 100us
unsigned int count;
void timer0_8_CLKO_Init() { EA = 1; ET0 = 0; //prohibit Timer0 interrupt TR0 = 1; TMOD |= 0x02; TH0 = TL0 = TIMS; AURX = 0x00; WAKE_CLKO = 0x01; // set T0CLKO P3^4 Timer0 out } void main() { timer0_8_CLKO_Init(); while(1); }
|