// SSD1306 OEL Display example c code for the Raspberry pi // // Displays text on the OLED screen. Address 0x3C ( 0x78 /2 ) // // Original IIC code By James Henderson, 2012. // modified by Doug Rice // /* comment out i2c in black list in /etc/modprobe.d and reboot then run: sudo modprobe i2c-dev sudo chmod o+rw /dev/ic* */ #include #include #include #include #include #include #include #include #include int read_ip; int fd; int fd2; int count; // File descrition //char *fileName = "/dev/i2c-0"; // I needed to change the device char *fileName = "/dev/i2c-1"; char buf[7][128] = { /* 0 */ { 0x80, 0xAF, // Turn on display 0xA5, // LAMP TEST 0xA6, // Inverse display 0x20,0x02, // Set Address mode 0,= horiz,1=vert,2=page. 0x22,0x00,0x07, // set page address 0x21,0x00,0x7F, // set column address 0x8D,0x14, // Turn on charge pump 0xAF, // Turn on display 0xa8,63, // Set Mux Ration 0xA1, // Segment Remap. 0xA0 need Font lower line in Bit8,0xA1 need Font lower line in Bit0 0xC8, // Segment Remap. 0x40, // GDpage start 0xD3,0x3F, // DISPLAY offset ROW0 - L0xD3,0x00,eaves one row of pixels at top. 0xD3,0x3F is better. 0xB0, // Set GDRAM Page Address 0x00,0x10, // Set Column for page addressig 0x20,0x00, // Set Address mode 0,= horiz,1=vert,2=page. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }, { /* 1 */ /* if 0x40 then this write sends data */ 0x40, // Control word 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, { /* 2 */ /* if 0x40 then this write sends data */ 0x40, // Control word 0xFF,0x08,0x08,0x08,0x08,0x08,0x08,0x08, 0x7E,0x81,0x81,0x7E,0, // 0 0x00,0x41,0xFF,0x01,0, // 1 0B01000001, // 2 0B10000111, 0B10001101, 0B01110001, 0, // 0 0B01000010, // 3 0B10000001, 0B10011001, 0B01100110, 0, // 0 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, { /* 3 */ 0x40, 0x00, 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 , // 0 0x00, 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 , // 1 0x00, 0x00, 0x42, 0x61, 0x51, 0x49, 0x46, 0x00 , // 2 0x00, 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31, 0x00 , // 3 0x00, 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 , // 4 0x00, 0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 , // 5 0x00, 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00 , // 6 0x00, 0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00 , // 7 0x00, 0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 , // 8 0x00, 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E, 0x00 , // 9 /* 0x00, 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 , // A 0x00, 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 , // B 0x00, 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 , // C 0x00, 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00 , // D 0x00, 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 , // E 0x00, 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 , // F */ 0 }, { /* 4 */ 0x40, 0x00, 0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00 , // : 0x00, 0x00, 0x00, 0x56, 0x36, 0x00, 0x00, 0x00 , // ; 0x00, 0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00 , // < 0x00, 0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 , // = 0x00, 0x00, 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 , // > 0x00, 0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x00 , // ? 0x00, 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E, 0x00 , // @ 0x00, 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 , // A 0x00, 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 , // B 0x00, 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 , // C 0x00, 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00 , // D 0x00, 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 , // E 0x00, 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 , // F 0 }, { /* 5 */ 0x40, 0x00, 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00, 0x00 , // 0x00, 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55, 0x00 , // 55 0x00, 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00, 0x00 , // ] 0x00, 0x00, 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 , // ^ 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 , // _ 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x00, 0x00 , // ' 0x00, 0x00, 0x20, 0x54, 0x54, 0x54, 0x78, 0x00 , // a 0x00, 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38, 0x00 , // b 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x20, 0x00 , // c 0x00, 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F, 0x00 , // d 0x00, 0x00, 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 , // e 0x00, 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02, 0x00 , // f 0x00, 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C, 0x00 , // g 0 } }; char font8_8[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , // sp 0x00, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00 , // ! 0x00, 0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00 , // " 0x00, 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14, 0x00 , // # 0x00, 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12, 0x00 , // $ 0x00, 0x00, 0x62, 0x64, 0x08, 0x13, 0x23, 0x00 , // % 0x00, 0x00, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00 , // & 0x00, 0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00 , // ' 0x00, 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00, 0x00 , // [ 0x00, 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00, 0x00 , // ] 0x00, 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14, 0x00 , // * 0x00, 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 , // + 0x00, 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00, 0x00 , // , 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 , // - 0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00 , // . 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00 , // / 0x00, 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 , // 0 0x00, 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 , // 1 0x00, 0x00, 0x42, 0x61, 0x51, 0x49, 0x46, 0x00 , // 2 0x00, 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31, 0x00 , // 3 0x00, 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 , // 4 0x00, 0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 , // 5 0x00, 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00 , // 6 0x00, 0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00 , // 7 0x00, 0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 , // 8 0x00, 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E, 0x00 , // 9 0x00, 0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00 , // : 0x00, 0x00, 0x00, 0x56, 0x36, 0x00, 0x00, 0x00 , // ; 0x00, 0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00 , // < 0x00, 0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 , // = 0x00, 0x00, 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 , // > 0x00, 0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x00 , // ? 0x00, 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E, 0x00 , // @ 0x00, 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 , // A 0x00, 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 , // B 0x00, 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 , // C 0x00, 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00 , // D 0x00, 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 , // E 0x00, 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 , // F 0x00, 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A, 0x00 , // G 0x00, 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 , // H 0x00, 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 , // I 0x00, 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 , // J 0x00, 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 , // K 0x00, 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 , // L 0x00, 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x00 , // M 0x00, 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 , // N 0x00, 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 , // O 0x00, 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 , // P 0x00, 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 , // Q 0x00, 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 , // R 0x00, 0x00, 0x46, 0x49, 0x49, 0x49, 0x31, 0x00 , // S 0x00, 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x00 , // T 0x00, 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 , // U 0x00, 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 , // V 0x00, 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 , // W 0x00, 0x00, 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 , // X 0x00, 0x00, 0x07, 0x08, 0x70, 0x08, 0x07, 0x00 , // Y 0x00, 0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00 , // Z 0x00, 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00, 0x00 , // 0x00, 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55, 0x00 , // 55 0x00, 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00, 0x00 , // ] 0x00, 0x00, 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 , // ^ 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 , // _ 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x00, 0x00 , // ' 0x00, 0x00, 0x20, 0x54, 0x54, 0x54, 0x78, 0x00 , // a 0x00, 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38, 0x00 , // b 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x20, 0x00 , // c 0x00, 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F, 0x00 , // d 0x00, 0x00, 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 , // e 0x00, 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02, 0x00 , // f 0x00, 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C, 0x00 , // g 0x00, 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00 , // h 0x00, 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00 , // i 0x00, 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00, 0x00 , // j 0x00, 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00 , // k 0x00, 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00 , // l 0x00, 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78, 0x00 , // m 0x00, 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78, 0x00 , // n 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00 , // o 0x00, 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18, 0x00 , // p 0x00, 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC, 0x00 , // q 0x00, 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08, 0x00 , // r 0x00, 0x00, 0x48, 0x54, 0x54, 0x54, 0x20, 0x00 , // s 0x00, 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20, 0x00 , // t 0x00, 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00 , // u 0x00, 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00 , // v 0x00, 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00 , // w 0x00, 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00 , // x 0x00, 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C, 0x00 , // y 0x00, 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00 , // z 0x00, 0x00, 0x00, 0x06, 0x09, 0x09, 0x06, 0x00 // horiz lines }; void sendSSD1306_oled2(int fd, void * addr, int len ); void sendSSD1306_oled2(int fd, void * addr, int len ){ if ( ( write( fd, (void * ) addr, len ) != len ) ) { // clear the screen printf("Error writing to i2c slave\n"); exit(1); } usleep( 3000 ); }; int iis_start( int address ); int iis_start( int address ){ if ((fd = open(fileName, O_RDWR)) < 0) { // Open port for reading and writing printf("Failed to open i2c port\n"); printf(" run: \n"); printf(" sudo modprobe i2c-dev \n"); printf(" sudo chmod o+rw /dev/i2c* \n"); exit(1); } if (ioctl(fd, I2C_SLAVE, address) < 0) { // Set the port options and set the address of the device we wish to speak to printf("Unable to get bus access to talk to slave\n"); exit(1); } } int iis_close( ); int iis_close( ){ close(fd ); } int disp_buff( char * buff ,int len ); int disp_buff( char * buff ,int len ){ int count; int address = 0x3C; char tmpBuff[9]; char * ptr2 ; // we really need a single write. concatonated writes do not work. len = strlen( buff ); for( count = 0 ; count < len ; count++){ // printf("%c %p,", buff[count], (&buf[4][ ( buff[count]-'0' )*8 ] ) ); ptr2 = &buf[3][ ( buff[count]-'0' )*8 ]; ptr2 = &font8_8[ (buff[count]-' ')*8 ]; tmpBuff[0] = 0x40; tmpBuff[1] = *ptr2++; tmpBuff[2] = *ptr2++; tmpBuff[3] = *ptr2++; tmpBuff[4] = *ptr2++; tmpBuff[5] = *ptr2++; tmpBuff[6] = *ptr2++; tmpBuff[7] = *ptr2++; tmpBuff[8] = *ptr2++; iis_start( address ); write(fd,tmpBuff,9 ); iis_close( address ); } return 1; } int main(int argc, char **argv) { char buff[100]; int buffIndex; int readCnt; char ipChar; printf("**** SSD1306 test program ****\n"); printf( " " ); int address = 0x3C; iis_start( address ); write(fd,buf[0],49); iis_close( address ); /* blank display */ for( count = 0 ; count < 64 ; count++){ iis_start( address ); write(fd,buf[1],17); iis_close( address ); } /* for( count = 0 ; count < 1 ; count++){ iis_start( address ); write(fd,buf[2],32); iis_close( address ); } */ /* iis_start( address ); write(fd,buf[1],8); iis_close( address ); iis_start( address ); write(fd,buf[3],82); iis_close( address ); iis_start( address ); write(fd,buf[4],82); write(fd,buf[4],9); iis_close( address ); */ /* iis_start( address ); write(fd,buf[5],82); iis_close( address ); */ // disp_buff( "1234" ,4 ); // disp_buff( "7777" ,4 ); // disp_buff( "02920262039" ,11 ); // disp_buff( " --- Hello world ---" ,18 ); disp_buff( " THIS IS A TEXT",18); disp_buff( "######!£$%^&*()_-+=[]{}###################" ,18 ); } #ifdef comment #endif //var font8_8[92][8] =