旋转编码开关控制程序.doc
文本预览下载声明
#includereg52.h
#include intrins.h
#define uchar unsigned char
#define uint unsigned int
sbit BMC=P0^0;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
unsigned char dis_code[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0X7F,0xff};
char count=0;
void delay(unsigned char ms)// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void display (unsigned char j)//数码管显示
{
j=2;
while (j--)
{
P1_3 = 0; //显示在第四位
P2 = dis_code[(count%10)];//在第四位显示个位
delay(1);
P1_3 = 1;
P1_2 = 0; //显示在第三位
P2 = dis_code[(count%100)/10];//在第三位显示十位
delay(1);
P1_2 = 1;
P1_1 = 0; //显示在第二位
P2 = dis_code[count/100];//在第2位显示十位
delay(1);
P1_1 = 1;
}
}
void main()
{
EA=1; //总中断允许
EX0=1; //外部中断0允许
IT0=1; //外部中断0为边沿触发方式
while(1)
{
display(2);
if(BMC==0) //按下旋转编码开关则计数清零
{
count=0;
}
}
}
void encoder(void) interrupt 0//外部中断0 旋转码开关中断程序
{
if(INT1==1)
{
count++;
if(count30)
{
count=0;
}
}
if(INT1==0)
{
count--;
if(count0)
{
count=30;
}
}
}
显示全部