void key_scan(void)//°´¼üɨÃè³ÌÐò
{
uchar hang,lie;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
delay(1);
if((P1&0xf0)!=0xf0)
{
hang=0xfe;
while((hang&0x10)!=0)
{
P1=hang;
if((P1&0xf0)!=0xf0)
{
lie=(P1&0xf0)|0x0f;
buff=((~hang)+(~lie));
switch(buff)
{
case 0x81:key=0;break;
case 0x41:key=1;break;
case 0x21:key=2;break;
case 0x11:key=3;break;
case 0x82:key=4;break;
case 0x42:key=5;break;
case 0x22:key=6;break;
case 0x12:key=7;break;
case 0x84:key=8;break;
case 0x44:key=9;break;
case 0x24:key=10;break;
case 0x14:key=11;;break;
case 0x88:key=12;break;
case 0x48:key=13;break;
case 0x28:key=14;break;
case 0x18:key=15;break;
}
}
else hang=(hang<<1)|0x01;
}
}
}
}
void anjian()
{
key_scan();
if (key==0)
{speed_s=70;
}
if (key==1)
{speed_s=80;
}
if (key==2)
{speed_s=90;
}
if (key==3)
{TR0=1;TR1=1;IN_Z=1;IN_F=0;LedF=1;LedZ=0;LedT=1;//Õýת
}
if (key==4)
{speed_s=40;
}
if (key==5)
{speed_s=50;
}
if (key==6)
{speed_s=60;
}
if (key==7)
{TR0=1;TR1=1;IN_Z=0;IN_F=1;LedF=0;LedZ=1;LedT=1;//·´×ª
}
if (key==8)
{speed_s=10;
}
if (key==9)
{speed_s=20;
}
if (key==10)
{speed_s=30;
}
if (key==11)
{if (speed_s<=0){speed_s=0;}else speed_s--;
}
if (key==12)
{TR0=0;IN_Z=0;IN_F=0;speed_s=0;LedF=1;LedZ=1;LedT=0;//Í£Ö¹
}
if (key==13)
{speed_s=100;
}
if (key==14)
{speed_s=110;
}
if (key==15)
{if (speed_s>=160){speed_s=160;}else speed_s++;
}
key=16;
显示不出来还是显示的不正确,比如256显示为0,这是显示不正确,应为uchar 最大是255;如果是这样,将uchar 改成unsigned short或者int就可以了;如果是没有数字或者乱码就很奇怪了
51单片机虽然是8位的,但是可以处理16位的short和32位int数据,不需要进行特别的处理;这些编译器会做的
输入大于255的数,显示的是减去256的数,定义改成unsigned int也没变化
#include"reg52.h"
#include "intrins.h"
/*******************ºê¶¨Òå**************************************************/
#define uchar unsigned char
#define uint unsigned int //ºê¶¨Òå
//#define P0_value 0x3f
/*******************¶Ë¿Ú¶¨Òå¼°¿ª±ÙÏàÓ¦µÄ´æ´¢µ¥Ôª***************************/
sbit lcd_ce=P2^2; //ʹÄܶË
sbit lcd_rs=P2^0; //Êý¾Ý/ÃüÁîÑ¡Ôñ¶Ë
sbit lcd_rw=P2^1; //¶Á/дѡÔñ¶Ë
sbit LedT=P2^3;//Í£Ö¹µÆ
sbit LedZ=P2^4;//ÕýתµÆ
sbit LedF=P2^5;//·´×ªµÆ
sbit PWM=P3^7;
sbit IN_Z=P3^0;
sbit IN_F=P3^1;
uchar code table1[]="SPEED:0000 r/min"; //×Ö·û´®ÏÔʾÊý×é
uchar code table2[]="S E T:0000 r/min";
unsigned int speed =0;
unsigned int speed_s=50;
char pwm=30;
uchar aa,bb,cc; //±£´æµ±Ç°¶¨Ê±Æ÷µÄÖµ
uchar key=16;uchar buff;
void chushihua(); //ÉêÃ÷³õʼ»¯×Óº¯Êý
void delay(uint z); //ÉêÃ÷ÑÓʱº¯Êý
void write_ml(uchar m); //ÉêÃ÷дÃüÁî×Óº¯Êý
void write_data(uchar s); //ÉêÃ÷дÊý¾Ý×Óº¯Êý
void write_fsm1(uchar add,uchar date);
void write_fsm2(uchar add,uchar dat);
void motor();
void key_scan(void)//°´¼üɨÃè³ÌÐò
{
uchar hang,lie;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
delay(1);
if((P1&0xf0)!=0xf0)
{
hang=0xfe;
while((hang&0x10)!=0)
{
P1=hang;
if((P1&0xf0)!=0xf0)
{
lie=(P1&0xf0)|0x0f;
buff=((~hang)+(~lie));
switch(buff)
{
case 0x81:key=0;break;
case 0x41:key=1;break;
case 0x21:key=2;break;
case 0x11:key=3;break;
case 0x82:key=4;break;
case 0x42:key=5;break;
case 0x22:key=6;break;
case 0x12:key=7;break;
case 0x84:key=8;break;
case 0x44:key=9;break;
case 0x24:key=10;break;
case 0x14:key=11;;break;
case 0x88:key=12;break;
case 0x48:key=13;break;
case 0x28:key=14;break;
case 0x18:key=15;break;
}
}
else hang=(hang<<1)|0x01;
}
}
}
}
void anjian()
{
key_scan();
if (key==0)
{speed_s=70;
}
if (key==1)
{speed_s=80;
}
if (key==2)
{speed_s=90;
}
if (key==3)
{TR0=1;TR1=1;IN_Z=1;IN_F=0;LedF=1;LedZ=0;LedT=1;//Õýת
}
if (key==4)
{speed_s=40;
}
if (key==5)
{speed_s=50;
}
if (key==6)
{speed_s=60;
}
if (key==7)
{TR0=1;TR1=1;IN_Z=0;IN_F=1;LedF=0;LedZ=1;LedT=1;//·´×ª
}
if (key==8)
{speed_s=10;
}
if (key==9)
{speed_s=20;
}
if (key==10)
{speed_s=30;
}
if (key==11)
{if (speed_s<=0){speed_s=0;}else speed_s--;
}
if (key==12)
{TR0=0;IN_Z=0;IN_F=0;speed_s=0;LedF=1;LedZ=1;LedT=0;//Í£Ö¹
}
if (key==13)
{speed_s=100;
}
if (key==14)
{speed_s=110;
}
if (key==15)
{if (speed_s>=160){speed_s=160;}else speed_s++;
}
key=16;
}
void timer0() interrupt 1
{
TH0 =(65536 - 100)/256;//100us³õÖµ
TL0 =(65536 - 100)%256;
aa++;
if(aa>=100){
aa=0;
bb++;
}
if(bb>=45){
speed=TH1*256+TL1;
speed=speed*5;
TH1=0;
TL1=0;
bb=0;
cc++;
}
if(cc>=5)//200ms
{motor();
cc=0;
}
if(aa<=pwm) //pwm£¬¸ßµçƽÂö³åʱ¼ä
{
PWM=1;
}
else//µÍµçƽʱ¼ä
PWM=0;
}
void motor()
{
if(speed_s>speed){if(pwm<100){pwm++;}}
if(speed_s<speed){if(pwm>0){pwm--;}}
}
void chushihua() //³õʼ»¯×Óº¯Êý
{
uchar num;
TMOD=0x51; //¶¨Ê±Æ÷0ÉèΪ¹¤×÷·½Ê½1
TH0 =(65536 - 100)/256;
TL0 =(65536 - 100)%256;
TH1 = 0xfe;
TL1 = 0xfe;
EA=1; //¿ª×ÜÖжÏ
ET0=1; //¿ª¶¨Ê±Æ÷0ÖжÏ
TR0=0; //¶¨Ê±Æ÷0¹Ø±Õ
ET1=1; //¼ÆÊýÆ÷
TR1=1; //¼ÆÊýÆ÷
IN_Z=0;
IN_F=0;
LedF=1;
LedZ=1;
LedT=0;
PWM=0;
lcd_ce=0;
lcd_rw=0;
write_ml(0x38); //ÏÔʾģʽÉèÖÃ
write_ml(0x0c); //ÏÔʾ¿ª¹Ø¼°¹â±êÉèÖÃ
write_ml(0x06); //µØÖ·¼Ó1ÕûÆÁ²»Òƶ¯
write_ml(0x01); //ÇåÁã
write_ml(0x80+0x00);
for(num=0;num<16;num++)
{
write_data(table1[num]);
delay(5);
}
write_ml(0x80+0x40);
for(num=0;num<16;num++)
{write_data(table2[num]);delay(5);}
}
void write_fsm1(uchar add,uchar date)//ÏÔʾÊýÖµµÚÒ»ÐÐ
{
uchar qian,bai,shi,ge;
qian=date/1000;
bai=date%1000/100;
shi=date%1000%100/10;
ge=date%1000%100%10;
write_ml(0x80+add);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_fsm2(uchar add,uchar dat)
{uchar qian,bai,shi,ge;
qian=dat/1000;
bai=dat%1000/100;
shi=dat%1000%100/10;
ge=dat%1000%100%10;
write_ml(0x80+0x40+add);
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);}
void write_ml(uchar m)
{
lcd_rs=0;
P0=m;
delay(5);
lcd_ce=1;
delay(5);
lcd_ce=0;
}
void write_data(uchar s)
{
lcd_rs=1;
P0=s;
delay(5);
lcd_ce=1;
delay(5);
lcd_ce=0;
}
void delay(uint z) //ÑÓʱ×Óº¯Êý
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--); //´óÔ¼1ºÁÃë
}
void main()
{
chushihua();
IN_Z=0;
while(1)
{
anjian();
motor();
write_fsm1(6,speed);
write_fsm2(6,speed_s);
}
}
这是整个程序的代码,就是设置speed_s和测得的speed显示大于255就变成减去256的数
四楼说的正确,void write_fsm1(uchar add,uchar date)里的date要定义为uint,不然其他类型的数据会强制转换为uchar,就只有一个字节了
一周热门 更多>