- #include<reg52.h>//包含52单片机内部寄存器头文件
- #define uchar unsigned char // 自定义uchar为unsigned char(即无符号字符型数据,数据范围:0到255。)
- #define int int//自定义int为int(即有符号整数型数据,数据范围:-32768到32767。)
- sbit K1=P1^0;//位定义EC11旋转编码器A端引脚K1
- sbit K2=P1^1;//位定义EC11旋转编码器B端引脚K2
- sbit K3=P3^4;//位定义EC11旋转编码器C端引脚K3
- int KeyValue;//声明数值变量
- int ge ,shi,bai,qian;//声明数值变量个位变量、数值变量十位变量、数值变量百位变量、数值变量千位变量、
- //uchar qitingflag;
- //unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳数码管段码数字0~9字码表,低电平点亮数码管段码数字0~9。
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f} ; //共阴数码管段码数字0~9字码表,高电平点亮数码管段码数字0~9。
- void Delay(int z)//延时程序
- {
- int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void ShuMaGuanDisplayKeyValue(int KeyValue)//数码管显示数值函数
- {
- qian=KeyValue/1000;//数值变量个位变量值
- bai=KeyValue%1000/100;//数值变量十位变量值
- shi=KeyValue%100/10;//数值变量百位变量值
- ge=KeyValue%10;//数值变量千位变量值
-
- P2=0xfe;//数码管千位显示位
- P0=table[qian];//数码管千位数值显示
- Delay(5);//延时
- P2=0xfd;//数码管百位显示位
- P0=table[bai];//数码管百位数值显示
- Delay(5);//延时
- P2=0xfb;//数码管十位显示位
- P0=table[shi];//数码管十位数值显示
- Delay(5);//延时
-
- P2=0xf7;//数码管个位显示位
- P0=table[ge];//数码管个位数值显示
- Delay(5);//延时
- }
- void KeyScan()//按键扫描函数
- {
- if(K1==0)//开始检测是否旋转了开关
- {
- // delay(10);//消除抖动
- ShuMaGuanDisplayKeyValue(KeyValue);//延时
- if(K1==0) //确实是旋转了,进行正反转判断。
- {
- // delay(10);//消除抖动
- while(!K1)//等待开关A端复位(断开),防止出现乱加减的现象。
- ShuMaGuanDisplayKeyValue(KeyValue);//延时
- if(K2==1)//正转,进行正转处理。
- {
- KeyValue=KeyValue+5;//数码管数值每次增加5
- }
- if(K2==0)//反转,进行反转处理。
- {
- KeyValue=KeyValue-5;//数码管数值每次减少5
- }
- }
- }
- }
- void main()//主函数
- {
- while(1)//死循环
- {
- KeyScan();//按键扫描函数
- // if(qitingflag==1)
- ShuMaGuanDisplayKeyValue(KeyValue);//数码管显示数值函数
- if(KeyValue<0)//判断数值变量是否小于0
- {
- KeyValue=0;//数值变量置0
- }
- if(KeyValue>9999)//判断数值变量是否大于9999
- {
- KeyValue=0;//数值变量置0
- }
- }
-
- // if(qitingflag==0)//判断启停标志位变量是否为0
- // {
- // P2=0Xff;//
- // P0=0x00;//
- // KeyValue=0;
- // }
- }
|