/* ----------------------------------------------------- oled_2313demo.c Minimalprogramm fuer Textausgabe auf einem OLED-Display mit SSD1306 oder SSD1315 Display, explizit fuer einen ATtiny2313 Mikrocontroller MCU : ATtiny2313 Takt : 8 MHz Fuses : Low: 0xe4 | High: 0xdf | Ext: 0xff IDE : keine 25.02.2026 R. seelig ------------------------------------------------------ */ /* ATTINY2313 Anschlusspins IC +-------------+ /reset - PA2 | 1 20 | Vcc RxD - PD0 | 2 19 | PB7 - UCSK - SCL - PCINT7 TxD - PD1 | 3 A 18 | PB6 - MISO - DO - PCINT6 XTAL2 - PA1 | 4 T 17 | PB5 - MOSI - DI - SDA - PCINT5 XTAL1 - PA0 | 5 t 16 | PB4 - OC1B - PCINT4 CKOUT - XCK - INT0 PD2 | 6 i 15 | PB3 - OC1A - PCINT3 INT1 - PD3 | 7 n 14 | PB2 - OC0A - PCINT2 T0 - PD4 | 8 y 13 | PB1 - AIN1 - PCINT1 OC0B - T1 - PD5 | 9 12 | PB0 - AIN0 - PCINT0 GND | 10 11 | PD6 - ICP +-------------+ */ #include #include #include #include /* ------------------------------------------------------------ Konfiguration ------------------------------------------------------------ */ #define i2c_delay 0 // Verzoegerungszeit fuer die I2C Bitbanging Funktion, normalerweise 0 #define oled_addr 0x3c // Deviceadresse des Displays #define oled_width 128 // x-Aufloesung in Pixel #define oled_pages 8 // y-Aufloesung in Reihen. Jede Reihe hat 8 Pixel, daher 8 => 64 Pixel #define oled_is_1315 0 // 0 = ssd1306, 1 = ssd1315 /* ------------------------------------------------------------ Anschlusspins I2C Bitbanging ------------------------------------------------------------ */ // Portpins fuer I2C (Bitbanging): SDA = PD4, SCL = PD3 // Hinweis: Da diese Software mittels I2C-Bitbanging realisiert ist und ein evtl. Konflikt mit // den Anschlusspins des ISP-Programmers (mosi, miso, clk) ausgeschlossen werden soll // wurden die I/O Pins NICHT auf die Pins des USI gelegt, mittels derer auch ein I2C // in Hardware realisiert werden kann #define i2c_port PORTD #define i2c_ddr DDRD #define i2c_pinr PIND #define sda_bit 4 #define scl_bit 3 // Bitbanging der Pins. Logische 1 wird durch setzen des Anschlusses auf Input !!! // bewirkt, der PullUp des Busses legt den Pin somit auf 1. // logisch 0 wird erreicht durch setzen des Ausganges auf 0 und setzen des Pins auf 0 #define sda_high() (i2c_ddr &= ~(1<> 4)); // hohes Nibble der Spalte } /* ------------------------------------------------------------ oled_clear loescht den Displayinhalt ------------------------------------------------------------ */ void oled_clear(void) { uint8_t i,p; for(p=0; p 127) { return; } for(col=0; col<8; col++) { oled_data(readfont(ascii,col)); } } /* ------------------------------------------------------------ oled_puts gibt einen String auf dem Display aus ------------------------------------------------------------ */ void oled_puts(const char *s) { while(*s) { oled_putchar(*s++); } } /* ------------------------------------------------------------ oled_putint gibt einen Integer dezimal aus. Ist Uebergabe "komma" != 0 wird ein "Kommapunkt" mit ausgegeben. Bsp.: oled_putint(12345,2) wird als 123.45 ausgegeben. (ermoeglicht Pseudofloatausgaben im Bereich) ------------------------------------------------------------ */ void oled_putint(int16_t i, char komma) { typedef enum boolean { FALSE, TRUE }bool_t; static uint16_t zz[] = { 10000, 1000, 100, 10 }; bool_t not_first = FALSE; uint8_t zi; int16_t z, b; komma= 5-komma; if (!i) { oled_putchar('0'); } else { if(i < 0) { oled_putchar('-'); i = -i; } for(zi = 0; zi < 4; zi++) { z = 0; b = 0; if ((zi== komma) && komma) { if (!not_first) oled_putchar('0'); oled_putchar('.'); not_first= TRUE; } while(z + zz[zi] <= i) { b++; z += zz[zi]; } if(b || not_first) { oled_putchar('0' + b); not_first = TRUE; } i -= z; } if (komma== 4) oled_putchar('.'); oled_putchar('0' + i); } } /* ------------------------------------------------------------ main ------------------------------------------------------------ */ int main(void) { uint8_t counter = 0; oled_init(); oled_gotoxy(3,1); oled_puts("Hallo Welt"); oled_gotoxy(0,2); oled_puts("----------------"); oled_gotoxy(1,4); oled_puts("Counter"); while(1) { oled_gotoxy(10,4); oled_puts(" "); // Ausgabeposition des uint8_t loeschen // damit bei einem Ueberlauf kein Rest auf // der Anzeige verbleibt oled_gotoxy(10,4); oled_putint(counter, 0); counter++; _delay_ms(500); } } /* ------------------------------------------------------------ Bitmap eines 8x8 Pixel Zeichensatzes ------------------------------------------------------------ */ const uint8_t font8x8[][8] PROGMEM = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Ascii 32 = ' ' { 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x00, 0x00, 0x00 }, // Ascii 33 = '!' { 0x00, 0x07, 0x07, 0x00, 0x07, 0x07, 0x00, 0x00 }, // Ascii 34 = '"' { 0x14, 0x7f, 0x7f, 0x14, 0x7f, 0x7f, 0x14, 0x00 }, // Ascii 35 = '#' { 0x00, 0x24, 0x2a, 0x7f, 0x7f, 0x2a, 0x12, 0x00 }, // Ascii 36 = '$' { 0x00, 0x46, 0x66, 0x30, 0x18, 0x0c, 0x66, 0x62 }, // Ascii 37 = '%' { 0x00, 0x30, 0x7a, 0x4f, 0x5d, 0x37, 0x7a, 0x48 }, // Ascii 38 = '&' { 0x00, 0x00, 0x04, 0x07, 0x03, 0x00, 0x00, 0x00 }, // Ascii 39 = ''' { 0x00, 0x00, 0x1c, 0x3e, 0x63, 0x41, 0x00, 0x00 }, // Ascii 40 = '(' { 0x00, 0x00, 0x41, 0x63, 0x3e, 0x1c, 0x00, 0x00 }, // Ascii 41 = ')' { 0x08, 0x2a, 0x3e, 0x1c, 0x1c, 0x3e, 0x2a, 0x08 }, // Ascii 42 = '*' { 0x00, 0x08, 0x08, 0x3e, 0x3e, 0x08, 0x08, 0x00 }, // Ascii 43 = '+' { 0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00 }, // Ascii 44 = ',' { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 }, // Ascii 45 = '-' { 0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00 }, // Ascii 46 = '.' { 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x01, 0x00 }, // Ascii 47 = '/' { 0x3e, 0x7f, 0x51, 0x49, 0x45, 0x7f, 0x3e, 0x00 }, // Ascii 48 = '0' { 0x00, 0x00, 0x02, 0x7f, 0x7f, 0x00, 0x00, 0x00 }, // Ascii 49 = '1' { 0x00, 0x42, 0x63, 0x71, 0x59, 0x4f, 0x46, 0x00 }, // Ascii 50 = '2' { 0x00, 0x22, 0x63, 0x49, 0x49, 0x7f, 0x36, 0x00 }, // Ascii 51 = '3' { 0x18, 0x1c, 0x16, 0x13, 0x7f, 0x7f, 0x10, 0x00 }, // Ascii 52 = '4' { 0x00, 0x27, 0x67, 0x45, 0x45, 0x7d, 0x39, 0x00 }, // Ascii 53 = '5' { 0x00, 0x3e, 0x7f, 0x49, 0x49, 0x7b, 0x32, 0x00 }, // Ascii 54 = '6' { 0x00, 0x01, 0x01, 0x71, 0x79, 0x0f, 0x07, 0x00 }, // Ascii 55 = '7' { 0x00, 0x36, 0x7f, 0x49, 0x49, 0x7f, 0x36, 0x00 }, // Ascii 56 = '8' { 0x00, 0x06, 0x4f, 0x69, 0x39, 0x1f, 0x0e, 0x00 }, // Ascii 57 = '9' { 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x00, 0x00, 0x00 }, // Ascii 58 = ':' { 0x00, 0x00, 0x01, 0x6d, 0x6c, 0x00, 0x00, 0x00 }, // Ascii 59 = ';' { 0x00, 0x08, 0x1c, 0x36, 0x63, 0x41, 0x00, 0x00 }, // Ascii 60 = '<' { 0x00, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x00 }, // Ascii 61 = '=' { 0x00, 0x41, 0x63, 0x36, 0x1c, 0x08, 0x00, 0x00 }, // Ascii 62 = '>' { 0x00, 0x02, 0x03, 0x51, 0x59, 0x0f, 0x06, 0x00 }, // Ascii 63 = '?' { 0x3e, 0x7f, 0x41, 0x5d, 0x5d, 0x5f, 0x1e, 0x00 }, // Ascii 64 = '@' { 0x7c, 0x7e, 0x13, 0x11, 0x13, 0x7e, 0x7c, 0x00 }, // Ascii 65 = 'A' { 0x7f, 0x7f, 0x49, 0x49, 0x49, 0x7f, 0x36, 0x00 }, // Ascii 66 = 'B' { 0x1c, 0x3e, 0x63, 0x41, 0x41, 0x63, 0x22, 0x00 }, // Ascii 67 = 'C' { 0x7f, 0x7f, 0x41, 0x41, 0x63, 0x3e, 0x1c, 0x00 }, // Ascii 68 = 'D' { 0x7f, 0x7f, 0x49, 0x49, 0x49, 0x41, 0x41, 0x00 }, // Ascii 69 = 'E' { 0x7f, 0x7f, 0x09, 0x09, 0x09, 0x01, 0x01, 0x00 }, // Ascii 70 = 'F' { 0x1c, 0x3e, 0x63, 0x41, 0x51, 0x73, 0x72, 0x00 }, // Ascii 71 = 'G' { 0x7f, 0x7f, 0x08, 0x08, 0x08, 0x7f, 0x7f, 0x00 }, // Ascii 72 = 'H' { 0x00, 0x41, 0x7f, 0x7f, 0x41, 0x00, 0x00, 0x00 }, // Ascii 73 = 'I' { 0x30, 0x70, 0x40, 0x40, 0x40, 0x7f, 0x3f, 0x00 }, // Ascii 74 = 'J' { 0x7f, 0x7f, 0x09, 0x1d, 0x36, 0x63, 0x41, 0x00 }, // Ascii 75 = 'K' { 0x7f, 0x7f, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 }, // Ascii 76 = 'L' { 0x7f, 0x7f, 0x06, 0x0c, 0x06, 0x7f, 0x7f, 0x00 }, // Ascii 77 = 'M' { 0x7f, 0x7f, 0x06, 0x0c, 0x18, 0x7f, 0x7f, 0x00 }, // Ascii 78 = 'N' { 0x1c, 0x3e, 0x63, 0x41, 0x63, 0x3e, 0x1c, 0x00 }, // Ascii 79 = 'O' { 0x7f, 0x7f, 0x09, 0x09, 0x09, 0x0f, 0x06, 0x00 }, // Ascii 80 = 'P' { 0x1c, 0x3e, 0x63, 0x51, 0x33, 0x6e, 0x5c, 0x00 }, // Ascii 81 = 'Q' { 0x7f, 0x7f, 0x09, 0x09, 0x19, 0x7f, 0x66, 0x00 }, // Ascii 82 = 'R' { 0x26, 0x6f, 0x49, 0x49, 0x49, 0x7b, 0x32, 0x00 }, // Ascii 83 = 'S' { 0x01, 0x01, 0x7f, 0x7f, 0x01, 0x01, 0x00, 0x00 }, // Ascii 84 = 'T' { 0x3f, 0x7f, 0x40, 0x40, 0x40, 0x7f, 0x3f, 0x00 }, // Ascii 85 = 'U' { 0x1f, 0x3f, 0x60, 0x40, 0x60, 0x3f, 0x1f, 0x00 }, // Ascii 86 = 'V' { 0x7f, 0x7f, 0x30, 0x18, 0x30, 0x7f, 0x7f, 0x00 }, // Ascii 87 = 'W' { 0x63, 0x77, 0x1c, 0x08, 0x1c, 0x77, 0x63, 0x00 }, // Ascii 88 = 'X' { 0x00, 0x07, 0x0f, 0x78, 0x78, 0x0f, 0x07, 0x00 }, // Ascii 89 = 'Y' { 0x41, 0x61, 0x71, 0x59, 0x4d, 0x47, 0x43, 0x00 }, // Ascii 90 = 'Z' { 0x00, 0x00, 0x7f, 0x7f, 0x41, 0x41, 0x00, 0x00 }, // Ascii 91 = '[' { 0x01, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x00 }, // Ascii 92 = '\' { 0x00, 0x00, 0x41, 0x41, 0x7f, 0x7f, 0x00, 0x00 }, // Ascii 93 = ']' { 0x00, 0x08, 0x0c, 0x06, 0x03, 0x06, 0x0c, 0x08 }, // Ascii 94 = '^' { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }, // Ascii 95 = '_' { 0x00, 0x00, 0x01, 0x03, 0x06, 0x04, 0x00, 0x00 }, // Ascii 96 = '`' { 0x20, 0x74, 0x54, 0x54, 0x54, 0x7c, 0x78, 0x00 }, // Ascii 97 = 'a' { 0x7f, 0x7f, 0x44, 0x44, 0x44, 0x7c, 0x38, 0x00 }, // Ascii 98 = 'b' { 0x38, 0x7c, 0x44, 0x44, 0x44, 0x6c, 0x28, 0x00 }, // Ascii 99 = 'c' { 0x38, 0x7c, 0x44, 0x44, 0x44, 0x7f, 0x7f, 0x00 }, // Ascii 100 = 'd' { 0x38, 0x7c, 0x54, 0x54, 0x54, 0x5c, 0x18, 0x00 }, // Ascii 101 = 'e' { 0x08, 0x7e, 0x7f, 0x09, 0x09, 0x03, 0x02, 0x00 }, // Ascii 102 = 'f' { 0x98, 0xbc, 0xa4, 0xa4, 0xa4, 0xfc, 0x7c, 0x00 }, // Ascii 103 = 'g' { 0x7f, 0x7f, 0x04, 0x04, 0x04, 0x7c, 0x78, 0x00 }, // Ascii 104 = 'h' { 0x00, 0x00, 0x00, 0x7d, 0x7d, 0x00, 0x00, 0x00 }, // Ascii 105 = 'i' { 0x40, 0xc0, 0x80, 0x80, 0xfd, 0x7d, 0x00, 0x00 }, // Ascii 106 = 'j' { 0x7f, 0x7f, 0x10, 0x10, 0x38, 0x6c, 0x44, 0x00 }, // Ascii 107 = 'k' { 0x00, 0x00, 0x00, 0x7f, 0x7f, 0x00, 0x00, 0x00 }, // Ascii 108 = 'l' { 0x78, 0x7c, 0x0c, 0x18, 0x0c, 0x7c, 0x78, 0x00 }, // Ascii 109 = 'm' { 0x7c, 0x7c, 0x04, 0x04, 0x04, 0x7c, 0x78, 0x00 }, // Ascii 110 = 'n' { 0x38, 0x7c, 0x44, 0x44, 0x44, 0x7c, 0x38, 0x00 }, // Ascii 111 = 'o' { 0xfc, 0xfc, 0x24, 0x24, 0x24, 0x3c, 0x18, 0x00 }, // Ascii 112 = 'p' { 0x18, 0x3c, 0x24, 0x24, 0x24, 0xfc, 0xfc, 0x00 }, // Ascii 113 = 'q' { 0x7c, 0x7c, 0x04, 0x04, 0x04, 0x0c, 0x08, 0x00 }, // Ascii 114 = 'r' { 0x48, 0x5c, 0x54, 0x54, 0x54, 0x74, 0x20, 0x00 }, // Ascii 115 = 's' { 0x04, 0x3f, 0x7f, 0x44, 0x44, 0x64, 0x20, 0x00 }, // Ascii 116 = 't' { 0x3c, 0x7c, 0x40, 0x40, 0x40, 0x7c, 0x7c, 0x00 }, // Ascii 117 = 'u' { 0x1c, 0x3c, 0x60, 0x40, 0x60, 0x3c, 0x1c, 0x00 }, // Ascii 118 = 'v' { 0x3c, 0x7c, 0x60, 0x30, 0x60, 0x7c, 0x3c, 0x00 }, // Ascii 119 = 'w' { 0x44, 0x6c, 0x38, 0x10, 0x38, 0x6c, 0x44, 0x00 }, // Ascii 120 = 'x' { 0x9c, 0xbc, 0xa0, 0xa0, 0xa0, 0xfc, 0x7c, 0x00 }, // Ascii 121 = 'y' { 0x00, 0x44, 0x64, 0x74, 0x5c, 0x4c, 0x44, 0x00 }, // Ascii 122 = 'z' { 0x00, 0x08, 0x08, 0x3e, 0x77, 0x41, 0x41, 0x00 }, // Ascii 123 = '{' { 0x00, 0x00, 0x00, 0x77, 0x77, 0x00, 0x00, 0x00 }, // Ascii 124 = '|' { 0x00, 0x41, 0x41, 0x77, 0x3e, 0x08, 0x08, 0x00 }, // Ascii 125 = '}' { 0x00, 0x02, 0x01, 0x01, 0x02, 0x02, 0x01, 0x00 }, // Ascii 126 = '~' { 0x70, 0x78, 0x4c, 0x46, 0x46, 0x4c, 0x78, 0x70 }, // Ascii 127 = '' { 0x3c, 0x42, 0x81, 0x81, 0x81, 0x42, 0x3c, 0x00 }, // Ascii 128 abweichend: rundes o { 0x3c, 0x42, 0xbd, 0xbd, 0xbd, 0x42, 0x3c, 0x00 }, // Ascii 129 abweichend: ausgefuelltes o { 0x00, 0x00, 0x0e, 0x11, 0x11, 0x11, 0x0e, 0x00 } // Ascii 130 abweichend: hochgestelltes o };