资源描述:
《按键设置电压显示》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、按键设置电压显示#defineucharunsignedchar#defineuintunsignedint#defineBIT(x)(1<<(x))uchartable[]="0123456789";uchartable1[]="Voltageinput:";uchartable2[]="0.00V";uinta,b,c,num,temp,temp1,temp2,temp3,n0,n1,n2;voiddelay(ucharz)//延时函数{ucharx,y;for(x=z;x>0;x--)for(y=1100;y>0;y--);}vo
2、idwrite_com(ucharcom){PORTC&=~BIT(2);//PORTC&=~BIT(3);PORTA=com;PORTC
3、=BIT(4);delay(5);PORTC&=~BIT(4);delay(5);}voidwrite_dat(uchardat){PORTC
4、=BIT(2);//PORTC&=~BIT(3);PORTA=dat;PORTC
5、=BIT(4);delay(5);PORTC&=~BIT(4);delay(5);}voidinit_1602(){PORTC&=~(1<<3);write_com(0x38)
6、;write_com(0x01);write_com(0x06);write_com(0x0c);write_com(0x80);}ucharkey_press()//检测键盘是否被按下{uchari;DDRD=0XFF;PORTD=0XF0;DDRD=0X0F;i=PIND;if(i==0XF0){DDRD=0XFF;return0;}else{DDRD=0XFF;return1;}}voidkey_scan()//键盘扫描函数{ucharkey,i=0X7F,j;//delay(5);if(key_press()){do{DDRD=
7、0X0F;//设置D口高四位输入,低四位输出i=(i<<1
8、i>>7);PORTD=i;//把i的值赋给D口key=PIND;//读出D口的数据j=key&0XF0;}while(j==0XF0);while(key_press());//松手检测switch(key){case0xEE:{num++;a=0;}break;case0xDE:{num++;a=1;}break;case0xBE:{num++;a=2;}break;case0x7E:{num++;a=3;}break;case0xED:{num++;a=4;}break;
9、case0xDD:{num++;a=5;}break;case0xBD:{num++;a=6;}break;case0x7D:{num++;a=7;}break;case0xEB:{num++;a=8;}break;case0xDB:{num++;a=9;}break;case0xBB:{write_com(0x80+0x40+1);write_dat('.');}break;case0x7B:{num=0;write_com(0x80+0x40);for(b=0;b<5;b++){write_dat(table2[b]);}}brea
10、k;/*case0xE7:key=0xC;break;case0xD7:key=0xD;break;case0xB7:key=0xE;break;case0x77:key=0xF;break;default:key=16;*/}}if(num==1){write_com(0x80+0x40);write_dat(table[a]);temp=a*100;}if(num==2){write_com(0x80+0x40+2);write_dat(table[a]);temp1=a*10;}if(num==3){write_com(0x80+
11、0x40+3);write_dat(table[a]);temp2=a;}temp3=(temp+temp1+temp2)/2+5;n0=temp3/100;n1=temp3%100/10;n2=temp3%10;write_com(0x80+0x40+8);write_dat(table[n0]);write_dat(table[n1]);write_dat(table[n2]);//returnkey;}intmain(){DDRA=0xff;DDRC=0xff;PORTA=0xff;PORTC
12、=0x00;init_1602();
13、write_com(0x80);for(b=0;b<14;b++){write_dat(table1[b]);}write_com(0x80+0x40);for(b=0;b<5;b++){write_dat