summaryrefslogtreecommitdiff
path: root/Core/Src/lcd_1602.c
blob: 4dc09b96c1825c8a6865d1795599861a3a0c79ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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++);
}