HALLOWELT.C // LED blinken lassen und Ausgabe "Hallo Welt" auf dem LC-Display #include "led.h" #include "lcd.h" int main() { LED_init(); LCD_init(); LCD_instruction(LCD_DISP_ON); LED_blinken(5); LCD_puts("Hallo Welt"); while(1) { LED_blinken(5); } } ____________________________________________________________________________________________________________________________ LED.C #include #include #include "ledmap.h" #define delaytime 500 void LED_init() { //PA1 als Ausgang setzen LED_DDR = LED_DDR | 1 << LED_PIN; } void LED_blinken() { int i; for(i=1; i<=5; i=i+1) { LED_PORT = LED_PORT | (1 << LED_PIN);//LED an _delay_ms(delaytime); LED_PORT = LED_PORT & ~(1 << LED_PIN);//LED aus _delay_ms(delaytime); } } ____________________________________________________________________________________________________________________________ LED.H void LED_init (); void LED_blinken(unsigned char anzahl); ____________________________________________________________________________________________________________________________ LEDMAP.H // LED Verbindungen mit AVR #define LED_DDR DDRA #define LED_PORT PORTA #define LED_PIN PA0 ____________________________________________________________________________________________________________________________ LCD.C /******************************************************************************* File: lcd.c Compiler: AVR-GCC 3.3 Hardware: AVR Date: 04.06.2009 Basic routines for interfacing a HD44780U-based text LC-Display 4bit mode, microcontroller I/O port, write only (rw pin -> GND), dual- or single-line display *******************************************************************************/ #include #include #include #include "lcd.h" #include "lcdmap.h" // execution time for instructions (LCD specific) #define EXETIMELONG 1640 // execution time (us) for CLR and HOME instruction #define EXETIMESHORT 40 // execution time (us) for all other instructions /******************************************************************************* Low-level function to write byte to LCD controller Input: b byte to write to LCD rs 1: data write 0: instruction write *******************************************************************************/ static void write(unsigned char b, _Bool rs) { if(rs) PORT(RS_PORT) |= (1 << RS); else PORT(RS_PORT) &= ~(1 << RS); // output high nibble PORT(EN_PORT) |= (1 << EN); if(b & 0x80) PORT(DB7_PORT) |= (1 << DB7); else PORT(DB7_PORT) &= ~(1 << DB7); if(b & 0x40) PORT(DB6_PORT) |= (1 << DB6); else PORT(DB6_PORT) &= ~(1 << DB6); if(b & 0x20) PORT(DB5_PORT) |= (1 << DB5); else PORT(DB5_PORT) &= ~(1 << DB5); if(b & 0x10) PORT(DB4_PORT) |= (1 << DB4); else PORT(DB4_PORT) &= ~(1 << DB4); PORT(EN_PORT) &= ~(1 << EN); // output low nibble PORT(EN_PORT) |= (1 << EN); if(b & 0x08) PORT(DB7_PORT) |= (1 << DB7); else PORT(DB7_PORT) &= ~(1 << DB7); if(b & 0x04) PORT(DB6_PORT) |= (1 << DB6); else PORT(DB6_PORT) &= ~(1 << DB6); if(b & 0x02) PORT(DB5_PORT) |= (1 << DB5); else PORT(DB5_PORT) &= ~(1 << DB5); if(b & 0x01) PORT(DB4_PORT) |= (1 << DB4); else PORT(DB4_PORT) &= ~(1 << DB4); PORT(EN_PORT) &= ~(1 << EN); } /******************************************************************************* Low-level function to write nibble to LCD controller Input: byte, high nibble is written to LCD *******************************************************************************/ static void write_nibble(unsigned char b) { PORT(RS_PORT) &= ~(1 << RS); // instruction // output high nibble PORT(EN_PORT) |= (1 << EN); if(b & 0x80) PORT(DB7_PORT) |= (1 << DB7); else PORT(DB7_PORT) &= ~(1 << DB7); if(b & 0x40) PORT(DB6_PORT) |= (1 << DB6); else PORT(DB6_PORT) &= ~(1 << DB6); if(b & 0x20) PORT(DB5_PORT) |= (1 << DB5); else PORT(DB5_PORT) &= ~(1 << DB5); if(b & 0x10) PORT(DB4_PORT) |= (1 << DB4); else PORT(DB4_PORT) &= ~(1 << DB4); PORT(EN_PORT) &= ~(1 << EN); } /******************************************************************************* Initialize display, see HD44780 data sheet After initialisation the display is off! *******************************************************************************/ void LCD_init() { // configure RW pin, if available #ifdef RW DDR(RW_PORT) |= (1 << RW); // output PORT(RW_PORT) &= ~(1 << RW); // write #endif // configure data pins as output DDR(DB7_PORT) |= (1 << DB7); DDR(DB6_PORT) |= (1 << DB6); DDR(DB5_PORT) |= (1 << DB5); DDR(DB4_PORT) |= (1 << DB4); // configure control pins as output DDR(RS_PORT) |= (1 << RS); DDR(EN_PORT) |= (1 << EN); _delay_us(15000); // wait for more then 15ms write_nibble(0x30); _delay_us(4100); // wait for more then 4.1ms write_nibble(0x30); _delay_us(100); // wait for more then 100us write_nibble(0x30); _delay_us(EXETIMESHORT); write_nibble(0x20); // Function Set: 4bit Mode _delay_us(EXETIMESHORT); #if DISPLAY_LINES == 1 LCD_instruction(0x20); // Function Set: 1 line, 5x8 dot character font #else LCD_instruction(0x28); // Function Set: 2 lines, 5x8 dot character font #endif LCD_instruction(LCD_DISP_OFF); // Display off, Cursor off, Blinking off LCD_instruction(LCD_CLR); // Clear display LCD_instruction(LCD_ENTRY_INC); // Entry Mode Set: Increment, no shift } /******************************************************************************* Load custom character. This function change the cursor position! Input: pointer to array of char character code (0x00 - 0x07) *******************************************************************************/ void LCD_loadCustomChar(unsigned char* customCharArray, unsigned char charCode) { register unsigned char i; charCode = (charCode << 3); // calculate CG RAM address // copy the 8 bytes into CG (character generator) RAM for(i=0; i<8; i++) { write(0x40 | (charCode + i), 0); // set CG RAM address _delay_us(EXETIMESHORT); write(pgm_read_byte(customCharArray + i), 1); // write character data _delay_us(EXETIMESHORT); } } /******************************************************************************* Send instruction to LCD unit Input: Instruction code (use predefined codes from lcd.h) *******************************************************************************/ void LCD_instruction(unsigned char cmd) { write(cmd, 0); if (cmd==LCD_CLR || cmd==LCD_HOME) _delay_us(EXETIMELONG); else _delay_us(EXETIMESHORT); } /******************************************************************************* Set cursor to specified position Input: vertical position (0: first line) horizontal position (0: left most position) *******************************************************************************/ void LCD_setpos(unsigned char y, unsigned char x) { if (y==0) // first line write(0x80 | x, 0); else // second line write(0x80 | (0x40+x), 0); _delay_us(EXETIMESHORT); } /******************************************************************************* Display character at current cursor position Input: character to be displayed *******************************************************************************/ void LCD_putchar(char c) { write(c, 1); _delay_us(EXETIMESHORT); } /******************************************************************************* Display string without auto linefeed Input: string to be displayed *******************************************************************************/ void LCD_puts(const char *string) { char c; while (c = *string++) LCD_putchar(c); } ____________________________________________________________________________________________________________________________ LCD.H /******************************************************************************* File: lcd.h Compiler: AVR-GCC 3.3 Hardware: AVR Date: 04.06.2009 Basic routines for interfacing a HD44780U-based text LC-Display *******************************************************************************/ // Clear enter display area, restore display from shift, and load address // counter with DD RAM adress 0x00 #define LCD_CLR 0x01 // Restore display from shift, and load address counter with DD RAM adress 0x00 #define LCD_HOME 0x02 // Specify cursor advance direction and display shift mode // This operation takes place after each data transfer #define LCD_ENTRY_DEC 0x04 // cursor moves to the left #define LCD_ENTRY_INC 0x06 // cursor moves to the right #define LCD_ENTRY_DEC_SHIFT 0x05 // shift display to the left #define LCD_ENTRY_INC_SHIFT 0x07 // shift display to the right // Specify activation of display, cursor, and blinking of character #define LCD_DISP_OFF 0x08 // display off #define LCD_DISP_ON 0x0C // display on #define LCD_DISP_ON_CURSOR 0x0E // display on, cursor on #define LCD_DISP_ON_BLINK 0x0D // display on, blink char #define LCD_DISP_ON_CURSOR_BLINK 0x0F // display on, cursor on, blink char // Shift display or move cursor #define LCD_MOVE_CURSOR_LEFT 0x10 // move cursor left (decrement) #define LCD_MOVE_CURSOR_RIGHT 0x14 // move cursor right (increment) #define LCD_MOVE_DISP_LEFT 0x18 // shift display left #define LCD_MOVE_DISP_RIGHT 0x1C // shift display right /******************************************************************************* Initialize display, see HD44780 data sheet After initialisation the display is off! *******************************************************************************/ void LCD_init(); /******************************************************************************* Load custom character. This function change the cursor position! Input: pointer to array of char character code (0x00 - 0x07) *******************************************************************************/ void LCD_loadCustomChar(unsigned char* customCharArray, unsigned char charCode); /******************************************************************************* Send instruction to LCD unit Input: Instruction code (use predefined codes from lcd.h) *******************************************************************************/ void LCD_instruction(unsigned char); /******************************************************************************* Set cursor to specified position Input: vertical position (0: first line) horizontal position (0: left most position) *******************************************************************************/ void LCD_setpos(unsigned char, unsigned char); /******************************************************************************* Display character at current cursor position Input: character to be displayed *******************************************************************************/ void LCD_putchar(char); /******************************************************************************* Display string without auto linefeed Input: string to be displayed *******************************************************************************/ void LCD_puts(const char *); ____________________________________________________________________________________________________________________________ LCDMAP.H /******************************************************************************* File: lcdmap.h Compiler: AVR-GCC 3.3 Hardware: AVR Date: 21.05.2009 LCD hardware settings and pin connections Layout for olimex-board *******************************************************************************/ #define DISPLAY_LINES 1 // Display Lines (1 or 2) #define RS_PORT B // register select #define RS PB0 #define RW_PORT B // read/write #define RW PB1 #define EN_PORT B // enable #define EN PB2 #define DB4_PORT D #define DB4 PD4 #define DB5_PORT D #define DB5 PD5 #define DB6_PORT D #define DB6 PD6 #define DB7_PORT D #define DB7 PD7 // extend register names with port characters #define GLUE(a, b) a##b #define PORT(x) GLUE(PORT, x) #define PIN(x) GLUE(PIN, x) #define DDR(x) GLUE(DDR, x)