Rev 44 | Rev 46 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <avr/io.h>
#include "wire.h"
#include "lcd.h"
#include "macros.h"
#include <util/delay.h>
struct {
uint8_t display;
uint8_t function;
uint8_t backlight;
} lcd;
void lcd_init() {
lcd.backlight = LCD_BACKLIGHT;
lcd.display = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
lcd.function = LCD_2LINE;
lcd_pulse(0x30);
_delay_ms(150);
lcd_pulse(0x30);
_delay_ms(150);
lcd_pulse(0x30);
_delay_ms(50);
lcd_pulse(0x20);
_delay_ms(50);
lcd_command(LCD_FUNCTIONSET | lcd.function);
_delay_ms(50);
lcd_command(LCD_DISPLAYCONTROL | lcd.display);
_delay_ms(50);
i2c_clear();
_delay_ms(50);
i2c_home();
_delay_ms(50);
}
/** Helper functions **/
void i2c_clear() {
lcd_command(LCD_CLEARDISPLAY);
}
void i2c_home() {
lcd_command(LCD_RETURNHOME);
}
void i2c_display() {
lcd.display |= LCD_DISPLAYON;
lcd_command(LCD_DISPLAYCONTROL | lcd.display);
}
void i2c_noDisplay() {
lcd.display &= ~LCD_DISPLAYON;
lcd_command(LCD_DISPLAYCONTROL | lcd.display);
}
void i2c_backlight() {
lcd.backlight = LCD_BACKLIGHT;
}
void i2c_noBacklight() {
lcd.backlight = LCD_NOBACKLIGHT;
}
/** End helpers **/
void lcd_command(uint8_t data) {
lcd_send(data, LCD_MODE_CM);
}
void lcd_char(uint8_t data) {
lcd_send(data, LCD_MODE_RS);
}
void lcd_send(uint8_t value, uint8_t mode) {
uint8_t high = value & 0xf0;
uint8_t low = (value << 4) & 0xf0;
lcd_pulse((high)|mode);
lcd_pulse((low)|mode);
}
void lcd_write(uint8_t data) {
i2c_beginTransmission(LCD_ADDR);
i2c_writeByte((int) data | lcd.backlight);
i2c_endTransmission(1);
}
void lcd_pulse(uint8_t data) {
lcd_write(data | LCD_MODE_EN);
_delay_us(40);
//lcd_write(data & ~LCD_MODE_EN);
lcd_write(data);
_delay_us(40);
}