仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "STC15F2K.h"
- #include "intrins.h"
- #include <math.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define shuzhi P2
- sbit tongdian=P1^0;
- sbit kaishi=P1^1;
- uint t;
- uint t1; //1分钟
- uchar dingshi;
- void InitTimer0(void);
- void Delayms(uint t)
- {
- uchar i;
- while(t--)
- for(i=0;i<123;i++);
- }
- void InitTimer0(void)
- {
- TMOD = 0x22;
- AUXR = 0x80; //timer0 work in 1T mode
- TH0 = 0x0DC;
- TL0 = 0x00;
- EA = 1;
- ET0 = 1;
- TR0 = 0;
- }
- void main()
- {
- #ifdef MODE1T
- AUXR = 0x80; //timer0 work in 1T mode
- #endif
- InitTimer0();
- Delayms(1);
- tongdian=1;
- kaishi=1 ;
- t1=10000;
- P2=0xff;
- while(1)
- {
- uint aa=~shuzhi*60*10; // IRC频率 15.5 MHz
- if (t1<=aa)tongdian=0;
- if (t1>aa){TR0 = 0;tongdian=1;t1=10000;}
- if (kaishi==0) {TR0 = 1;t1=0;t=0;}
- }
- }
- void Timer0Interrupt(void) interrupt 1 //(一次10微秒)
- {
- TH0 = 0x0DC;
- TL0 = 0x00;
- t++;
- if (t==60000 ) { t1++; t=0 ; } //1秒
-
- }
复制代码
Keil代码与Proteus仿真下载:
烧水壶定时器2021-11-19.7z
(70.52 KB, 下载次数: 29)
|