|
是用51hei單片機開發板 電路圖詳見:http://www.zg4o1577.cn/f/51hei-5.pdf 的數碼管部分, 用2個74hc573 鎖存,p0口作為數據口 ,p3.6和p3.7分別是段和位的鎖存端口. 還有p2口有3個按鍵 詳見以下程序的定義:
- #include<reg52.h>
- #include<math.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- sbit D=P3^6;
- sbit V=P3^7;
- uchar ACT[4]={0xfe,0xfd,0xfb,0xf7};
- //=====================
- sbit P2_4=P2^4;
- sbit P2_5=P2^5;
- sbit P2_6=P2^6;
- int c,f;
- int temp;
- uchar status;
- //==============
- void key_s1(void);
- void key_s2(void);
- void key_s4(void);
- int conv(int fin);
- void delay_1ms(void);
- //====================
- void main(void)
- {int temp_f,temp_c;
- while(1)
- {
- key_s1();
- key_s2();
- key_s4();
- if(status==0)
- {
- if(f<0)
- {temp_f=abs(f);
- D=1;P0=SEG7[temp_f%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
- D=1;P0=SEG7[(temp_f%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
- D=1;P0=SEG7[(temp_f/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
- D=1;P0=0x40;D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay_1ms();
- }
- else
- {
- D=1;P0=SEG7[f%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
- D=1;P0=SEG7[(f%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
- D=1;P0=SEG7[(f/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
- }
- }
- else
- {
- c=conv(f);
- if(c<0)
- {temp_c=abs(c);
- D=1;P0=SEG7[temp_c%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
- D=1;P0=SEG7[(temp_c%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
- D=1;P0=SEG7[(temp_c/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
- D=1;P0=0x40;D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay_1ms();
- }
- else
- {
- D=1;P0=SEG7[c%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay_1ms();
- D=1;P0=SEG7[(c%100)/10];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay_1ms();
- D=1;P0=SEG7[(c/100)%10];D=0;P0=0xff;V=1;P0=ACT[1];V=0;delay_1ms();
- }
- }
- }
- }
- //================
- void key_s1(void)
- {
- if(!P2_4)
- {if(temp>50)temp=0;
- if(temp==0)f++;
- if(f>999)f=999;
- temp++;
- }
- }
- //===============
- void key_s2(void)
- {
- if(!P2_5)
- {if(temp>50)temp=0;
- if(temp==0)f--;
- if(f<-500)f=-500;
- temp++;
- }
- }
- //============
- void key_s4(void)
- {
- if(!P2_6)status=1;
- else status=0;
- }
- //===========
- int conv(int fin)
- {int ddata;
- ddata=fin-32;
- ddata=(ddata*5)/9;
- return ddata;
- }
- //============
- void delay_1ms(void)
- {
- uint k;
- for(k=0;k<121;k++);
- }
復制代碼
|
|