1602显示异常,字符歪了,自上而下滚动,求指点,附上程序
程序:
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define lcd_data P0
- sbit rs=P1^2; //加上拉
- sbit rw=P1^1; //加上拉
- sbit e =P1^0; //加上拉
- sbit key1=P3^3;
- sbit key2=P3^4;
- sbit key3=P3^5
- sbit led=P2^0;
- sbit in=P3^2;
- sbit beer=P3^7;
- uchar table[]="0123456789" ;
- uchar i,a,shuzhi,kaiji,show,save;
- uint num,mun,num1,num2,num3,num4,numall;
- bit fleg;
- void init()
- {
- fleg=1;
- beer=0;
- key1=1;
- key2=1;
- key3=1;
- in=1;
- EA=1;
- EX0=1;
- IT0=1;
- }
- void delayms(uchar aa)
- {
- for(i=aa;i>0;i--)
- {
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- }
- }
- void delay(uchar z)
- {
- uchar x,y;
- {
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- }
- //1602LCD写地址函数
- void adr_write(unsigned char adr)
- {
- delay(20);
- e=0;
- rs=0;
- rw=0;
- e=1;
- lcd_data=adr;
- e=0;
- }
- void lcd_init(void)
- {
- e=0;
- adr_write(0x38); //两行显示,5*7模式
- adr_write(0x01);//清显示
- adr_write(0x0c); //整体显示打开,不显示光标
- adr_write(0x06);
- adr_write(0x80);//从第一行的第一个位置开始显示
- }
- void ddr_write(unsigned char ddr)
- {
- // adr_write(0x06);
- delay(20);
- e=0;
- rs=1;
- rw=0;
- e=1;
- lcd_data=ddr;
- e=0;
- }
- void str_write(unsigned char *str)
- {
- // adr_write(0x07);
- while(*str!=' ')
- {
- ddr_write(*str);
- // delay_1602(200);
- str++;
- }
- }
- void set_point(unsigned char x,y)
- {
- if(!x)
- adr_write(0x80+y);
- else
- adr_write(0x80+0x40+y);
- }
- void display_init()
- {
- set_point(0,0) ;
- str_write("Please Power On!");
- set_point(1,0) ;
- str_write(" ");
- }
- void display_on()
- {
- set_point(0,0) ;
- str_write("Has Boot! ");
- set_point(1,0) ;
- str_write("Think You! ");
- }
- void dis_play()
- {
- set_point(0,0) ;
- str_write("DianDuShuZhi:");
- ddr_write(table[numall/100]);
- ddr_write(table[numall/10%10]);
- ddr_write(table[numall%10]);
- set_point(1,0) ;
- str_write(" ");
- }
- void dis_play_all()
- {
- set_point(0,0) ;
- str_write("DS1:");
- ddr_write(table[num1/100]);
- ddr_write(table[num1/10%10]);
- ddr_write(table[num1%10]);
- str_write(" ");
- str_write("DS2:");
- ddr_write(table[num2/100]);
- ddr_write(table[num2/10%10]);
- ddr_write(table[num2%10]);
- set_point(1,0) ;
- str_write("DS3:");
- ddr_write(table[num3/100]);
- ddr_write(table[num3/10%10]);
- ddr_write(table[num3%10]);
- str_write(" ");
- str_write("DS4:");
- ddr_write(table[num4/100]);
- ddr_write(table[num4/10%10]);
- ddr_write(table[num4%10]);
- }
- void key_scan()
- {
- if(key1==0)
- {
- delay(10);
- if(key1==0)
- {
- while(!key1);
- kaiji=1;
- led=0;
- beer=1;
- delay(100);
- beer=0;
- display_on();
- }
- }
- if(kaiji==1&&key2==0)
- {
- delay(10);
- if(key2==0)
- {
- while(!key2);
- beer=1;
- delay(100);
- beer=0;
- shuzhi++;
- if(shuzhi==4) shuzhi=0;
- if(shuzhi==0)
- {
- num1=mun;
- numall=num1;
- dis_play();
- }
- if(shuzhi==1)
- {
- num2=mun;
- numall=num2;
- dis_play();
- }
- if(shuzhi==2)
- {
- num3=mun;
- numall=num3;
- dis_play();
- }
- if(shuzhi==3)
- {
- num4=mun;
- numall=num4;
- dis_play();
- }
- }
- }
- if(kaiji==1&&key3==0)
- {
- delay(10);
- if(key3==0)
- {
- while(!key3);
- beer=1;
- delay(100);
- beer=0;
- dis_play_all();
- }
- }
- }
- void main()
- {
- init();
- lcd_init();
- display_init();
- //delayms(45);//0.642ms
- //delayms(35);//0.502ms
- //delayms(115);//1.623ms
- //delayms(72);//1.02ms
- //delayms(84);//1.188ms
- //delayms(31);//0.446ms
- while(1)
- {
- key_scan();
- }
- }
- void sieasdf() interrupt 0
- {
- EX0=0;
- for(a=5;a>0;a--)
- {
- delayms(35);//延时0.5ms 判断5次 5*0.5=2.5ms
- if(in)fleg=0;
- }
- if(fleg)
- {
- delayms(72);//延时1ms 判断是不是高电平了
- if(in)
- {
- delayms(115);//延时让它超过2ms; 2.5+1+1.623=5.123ms 开始读数据
- for(a=10;a>0;a--)
- {
- while(!in);
- delayms(86);
- num=num>>1;
- if(in)
- {
- num=num|0x200;
- delayms(31);//延时0.6ms 因为上面延时1.2ms+0.6 刚好跳过1.5ms
- }
- }
- mun=num;
- }
- }
- fleg=1;
- EX0=1;}
复制代码
放正,字符还是从上往下移动,我要从左往右移动
一周热门 更多>