00 :00 . 00
分 秒 分秒
代码:
- /***************************************************************
- 文件名:秒表.c
- 晶振频率:12MHz
- ****************************************************************/
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define dataport P0
- uchar code weima[]={
- 0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- uchar code duanma[]={
- 0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f};
- uchar tempdata[8];
- uchar tt0,tt1,tt2;
- sbit wela=P2^2;
- sbit dula=P2^3;
- void display(uchar,uchar);
- void Init_Timer0(void);
- void delayms(uint);
- void main(void)
- {
- Init_Timer0();
- while(1)
- display(0,6);
- }
- void Init_Timer0(void)
- {
- TMOD=0x01;
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- IE=0x82;
- //EA=1;
- //ET0=1;
- TR0=1;
- }
- void T0_isr(void)interrupt 1
- {
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- tt0++;
- if(tt0==100) //1秒
- {
- tt0=0;
- tt1++;
- if(tt1==60)
- {
- tt1=0;
- tt2++;
- if(tt2==60)
- tt2=0;
- }
- }
- tempdata[0]=duanma[tt2/10]; //分前一位
- tempdata[1]=duanma[tt2%10]; //分后一位
- tempdata[2]=duanma[tt1/10]; //秒前一位
- tempdata[3]=duanma[tt1%10]; //秒后一位
- tempdata[4]=duanma[tt0/10]; //分秒前一位
- tempdata[5]=duanma[tt0%10]; //分秒后一位
- }
- void display(uchar firstbit,uchar num)
- {
- uchar i;
- for(i=0;i<num;i++)
- {
- dataport=0;
- dula=1;
- dula=0;
- dataport=weima[i+firstbit];
- wela=1;
- wela=0;
- dataport=tempdata[i];
- dula=1;
- dula=0;
- delayms(2);
- }
- }
- void delayms(uint ms)
- {
- uint i;
- while(ms--)
- for(i=110;i>0;i--);
- }
复制代码不知道问题出现在哪,数码管显示不对,高手指点下
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
楼主,我写了个。在我的实验板上调试通了。
你把位选、段选定义改一下就好了。
- /*
- *板极调试成功
- *6位数码管显示
- *
- */
- #include <reg52.h>
- sbit DuCs = P2^6;
- sbit WeCs = P2^7;
- #define DATA P0
- unsigned char Second = 0;
- unsigned char Minute =0;
- unsigned char Hour = 0;
- unsigned char Count = 0;
- unsigned char Scan = 0;
- unsigned char SMG_Buffer[8] = {0};
- unsigned char code TAB[] = {
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
- 0x7d,0x07,0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71
- };
- unsigned char code WEI[]={
- 0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- void Timer0_Init(void){
- TMOD=0x01;
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- ET0 = 1;
- TR0 = 1;
- }
- void Timer1_Init(void) {
- TMOD = 0x21;
- TH1 = (256 - 250);
- TL1 = (256 - 250);
-
- ET1 = 1; //打开定时器中断
- TR1 = 1; //开启定时器
- }
- void main(){
- Timer0_Init();
- Timer1_Init();
- EA = 1;
- while (1){
- SMG_Buffer[4] = Second /10;
- SMG_Buffer[5] = Second % 10;
-
-
- SMG_Buffer[2] = Minute / 10;
- SMG_Buffer[3] = Minute % 10;
- SMG_Buffer[0] = Hour / 10;
- SMG_Buffer[1] = Hour % 10;
- }
- }
- void Timer0_SRV(void) interrupt 1{
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- Second ++;
- if (100 == Second) {
- Second = 0;
- Minute ++;
- if (Minute == 60) {
- Minute = 0;
- Hour ++;
- if (Hour == 60){
- Hour =0;
- }
- }
-
- }
- }
- void Timer1_Service(void) interrupt 3{
- TH1 = (256 - 250);
- TL1 = (256 - 250);
- P0 = ~ (1 << Scan);
- WeCs = 1;
- WeCs = 0;
-
- P0 = TAB[SMG_Buffer[Scan]];
- DuCs = 1;
- DuCs = 0;
- Scan ++;
- if (Scan > 7){
- Scan = 0;
- }
- }
复制代码一周热门 更多>