summaryrefslogtreecommitdiff
path: root/n/lcd/src/lcd.c
blob: 518c900aa9ce01a09f15f803b51b557f985f96d2 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* lcd.c */
/* lcd - affiche des messages sur l'�cran lcd. {{{
 *
 * Copyright (C) 2006 Christophe Le Blanc
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "modules/utils/utils.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"
#include "io.h"

#include <string.h>

void lcd_send_command (u8 cmd);

/* Envois une commande transit **/
void
lcd_transit_command (u8 cmd)
{
    lcd_send_command(cmd>>4);
    lcd_send_command(cmd);
}

/** Envois une commande. */
void
lcd_send_command (u8 cmd)
{
    PORTC = (PORTC & 0xf0) | (cmd & 0x0f);
    // Pas de RS car on n'�crit pas sur le lcd, c'est une commande !
    PORTD = PORTD | 0x80;
    PORTD = PORTD & 0x7f;
    utils_delay_us (40);
}

/* Envois un caract�re. */
void
lcd_send_character (u8 c)
{
    PORTC = (PORTC & 0xf0) | c; // c'est �a !
    PORTD = (PORTD | 0x40); // 1 � RS
    PORTD = PORTD | 0x80; // puis 1 � E
    PORTD = PORTD & 0x7f; // puis on remet 0 � E
    utils_delay_us (40);
}

/* Envois une cha�ne de caract�re. */
void
lcd_send_string_n (const char *s,u8 i)
{
    u8 cpt ;
    lcd_transit_command(0x01); // suffit d'envoyer les bits de gauche � droite : D7D6D5D4 en hexa
    utils_delay_ms(2);
    lcd_transit_command(0x02);
    utils_delay_ms(2);
    for (cpt = 0 ; cpt < i ; cpt++)
      {
	if (cpt == 16)
	  {
	    lcd_transit_command(0xc0);
	    utils_delay_us(40);
	  }
	lcd_send_character(s[cpt]);
      }
}

/* Envois une cha�ne de caract�re. */
void
lcd_send_string (const char *s)
{
    lcd_send_string_n (s, strlen (s));
}

/* Initialisation du lcd.  */
void 
lcd_init (void)
{
    DDRC = 0x0f;
    DDRD = 0xfc;
    DDRB = 0x01;
    utils_delay_ms (15); // suffit d'envoyer les bits de gauche � droite : D7D6D5D4 en hexa
    lcd_transit_command (0x01); // efface ecran
    lcd_transit_command (0x01);
    lcd_transit_command (0x13); // force 8 bits
    utils_delay_ms (4.5);
    lcd_transit_command (0x33);
    utils_delay_ms (1);
    lcd_transit_command (0x22); // passage mode 4 bits // def afficheur
    lcd_transit_command (0x80);
    utils_delay_ms (2); // affichage fonction
    lcd_transit_command (0xc0); // d�placement curseur
    lcd_transit_command (0x60); // efface ecran
    lcd_transit_command (0x10);
}

void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
    switch (cmd)
      {
      case 'z':
	if (size != 0)
	  {
	    proto_send0 ('?');
	    return;
	  }
	else
	  {
	    utils_reset ();
	  }
	break;
      case 'p':
	lcd_send_string_n (args, size);
	break;
      default:
	/* This is to handle default commands, return an error. */
	proto_send0 ('?');
	return;
      }
    /* When no error acknoledge. */
    proto_send (cmd, size, args);
}

/* Fonction principale */
int
main (void)
{
    sei ();
    uart0_init ();
    proto_send0 ('z');
    lcd_init ();
    while (1)
      {
	uint8_t c = uart0_getc ();
	proto_accept (c);
      }
    return 0;
}