Subversion Repositories group.electronics

Rev

Rev 46 | Rev 48 | 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(50);

        lcd_pulse(0x30);        
        _delay_ms(50);

        lcd_pulse(0x30);        
        _delay_ms(50);

        lcd_pulse(0x20);        
        _delay_ms(10);

        lcd_command(LCD_FUNCTIONSET | lcd.function);
        _delay_ms(10);

        lcd_command(LCD_DISPLAYCONTROL | lcd.display);
        _delay_ms(10);

        lcd_clear();    
        _delay_ms(10);

        lcd_home();
        _delay_ms(10);
}

/** Helper functions **/

inline void lcd_clear() {
        lcd_command(LCD_CLEARDISPLAY);
}

inline void lcd_home() {
        lcd_command(LCD_RETURNHOME);
}

inline void lcd_backlight() {
        lcd.backlight = LCD_BACKLIGHT;
}

inline void lcd_noBacklight() {
        lcd.backlight = LCD_NOBACKLIGHT;
}

void lcd_display() {
        lcd.display |= LCD_DISPLAYON;
        lcd_command(LCD_DISPLAYCONTROL | lcd.display);
}

void lcd_noDisplay() {
        lcd.display &= ~LCD_DISPLAYON;
        lcd_command(LCD_DISPLAYCONTROL | lcd.display);
}

void lcd_setCursor(uint8_t col, uint8_t row) {
        uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
        lcd_command(LCD_SETDDRAMADDR | (col+ row_offsets[row - 1]));
}

/** 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_pulse(uint8_t data) {
        i2c_beginTransmission(LCD_ADDR);
        i2c_writeByte((int) data | LCD_MODE_EN | lcd.backlight);
        i2c_writeByte((int) data | lcd.backlight);
        i2c_endTransmission(1);
        _delay_us(5);
}