/******************************************* program name: 1602 LCD Clock program writing time: October 4, 2015 hardware support: LCD1602 LCD screen &NBSP;STC12C4052AD external 12MHZ crystal oscillator Wiring definition:db7 --> p1^7db6 --> p1^6db5 --> p1^5db4 --> p1^5rs = p3 ^ 2; rw = P3 ^ 3; E = P3 ^ 4; function: LCD1602 display, with 4-bit bus display numbers, characters, Custom characters, strings, cursors *******************************************/#include <AT89X52.h> // Include header files //typedef unsigned char uint8; // unsigned 8-bit integer variable //#define LCD1602_DB0_DB7 P1 // defining the LCD1602 data bus sbit lcd1602_rs = p3 ^ 2; // define LCD1602 's RS control line sbit lcd1602_rw = p3 ^ 3; // Define the RW control line of the LCD1602 sbit lcd1602_e = p3 ^ 4; // define LCD1602 's e control line sbit lcd1602_busy = p1 ^ 7; Define LCD1602 lines (associated with LCD1602_DB0_DB7)// define LCM2402 instruction set // (see Technical Manual for details) #define CMD_clear 0x01 // Clear Screen #define cmd_back 0x02 // ddram back to 0-bit #define CMD_dec1 0x04 // ac (hands) minus 1 after reading, Write #define   to the left; cmd_add1 0x06 // after reading AC (hands) plus 1, Write to the right #define cmd_dis_gb1 0x0f // Open Cursor _ open cursor Blinking # # CMD_dis_gb2 0x0e // on display _ Open cursor _ off cursor blink #define CMD_dis_gb3 0x0c // on show _ Off cursor blink #define CMD_OFF_dis 0x08 // off cursor-off cursor blinking #define cmd_set82 0x38 // 8 bit bus _2 line display #define cmd_set81 0x30 // 8-bit bus _1 line display (top row) #define CMD_set42 0x28 // 4 bit bus _2 line display #define CMD_set41 0x20 // 4-bit bus _1 line display (top row) #define lin_1 0x80 // 4-bit bus _1 line display (top row) #define lin_2 0xc0 // 4 bit bus _1 line display (top row) void delay_ms (unsigned int a) { unsigned int i; while ( --a != 0 ) { for (i=0;i<=600;i++); }} lcd1602 is busy, if LCD1602 is busy, this function waits to non-busy state //void lcd1602_testbusy (void) { lcd1602_db0_db7 = 0xff; //Device Read Status LCD1602_RS = 0; lcd1602_rw = 1; lCd1602_e = 1; while (lcd1602_busy); //wait for LCM not busy LCD1602_E = 0; }// Write instruction program //void lcd1602_writecmd (uint8 lcd1602_command) { lcd1602_testbusy (); lcd1602_rs = 0; LCD1602_RW = 0; LCD1602_DB0_DB7 = (lcd1602_ COMMAND/16) <<4; //The result to the left 4 bits, the same as the *16 effect lcd1602_e = 1; delay _ms (5); // Modify the delay, change the display speed lcd1602_e = 0; lcd1602_db0_ db7 = (lcd1602_command%16) <<4; //The result to the left 4 bits, the same as the *16 effect LCD1602_E = 1; delay_ms (5); // Modify the delay, change the display speed lcd1602_e = 0;} Write Data program //void lcd1602_writedata (uint8 lcd1602_data) { lcd1602_ Testbusy (); &NBSP;&NBSP;&NBSP;LCd1602_rs = 1; lcd1602_rw = 0; lcd1602_db0_db7 = (LCD1602_DATA/16) <<4;//The result to the left 4-bit, same as the *16 effect LCD1602_E = 1; delay_ms (5); // Modify the delay, change the display speed LCD1602_E = 0; lcd1602_db0_db7 = (lcd1602_data%16) <<4;//The result to the left 4 bits, the same as the *16 effect lcd1602_e = 1; delay_ms (5); // Modify the delay, change the display speed lcd1602_e = 0 ;} // Print String Program (this function calls pointer function) length within 48 characters void print (UINT8&NBSP;A,UINT8&NBSP;*STR) { lcd1602_writecmd (a | 0x80); while (*str != ') { LCD1602 _writedata (*str++); } *str = 0;} Print single character program // void print2 (uint8 a,uint8 t) { lcd1602_writecmd (a | &NBSP;0X80); lcd1602_writedata (t);} Custom character can write 16 literal characters altogether,The display can be extracted directly after writing with its Cgram code. Uint8 code xword[]={ 0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00, //℃, Code 0x00 0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00, //One, code 0x01 0x00,0x00,0x00,0x0e,0x00,0xff,0x00,0x00, //Two, Code 0x02 0x00,0x00,0xff,0x00,0x0e,0x00,0xff,0x00, //Three, code &NBSP;0X03&NBSP;&NBSP;&NBSP;&NBSP;0X00,0X00,0XFF, 0xf5,0xfb,0xf1,0xff,0x00, //Four, code 0x04 0x00,0xfe,0x08,0xfe,0x0a,0x0a, 0xff,0x00, //Five, Code 0x05 0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00, //Six, Code 0x06 0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00, //day, code 0x07};void cgramwrite (void) { // loading cgram // uint8 i; lcd1602_writecmd (0x06); // cgram address Auto plus 1 LCD1602_writecmd (0x40) // cgram address is set at 00 for (i=0;i<64;i++) { lcd1602_writedata (Xword[i]);// write data by array }}// LCD1602 initialization //(user customizable, add * line must be retained but modifiable) void lcd1602_init (void) { lcd1602_ Writecmd (CMD_SET42); //* display mode setting: 4-bit data display 2 lines, lcd1602_writecmd (cmd_set42); //* must run once more! Or it's going to go wrong. lcd1602_writecmd (cmd_clear); // Display clear Screen lcd1602_writecmd ( Cmd_back) The; //* data pointer points to the 1th line 1th character position lcd1602_writecmd (CMD_ADD1); // Display cursor Movement settings: text does not move, the cursor moves right lcd1602_writecmd (CMD_DIS_GB1); // Display on and cursor settings: Display on, cursor on, flashing open cgramwrite (); // write custom characters to Cgram} void main (void) { lcd1602_init ();//lcd1602 initialization while (1) { print2 (0x80, 1+0x30); // shows the number 1 print2 (0x40,5+0x30); // on line 1th 1th digit Display the number 2 print2 (0x85,0x05) on line 1th 1th Digit; // The custom character in the 1th line 5th bit Print2 (0x88,0xe4); // upeer 4bit 1110 in 1th row 9th place ASCII lower 4bit 0100 corresponding μ delay_ms (a); // open cursor display, flashing, A bouncing cursor is displayed after μ print2 (0x81, ' D '); // displays the custom character in the 5th bit of line 1th print (0x42, "www.51cto.com"); //Prints the "blog.51cto.com" string print (0x8e, "HI") from left to right at the first place on line 2nd; //print "Hi" string from left to right at 15th place on line 1th } } //LCD1602 display numbers, characters, custom characters, strings, cursors
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/74/0B/wKioL1YQ7RTRYzNkAAJ68eTjxSo820.jpg "title=" 4-bit. png "alt=" Wkiol1yq7rtryznkaaj68etjxso820.jpg "/>
LCD1602 display, with 4-bit bus display numbers, characters, custom characters, strings, cursors