- /* 名称:数码管显示拨码开关编码
- 说明:系统显示拨码开关所设置的编码 000~255
- */
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //各数字的数码管段码(共阴)
- uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- //显示缓冲
- uchar DSY_Buffer[3]={0,0,0};
- //延时
- void DelayMS(uint ms)
- {
- uchar t;
- while(ms--)for(t=0;t<120;t++);
- }
- //主程序
- void main()
- {
- uchar i,m,Num;
- P0=0xff;
- P2=0xff;
- while(1)
- {
- m=0xfe;
- Num=P1;//读取拨码开关的值
- DSY_Buffer[0]=Num/100;
- DSY_Buffer[1]=Num/10%10;
- DSY_Buffer[2]=Num%10;
- for(i=0;i<3;i++) //刷新显示在数码管上
- {
- m=_crol_(m,1);
- P2=m;
- P0=DSY_CODE[DSY_Buffer[i]];
- DelayMS(10);
- }
- }
复制代码 |