Amazon Store

Tuesday, 17 March 2015

Lcd interfacing with Attiny Microcontroller

#include

#define F_CPU 1000000UL

#include

#include

#define LCD_data PORTB

#define LCD_D7 PORTB

#define LCD_rs PORTA

#define LCD_rw PORTA

#define LCD_en PORTD

#define HIGH LCD_en | (0x01 << 5)

#define LOW LCD_en & ~(0x01 << 5)

void LCD_busy()

{

LCD_D7 = LCD_D7 | (0x01 << 7); //Make D7th bit of LCD as i/p

LCD_en = LCD_en | (0x01 << 5); //Make port pin as o/p

LCD_rs = LCD_rs & ~(0x01 << 0); //Selected command register

LCD_rw = LCD_rw | (0x01 << 1); //We are reading

while(LCD_D7 & (0x01 << 7))

{ //read busy flag again and again till it becomes 0

LCD_en = LOW; //Enable H->L

LCD_en = HIGH;

}

}

void LCD_command(unsigned char var)

{

LCD_data = var; //Function set: 2 Line, 8-bit, 5x7 dots

LCD_rs = LCD_rs & ~(0x01 << 0); //Selected command register

LCD_rw = LCD_rw & ~(0x01 << 1); //We are writing in instruction register

LCD_en = HIGH; //Enable H->L

LCD_en = LOW;

LCD_busy(); //Wait for LCD to process the command

} // Using the above function is really simple

// var will carry the command for LCD

// e.g. // // LCD_command(0x01); which is used to clear lcd

void LCD_init()

{

LCD_command(0x38); //Function set: 2 Line, 8-bit, 5x7 dots

LCD_busy(); //Wait for LCD to process the command

LCD_command(0x0F); //Display on, Curson blinking command

LCD_busy(); //Wait for LCD to process the command

LCD_command(0x01); //Clear LCD

LCD_busy(); //Wait for LCD to process the command

LCD_command(0x06); //Entry mode, auto increment with no shift LCD_busy();

}

void LCD_clear()

{

LCD_command(0x01);

LCD_busy();

}

void LCD_senddata(unsigned char var)

{

LCD_data = var; //Function set: 2 Line, 8-bit, 5x7 dots

LCD_rs = LCD_rs | (0x01 << 0); //Selected data register

LCD_rw = LCD_rw & ~(0x01 << 1); //We are writing

LCD_en = HIGH; //Enable H->L

LCD_en = LOW;

LCD_busy(); //Wait for LCD to process the command

} // Using the above function is really simple

// we should pass the character to display as argument to function // e.g. // LCD_senddata('A');

main()

{

DDRB = 0xff;

DDRA = 0x03;

DDRD = 0x40;

LCD_init();

unsigned char ch = 'A';

while(1)

{

if(ch == 'Z')

{ ch = 'A';

LCD_clear();

}

LCD_senddata('A'); ch++; _delay_ms(1000);

}

}

 

No comments:

Post a Comment