//--------------------------------------------------------------------------------------- // Programm Nr.: 08 // Content : LCD demo programm with keys // 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 uint8_t KEYS; uint8_t KEYS_OLD; //--MAIN PROGRAMM------------------------------------------------------------------------ int main(void) { //--INITIALISATION--------------------------------------------------------------- DDRC=225; PORTC=30; 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 KEYS_OLD=PINC; //--MAIN LOOP-------------------------------------------------------------------- while(1) { KEYS=PINC; if (KEYS_OLD!=KEYS) { if ((KEYS&226)==!(0x02)) //Key 1 { //cursor at start (0,1) lcd_data('T'); lcd_data('e'); lcd_data('s'); lcd_data('t'); } if ((KEYS&228)==!(0x04)) //Key 2 { set_cursor(0,2); //cursor at 2nd row, digit 0 lcd_string("Hello World!"); } if ((KEYS&232)==!(0x08)) //Key 3 { lcd_clear(); //clear display content } if ((KEYS&240)==!(0x10)) //Key 4 { lcd_home(); //set cursor to home position } } KEYS_OLD=KEYS; } 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<