采用中断的方法‘,使用了两个74HC595和两个数码管,
完全可以用1个6位数码管加一个74HC595完成的,不过开始没想好,画好了原理图也就不想改了。
附上代码和原理图
欢迎探讨。
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- unsigned int a=4000;
- unsigned char temp0,temp1,temp2,temp3,temp4,temp5;
- unsigned int time_buf[]={0,0,0};
- unsigned char code t_display[]={
- // 0 1 2 3 4 5 6 7 8 9 A B C D E F 消隐
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00}; //段码
- sbit ST_CP1=P1^0;
- sbit SH_CP1=P1^2;
- sbit DS1=P1^1;
- sbit ST_CP2=P1^3;
- sbit SH_CP2=P1^5;
- sbit DS2=P1^4;
-
- sbit w1=P1^7;
- sbit w2=P1^6;
-
- void delay(unsigned char i)
- {
- unsigned char m,n,k;
- for(m=i;m>0;m--)
- for(k=128;k>0;k--)
- for(n=130;n>0;n--);
- }
- void input1_595(unsigned char dat) //发送一个字节
- {
- unsigned char i;
- for(i=0; i<8; i++)
- {
- if(dat & 0x80) DS1 = 1;
- else DS1 = 0;
- SH_CP1 = 1;
- SH_CP1 = 0;
- dat = dat << 1;
- }
- }
- void output1_595()
- {
- ST_CP1=0;
- _nop_();
- ST_CP1=1;
- _nop_();
- ST_CP1=0;
- }
- void input2_595(unsigned char dat) //发送一个字节
- {
- unsigned char i;
- for(i=0; i<8; i++)
- {
- if(dat & 0x80) DS2 = 1;
- else DS2 = 0;
- SH_CP2 = 1;
- SH_CP2 = 0;
- dat = dat << 1;
- }
- }
- void output2_595()
- {
- ST_CP2=0;
- _nop_();
- ST_CP2=1;
- _nop_();
- ST_CP2=0;
- }
- void main()
- {
- unsigned char a,b,c;
- //unsigned char i;
- while(1)
- {
- TH0=0X06;
- TL0=0X06;
- TMOD=0X02;
- IE=0X82;
- TR0=1; //TR0启动加一计数
- temp0=time_buf[2]%10;
- temp1=time_buf[2]/10;
- temp2=time_buf[1]%10;
- temp3=time_buf[1]/10;
- temp4=time_buf[0]%10;
- temp5=time_buf[0]/10;
- //for(a=128;a>0;a--)
- w2=0;w1=0;
- for(b=130;b>0;b--)
- for(c=20;c>0;c--)
- {
- w2=1;w1=1;
- input1_595(t_display[temp0]);
- output1_595();
- input2_595(t_display[temp2]);
- output2_595();
- w2=0;w1=1;
- for(a=5;a>0;a--);
- // _nop_(); _nop_(); _nop_();
- w2=1;w1=1;
- input1_595(t_display[temp1]);
- output1_595();
- input2_595(t_display[temp3]);
- output2_595();
- w2=1;w1=0;
- for(a=5;a>0;a--);
- // _nop_(); _nop(); _nop_();
- }/*
- input1_595(t_display[temp2]);
- output1_595();
- input2_595(t_display[temp3]);
- output2_595();
- delay(20);
- input1_595(t_display[temp4]);
- output1_595();
- input2_595(t_display[temp5]);
- delay(20);*/
- }
- }
- timer() interrupt 1 using 1
- {
- a--;
- if(a==0)
- {
- a=4000;
- time_buf[2]+=1;
- if(time_buf[2]>=60)
- {
- time_buf[2]=0;
- time_buf[1]+=1;
- if(time_buf[1]>=60)
- {
- time_buf[1]=0;
- time_buf[0]+=1;
- }
- }
- }
- }
复制代码
所有资料51hei提供下载:
中断.zip
(27.18 KB, 下载次数: 20)
|