вот кусок для LCD, может, пригодится (под ардуину): lcd_setup - инициализация, lcd_refresh - асинхронное обновление буфера (вызывать с частотой >100Гц),
lcd_wr_XX, lcd_fill - запись в буфер в разных форматах.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LCD
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define LCD_ROWS 2
#define LCD_COLS 8
u8 lcd_buf[LCD_ROWS * LCD_COLS];
enum {LCD_D4=A3, LCD_D5=A2, LCD_D6=A5, LCD_D7=A4, LCD_RS=A1, LCD_E=13};
const u8 led_pins[] = {LCD_D4, LCD_D5, LCD_D6, LCD_D7, LCD_RS, LCD_E};
//set data bus
void lcd_d(u8 d) {
digitalWrite(LCD_D4, !!(d & 1)); digitalWrite(LCD_D5, !!(d & 2));
digitalWrite(LCD_D6, !!(d & 4)); digitalWrite(LCD_D7, !!(d & 8));
}
//set RS
void lcd_rs(u8 rs) {digitalWrite(LCD_RS, !!rs);}
//set E
void lcd_e(u8 e) {digitalWrite(LCD_E, !!e);}
//write 4 bits
void lcd_d4(u8 d, u8 rs) {
lcd_rs(rs); lcd_d(d); delayMicroseconds(10);
lcd_e(1); delayMicroseconds(50);
lcd_e(0); delayMicroseconds(10);
}
//write 8 bits
void lcd_d8(u8 d, u8 rs) {
lcd_d4(d >> 4, rs);
lcd_d4(d, rs);
}
//write command
void lcd_cmd(u8 cmd) {
lcd_d8(cmd, 0);
}
//setup
void lcd_setup() {
memset(&lcd_buf, ' ', sizeof(lcd_buf));
for (u8 lp=0; lp<sizeof(led_pins); lp++) {
pinMode(led_pins[lp], OUTPUT);
digitalWrite(led_pins[lp], LOW);
}
delay(20);
lcd_d4(3,0); delay(10); lcd_d4(3,0); delay(10);
lcd_d4(3,0); delay(10); lcd_d4(2,0); delay(10);
lcd_cmd(0x20 | (((LCD_ROWS & 1) + 1) << 3));
lcd_cmd(0x08); lcd_cmd(0x0C);
lcd_cmd(0x01); delay(10);
lcd_cmd(0x06);
}
volatile u8 lcd_row,lcd_col; //current row,column
//refresh LCD buffer
void lcd_refresh() {
if (lcd_col == 0) {
lcd_cmd(0x80 +
((lcd_row & 1) ? 0x40 : 0) +
((lcd_row & 2) ? LCD_COLS : 0)
);
}
lcd_d8(lcd_buf[lcd_row * LCD_COLS + lcd_col], 1);
if (++lcd_col >= LCD_COLS) {
lcd_col = 0;
if (++lcd_row >= LCD_ROWS) lcd_row = 0;
}
}
//write char to LCD buffer
inline void lcd_wr_c(u8 pos, char c) { lcd_buf[pos] = c; }
//fill buffer in LCD buffer
inline void lcd_fill(u8 pos, u8 w, char c) { memset( &lcd_buf[pos], c, w ); }
//write string to LCD buffer
void lcd_wr_s(u8 pos, const char *s) {
u8 *buf = &lcd_buf[pos];
while (*s != 0) *buf++ = *s++;
}
//convert binary 0..15 to HEX char
u8 lcd_b2h(u8 b) { return (b <= 9) ? (b + '0') : (b + 'A' - 10); }
//write 4-bit HEX
void lcd_wr_h4(u8 pos, u8 b) { lcd_buf[pos] = lcd_b2h(b & 0xF); }
//write 8-bit HEX
void lcd_wr_h8(u8 pos, u8 b) { lcd_wr_h4(pos, b); lcd_wr_h4(pos-1, b >> 4); }
//write 16-bit HEX
void lcd_wr_h16(u8 pos, u16 w) { lcd_wr_h8(pos, w); lcd_wr_h8(pos-2, w >> 8); }
//write 24-bit HEX
void lcd_wr_h24(u8 pos, u32 w) { lcd_wr_h16(pos, w); lcd_wr_h8(pos-4, w >> 16); }
//write 32-bit HEX
void lcd_wr_h32(u8 pos, u32 w) { lcd_wr_h16(pos, w); lcd_wr_h16(pos-4, w >> 16); }
//write 32-bit signed decimal n to buffer pos, width w, and point position p
void lcd_wr_s32(u8 pos, s32 n, u8 w, u8 p) {
u8 dp = 0;
u8 s = 0;
if (n < 0) { n = -n; s = 1; }
if (p) w--;
while (w--) {
if (p && (p == dp)) lcd_buf[pos--] = '.';
if ((dp == 0)||(n > 0)) lcd_buf[pos] = lcd_b2h(n % 10);
else {
if (s) {
lcd_buf[pos] = '-';
s = 0;
} else lcd_buf[pos] = ' ';
}
n /= 10;
pos--; dp++;
}
}