/*LCD1602的左右移动*/ #include<reg51.h> #define uchar unsigned char #define uint unsigned int sbit RS=P1^0; sbit RW=P1^1; sbit E=P1^2; uchar code table1[]="Welcome to china!"; uchar code table2[]="My name is boy !"; uchar code table3[]=" I love you !"; uchar code table4[]=" And I love MCU!"; void mdelay(uint ms) { uint n; for(;ms>0;ms--) for(n=124;n>0;n--); } void write_dictation(uchar dictation) { RS=0; RW=0; E=0; P0=dictation; mdelay(5); E=1; mdelay(20); E=0; } void write_digtal(uchar digtal) { RS=1; RW=0; E=0; P0=digtal; mdelay(5); E=1; mdelay(20); E=0; } void Init() { mdelay(15); write_dictation(0x38); mdelay(5); write_dictation(0x38); mdelay(5); write_dictation(0x38); write_dictation(0x38); write_dictation(0x08); write_dictation(0x01); write_dictation(0x06); write_dictation(0x0c); } void display_left(uchar num1[16],uchar num2[16]) { uchar i; write_dictation(0x80); for(i=0;i<16;i++) { write_digtal(num1); mdelay(1); } write_dictation(0x80+0x40); for(i=0;i<16;i++) { write_digtal(num2); mdelay(1); }
} void display_right(uchar num3[16],uchar num4[16]) { uchar i; write_dictation(0x80+0x10); for(i=0;i<16;i++) { write_digtal(num3); mdelay(1); } write_dictation(0x80+0x40+0x10); for(i=0;i<16;i++) { write_digtal(num4); mdelay(1); } } void left_move() { uchar i; for(i=0;i<16;i++) { write_dictation(0x18); mdelay(3000); } } void right_move() { uchar i; for(i=0;i<16;i++) { write_dictation(0x1c); mdelay(3000); } } void main() { Init(); display_left(table1,table2); display_right(table3,table4); while(1) { left_move(); mdelay(3000); right_move(); mdelay(3000); } } |