写了个1602的程序,调试时发现刚开始时显示正常,但过不久显示混乱.软件仿真时,发现字符串lcd_1,lcd_2均被修改. 解决一,在main函数最后加上while(1);的超级循环,OK. 解决二,把lcd_1,lcd_2定义在code区,同样OK.
请问: 一:在main函数后为什么加个while(1);就可以,难道 LCD_int();write_string_LCD(0,lcd_1);write_string_LCD(1,lcd_2); 会被不断执行? 二:字符串被修改,程序错在那里?
还是别有原因?盼答
#include<at89x52.h>
#define rs P3_6 #define rw P3_5 #define e P3_4
const char lcd_1[]={"welcom to china "}; const char lcd_2[]={" Right Reserved "}; void delay(const int MS)
{ int t; for(t=0;t<MS;t++); }
void write_LCD(const bit flag,const char data_G)//写函数 { e=0; rs=flag; rw=0; e=1; P1=data_G; e=0; }
void write_string_LCD(const bit line,const char str[])//写字符串函数 { char x;
if(line==0) //行 { write_LCD(0,0x80|0x00); } else { write_LCD(0,0x80|0x40); }
delay(50);
for(x=0;x<16;x++) { write_LCD(1,str[x]); delay(50); } }
void LCD_int() //初始化
{ char i; for(i=0;i<3;i++) { delay(3000); write_LCD(0,0x38); } write_LCD(0,0x38); delay(50); write_LCD(0,0x06); delay(50); write_LCD(0,0x0E); delay(50); write_LCD(0,0x01); delay(50); }
void main() { LCD_int(); write_string_LCD(0,lcd_1); write_string_LCD(1,lcd_2); } |