5个定时器就这样用
- /*---------------------------------------------------------------------*/
- /* --- STC MCU Limited ------------------------------------------------*/
- /* --- STC15F4K60S4 系列 定时器0的16位自动重装载模式举例---------------*/
- /* --- STC15F4K60S4 系列 定时器1的16位自动重装载模式举例---------------*/
- /* --- STC15F4K60S4 系列 定时器2的16位自动重装载模式举例---------------*/
- /* --- STC15F4K60S4 系列 定时器3的16位自动重装载模式举例---------------*/
- /* --- STC15F4K60S4 系列 定时器4的16位自动重装载模式举例---------------*/
- /* --- Mobile: (86)13922805190 -------------- -------------------------*/
- /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
- /* --- Tel: 86-0513-55012928,55012929,55012966-------------------------*/
- /* --- Web: www.STCMCU.com --------------------------------------------*/
- /* --- Web: www.GXWMCU.com --------------------------------------------*/
- /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
- /* 如果要在文章中应用此代码,请在文章中注明使用了STC的资料及程序 */
- /*---------------------------------------------------------------------*/
- //本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
- //若无特别说明,工作频率一般为11.0592MHz
- #include "reg51.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- //-----------------------------------------------
- #define FOSC 11059200L
- #define T0_1MS (65536-FOSC/1000) //1T模式
- //#define T0_1MS (65536-FOSC/12/1000) //12T模式
- #define T1_2MS (65536-FOSC/2000) //1T模式
- //#define T1_2MS (65536-FOSC/12/2000) //12T模式
- #define T2_3MS (65536-FOSC/3000) //1T模式
- //#define T2_3MS (65536-FOSC/12/3000) //12T模式
- #define T3_38_4KHz (256-18432000/38400/2) //1T模式
- //#define T3_38_4KHz (256-18432000/12/38400/2) //12T模式
- #define T4_38_4KHz (256-18432000/38400/2) //1T模式
- //#define T4_38_4KHz (256-18432000/12/38400/2) //12T模式
- sfr P0M1 = 0x93;
- sfr P0M0 = 0x94;
- sfr P1M1 = 0x91;
- sfr P1M0 = 0x92;
- sfr P2M1 = 0x95;
- sfr P2M0 = 0x96;
- sfr P3M1 = 0xb1;
- sfr P3M0 = 0xb2;
- sfr P4M1 = 0xb3;
- sfr P4M0 = 0xb4;
- sfr P5M1 = 0xC9;
- sfr P5M0 = 0xCA;
- sfr P6M1 = 0xCB;
- sfr P6M0 = 0xCC;
- sfr P7M1 = 0xE1;
- sfr P7M0 = 0xE2;
- sfr IE2 = 0xAF; //中断使能寄存器2
- sfr AUXR = 0x8e; //辅助寄存器 //Auxiliary register
- sfr T2H = 0xD6; //定时器2高8位
- sfr T2L = 0xD7; //定时器2低8位
- sfr T4T3M = 0xD1; //定时器3和定时器4控制寄存器
- sfr T4H = 0xD2; //定时器4高8位
- sfr T4L = 0xD3; //定时器4低8位
- sfr T3H = 0xD4; //定时器3高8位
- sfr T3L = 0xD5; //定时器3低8位
-
- sbit P10 = P1^0;
- sbit P11 = P1^1;
- sbit P12 = P1^2;
- sbit P13 = P1^3;
- sbit P14 = P1^4;
- //-----------------------------------------------
- /* Timer0 interrupt routine */
- void tm0_isr() interrupt 1
- {
- P10 = ! P10; //将测试口取反
- }
- //-----------------------------------------------
- //-----------------------------------------------
- /* Timer1 interrupt routine */
- void tm1_isr() interrupt 3
- {
- P11 = ! P11; //将测试口取反
- }
- //-----------------------------------------------
- //中断服务程序
- void t2int() interrupt 12 //中断入口
- {
- P12 = !P12; //将测试口取反
-
- // IE2 &= ~0x04; //若需要手动清除中断标志,可先关闭中断,此时系统会自动清除内部的中断标志
- // IE2 |= 0x04; //然后再开中断即可
- }
- //-----------------------------------------------
- //中断服务程序
- void t3int() interrupt 19 //中断入口
- {
- P13 = !P13; //将测试口取反
-
- // IE2 &= ~0x20; //若需要手动清除中断标志,可先关闭中断,此时系统会自动清除内部的中断标志
- // IE2 |= 0x20; //然后再开中断即可
- }
- //-----------------------------------------------
- //中断服务程序
- void t4int() interrupt 20 //中断入口
- {
- P14 = !P14; //将测试口取反
-
- // IE2 &= ~0x40; //若需要手动清除中断标志,可先关闭中断,此时系统会自动清除内部的中断标志
- // IE2 |= 0x40; //然后再开中断即可
- }
- //-----------------------------------------------
- /* main program */
- void main()
- {
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00;
- AUXR |= 0x80; //定时器0为1T模式
- // AUXR &= 0x7f; //定时器0为12T模式
- AUXR |= 0x40; //定时器1为1T模式
- // AUXR &= 0xdf; //定时器1为12T模式
- AUXR |= 0x04; //定时器2为1T模式
- // AUXR &= ~0x04; //定时器2为12T模式
- T4T3M |= 0x02; //定时器3为1T模式
- // T4T3M &= ~0x02; //定时器3为12T模式
- T4T3M |= 0x20; //定时器4为1T模式
- // T4T3M &= ~0x20; //定时器4为12T模式
- TMOD = 0x00; //设置定时器为模式0(16位自动重装载)
- TL0 = T0_1MS; //初始化计时值
- TH0 = T0_1MS >> 8;
- TR0 = 1; //定时器0开始计时
- ET0 = 1; //使能定时器0中断
- TMOD |= 0x00; //设置定时器为模式0(16位自动重装载)
- TL1 = T1_2MS; //初始化计时值
- TH1 = T1_2MS >> 8;
- TR1 = 1; //定时器1开始计时
- ET1 = 1; //使能定时器0中断
- T2L = T2_3MS; //初始化计时值
- T2H = T2_3MS >> 8;
- AUXR |= 0x10; //定时器2开始计时
-
- IE2 |= 0x04; //开定时器2中断
-
- T3L = T3_38_4KHz; //初始化计时值
- T3H = T3_38_4KHz >> 8;
-
- T4T3M |= 0x08; //定时器3开始计时
- IE2 |= 0x20; //开定时器3中断
- T4L = T4_38_4KHz; //初始化计时值
- T4H = T4_38_4KHz >> 8;
-
- T4T3M |= 0x80; //定时器4开始计时
- IE2 |= 0x40; //开定时器4中断
-
- EA = 1;
- while (1);
- }
复制代码 |