![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LCD1602 com o minimo de arquivos necessarios
Revision 0:12860941a8a1, committed 2020-11-26
- Comitter:
- Marcelocostanzo
- Date:
- Thu Nov 26 18:11:13 2020 +0000
- Commit message:
- LCD1602 com minimo de arquivos necessarios
Changed in this revision
diff -r 000000000000 -r 12860941a8a1 LCD_1602.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_1602.cpp Thu Nov 26 18:11:13 2020 +0000 @@ -0,0 +1,136 @@ +#include "LCD_1602.h" + +DigitalOut LCD_RS_pin(D8); +DigitalOut LCD_EN_Pin(D9); +DigitalOut LCD_D7_Pin(D7); +DigitalOut LCD_D6_Pin(D6); +DigitalOut LCD_D5_Pin(D5); +DigitalOut LCD_D4_Pin(D4); + + +void send_to_lcd (char data, int rs) +{ + LCD_RS_pin = rs; // rs = 1 for data, rs=0 for command + + /* write the data to the respective pin */ + LCD_D7_Pin = (data>>3)&0x01; + LCD_D6_Pin = (data>>2)&0x01; + LCD_D5_Pin = (data>>1)&0x01; + LCD_D4_Pin = (data>>0)&0x01; + + /* Toggle EN PIN to send the data + * if the HCLK > 100 MHz, use the 20 us delay + * if the LCD still doesn't work, increase the delay to 50, 80 or 100.. + */ + LCD_EN_Pin = 1; + wait_ms(1); + LCD_EN_Pin = 0; + wait_ms(1); +} + +void lcd_cursor_mode(char cursor, char blink) +{ + if(cursor == 1) + { + lcd_send_cmd((0x0C)|0x02); + } + + else + { + lcd_send_cmd((0x0C)&0x00); + } + + if(blink == 1) + { + lcd_send_cmd((0x0C)|0x01); + } + + else + { + lcd_send_cmd((0x0C)&0x00); + } +} + +void lcd_send_cmd (char cmd) +{ + char datatosend; + + /* send upper nibble first */ + datatosend = ((cmd>>4)&0x0f); + send_to_lcd(datatosend,0); // RS must be 0 while sending command + + /* send Lower Nibble */ + datatosend = ((cmd)&0x0f); + send_to_lcd(datatosend, 0); +} + +void lcd_send_char (char data) +{ + char datatosend; + + /* send higher nibble */ + datatosend = ((data>>4)&0x0f); + send_to_lcd(datatosend, 1); // rs =1 for sending data + + /* send Lower nibble */ + datatosend = ((data)&0x0f); + send_to_lcd(datatosend, 1); +} + +void lcd_clear (void) +{ + lcd_send_cmd(0x01); + wait_ms(2); +} + +void lcd_put_cur(int row, int col) +{ + switch (row) + { + case 0: + col |= 0x80; + break; + case 1: + col |= 0xC0; + break; + } + + lcd_send_cmd (col); +} + + +void lcd_init (void) +{ + // 4 bit initialisation + wait_ms(50); // wait for >40ms + lcd_send_cmd (0x00); + wait_ms(5); // wait for >4.1ms + lcd_send_cmd (0x03); + wait_ms(1); // wait for >100us + lcd_send_cmd (0x03); + wait_ms(10); + lcd_send_cmd (0x03); // 4bit mode + wait_ms(10); + + // dislay initialisation + lcd_send_cmd (0x02); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters) + wait_ms(1); + lcd_send_cmd (0x02); //Display on/off control --> D=0,C=0, B=0 ---> display off + wait_ms(1); + lcd_send_cmd (0x08); // clear display + wait_ms(1); + wait_ms(1); + lcd_send_cmd (0x00); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift) + wait_ms(1); + lcd_send_cmd (0x0C); //Display on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits) + wait_ms(1); + lcd_send_cmd (0x00); + wait_ms(1); + lcd_send_cmd (0x06); +} + +void lcd_send_string (char *str) +{ + while (*str) lcd_send_char (*str++); +} +
diff -r 000000000000 -r 12860941a8a1 LCD_1602.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_1602.h Thu Nov 26 18:11:13 2020 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +#ifndef LCD_1602_H +#define LCD_1602_H + +void lcd_init (void); // initialize lcd + +void lcd_send_cmd (char cmd); // send command to the lcd + +void lcd_send_char (char data); // send data to the lcd + +void lcd_send_string (char *str); // send string to the lcd + +void lcd_put_cur(int row, int col); // put cursor at the entered position row (0 or 1), col (0-15); + +void lcd_clear (void); + +void lcd_cursor_mode(char cursor, char blink); + +#endif
diff -r 000000000000 -r 12860941a8a1 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Nov 26 18:11:13 2020 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" +#include "meuLCD1602.h" + +DigitalOut led(LED1); + +int i; +char strBuffer[64]; + +int main() +{ + lcd_init(); + lcd_clear(); + lcd_cursor_mode(0,0); + + lcd_send_char(0x4F);//O + lcd_send_char(0x4C);//L + lcd_send_char(0x41);//A + lcd_send_char(0x20);// + lcd_send_char(0x32);//2 + lcd_send_char(0x30);//0 + lcd_send_char(0x32);//2 + lcd_send_char(0x30);//0 + + wait_ms(1000); + lcd_clear(); + + while(1) + { + lcd_put_cur(0,0); + sprintf(strBuffer, "%i", i); + lcd_send_string(strBuffer); + i++; + wait_ms(1000); + } +}
diff -r 000000000000 -r 12860941a8a1 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 26 18:11:13 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file