实现的功能:
开发板上电关闭LED,关闭蜂鸣器继电器,数码管依次显示自己QQ号前8位(人眼分辨不出闪烁);
按下S7按键,数码管前五位熄灭,后三位数码管以自己QQ号前三位为基础顺序开始倒计时(用定时器实现);
按下S6按键,当数码管倒计时为偶数时L1灯点亮(其他灯保持熄灭),计时为奇数时所有LED灯熄灭;
按下S5按键,关闭LED灯灯闪烁功能,倒计时依然继续;
按下S4按键,打开继电器,L10灯点亮,倒计时依然继续;
程序没问题,就是按s6按键应该p01亮,但是按下s6,其他七个也会亮,只是亮的不明显不知为啥,
单片机源程序:
- #include <STC15F2K60S2.H>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar tab[]={0xc0,0xf9,0xa4,
- 0xb0,0x99,0x92,0x82,0xf8,
- 0x80,0x90,0xff};
- uchar tab1[]={0xfe,0xff};
- uchar wei,pian,temp,s1=16;
- uchar wl0=0,wl1=0,wl2=0;
- uchar yi,er,san,si,count=0;
- uchar wu,liu;qi;ba;
- uint adc,num=202;
- void allinit();
- void delayms(uint b);
- void Timer0Init(void);
- void led(uint c);
- void smgxianshi0(uchar yi,uchar er);
- void smgxianshi1(uchar san,uchar si);
- void smgxianshi2(uchar wu,uchar liu);
- void smgxianshi3(uchar qi,uchar ba);
- void keycan();
- void anxiaanjian();
- void main()
- {
- allinit();
- smgxianshi0(2,0);
- smgxianshi1(2,0);
- smgxianshi2(1,0);
- smgxianshi3(4,5);
- keycan();
- if(s1==0)
- {
- anxiaanjian();
- }
- }
- void anxiaanjian()
- {
- liu=2;qi=0;ba=2;
- Timer0Init();
-
- while(1)
- {
- keycan();
- smgxianshi0(10,10);
- smgxianshi1(10,10);
- smgxianshi2(10,liu);
- smgxianshi3(qi,ba);
- if(s1==1)
- {
- if(ba%2==0)
- {
- led(0);
- }
- else
- {
- led(1);
- }
- }
- else if(s1==2)
- {
- led(1);
- P2=0xa0;
- P0=0x00;
- }
- else if(s1==3)
- {
- P2=0xa0;
- P0=0x10;
- led(1);
- }
- }
- }
- void allinit()
- {
- P2=0x80;P0=0xff;
- P2=0xa0;P0=0x00;
- P2=0xc0;P0=0xff;
- P2=0xe0;P0=0xff;
- }
- void delayms(uint b)
- {
- uint i,j;
- for(i=b;i>0;i--)
- for(j=845;j>0;j--);
- }
- void Timer0() interrupt 1
- {
- TH0=0x3c;
- TL0=0xbb;
- count++;
- if(count==19)
- {
- count=0;
- num=num-1;//num--;
- liu=num/100;
- qi=num%100/10;
- ba=num%10;
- }
- if(num==0)num=202;
- }
- void Timer0Init(void)
- {
- AUXR&=0x7f;
- TMOD|=0x01;
- TL0=0xbb;
- TH0=0x3c;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void led(uint c)
- {
- P2=0x80;
- P0=tab1[c];
- }
- void smgxianshi0(uchar yi,uchar er)
- {
- P2=0xc0;
- P0=0x01;
- P2=0xe0;
- P0=tab[yi];
- delayms(1);
-
- P2=0xc0;
- P0=0x02;
- P2=0xe0;
- P0=tab[er];
- delayms(1);
- }
- void smgxianshi1(uchar san,uchar si)
- {
- P2=0xc0;
- P0=0x04;
- P2=0xe0;
- P0=tab[san];
- delayms(1);
-
- P2=0xc0;
- P0=0x08;
- P2=0xe0;
- P0=tab[si];
- delayms(1);
- }
- void smgxianshi2(uchar wu,uchar liu)
- {
- P2=0xc0;
- P0=0x10;
- P2=0xe0;
- P0=tab[wu];
- delayms(1);
-
- P2=0xc0;
- P0=0x20;
- P2=0xe0;
- P0=tab[liu];
- delayms(1);
- }
- void smgxianshi3(uchar qi,uchar ba)
- {
- P2=0xc0;
- P0=0x40;
- P2=0xe0;
- P0=tab[qi];
- delayms(1);
-
- P2=0xc0;
- P0=0x80;
- P2=0xe0;
- P0=tab[ba];
- delayms(1);
- }
- void keycan()
- {
- P3=0x7f;P4=0xef;
- temp=P3;
- if(P3!=0x7f)
- {
- delayms(5);
- temp=P3;
- if(P3!=0x7f)
- {
- switch(temp)
- {
- case 0x7e : s1=0; break;
- case 0x7d : s1=1; break;
- case 0x7b : s1=2; break;
- case 0x77 : s1=3; break;
- }
- while(temp!=0x7f)
- {
- temp=P3;
- delayms(5);
- }
- }
- }
- }
复制代码
|