Marcelo Costanzo Miranda / Mbed 2 deprecated LCD_1602

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD_1602.cpp Source File

LCD_1602.cpp

00001 #include "LCD_1602.h"
00002 
00003 DigitalOut LCD_RS_pin(D8);
00004 DigitalOut LCD_EN_Pin(D9);
00005 DigitalOut LCD_D7_Pin(D7);
00006 DigitalOut LCD_D6_Pin(D6);
00007 DigitalOut LCD_D5_Pin(D5);
00008 DigitalOut LCD_D4_Pin(D4);
00009 
00010 
00011 void send_to_lcd (char data, int rs)
00012 {
00013     LCD_RS_pin = rs;  // rs = 1 for data, rs=0 for command
00014 
00015     /* write the data to the respective pin */
00016     LCD_D7_Pin = (data>>3)&0x01;
00017     LCD_D6_Pin = (data>>2)&0x01;
00018     LCD_D5_Pin = (data>>1)&0x01;
00019     LCD_D4_Pin = (data>>0)&0x01;
00020 
00021     /* Toggle EN PIN to send the data
00022      * if the HCLK > 100 MHz, use the  20 us delay
00023      * if the LCD still doesn't work, increase the delay to 50, 80 or 100..
00024      */
00025     LCD_EN_Pin = 1;
00026     wait_ms(1);
00027     LCD_EN_Pin = 0;
00028     wait_ms(1);
00029 }
00030 
00031 void lcd_cursor_mode(char cursor, char blink)
00032 {
00033     if(cursor == 1)
00034     {
00035         lcd_send_cmd((0x0C)|0x02);
00036     }
00037     
00038     else
00039     {
00040         lcd_send_cmd((0x0C)&0x00);
00041     }
00042     
00043     if(blink == 1)
00044     {
00045         lcd_send_cmd((0x0C)|0x01);
00046     }
00047     
00048     else
00049     {
00050         lcd_send_cmd((0x0C)&0x00);
00051     }
00052 }
00053 
00054 void lcd_send_cmd (char cmd)
00055 {
00056     char datatosend;
00057 
00058     /* send upper nibble first */
00059     datatosend = ((cmd>>4)&0x0f);
00060     send_to_lcd(datatosend,0);  // RS must be 0 while sending command
00061 
00062     /* send Lower Nibble */
00063     datatosend = ((cmd)&0x0f);
00064     send_to_lcd(datatosend, 0);
00065 }
00066 
00067 void lcd_send_char (char data)
00068 {
00069     char datatosend;
00070 
00071     /* send higher nibble */
00072     datatosend = ((data>>4)&0x0f);
00073     send_to_lcd(datatosend, 1);  // rs =1 for sending data
00074 
00075     /* send Lower nibble */
00076     datatosend = ((data)&0x0f);
00077     send_to_lcd(datatosend, 1);
00078 }
00079 
00080 void lcd_clear (void)
00081 {
00082     lcd_send_cmd(0x01);
00083     wait_ms(2);
00084 }
00085 
00086 void lcd_put_cur(int row, int col)
00087 {
00088     switch (row)
00089     {
00090         case 0:
00091             col |= 0x80;
00092             break;
00093         case 1:
00094             col |= 0xC0;
00095             break;
00096     }
00097 
00098     lcd_send_cmd (col);
00099 }
00100 
00101 
00102 void lcd_init (void)
00103 {
00104     // 4 bit initialisation
00105     wait_ms(50);  // wait for >40ms
00106     lcd_send_cmd (0x00);
00107     wait_ms(5);  // wait for >4.1ms
00108     lcd_send_cmd (0x03);
00109     wait_ms(1);  // wait for >100us
00110     lcd_send_cmd (0x03);
00111     wait_ms(10);
00112     lcd_send_cmd (0x03);  // 4bit mode
00113     wait_ms(10);
00114 
00115   // dislay initialisation
00116     lcd_send_cmd (0x02); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
00117     wait_ms(1);
00118     lcd_send_cmd (0x02); //Display on/off control --> D=0,C=0, B=0  ---> display off
00119     wait_ms(1);
00120     lcd_send_cmd (0x08);  // clear display
00121     wait_ms(1);
00122     wait_ms(1);
00123     lcd_send_cmd (0x00); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)
00124     wait_ms(1);
00125     lcd_send_cmd (0x0C); //Display on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)
00126     wait_ms(1);
00127     lcd_send_cmd (0x00); 
00128     wait_ms(1);
00129     lcd_send_cmd (0x06);
00130 }
00131 
00132 void lcd_send_string (char *str)
00133 {
00134     while (*str) lcd_send_char (*str++);
00135 }
00136