lcd.h

[atmega162][C] Inicjalizacja i obsługa LCD.

Wiele sie nie znam ale uzywam czegos takiego dla atmega16/32. poniższy plik (zalacznik lcd.h) ma lezec w ...Winavr\avr\include a ponizej przykładowy kod programu . Moze i sa lekkie niedociagniecia ale dziala :) #include <LCD.h> // obsługa LCD z // F_CPU 8000000UL // <util/delay.h> // <avr/io.h> // <stdlib.h> int main(void) // program główny { LCD_init(); LCD_clr(); while(1) { LCD_xy(9,0); write_text("test"); } }


/*
Piny/Pins:
PA1 - RS
PA2 - E
PA3 - D4
PA4 - D5
PA5 - D6
PA6 - D7
*/

#define F_CPU 8000000UL
#include &amp; lt; util/delay.h &amp; gt;
#include &amp; lt; avr/io.h &amp; gt;
#include &amp; lt; stdlib.h &amp; gt;



#define DDR_DB4 DDRA
#define PORT_DB4 PORTA
#define DB4 PA3

#define DDR_DB5 DDRA
#define PORT_DB5 PORTA
#define DB5 PA4

#define DDR_DB6 DDRA
#define PORT_DB6 PORTA
#define DB6 PA5

#define DDR_DB7 DDRA
#define PORT_DB7 PORTA
#define DB7 PA6


#define DDR_RS DDRA
#define PORT_RS PORTA
#define RS PA1

#define DDR_E DDRA
#define PORT_E PORTA
#define E PA2


#define SET_B1 PORT_B1 |= _BV(B1)
#define CLR_B1 PORT_B1 &amp; = ~_BV(B1)

#define SET_B2 PORT_B2 |= _BV(B2)
#define CLR_B2 PORT_B2 &amp; = ~_BV(B2)

#define SET_B3 PORT_B3 |= _BV(B3)
#define CLR_B3 PORT_B3 &amp; = ~_BV(B3)

#define SET_B4 PORT_B4 |= _BV(B4)
#define CLR_B4 PORT_B4 &amp; = ~_BV(B4)

#define SET_DB4 PORT_DB4 |= _BV(DB4)
#define CLR_DB4 PORT_DB4 &amp; = ~_BV(DB4)

#define SET_DB5 PORT_DB5 |= _BV(DB5)
#define CLR_DB5 PORT_DB5 &amp; = ~_BV(DB5)

#define SET_DB6 PORT_DB6 |= _BV(DB6)
#define CLR_DB6 PORT_DB6 &amp; = ~_BV(DB6)

#define SET_DB7 PORT_DB7 |= _BV(DB7)
#define CLR_DB7 PORT_DB7 &amp; = ~_BV(DB7)

#define SET_E PORT_E |= _BV(E)
#define CLR_E PORT_E &amp; = ~_BV(E)

#define SET_RS PORT_RS |= _BV(RS)
#define CLR_RS PORT_RS &amp; = ~_BV(RS)

#define LCD_X 16
#define LCD_Y 2


char buff[3];


union {
int tds;
char nds[2];
}ds;

char buf[6];


void out_nibble(char x)
{
CLR_DB4;
CLR_DB5;
CLR_DB6;
CLR_DB7;
if(x &amp; _BV(0)) SET_DB4;
if(x &amp; _BV(1)) SET_DB5;
if(x &amp; _BV(2)) SET_DB6;
if(x &amp; _BV(3)) SET_DB7;
}


void write_to_lcd(char x)
{
SET_E;
out_nibble(x &amp; gt; &amp; gt; 4);
CLR_E;
SET_E;
out_nibble(x);
CLR_E;
_delay_ms(1);


}

void delay(int useconds)
{
int s;
useconds = useconds;
for (s=0; s &amp; lt; useconds;s++);
}

void write_command(char x)
{
CLR_RS;
write_to_lcd(x);
}

void write_char(char x)
{
SET_RS;
write_to_lcd(x);
}

void write_text(char * s)
{
while(*s)
{
write_char(*s);
s++;
}
}

void LCD_init(void)
{
DDR_DB4 |= _BV(DB4);
DDR_DB5 |= _BV(DB5);
DDR_DB6 |= _BV(DB6);
DDR_DB7 |= _BV(DB7);
DDR_E |= _BV(E);
DDR_RS |= _BV(RS);


_delay_ms(15);
CLR_E;
CLR_RS;
char i;
for(i = 0; i &amp; lt; 3; i++)
{
SET_E;
out_nibble(0x03);
CLR_E;
_delay_ms(5);

}
SET_E;
out_nibble(0x02);
CLR_E;
_delay_ms(1);
//delay_ms(1);
write_command(0x28); // interfejs 4-bity, 2-linie, znak 5x7
write_command(0x08); // wy??cz LCD, kursor i miganie
write_command(0x01); // czyśae LCD
write_command(0x06); // bez przesuwania w prawo
write_command(0x0C); // w??cz LCD, bez kursora i mrugania 0x0C
}


void LCD_xy(uint8_t x, uint8_t y)
{
switch(y)
{
case 1: y=0x40; break;
case 2: y=0x14; break;
}
write_command(0x80+y+x);
}

void LCD_clr(void)
{
write_command(0x01);
_delay_ms(1);

LCD_xy(0,0);
}


Download file - link to post