// we need several functions from these libraries #include #include #include // default options // output port for the segment leds #ifndef LCDPORT_CHARACTER #define LCDPORT_CHARACTER PORTB #endif // output port for the selection of characters #ifndef LCDPORT_SIGN #define LCDPORT_SIGN PORTA #endif // ddr for selection of segments #ifndef LCDDDR_CHARACTER #define LCDDDR_CHARACTER DDRB #endif // ddr for selection of characters #ifndef LCDDDR_SIGN #define LCDDDR_SIGN DDRA #endif // how often to loop flashing the characters #ifndef LCD_LOOP #define LCD_LOOP 20 #endif #define LCD_WAIT _delay_ms(5) #define LCD_ABS(x) ((x < 0) ? (-x) : (x)) // this is the allocation of the segments /* -- SEG1 -- | | SEG2 SEG3 | | -- SEG4 -- | | SEG5 SEG6 | | -- SEG7 -- SEG8 */ // this is the relationship between segment and port // if your lcd has another relationship, change this here, so you need no change in all character-definitions #define SEG1 PD0 #define SEG2 PD2 #define SEG3 PD3 #define SEG4 PD4 #define SEG5 PD5 #define SEG6 PD6 #define SEG7 PD7 #define SEG8 PD1 // used to make the definition of the characters easier #define segments(s1, s2, s3, s4, s5, s6, s7, s8) ((s1 == 0) ? switchLow(LCDPORT_CHARACTER, SEG1) : switchHigh(LCDPORT_CHARACTER, SEG1)); \ ((s2 == 0) ? switchLow(LCDPORT_CHARACTER, SEG2) : switchHigh(LCDPORT_CHARACTER, SEG2)); \ ((s3 == 0) ? switchLow(LCDPORT_CHARACTER, SEG3) : switchHigh(LCDPORT_CHARACTER, SEG3)); \ ((s4 == 0) ? switchLow(LCDPORT_CHARACTER, SEG4) : switchHigh(LCDPORT_CHARACTER, SEG4)); \ ((s5 == 0) ? switchLow(LCDPORT_CHARACTER, SEG5) : switchHigh(LCDPORT_CHARACTER, SEG5)); \ ((s6 == 0) ? switchLow(LCDPORT_CHARACTER, SEG6) : switchHigh(LCDPORT_CHARACTER, SEG6)); \ ((s7 == 0) ? switchLow(LCDPORT_CHARACTER, SEG7) : switchHigh(LCDPORT_CHARACTER, SEG7)); \ ((s8 == 0) ? switchLow(LCDPORT_CHARACTER, SEG8) : switchHigh(LCDPORT_CHARACTER, SEG8)) void lcd_init() { LCDDDR_CHARACTER = 0xff; LCDDDR_SIGN = 0xff; } void lcd_char(uint8_t character) { if (character == 0 || character == '0') { segments(1,1,1,0,1,1,1,0); } else if (character == 1 || character == '1') { segments(0,0,1,0,0,1,0,0); } else if (character == 2 || character == '2') { segments(1,0,1,1,1,0,1,0); } else if (character == 3 || character == '3') { segments(1,0,1,1,0,1,1,0); } else if (character == 4 || character == '4') { segments(0,1,1,1,0,1,0,0); } else if (character == 5 || character == '5') { segments(1,1,0,1,0,1,1,0); } else if (character == 6 || character == '6') { segments(1,1,0,1,1,1,1,0); } else if (character == 7 || character == '7') { segments(1,0,1,0,0,1,0,0); } else if (character == 8 || character == '8') { segments(1,1,1,1,1,1,1,0); } else if (character == 9 || character == '9') { segments(1,1,1,1,0,1,1,0); } else if (character == 'a' || character == 'A') { segments(1,1,1,1,1,1,0,0); } else if (character == 'b' || character == 'B') { segments(0,1,0,1,1,1,1,0); } else if (character == 'c' || character == 'C') { segments(1,1,0,0,1,0,1,0); } else if (character == 'd' || character == 'D') { segments(0,0,1,1,1,1,1,0); } else if (character == 'e' || character == 'E') { segments(1,1,0,1,1,0,1,0); } else if (character == 'f' || character == 'F') { segments(1,1,0,1,1,0,0,0); } else if (character == 'g' || character == 'G') { segments(1,1,1,1,0,1,1,0); } else if (character == 'h' || character == 'H') { segments(0,1,1,1,1,1,0,0); } else if (character == 'i' || character == 'I') { segments(0,1,0,0,1,0,0,0); } else if (character == 'j' || character == 'J') { segments(0,0,1,0,1,1,1,0); } else if (character == 'k' || character == 'K') { segments(0,1,1,1,1,1,0,0); } else if (character == 'l' || character == 'L') { segments(0,1,0,0,1,0,1,0); } else if (character == 'm' || character == 'M') { segments(1,0,0,0,1,1,0,0); } else if (character == 'n' || character == 'N') { segments(0,0,0,1,1,1,0,0); } else if (character == 'o' || character == 'O') { segments(1,1,1,0,1,1,1,0); } else if (character == 'p' || character == 'P') { segments(1,1,1,1,1,0,0,0); } else if (character == 'q' || character == 'Q') { segments(1,1,1,1,0,1,0,0); } else if (character == 'r' || character == 'R') { segments(0,0,0,1,1,0,0,0); } else if (character == 's' || character == 'S') { segments(1,1,0,1,0,1,1,0); } else if (character == 't' || character == 'T') { segments(0,1,0,1,1,0,1,0); } else if (character == 'u' || character == 'U') { segments(0,1,1,0,1,1,1,0); } else if (character == 'v' || character == 'V') { segments(0,0,0,0,1,1,1,0); } else if (character == 'w' || character == 'W') { segments(0,1,1,0,0,0,1,0); } else if (character == 'x' || character == 'X') { segments(0,1,1,1,1,1,0,0); } else if (character == 'y' || character == 'Y') { segments(0,1,1,1,0,1,1,0); } else if (character == 'z' || character == 'Z') { segments(1,0,1,1,1,0,1,0); } else if (character == '-') { segments(0,0,0,1,0,0,0,0); } else if (character == '_') { segments(0,0,0,0,0,0,1,0); } else if (character == ' ') { segments(0,0,0,0,0,0,0,0); } } void lcd_sign(uint8_t sign) { if (sign == 0) { switchHigh(LCDPORT_SIGN, PA0); switchLow(LCDPORT_SIGN, PA2); switchLow(LCDPORT_SIGN, PA4); switchLow(LCDPORT_SIGN, PA6); } else if (sign == 1) { switchLow(LCDPORT_SIGN, PA0); switchHigh(LCDPORT_SIGN, PA2); switchLow(LCDPORT_SIGN, PA4); switchLow(LCDPORT_SIGN, PA6); } else if (sign == 2) { switchLow(LCDPORT_SIGN, PA0); switchLow(LCDPORT_SIGN, PA2); switchHigh(LCDPORT_SIGN, PA4); switchLow(LCDPORT_SIGN, PA6); } else if (sign == 3) { switchLow(LCDPORT_SIGN, PA0); switchLow(LCDPORT_SIGN, PA2); switchLow(LCDPORT_SIGN, PA4); switchHigh(LCDPORT_SIGN, PA6); } else { switchLow(LCDPORT_SIGN, PA0); switchLow(LCDPORT_SIGN, PA2); switchLow(LCDPORT_SIGN, PA4); switchLow(LCDPORT_SIGN, PA6); } } uint8_t lcd_str_len(char * str) { for(uint8_t i = 0; i < 255; i++) { if (str[i] == 0) { return i; } } return 0; } void lcd_str(char * str) { uint8_t strlength = lcd_str_len(str); for(uint8_t l = 0; l < ((strlength <= 4) ? 1 : (strlength - 3)); l++) { for(uint32_t i = 0; i < LCD_LOOP / 2; i++) { if (strlength == 0) { lcd_char(' '); } else { lcd_char(str[l]); } lcd_sign(0x00); LCD_WAIT; if (strlength <= 1) { lcd_char(' '); } else { lcd_char(str[l + 1]); } lcd_sign(0x01); LCD_WAIT; if (strlength <= 2) { lcd_char(' '); } else { lcd_char(str[l + 2]); } lcd_sign(0x02); LCD_WAIT; if (strlength <= 3) { lcd_char(' '); } else { lcd_char(str[l + 3]); } lcd_sign(0x03); LCD_WAIT; lcd_sign(0x04); } } } void lcd_uint16(uint16_t number) { uint16_t saveNumber = number; for(uint32_t i = 0; i < LCD_LOOP; i++) { number = saveNumber; if (number > 9999) { lcd_str("----"); return; } lcd_char(number % 10); lcd_sign(0x03); LCD_WAIT; number /= 10; if (number > 0) { lcd_char(number % 10); } else { lcd_char(' '); } lcd_sign(0x02); LCD_WAIT; number /= 10; if (number > 0) { lcd_char(number % 10); } else { lcd_char(' '); } lcd_sign(0x01); LCD_WAIT; number /= 10; if (number > 0) { lcd_char(number % 10); } else { lcd_char(' '); } lcd_sign(0x00); LCD_WAIT; lcd_sign(0x04); } } void lcd_int16(int16_t number) { int16_t saveNumber = number; for(uint32_t i = 0; i < LCD_LOOP; i++) { number = saveNumber; if (number < -999 || number > 9999) { lcd_str("----"); return; } uint8_t negative = number < 0 ? 1 : 0; lcd_char(LCD_ABS(number) % 10); lcd_sign(0x03); LCD_WAIT; number /= 10; if (LCD_ABS(number) > 0) { lcd_char(LCD_ABS(number) % 10); } else { lcd_char(' '); } lcd_sign(0x02); LCD_WAIT; number /= 10; if (LCD_ABS(number) > 0) { lcd_char(LCD_ABS(number) % 10); } else { lcd_char(' '); } lcd_sign(0x01); LCD_WAIT; number /= 10; if (negative == 1) { lcd_char('-'); } else if (number > 0) { lcd_char(LCD_ABS(number) % 10); } else { lcd_char(' '); } lcd_sign(0x00); LCD_WAIT; lcd_sign(0x04); } }