教材上面的程序好像不能实现数码管59s计数的功能,经过一些资料查询,修改了教材上的程序,比较好的实现数码管的计数。(消影!!!)
程序自己写的。因为仿真平台不是我自己搭的,所以不能分享出来哈(不能侵权嘻嘻!)
希望帮助到有需要的人哈!!
单片机源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit DULA = P2^6;
- sbit WELA = P2^7;
- sbit Led1 = P1^0;
- /*字符编码0-F共16个*/
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71
- };
- /*数码管位编码*/
- uchar code tablewe[]={
- 0xfe,0xfd,0xfb,0xf7,0xef,0xdf
- };
- /*变量声明和初始化*/
- uint shi,ge,num0,num1,num;
- /*延时函数声明*/
- void delay_ms(uint xms); // 实现延时xms毫秒
- /*数码管显示函数*/
- void display(uint shi,uint ge);
- void main(void)
- {
- /*寄存器初始化*/
- TMOD = 0x11; // 设置定时器0/1的工作方式
- TH0 = (65536-45872)/256; // 计数器的初值
- TL0 = (65536-45872)%256;
- TH1 = (65536-45872)/256;
- TL1 = (65536-45872)%256;
- EA = 1; // 使能总中断
- ET0 = 1; // 开启定时器中断
- ET1 = 1;
- TR0 = 1; // 启动定时器
- TR1 = 1;
- // WELA=1;
- // P0=0xfc;
- // WELA=0;
- // DULA=1;
- // P0=0x3f;
- // DULA=0;
-
-
- while(1)
- {
- // WELA=1;
- // P0=0xfe;
- // WELA=0;
- //
- // P0=0xff;
- //
- // DULA=1;
- // P0=0x3f;
- // DULA=0;
- //
- // P0=0xff;
- //
- // delay_ms(1);
- //
- // WELA=1;
- // P0=0xfd;
- // WELA=0;
- //
- // P0=0xff;
- //
- // DULA=1;
- // P0=table[ge];
- // DULA=0;
- //
- // P0=0xff;
- // delay_ms(1);
- display(shi,ge);
- }
- }
- void delay_ms(uint xms)
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--); // 通过断点测试,该语句执行完约1ms
- }
- void display(uint shi,uint ge)
- {
- /*第一个数码管显示十位*/
- WELA = 1;
- P0 = tablewe[0];
- WELA = 0;
-
- P0 = 0xff; // “消影”
-
- DULA = 1;
- P0 = table[shi];
- DULA = 0;
-
- P0 = 0xff; // “消影”
-
- delay_ms(5);
-
- /*第一个数码管显示个位*/
- WELA = 1;
- P0 = tablewe[1];
- WELA = 0;
-
- P0 = 0xff; // “消影”
-
- DULA = 1;
- P0 = table[ge];
- DULA = 0;
-
- P0 = 0xff; // “消影”
- delay_ms(5);
- }
- void Tim0() interrupt 1
- {
- TH0 = (65536-45872)/256; // 重装初值
- TL0 = (65536-45872)%256;
-
- num0++;
- if(num0 == 4)
- {
- num0 = 0;
- Led1 = ~Led1;
- }
- }
- void Tim1() interrupt 3
- {
- TH1 = (65536-45872)/256; // 重装初值
- TL1 = (65536-45872)%256;
-
- num1++;
- if(num1 == 20)
- {
- num1 = 0;
- num++;
- if(num == 60)
- num = 0;
-
- shi = num/10;
- ge = num%10;
- }
- }
复制代码
所有资料51hei提供下载:
text_TimerAndInterrupt.rar
(28.02 KB, 下载次数: 5)
|