1、器件接线图:
0.96寸的oled显示屏(SSD1315)的电源和地分别接开发板扩展端子(J8)的2脚和4脚,显示屏的sda和scl分别接开发板扩展端子(J4)12脚和11脚。
2、软件部分:
各文件结构如下:
2.1、主程序文件main.c
/***** Includes *****/ #include <stdio.h> #include <stdint.h> #include <string.h> #include "board.h" #include "mxc_device.h" #include "mxc_delay.h" #include "nvic_table.h" #include "user_oled.h" /***** Definitions *****/ /***** Globals *****/ uint8_t counter = 0; // ***************************************************************************** int main(void) { OLED_Init(); OLED_CLear(0); GUI_ShowString(0,0,(uint8_t *)"OLED Test",16,0); OLED_Display(); while (1) { LED_On(LED1); MXC_Delay(500000); LED_Off(LED1); MXC_Delay(500000); LED_On(LED2); MXC_Delay(500000); LED_Off(LED2); MXC_Delay(500000); LED_On(LED3); MXC_Delay(500000); LED_Off(LED3); MXC_Delay(500000); printf("counter : %d\n", counter++); } return 0; }
2.2、字体头文件user_fonts.h
/************************************6*8的点阵************************************/ const unsigned char Font6x8[95][6] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // ' ' {0x00, 0x00, 0x5F, 0x00, 0x00, 0x00}, // '!' {0x00, 0x07, 0x00, 0x07, 0x00, 0x00}, // '"' {0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00}, // '#' {0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00}, // '$' {0x23, 0x13, 0x08, 0x64, 0x62, 0x00}, // '%' {0x36, 0x49, 0x55, 0x22, 0x50, 0x00}, // '&' {0x00, 0x05, 0x03, 0x00, 0x00, 0x00}, // ''' {0x00, 0x1C, 0x22, 0x41, 0x00, 0x00}, // '(' {0x00, 0x41, 0x22, 0x1C, 0x00, 0x00}, // ')' {0x14, 0x08, 0x3E, 0x08, 0x14, 0x00}, // '*' {0x08, 0x08, 0x3E, 0x08, 0x08, 0x00}, // '+' {0x00, 0x50, 0x30, 0x00, 0x00, 0x00}, // ',' {0x08, 0x08, 0x08, 0x08, 0x08, 0x00}, // '-' {0x00, 0x60, 0x60, 0x00, 0x00, 0x00}, // '.' {0x20, 0x10, 0x08, 0x04, 0x02, 0x00}, // '/' {0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00}, // '0' {0x00, 0x42, 0x7F, 0x40, 0x00, 0x00}, // '1' {0x42, 0x61, 0x51, 0x49, 0x46, 0x00}, // '2' {0x21, 0x41, 0x45, 0x4B, 0x31, 0x00}, // '3' {0x18, 0x14, 0x12, 0x7F, 0x10, 0x00}, // '4' {0x27, 0x45, 0x45, 0x45, 0x39, 0x00}, // '5' {0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00}, // '6' {0x01, 0x71, 0x09, 0x05, 0x03, 0x00}, // '7' {0x36, 0x49, 0x49, 0x49, 0x36, 0x00}, // '8' {0x06, 0x49, 0x49, 0x29, 0x1E, 0x00}, // '9' {0x00, 0x36, 0x36, 0x00, 0x00, 0x00}, // ':' {0x00, 0x56, 0x36, 0x00, 0x00, 0x00}, // ';' {0x08, 0x14, 0x22, 0x41, 0x00, 0x00}, // '<' {0x14, 0x14, 0x14, 0x14, 0x14, 0x00}, // '=' {0x00, 0x41, 0x22, 0x14, 0x08, 0x00}, // '>' {0x02, 0x01, 0x51, 0x09, 0x06, 0x00}, // '?' {0x32, 0x49, 0x79, 0x41, 0x3E, 0x00}, // '@' {0x7E, 0x11, 0x11, 0x11, 0x7E, 0x00}, // 'A' {0x7F, 0x49, 0x49, 0x49, 0x36, 0x00}, // 'B' {0x3E, 0x41, 0x41, 0x41, 0x22, 0x00}, // 'C' {0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00}, // 'D' {0x7F, 0x49, 0x49, 0x49, 0x41, 0x00}, // 'E' {0x7F, 0x09, 0x09, 0x09, 0x01, 0x00}, // 'F' {0x3E, 0x41, 0x49, 0x49, 0x7A, 0x00}, // 'G' {0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00}, // 'H' {0x00, 0x41, 0x7F, 0x41, 0x00, 0x00}, // 'I' {0x20, 0x40, 0x41, 0x3F, 0x01, 0x00}, // 'J' {0x7F, 0x08, 0x14, 0x22, 0x41, 0x00}, // 'K' {0x7F, 0x40, 0x40, 0x40, 0x40, 0x00}, // 'L' {0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x00}, // 'M' {0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00}, // 'N' {0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00}, // 'O' {0x7F, 0x09, 0x09, 0x09, 0x06, 0x00}, // 'P' {0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00}, // 'Q' {0x7F, 0x09, 0x19, 0x29, 0x46, 0x00}, // 'R' {0x46, 0x49, 0x49, 0x49, 0x31, 0x00}, // 'S' {0x01, 0x01, 0x7F, 0x01, 0x01, 0x00}, // 'T' {0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00}, // 'U' {0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00}, // 'V' {0x7F, 0x20, 0x18, 0x20, 0x7F, 0x00}, // 'W' {0x63, 0x14, 0x08, 0x14, 0x63, 0x00}, // 'X' {0x07, 0x08, 0x70, 0x08, 0x07, 0x00}, // 'Y' {0x61, 0x51, 0x49, 0x45, 0x43, 0x00}, // 'Z' {0x00, 0x7F, 0x41, 0x41, 0x00, 0x00}, // '[' {0x02, 0x04, 0x08, 0x10, 0x20, 0x00}, // '\' {0x00, 0x41, 0x41, 0x7F, 0x00, 0x00}, // ']' {0x04, 0x02, 0x01, 0x02, 0x04, 0x00}, // '^' {0x40, 0x40, 0x40, 0x40, 0x40, 0x00}, // '_' {0x00, 0x01, 0x02, 0x04, 0x00, 0x00}, // '`' {0x20, 0x54, 0x54, 0x54, 0x78, 0x00}, // 'a' {0x7F, 0x48, 0x44, 0x44, 0x38, 0x00}, // 'b' {0x38, 0x44, 0x44, 0x44, 0x20, 0x00}, // 'c' {0x38, 0x44, 0x44, 0x48, 0x7F, 0x00}, // 'd' {0x38, 0x54, 0x54, 0x54, 0x18, 0x00}, // 'e' {0x08, 0x7E, 0x09, 0x01, 0x02, 0x00}, // 'f' {0x0C, 0x52, 0x52, 0x52, 0x3E, 0x00}, // 'g' {0x7F, 0x08, 0x04, 0x04, 0x78, 0x00}, // 'h' {0x00, 0x44, 0x7D, 0x40, 0x00, 0x00}, // 'i' {0x20, 0x40, 0x44, 0x3D, 0x00, 0x00}, // 'j' {0x7F, 0x10, 0x28, 0x44, 0x00, 0x00}, // 'k' {0x00, 0x41, 0x7F, 0x40, 0x00, 0x00}, // 'l' {0x7C, 0x04, 0x18, 0x04, 0x78, 0x00}, // 'm' {0x7C, 0x08, 0x04, 0x04, 0x78, 0x00}, // 'n' {0x38, 0x44, 0x44, 0x44, 0x38, 0x00}, // 'o' {0x7C, 0x14, 0x14, 0x14, 0x08, 0x00}, // 'p' {0x08, 0x14, 0x14, 0x18, 0x7C, 0x00}, // 'q' {0x7C, 0x08, 0x04, 0x04, 0x08, 0x00}, // 'r' {0x48, 0x54, 0x54, 0x54, 0x20, 0x00}, // 's' {0x04, 0x3F, 0x44, 0x40, 0x20, 0x00}, // 't' {0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00}, // 'u' {0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00}, // 'v' {0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00}, // 'w' {0x44, 0x28, 0x10, 0x28, 0x44, 0x00}, // 'x' {0x0C, 0x50, 0x50, 0x50, 0x3C, 0x00}, // 'y' {0x44, 0x64, 0x54, 0x4C, 0x44, 0x00}, // 'z' {0x00, 0x08, 0x36, 0x41, 0x00, 0x00}, // '{' {0x00, 0x00, 0x7F, 0x00, 0x00, 0x00}, // '|' {0x00, 0x41, 0x36, 0x08, 0x00, 0x00}, // '}' {0x10, 0x08, 0x08, 0x10, 0x08, 0x00}, // '~' }; /****************************************8*16的点阵************************************/ const unsigned char Font8X16[]= { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0 0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x18,0x18,0x00,0x00,//! 1 0x00,0x12,0x36,0x24,0x48,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2 0x00,0x00,0x00,0x24,0x24,0x24,0xFE,0x48,0x48,0x48,0xFE,0x48,0x48,0x48,0x00,0x00,//# 3 0x00,0x00,0x10,0x38,0x54,0x54,0x50,0x30,0x1C,0x14,0x14,0x54,0x54,0x38,0x10,0x10,//$ 4 0x00,0x00,0x00,0x44,0xA4,0xA8,0xA8,0xA8,0x54,0x1A,0x2A,0x2A,0x2A,0x44,0x00,0x00,//% 5 0x00,0x00,0x00,0x30,0x48,0x48,0x48,0x50,0x6E,0xA4,0x94,0x88,0x89,0x76,0x00,0x00,//& 6 0x00,0x60,0x60,0x20,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7 0x00,0x02,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x08,0x08,0x04,0x02,0x00,//( 8 0x00,0x40,0x20,0x10,0x10,0x08,0x08,0x08,0x08,0x08,0x08,0x10,0x10,0x20,0x40,0x00,//) 9 0x00,0x00,0x00,0x00,0x10,0x10,0xD6,0x38,0x38,0xD6,0x10,0x10,0x00,0x00,0x00,0x00,//* 10 0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x10,0x00,0x00,0x00,//+ 11 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x20,0xC0,//, 12 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//- 13 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00,//. 14 0x00,0x00,0x01,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x40,0x00,/// 15 0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,//0 16 0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//1 17 0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x04,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00,//2 18 0x00,0x00,0x00,0x3C,0x42,0x42,0x04,0x18,0x04,0x02,0x02,0x42,0x44,0x38,0x00,0x00,//3 19 0x00,0x00,0x00,0x04,0x0C,0x14,0x24,0x24,0x44,0x44,0x7E,0x04,0x04,0x1E,0x00,0x00,//4 20 0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00,//5 21 0x00,0x00,0x00,0x1C,0x24,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x24,0x18,0x00,0x00,//6 22 0x00,0x00,0x00,0x7E,0x42,0x44,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,//7 23 0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00,//8 24 0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x26,0x1A,0x02,0x02,0x24,0x38,0x00,0x00,//9 25 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,//: 26 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x08,0x30,0x00,//; 27 0x00,0x00,0x00,0x02,0x04,0x08,0x10,0x20,0x40,0x20,0x10,0x08,0x04,0x02,0x00,0x00,//< 28 0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,//= 29 0x00,0x00,0x00,0x40,0x20,0x10,0x08,0x04,0x02,0x04,0x08,0x10,0x20,0x40,0x00,0x00,//> 30 0x00,0x00,0x00,0x3C,0x42,0x42,0x62,0x02,0x04,0x08,0x08,0x00,0x18,0x18,0x00,0x00,//? 31 0x00,0x00,0x00,0x38,0x44,0x5A,0xAA,0xAA,0xAA,0xAA,0xB4,0x42,0x44,0x38,0x00,0x00,//@ 32 0x00,0x00,0x00,0x10,0x10,0x18,0x28,0x28,0x24,0x3C,0x44,0x42,0x42,0xE7,0x00,0x00,//A 33 0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x44,0x42,0x42,0x42,0x44,0xF8,0x00,0x00,//B 34 0x00,0x00,0x00,0x3E,0x42,0x42,0x80,0x80,0x80,0x80,0x80,0x42,0x44,0x38,0x00,0x00,//C 35 0x00,0x00,0x00,0xF8,0x44,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x44,0xF8,0x00,0x00,//D 36 0x00,0x00,0x00,0xFE,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x42,0x42,0xFE,0x00,0x00,//E 37 0x00,0x00,0x00,0xFE,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x40,0x40,0xE0,0x00,0x00,//F 38 0x00,0x00,0x00,0x3C,0x44,0x44,0x80,0x80,0x80,0x8E,0x84,0x44,0x44,0x38,0x00,0x00,//G 39 0x00,0x00,0x00,0xE7,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//H 40 0x00,0x00,0x00,0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//I 41 0x00,0x00,0x00,0x3E,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x88,0xF0,//J 42 0x00,0x00,0x00,0xEE,0x44,0x48,0x50,0x70,0x50,0x48,0x48,0x44,0x44,0xEE,0x00,0x00,//K 43 0x00,0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x42,0xFE,0x00,0x00,//L 44 0x00,0x00,0x00,0xEE,0x6C,0x6C,0x6C,0x6C,0x54,0x54,0x54,0x54,0x54,0xD6,0x00,0x00,//M 45 0x00,0x00,0x00,0xC7,0x62,0x62,0x52,0x52,0x4A,0x4A,0x4A,0x46,0x46,0xE2,0x00,0x00,//N 46 0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00,0x00,//O 47 0x00,0x00,0x00,0xFC,0x42,0x42,0x42,0x42,0x7C,0x40,0x40,0x40,0x40,0xE0,0x00,0x00,//P 48 0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0xB2,0xCA,0x4C,0x38,0x06,0x00,//Q 49 0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x48,0x48,0x44,0x44,0x42,0xE3,0x00,0x00,//R 50 0x00,0x00,0x00,0x3E,0x42,0x42,0x40,0x20,0x18,0x04,0x02,0x42,0x42,0x7C,0x00,0x00,//S 51 0x00,0x00,0x00,0xFE,0x92,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00,//T 52 0x00,0x00,0x00,0xE7,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,//U 53 0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00,//V 54 0x00,0x00,0x00,0xD6,0x92,0x92,0x92,0x92,0xAA,0xAA,0x6C,0x44,0x44,0x44,0x00,0x00,//W 55 0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x18,0x18,0x18,0x24,0x24,0x42,0xE7,0x00,0x00,//X 56 0x00,0x00,0x00,0xEE,0x44,0x44,0x28,0x28,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00,//Y 57 0x00,0x00,0x00,0x7E,0x84,0x04,0x08,0x08,0x10,0x20,0x20,0x42,0x42,0xFC,0x00,0x00,//Z 58 0x00,0x1E,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1E,0x00,//[ 59 0x00,0x00,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x04,0x04,0x02,0x02,0x02,0x01,0x01,//\ 60 0x00,0x78,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x78,0x00,//] 61 0x00,0x08,0x14,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,//_ 63 0x00,0x60,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x1E,0x22,0x42,0x42,0x3F,0x00,0x00,//a 65 0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x64,0x58,0x00,0x00,//b 66 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x22,0x40,0x40,0x40,0x22,0x1C,0x00,0x00,//c 67 0x00,0x00,0x00,0x06,0x02,0x02,0x02,0x1E,0x22,0x42,0x42,0x42,0x26,0x1B,0x00,0x00,//d 68 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x7E,0x40,0x40,0x42,0x3C,0x00,0x00,//e 69 0x00,0x00,0x00,0x0F,0x11,0x10,0x10,0x7E,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//f 70 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x44,0x44,0x38,0x40,0x3C,0x42,0x42,0x3C,//g 71 0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//h 72 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//i 73 0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x1C,0x04,0x04,0x04,0x04,0x04,0x04,0x44,0x78,//j 74 0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x4E,0x48,0x50,0x68,0x48,0x44,0xEE,0x00,0x00,//k 75 0x00,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//l 76 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF6,0x49,0x49,0x49,0x49,0x49,0xED,0x00,0x00,//m 77 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDC,0x62,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//n 78 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,//o 79 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x64,0x42,0x42,0x42,0x64,0x58,0x40,0xE0,//p 80 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x22,0x42,0x42,0x42,0x26,0x1A,0x02,0x07,//q 81 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xEE,0x32,0x20,0x20,0x20,0x20,0xF8,0x00,0x00,//r 82 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x42,0x40,0x3C,0x02,0x42,0x7C,0x00,0x00,//s 83 0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x7C,0x10,0x10,0x10,0x10,0x10,0x0C,0x00,0x00,//t 84 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC6,0x42,0x42,0x42,0x42,0x46,0x3B,0x00,0x00,//u 85 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x28,0x10,0x10,0x00,0x00,//v 86 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD7,0x92,0x92,0xAA,0xAA,0x44,0x44,0x00,0x00,//w 87 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6E,0x24,0x18,0x18,0x18,0x24,0x76,0x00,0x00,//x 88 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x28,0x18,0x10,0x10,0xE0,//y 89 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x44,0x08,0x10,0x10,0x22,0x7E,0x00,0x00,//z 90 0x00,0x03,0x04,0x04,0x04,0x04,0x04,0x08,0x04,0x04,0x04,0x04,0x04,0x04,0x03,0x00,//{ 91 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,//| 92 0x00,0x60,0x10,0x10,0x10,0x10,0x10,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x60,0x00,//} 93 0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x52,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94 };
2.3、oled头文件user_oled.h
#include "i2c.h" #define u8 uint8_t #define u32 uint32_t #define SSD1306_ADDR 0x3C // OLED的I2C地址(禁止修改) #define COM 0x00 // OLED 指令(禁止修改) #define DAT 0x40 // OLED 数据(禁止修改) #define OLED_PAGE_SIZE 8 #define XLevelL 0x00 #define XLevelH 0x10 #define YLevel 0xB0 #define Brightness 0xFF #define SSD1306_WIDTH 128 #define SSD1306_HEIGHT 64 int WriteCmd(unsigned char I2C_Command);//写命令 int WriteDat(unsigned char I2C_Data);//写数据 void OLED_Init(void);//初始化 void OLED_Display(void); void OLED_CLear(unsigned char clear_data); void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size,uint8_t mode); void GUI_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size,uint8_t mode);
2.4、user_oled.c
在函数I2c_Init_Fun中添加了从机地址扫描,目前地址只扫描范围是8-70,可以根据实际情况调整。
#include <stdio.h> #include <string.h> #include "mxc_delay.h" #include "mxc_device.h" #include "i2c.h" #include "user_fonts.h" #include "user_oled.h" #define I2C_MASTER MXC_I2C1 // SCL P0_16; SDA P0_17 #define I2C_SCL_PIN 16 #define I2C_SDA_PIN 17 #define I2C_FREQ 100000 // 100kHZ mxc_i2c_req_t req; //OLED显存总共分为8页 //每页8行,一行128个像素点 //数组每个bit存储OLED每个像素点的颜色值(1-亮(白色),0-灭(黑色)) //每个数组元素表示1列8个像素点,一共128列 uint8_t Flash_OLED_Buffer[1025] = {0}; uint8_t * OLED_buffer = Flash_OLED_Buffer+1 ; static void I2c_Init_Fun(void) { int error; //Setup the I2CM error = MXC_I2C_Init(I2C_MASTER, 1, 0); if (error != E_NO_ERROR) { printf("-->I2C Master Initialization failed, error:%d\n", error); } else { printf("\n-->I2C Master Initialization Complete\n"); } printf("-->Scanning started\n"); MXC_I2C_SetFrequency(I2C_MASTER, I2C_FREQ); req.i2c = I2C_MASTER; req.addr = 0; req.tx_buf = NULL; req.tx_len = 0; req.rx_buf = NULL; req.rx_len = 0; req.restart = 0; req.callback = NULL; uint8_t counter = 0; for (uint8_t address = 8; address < 70; address++) { printf("."); fflush(0); req.addr = address; if ((MXC_I2C_MasterTransaction(&req)) == 0) { printf("\nFound slave ID %03d; 0x%02X\n", address, address); counter++; } MXC_Delay(MXC_DELAY_MSEC(200)); } printf("\n-->Scan finished. %d devices found\n", counter); printf("I2c_Init ok\n"); } //写命令 int WriteCmd(unsigned char I2C_Command) { printf("WriteCmd data=%x\n",I2C_Command); uint8_t buffer[2]; buffer[0] = COM; buffer[1] = I2C_Command; req.tx_buf = buffer; req.i2c = I2C_MASTER; // 根据你的硬件选择 req.addr = SSD1306_ADDR; req.tx_len = 2; int ret = MXC_I2C_MasterTransaction(&req); if (ret != 0) { printf("oled WriteCmd I2C Write failed, ret=%d\n", ret); } MXC_Delay(MXC_DELAY_MSEC(5)); // 给总线一些时间 return ret; } int WriteDat(unsigned char I2C_Data) //写数据 { uint8_t buffer[2]; buffer[0] = DAT; buffer[1] = I2C_Data; req.tx_buf = buffer; req.i2c = I2C_MASTER; // 根据你的硬件选择 req.addr = SSD1306_ADDR; req.tx_len = 2; int ret = MXC_I2C_MasterTransaction(&req); if (ret != 0) { printf("oled WriteDat I2C Write failed ret=%d\n",ret); } MXC_Delay(MXC_DELAY_MSEC(5)); // 给总线一些时间 return ret; } void OLED_Init(void) { I2c_Init_Fun(); WriteCmd(0xAE); // 关闭显示 WriteCmd(0x20); // 设置内存寻址模式 WriteCmd(0x00); // 水平寻址模式 WriteCmd(0xB0); // 设置页地址 WriteCmd(0xC8); // 设置扫描方向 WriteCmd(0x00); // 设置列地址低 4 位 WriteCmd(0x10); // 设置列地址高 4 位 WriteCmd(0x40); // 设置显示起始行 WriteCmd(0x81); // 设置对比度 WriteCmd(0xFF); // 最大对比度 WriteCmd(0xA1); // 设置段重映射 WriteCmd(0xA6); // 设置正常显示 WriteCmd(0xA8); // 设置多路复用率 WriteCmd(0x3F); // 1/64 多路复用 WriteCmd(0xA4); // 恢复整体显示 WriteCmd(0xD3); // 设置显示偏移 WriteCmd(0x00); // 无偏移 WriteCmd(0xD5); // 设置时钟分频比/振荡器频率 WriteCmd(0xF0); // 设置分频比 WriteCmd(0xD9); // 设置预充电周期 WriteCmd(0x22); // 设置预充电周期 WriteCmd(0xDA); // 设置 COM 引脚硬件配置 WriteCmd(0x12); // 设置 COM 引脚硬件配置 WriteCmd(0xDB); // 设置 VCOMH 取消选择级别 WriteCmd(0x20); // 设置 VCOMH 取消选择级别 WriteCmd(0x8D); // 设置电荷泵 WriteCmd(0x14); // 启用电荷泵 WriteCmd(0xAF); // 开启显示 printf("OLED_Init done\n"); } void OLED_Set_Pos(unsigned char x, unsigned char y) { WriteCmd(YLevel+y/OLED_PAGE_SIZE); WriteCmd(((x&0xf0)>>4)|0x10); WriteCmd(x&0x0f); } void OLED_Display(void) { WriteCmd(0x21); // 设置列地址 WriteCmd(0x00); // 起始列地址 WriteCmd(SSD1306_WIDTH - 1); // 结束列地址 WriteCmd(0x22); // 设置页地址 WriteCmd(0x00); // 起始页地址 WriteCmd(OLED_PAGE_SIZE - 1); // 结束页地址 Flash_OLED_Buffer[0] = 0x40; req.tx_buf = Flash_OLED_Buffer; req.i2c = I2C_MASTER; // 根据你的硬件选择 req.addr = SSD1306_ADDR; req.tx_len = 1025; int ret = MXC_I2C_MasterTransaction(&req); if (ret != 0) { printf("OLED_Display failed ret=%d\n",ret); } } void OLED_CLear(unsigned char clear_data) { if(clear_data) { memset(OLED_buffer,0xff,SSD1306_WIDTH * OLED_PAGE_SIZE); } else { memset(OLED_buffer,0x00,SSD1306_WIDTH * OLED_PAGE_SIZE); } } void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) { if(color) { OLED_buffer[(y/OLED_PAGE_SIZE)*SSD1306_WIDTH+x]|= (1<<(y%OLED_PAGE_SIZE))&0xff; } else { OLED_buffer[(y/OLED_PAGE_SIZE)*SSD1306_WIDTH+x]&= ~((1<<(y%OLED_PAGE_SIZE))&0xff); } } void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size,uint8_t mode) { unsigned char c=0,i=0,tmp,j=0; c=chr-' ';//得到偏移后的值 if(x>SSD1306_WIDTH-1) { x=0; y=y+2; } if(Char_Size ==16) { for(i=0;i<16;i++) { if(mode) { tmp = Font8X16[c*16+i]; } else { tmp = ~(Font8X16[c*16+i]); } for(j=0;j<8;j++) { if(tmp&(0x80>>j)) { OLED_Set_Pixel(x+j, y+i,1); } else { OLED_Set_Pixel(x+j, y+i,0); } } } } else if(Char_Size==8) { for(i=0;i<8;i++) { if(mode) { tmp = Font6x8[c][i]; } else { tmp = ~(Font6x8[c][i]); } for(j=0;j<8;j++) { if(tmp&(0x80>>j)) { OLED_Set_Pixel(x+j, y+i,1); } else { OLED_Set_Pixel(x+j, y+i,0); } } } } else { return; } } void GUI_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size,uint8_t mode) { unsigned char j=0,csize; if(Char_Size == 16) { csize = Char_Size/2; } else if(Char_Size == 8) { csize = Char_Size/2+2; } else { return; } while (chr[j]!='\0') { OLED_ShowChar(x,y,chr[j],Char_Size,mode); x+=csize; if(x>120) { x=0; y+=Char_Size; } j++; } }
3、结果展示: