//--------------------------------------------------------------------------------------- // Programm Nr.: 07 // Content : LCD 2x16 characters with HD44780 - Routinen // Author : digital AG Halle // Date : 14.07.2008 // System : ATmega8-16PU - Dai Hoc Hue Development Board //--------------------------------------------------------------------------------------- //--included Files----------------------------------------------------------------------- #include #include //important for the LCD-protocol timing //works only with compiler optimization 00 !!! //--declarations------------------------------------------------------------------------- #ifndef F_CPU #define F_CPU 8000000 //CPU frequency = 8 MHz #endif /*---------------------------------------------------------- In this implementation, the LCD is used in the most easy way the 4-bit-mode. Only 6 lines are needed for this - but you don't read lcd status - you just wait a standard time ----------------------------------------------------------*/ #define LCD_PORT PORTD #define LCD_DDR DDRD #define LCD_RS 0x04 #define LCD_EN 5 //implement in your project only the needed routines! void lcd_enable(void); //setup communication to the lcd void lcd_data(unsigned char temp1); //send a byte to the lcd void lcd_clear(void); //erase all displayed signs void lcd_init(void); //initialise lcd void set_cursor(uint8_t x, uint8_t y); //set cursor to position x,y void lcd_string(char *data); //send a string to the lcd //--MAIN PROGRAMM------------------------------------------------------------------------ int main(void) { //--INITIALISATION--------------------------------------------------------------- DDRD=255; //Port D: all Bits defined as output (for the status LEDs) //--PROGRAMM CODE BEFORE MAIN LOOP (USED JUST ONE TIME)-------------------------- lcd_init(); //call of the lcd-init routine //--MAIN LOOP-------------------------------------------------------------------- while(1) { } return 0; } //--MAIN PROGRAMM END-------------------------------------------------------------------- //--EXTERNAL ROUTINES (FOR THE LCD)------------------------------------------------------ void lcd_data(unsigned char temp1) //send a data byte to the lcd { unsigned char temp2 = temp1; LCD_PORT |= (1<> 4; temp1 = temp1 & 0x0F; LCD_PORT &= 0xF0; LCD_PORT |= temp1; // set lcd_enable(); temp2 = temp2 & 0x0F; LCD_PORT &= 0xF0; LCD_PORT |= temp2; // set lcd_enable(); _delay_us(42); } void lcd_command(unsigned char temp1) //send a command to the lcd { unsigned char temp2 = temp1; LCD_PORT &= ~(1<> 4; // get high nibble temp1 = temp1 & 0x0F; // mask LCD_PORT &= 0xF0; LCD_PORT |= temp1; // set lcd_enable(); temp2 = temp2 & 0x0F; // get and mask low nibble LCD_PORT &= 0xF0; LCD_PORT |= temp2; // set lcd_enable(); _delay_us(42); } void lcd_enable(void) //send the enable puls to the lcd { LCD_PORT |= (1<