summaryrefslogtreecommitdiff
path: root/Core/Src/lcd_1602.c
diff options
context:
space:
mode:
Diffstat (limited to 'Core/Src/lcd_1602.c')
-rw-r--r--Core/Src/lcd_1602.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/Core/Src/lcd_1602.c b/Core/Src/lcd_1602.c
new file mode 100644
index 0000000..4dc09b9
--- /dev/null
+++ b/Core/Src/lcd_1602.c
@@ -0,0 +1,78 @@
+#include "lcd_1602.h"
+
+static void delay_us(uint32_t delay){
+ delay*=32;
+ while(delay--);
+}
+
+static void lcd_send_4bit(uint8_t data) {
+
+ if(data & 0x10) d4(1); else d4(0);
+ if(data & 0x20) d5(1); else d5(0);
+ if(data & 0x40) d6(1); else d6(0);
+ if(data & 0x80) d7(1); else d7(0);
+
+}
+
+static void lcd_send(int8_t rs,uint8_t data){
+ // rs : 1=data | 2=command
+ // rw : 0=write | 1=read
+ // en : 1=senddata | 0=donothing
+
+ rs(rs);
+ rw(0);
+ lcd_send_4bit(data); //MSB
+ en(1);
+ delay_us(100);
+ en(0);
+ lcd_send_4bit(data<<4); //LSB
+ en(1);
+ delay_us(100);
+ en(0);
+}
+
+void lcd_cmd(uint8_t command){
+ lcd_send(1,command);
+}
+
+void lcd_data(char c){
+ lcd_send(1,(uint8_t)c);
+
+}
+
+void lcd_init(void){
+ //bl(1);
+
+// lcd_send(0x22);
+
+ // 4 bit initialisation
+ lcd_send(0,0x20); // 4bit mode
+ HAL_Delay(10);
+
+ // Display initialisation
+ lcd_send(0,0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
+ HAL_Delay(1);
+ lcd_send(0,0x08); //Display on/off control --> D=0,C=0, B=0 ---> display off
+ HAL_Delay(1);
+ lcd_send(0,0x01); // clear display
+ HAL_Delay(2);
+ lcd_send(0,0x06); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)
+ HAL_Delay(1);
+ lcd_send(0,0x0C); //Display on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)
+ HAL_Delay(2);
+}
+
+void lcd_clr(void){
+ lcd_send(0,0x01);
+ HAL_Delay(2);
+}
+
+
+void lcd_gotoxy(char x, char y){
+ lcd_send(0,0x80+x+(y*0x40));
+ HAL_Delay(2);
+}
+
+void lcd_puts(char *text){
+ while (*text) lcd_data (*text++);
+}