/* * File: lcd.c * Author: www.henteko.org * * Created on 2015/07/20, 11:10 */ #include "lcd.h" #define LCD_STROBE ((LCD_E = 1), (LCD_E = 0)) unsigned char columns, lines; void lcd_write(unsigned char c) { if(c & 0x80) LCD_D7=1; else LCD_D7=0; if(c & 0x40) LCD_D6=1; else LCD_D6=0; if(c & 0x20) LCD_D5=1; else LCD_D5=0; if(c & 0x10) LCD_D4=1; else LCD_D4=0; LCD_STROBE; if(c & 0x08) LCD_D7=1; else LCD_D7=0; if(c & 0x04) LCD_D6=1; else LCD_D6=0; if(c & 0x02) LCD_D5=1; else LCD_D5=0; if(c & 0x01) LCD_D4=1; else LCD_D4=0; LCD_STROBE; __delay_us(40); } void lcd_puts(unsigned char *s) { LCD_RS = 1; while(*s) { lcd_write(*s++); } } void lcd_putch(unsigned char c) { LCD_RS = 1; lcd_write(c); /* LCD_RS = 1; lcd_write(c); columns++; if(columns == LCD_COL) { lines++; if(lines == 2) { lines = 0; lcd_gotopos(0, 0); } else { lcd_gotopos(0, 1); } } */ } void lcd_init(void) { __delay_ms(15); LCD_RS = 0; LCD_D4 = 1; LCD_D5 = 1; LCD_STROBE; __delay_ms(5); LCD_STROBE; __delay_us(100); LCD_STROBE; __delay_ms(5); LCD_D4 = 0; // 4bit mode LCD_STROBE; __delay_us(40); lcd_write(0x2c); // 4 bit mode, lcd_write(0x08); // display off lcd_clear(); lcd_write(0x0c); // display on, blink curson off lcd_write(0x06); // entry mode columns = lines = 0; } void lcd_gotopos(unsigned char col, unsigned char lin) { LCD_RS = 0; if(lin == 0) { lcd_write(0x80 + col); } else if(lin == 1) { lcd_write(0xc0 + col); } lines = lin; __delay_us(40); } void lcd_clear(void) { LCD_RS = 0; lcd_write(0x1); __delay_ms(2); columns = lines = 0; } void lcd_cursor(unsigned char c) { LCD_RS = 0; lcd_write(0x0e); __delay_us(40); }