130 lines
4.0 KiB
C
130 lines
4.0 KiB
C
/*
|
|
* LCD interface example
|
|
* Uses routines from delay.c
|
|
* This code will interface to a standard LCD controller
|
|
* like the Hitachi HD44780. It uses it in 4 bit mode, with
|
|
* the hardware connected as follows (the standard 14 pin
|
|
* LCD connector is used):
|
|
*
|
|
* LCD_D bits 4-7 are connected to the LCD data bits 4-7 (high nibble)
|
|
* LCD_RS bit 2 is connected to the LCD RS input (register select)
|
|
* LCD_E bit is connected to the LCD E bit (enable)
|
|
*
|
|
* To use these routines, set up the port I/O (TRISA, TRISB) then
|
|
* call lcd_init(), then other routines as required.
|
|
*
|
|
*/
|
|
|
|
#include "lcd_hd44780.h"
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
LCD_write_byte(dat): write data to LCD in 4 Bit mode
|
|
argument(s): dat -> operand
|
|
return: none.
|
|
created date 2014-08-12
|
|
-----------------------------------------------------------------------*/
|
|
void LCD_write_byte(unsigned char c) {
|
|
//PORTD = (PORTD & 0x0F) | (c & 0xF0);
|
|
if(c & 0x80) LCD_D7_set; else LCD_D7_clr;
|
|
if(c & 0x40) LCD_D6_set; else LCD_D6_clr;
|
|
if(c & 0x20) LCD_D5_set; else LCD_D5_clr;
|
|
if(c & 0x10) LCD_D4_set; else LCD_D4_clr;
|
|
LCD_STROBE;
|
|
//PORTD = (PORTD & 0x0F) | (c >> 4);
|
|
if(c & 0x08) LCD_D7_set; else LCD_D7_clr;
|
|
if(c & 0x04) LCD_D6_set; else LCD_D6_clr;
|
|
if(c & 0x02) LCD_D5_set; else LCD_D5_clr;
|
|
if(c & 0x01) LCD_D4_set; else LCD_D4_clr;
|
|
LCD_STROBE;
|
|
_delay_us(40);
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------
|
|
lcd_clear(): Clears and homes the LCD
|
|
argument(s): none.
|
|
return: none.
|
|
created date 2014-08-12
|
|
-----------------------------------------------------------------------*/
|
|
void LCD_clear(void) {
|
|
LCD_RS_clr;
|
|
LCD_write_byte(0x1);
|
|
_delay_ms(2);
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------
|
|
lcd_puts(s): write a string of chars to the LCD
|
|
argument(s): s -> string
|
|
return: none.
|
|
created date 2014-08-12
|
|
-----------------------------------------------------------------------*/
|
|
void LCD_write(const char * s) {
|
|
LCD_RS_set; // write characters
|
|
while(*s)
|
|
LCD_write_byte(*s++);
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------
|
|
lcd_putc(c): write one character to the LCD
|
|
argument(s): c -> char
|
|
return: none.
|
|
created date 2014-08-12
|
|
-----------------------------------------------------------------------*/
|
|
void LCD_putc(char c) {
|
|
LCD_RS_set; // write characters
|
|
LCD_write_byte(c);
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------
|
|
lcd_goto(pos): Go to the specified position
|
|
argument(s): pos -> position
|
|
return: none.
|
|
created date 2014-08-12
|
|
-----------------------------------------------------------------------*/
|
|
void LCD_goto(unsigned char pos) {
|
|
LCD_RS_clr;
|
|
LCD_write_byte(0x80+pos);
|
|
}
|
|
|
|
/* initialise the LCD - put into 4 bit mode */
|
|
/*-----------------------------------------------------------------------
|
|
lcd_init(): initializes the LCD - put into 4 bit mode
|
|
argument(s): none
|
|
return: none.
|
|
created date 2014-08-12
|
|
-----------------------------------------------------------------------*/
|
|
void LCD_init(void) {
|
|
LCD_DDR |= BIT(LCD_E_PIN); //PIN Port DDR
|
|
LCD_DDR |= BIT(LCD_RS_PIN);
|
|
LCD_DDR |= BIT(LCD_D7_PIN);
|
|
LCD_DDR |= BIT(LCD_D6_PIN);
|
|
LCD_DDR |= BIT(LCD_D5_PIN);
|
|
LCD_DDR |= BIT(LCD_D4_PIN);
|
|
|
|
LCD_RS_clr; // write control bytes
|
|
_delay_ms(15); // power on delay
|
|
//PORTD = (PORTD & 0x0F) | 0x30; // attention
|
|
LCD_D7_clr;
|
|
LCD_D6_clr;
|
|
LCD_D5_set;
|
|
LCD_D4_set;
|
|
LCD_STROBE;
|
|
_delay_ms(5);
|
|
LCD_STROBE;
|
|
_delay_us(100);
|
|
LCD_STROBE;
|
|
_delay_ms(5);
|
|
//PORTD = (PORTD & 0x0F) | 0x20; // set 4 bit mode
|
|
LCD_D7_clr;
|
|
LCD_D6_clr;
|
|
LCD_D5_set;
|
|
LCD_D4_clr;
|
|
LCD_STROBE;
|
|
_delay_us(40);
|
|
LCD_write_byte(0x28); // 4 bit mode, 1/16 duty, 5x8 font
|
|
LCD_write_byte(0x08); // display off
|
|
LCD_write_byte(0x0C); // display on, blink curson off
|
|
LCD_write_byte(0x06); // entry mode
|
|
}
|