#include <asf.h>
#include "user_board.h"
#include "lcd.h"
#include "ioport.h"

void LCD_init(void)
{
	u_char lcd_parameter;
	ioport_set_port_dir(IOPORT_PIOC,LCD_MASK_ALL,IOPORT_DIR_OUTPUT);
	ioport_set_port_mode(IOPORT_PIOC,LCD_MASK_ALL,IOPORT_MODE_PULLUP);
    ioport_set_pin_level(LCD_EX, IOPORT_PIN_LEVEL_LOW);  // LCD EX LOW
	lcd_parameter = 0xd8;
	LCD_command(lcd_parameter);
	lcd_parameter = 0x50;
	LCD_command(lcd_parameter);
	lcd_parameter = 0x33;
	LCD_command(lcd_parameter);
	LCD_clear();
}

void LCD_enable(void)
{
	ioport_set_pin_level(LCD_EX, IOPORT_PIN_LEVEL_HIGH);
	delay_us(1);
	ioport_set_pin_level(LCD_EX, IOPORT_PIN_LEVEL_LOW);
	delay_us(1);
}

void LCD_command(u_char byte)
{
	u_char	temp;
	temp = byte;
	temp = temp >> 4;
	ioport_set_pin_level(LCD_OC1, IOPORT_PIN_LEVEL_LOW);
	ioport_set_pin_level(LCD_OC2, IOPORT_PIN_LEVEL_LOW);
	ioport_set_pin_level(LCD_RW, IOPORT_PIN_LEVEL_LOW);
	ioport_set_port_level(IOPORT_PIOC,LCD_MASK_DB,temp<<5);
	LCD_enable();
	temp = byte & 0x0f;
	ioport_set_pin_level(LCD_OC1, IOPORT_PIN_LEVEL_LOW);
	ioport_set_pin_level(LCD_OC2, IOPORT_PIN_LEVEL_LOW);
	ioport_set_pin_level(LCD_RW, IOPORT_PIN_LEVEL_LOW);
	ioport_set_port_level(IOPORT_PIOC,LCD_MASK_DB,temp<<5);
	LCD_enable();
	delay_us(20);
}

void LCD_clear(void)
{
	u_char lcd_parameter;
	lcd_parameter = 0x01;
	LCD_command(lcd_parameter);
	delay_ms(2);
}

void LCD_gotoxy(u_char lcd_x, u_char Lcd_y)
{
	u_char lcd_parameter;
	u_char temp;
	switch(Lcd_y)
	{
		case 1:
		lcd_parameter = 0x00; break;
		case 2:
		lcd_parameter = 0x40; break;
		case 3:
		lcd_parameter = 0x80; break;
		case 4:
		lcd_parameter = 0xc0; break;
		case 5:
		lcd_parameter = 0x18; break;
		case 6:
		lcd_parameter = 0x58; break;
		case 7:
		lcd_parameter = 0x98; break;
		case 8:
		lcd_parameter = 0xd8; break;

		default:
		lcd_parameter = 0x00; break;
	}
	lcd_parameter = lcd_parameter + lcd_x;
	lcd_parameter = lcd_parameter -1;
	temp = lcd_parameter;
	temp = temp >> 4;
	ioport_set_pin_level(LCD_OC1, IOPORT_PIN_LEVEL_HIGH);
	ioport_set_pin_level(LCD_OC2, IOPORT_PIN_LEVEL_HIGH);
	ioport_set_port_level(IOPORT_PIOC,LCD_MASK_DB,temp<<5);
	LCD_enable();
	temp = lcd_parameter & 0x0f;
	ioport_set_pin_level(LCD_OC1, IOPORT_PIN_LEVEL_HIGH);
	ioport_set_pin_level(LCD_OC2, IOPORT_PIN_LEVEL_HIGH);
	ioport_set_port_level(IOPORT_PIOC,LCD_MASK_DB,temp<<5);
	LCD_enable();

}

void LCD_write_chr(u_char ch)
{
	u_char temp;
	temp = ch;
	temp = temp >> 4;
	ioport_set_pin_level(LCD_OC2, IOPORT_PIN_LEVEL_HIGH);
	ioport_set_pin_level(LCD_OC1, IOPORT_PIN_LEVEL_LOW);
	ioport_set_port_level(IOPORT_PIOC,LCD_MASK_DB,temp<<5);
	LCD_enable();
	temp = ch & 0x0f;
	ioport_set_pin_level(LCD_OC2, IOPORT_PIN_LEVEL_HIGH);
	ioport_set_pin_level(LCD_OC1, IOPORT_PIN_LEVEL_LOW);
	ioport_set_port_level(IOPORT_PIOC,LCD_MASK_DB,temp<<5);
	LCD_enable();
	delay_ms(1);
}

void LCD_write_txt(const char *s)
{
	while( *s!= '\0' )
	LCD_write_chr( *s++ );
}
