130 lines
3.2 KiB
C
130 lines
3.2 KiB
C
#include "lcd_definitions.h"
|
|
|
|
/* Configure the pins of arduino */
|
|
void dinit(void){
|
|
//control
|
|
LCD_E_DDR |= (1 << LCD_E_PIN);
|
|
LCD_RS_DDR |= (1 << LCD_RS_PIN);
|
|
//data
|
|
LCD_D4_DDR |= (1 << LCD_D4_PIN);
|
|
LCD_D5_DDR |= (1 << LCD_D5_PIN);
|
|
LCD_D6_DDR |= (1 << LCD_D6_PIN);
|
|
LCD_D7_DDR |= (1 << LCD_D7_PIN);
|
|
}
|
|
|
|
/* Initialize the LCD as described in the datasheet */
|
|
void lcd_init(void){
|
|
|
|
_delay_ms(50); //power delay more than 15ms
|
|
|
|
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //RS to low
|
|
LCD_E_PORT &= ~(1 << LCD_E_PIN); //E to low
|
|
|
|
|
|
// "reboot process"
|
|
lcd_write(LCD_FUNCTION_SET_8bit);
|
|
_delay_ms(10); //more than 4.1ms
|
|
lcd_write(LCD_FUNCTION_SET_8bit);
|
|
_delay_us(200); //more than 100us
|
|
lcd_write(LCD_FUNCTION_SET_8bit);
|
|
_delay_us(200); //more than 100us
|
|
|
|
//set to 4bit
|
|
lcd_write(LCD_FUNCTION_SET_4bit);
|
|
_delay_us(80); //more than 39us
|
|
lcd_cmd(LCD_FUNCTION_SET_4bit);
|
|
_delay_us(80); //more than 39us
|
|
|
|
//send instructions
|
|
lcd_cmd(LCD_DISPLAY_OFF);
|
|
_delay_us(80); //more than 39us
|
|
lcd_cmd(LCD_CLEAR);
|
|
_delay_ms(3); //more than 1.53 ms
|
|
lcd_cmd(LCD_ENTRY_MODE);
|
|
_delay_us(80); //more than 39us
|
|
lcd_cmd(LCD_DISPLAY_ON);
|
|
_delay_us(80); //more than 39us
|
|
|
|
}
|
|
|
|
/* send an 8bit information to the LCD register in order to be written */
|
|
void lcd_write(uint8_t info){
|
|
//(info & 1 << 4) ? LCD_D4_PORT |= (1 << LCD_D4_PIN) : LCD_D4_PORT &= ~(1 << LCD_D4_PIN)
|
|
if (info & 1<<4){
|
|
LCD_D4_PORT |= (1 << LCD_D4_PIN);
|
|
}
|
|
else {
|
|
LCD_D4_PORT &= ~(1 << LCD_D4_PIN);
|
|
}
|
|
|
|
if (info & 1<<5){
|
|
LCD_D5_PORT |= (1 << LCD_D5_PIN);
|
|
}
|
|
else {
|
|
LCD_D5_PORT &= ~(1 << LCD_D5_PIN);
|
|
}
|
|
|
|
if (info & 1<<6){
|
|
LCD_D6_PORT |= (1 << LCD_D6_PIN);
|
|
}
|
|
else {
|
|
LCD_D6_PORT &= ~(1 << LCD_D6_PIN);
|
|
}
|
|
if (info & 1<<7){
|
|
LCD_D7_PORT |= (1 << LCD_D7_PIN);
|
|
}
|
|
else {
|
|
LCD_D7_PORT &= ~(1 << LCD_D7_PIN);
|
|
}
|
|
|
|
//pulse the Enable pin to indicate we have written the data
|
|
LCD_E_PORT |= (1 << LCD_E_PIN); //high
|
|
_delay_us(1);
|
|
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
|
|
_delay_us(1);
|
|
}
|
|
|
|
/* send an 8bit instruction to the LCD instruction register */
|
|
void lcd_cmd(uint8_t cmd){
|
|
|
|
LCD_RS_PORT &= ~(1 << LCD_RS_PIN); //low
|
|
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
|
|
lcd_write(cmd); // upper 4 bits
|
|
lcd_write(cmd << 4); // lower 4 bits
|
|
}
|
|
|
|
/* send an 8bit data character to the LCD register */
|
|
void lcd_char(uint8_t char_data){
|
|
|
|
LCD_RS_PORT |= (1 << LCD_RS_PIN); //high
|
|
LCD_E_PORT &= ~(1 << LCD_E_PIN); //low
|
|
lcd_write(char_data); // upper 4 bits
|
|
lcd_write(char_data << 4); // lower 4 bits
|
|
}
|
|
|
|
/* give a string to display to the LCD */
|
|
void lcd_string(uint8_t lstring[]){
|
|
|
|
//uint8_t lstring = atoi(message);
|
|
volatile int i = 0;
|
|
for (i = 0; lstring[i] != 0 ; i++) {
|
|
lcd_char(lstring[i]);
|
|
_delay_us(80); //more than 39us
|
|
}
|
|
}
|
|
|
|
uint8_t msg[] = "Arduino";
|
|
uint8_t msg2[] = "Hello world";
|
|
|
|
int main(void) {
|
|
dinit();
|
|
lcd_init();
|
|
lcd_string(msg);
|
|
lcd_cmd(LCD_CURSOR_SET | LCD_START_LINE2);
|
|
_delay_us(80); // more than 39us
|
|
lcd_string(msg2);
|
|
|
|
while(1);
|
|
return 0;
|
|
}
|
|
|