implement timeout in LCD code

This commit is contained in:
Stefan Rupp 2014-03-19 03:49:57 +01:00
parent 8624c085d0
commit cc2ebfe37b

48
lcd.c
View File

@ -50,6 +50,10 @@
// !!!!!!!
#define T_TIMEOUT (10)
static uint8_t lcd_initialized;
static inline uint8_t lcd_read_byte(uint8_t RS)
{
@ -79,14 +83,19 @@ static inline uint8_t lcd_read_byte(uint8_t RS)
}
static inline void lcd_wait_while_busy(void)
static inline int8_t lcd_wait_while_busy(void)
{
uint8_t byte;
uint32_t t_enter = timer_get();
do {
if (timer_get() > t_enter + T_TIMEOUT) {
return 0;
}
byte = lcd_read_byte(0);
} while (byte & 0x80);
return;
return 1;
}
@ -105,9 +114,9 @@ static inline void send_nibble(uint8_t nibble)
* \param byte The byte to send to the LCD
* \param RS specifies whether byte sent is data (RS==1) or command (RS==0)
*/
static inline void lcd_send_byte(uint8_t byte, uint8_t RS)
static inline int8_t lcd_send_byte(uint8_t byte, uint8_t RS)
{
lcd_wait_while_busy();
if (lcd_wait_while_busy() ) {
if (RS)
LCD_DATA_MODE();
else
@ -116,28 +125,41 @@ static inline void lcd_send_byte(uint8_t byte, uint8_t RS)
send_nibble(byte >> 4);
_NOP();
send_nibble(0x0f & byte);
return 1;
}
return 0;
}
int lcd_get(void)
{
lcd_wait_while_busy();
if ( lcd_wait_while_busy() ) {
return lcd_read_byte(1);
}
return -1;
}
int lcd_put(char c)
{
lcd_send_byte(c, 1);
if (!lcd_initialized) {
return 0;
}
return lcd_send_byte(c, 1);
}
void lcd_puts(char *s)
{
if (!lcd_initialized) {
return;
}
while (*s) {
//lcd_put(*s++);
lcd_send_byte(*s++, 1);
if (!lcd_send_byte(*s++, 1)) {
return;
}
}
return;
}
void lcd_cursor(uint8_t blink)
@ -160,6 +182,8 @@ void lcd_init(void)
* display we be set to 4-bit data mode
*/
lcd_initialized = 0;
// wait until LCD gets ready to accept instructions.
timer_wait(50);
@ -189,7 +213,11 @@ void lcd_init(void)
// set to 5 x 8 dots per character,
// 16 characters per line, 2 lines
lcd_send_byte(0x28, 0);
int success = lcd_send_byte(0x28, 0);
if ( !success ) {
return;
}
// enable display, no cursor, no blinking
//lcd_send_byte(0x0c, 0);
@ -205,6 +233,8 @@ void lcd_init(void)
// clear dislay
lcd_clear();
lcd_initialized = 1;
return;
}