]> git.tdb.fi Git - model-railway-devices.git/blob - firmware/lcd.c
8c722d8d29e7e9150d83c76ee9eb63f04e22fb2a
[model-railway-devices.git] / firmware / lcd.c
1 #include <avr/io.h>
2 #include <util/delay.h>
3
4 #define USE_SHIFTREG 1
5
6 #define NOP() __asm__("nop")
7
8 void lcd_init(void);
9 void lcd_on(void);
10 void lcd_clear(void);
11 void lcd_goto(uint8_t);
12 void lcd_write(uint8_t);
13
14 void lcd_clock(void)
15 {
16         _delay_us(1);
17         PORTB |= 0x02;
18         _delay_us(1);
19         PORTB &= ~0x02;
20 }
21
22 void lcd_set_data(uint8_t d)
23 {
24 #ifdef USE_SHIFTREG
25         uint8_t i;
26         for(i=0; i<8; ++i)
27         {
28                 PORTB = (PORTB&~0x04)|((d&1)<<2);
29                 PORTB |= 0x08;
30                 NOP();
31                 PORTB &= ~0x08;
32                 d >>= 1;
33         }
34 #else
35         PORTD = (PORTD&0x3F) | (d<<6);
36         PORTB = (PORTB&0xC0) | (d>>2);
37 #endif
38 }
39
40 void lcd_command(uint8_t c)
41 {
42         PORTB &= ~0x01;
43         lcd_set_data(c);
44         lcd_clock();
45 }
46
47 void lcd_init(void)
48 {
49         DDRB |= 0x0F;
50         PORTB &= ~0x0F;
51         _delay_ms(20);
52         lcd_command(0x38);
53         _delay_us(50);
54 }
55
56 void lcd_on(void)
57 {
58         lcd_command(0x0C);
59         _delay_us(50);
60 }
61
62 void lcd_clear(void)
63 {
64         lcd_command(0x01);
65         _delay_us(1500);
66 }
67
68 void lcd_gotoxy(uint8_t x, uint8_t y)
69 {
70         uint8_t a = x+10*y;
71         if(y&1)
72                 a += 54;
73         lcd_command(a|0x80);
74         _delay_us(50);
75 }
76
77 void lcd_write(uint8_t c)
78 {
79         PORTB |= 0x01;
80         lcd_set_data(c);
81         lcd_clock();
82         _delay_us(50);
83 }