Created
February 17, 2012 12:22
-
-
Save jscrane/1853091 to your computer and use it in GitHub Desktop.
HD44780 LCD + XBee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include "xbee.h" | |
#include "lcd.h" | |
static int send_sync(int fd, uint8_t *remote, char *cmd, uint8_t *data, uint16_t n) | |
{ | |
uint8_t rd[256]; | |
send_remote_at_command_request(fd, remote, cmd, data, n); | |
return recv_response(fd, rd, &n); | |
} | |
static uint8_t on_off(int b) | |
{ | |
return b == 0? 0x04: 0x05; | |
} | |
static int rs(int fd, uint8_t *remote, int b) | |
{ | |
static int c = -1; | |
if (b != c) { | |
uint8_t x = on_off(b); | |
c = b; | |
return send_sync(fd, remote, "P1", &x, 1); | |
} | |
return 0; | |
} | |
static int enable(int fd, uint8_t *remote, int b) | |
{ | |
uint8_t x = on_off(b); | |
return send_sync(fd, remote, "P2", &x, 1); | |
} | |
static int bit(int fd, uint8_t *remote, int i, int b) | |
{ | |
static char *lines[] = { "D0", "D1", "D2", "D3" }; | |
static int c[] = { -1, -1, -1, -1 }; | |
if (c[i] != b) { | |
uint8_t x = on_off(b); | |
c[i] = b; | |
return send_sync(fd, remote, lines[i], &x, 1); | |
} | |
return 0; | |
} | |
static int nibble(int fd, uint8_t *remote, uint8_t n) | |
{ | |
int e = bit(fd, remote, 0, n & 1) || | |
bit(fd, remote, 1, n & 2) || | |
bit(fd, remote, 2, n & 4) || | |
bit(fd, remote, 3, n & 8) || | |
enable(fd, remote, 1) || | |
enable(fd, remote, 0) || | |
usleep(1000); | |
return -e; | |
} | |
static int byte(int fd, uint8_t *remote, uint8_t n) | |
{ | |
int e = nibble(fd, remote, n >> 4) || | |
nibble(fd, remote, n & 0x0f); | |
return -e; | |
} | |
int cmd_byte(int fd, uint8_t *remote, uint8_t n) | |
{ | |
int e = rs(fd, remote, 0) || | |
byte(fd, remote, n); | |
return -e; | |
} | |
int print_char(int fd, uint8_t *remote, uint8_t n) | |
{ | |
int e = rs(fd, remote, 1) || | |
byte(fd, remote, n); | |
return -e; | |
} | |
int print_str(int fd, uint8_t *remote, char *s) | |
{ | |
char c; | |
while ((c = *s++)) | |
if (0 > print_char(fd, remote, c)) | |
return -1; | |
return 0; | |
} | |
int lcd_init(int fd, uint8_t *remote) | |
{ | |
// initialization in 4-bit mode | |
int e = enable(fd, remote, 0) || | |
rs(fd, remote, 0) || | |
nibble(fd, remote, 0x03) || | |
usleep(5000) || | |
nibble(fd, remote, 0x03) || | |
usleep(5000) || | |
nibble(fd, remote, 0x03) || | |
usleep(1000) || | |
nibble(fd, remote, 0x02) || | |
cmd_byte(fd, remote, 0x28) || | |
cmd_byte(fd, remote, 0x0c) || | |
cmd_byte(fd, remote, 0x01) || | |
usleep(2000) || | |
cmd_byte(fd, remote, 0x06); | |
return -e; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment