//利用动态扫描和定时器1在数码管上显示出从765432
//开始以1/10秒的速度往下递减直至765398并保持显示此数,
//与此同时利用定时器0以500MS速度进行流水灯从上至下移动,
//当数码管上数减到停止时,实验板上流水灯也停止然后全部开始闪烁,
//3秒后(用T0定时)流水灯全部关闭、数码管上显示出“HELLO”。
//到此保持住。
- #include
- #include
- #define uchar unsigned char
- #define uint unsigned int
- ***it dula=P2^6;
- ***it wela=P2^7;
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,
- 0x76,0x79,0x38,0x3f,0};
- uchar a,m,n,i,bai,shi,ge,flag,flag1,flag2;
- uint num;
- void display(uchar aa,uchar bb,uchar cc,uchar bai, uchar shi, uchar ge);
- void initialization();
- void led();
- void delay(uint i);
- void main() //主程序开始
- {
- initialization();
- while(1)
- {
- if(flag1!=1)
- display(7,6,5,bai,shi,ge);
- else
- display(16,17,18,18,19,20);
-
- }
- }
- void delay(uint i) //延时子程序
- {
- uint x,y;
- for(x=i;x>0;x--)
- for(y=110;y>0;y--);
- }
- void timer0() interrupt 1 //定时器0中断
- {
- TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;
- n++;
- if(flag!=1)
- {
- if(n==10)
- {
- n=0;
- led();
- }
- }
- else
- {
- if(n==60)
- {
- n=0;
- TR0=0;
- flag1=1;
- P1=0xff;
- }
- else
- {
- if(n%4==0)
- P1=~P1;
- }
- }
- }
- void timer1() interrupt 3 //定时器1中断
- {
- TH1=(65536-46080)/256;
- TL1=(65536-46080)%256;
- m++;
- if(m==2)
- {
- m=0;
- bai=num/100;
- shi=num%100/10;
- ge=num%10;
- num--;
- if(num==397)
- {
- flag=1;
- n=0;
- //TR0=0;
- TR1=0;
- P1=0xff;
- }
- }
- }
- void led() //流水灯子程序
- {
- P1=a;
- delay(5);
- a=_cror_(a,1);
- }
-
- void initialization() //初始化子程序
- {
- dula=1;
- P0=0;
- dula=0;
- a=0x7f;
- n=0;
- num=432;
- flag=0;
- flag1=0;
- flag2=0;
- TMOD=0x11;
- TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;
- TH1=(65536-46080)/256;
- TL1=(65536-46080)%256;
- EA=1;
- ET0=1;
- ET1=1;
- TR0=1;
- TR1=1;
- }
- void display(uchar aa,uchar bb,uchar cc,uchar bai, uchar shi, uchar ge) //显示子程序
- {
- //1
- dula=1;
- P0=table[aa];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfe;
- wela=0;
- delay(5);
- //2
- dula=1;
- P0=table[bb];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfd;
- wela=0;
- delay(5);
- //3
- dula=1;
- P0=table[cc];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfb;
- wela=0;
- delay(5);
- //4
- dula=1;
- P0=table[bai];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xf7;
- wela=0;
- delay(5);
- //5
- dula=1;
- P0=table[shi];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xef;
- wela=0;
- delay(5);
- //6
- dula=1;
- P0=table[ge];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xdf;
- wela=0;
- delay(5);
-
- }
复制代码
|