简介
-  U8g2 还包括 U8x8 库。U8g2 和 U8x8 的功能包括: - U8g2
 包括所有图形程序(线/框/圆画)
 支持很丰富的字体库
 需要微控制器中的一些内存来渲染显示屏(需要消耗较多的ram空间资源)
- U8x8
 仅文本输出(字符)设备
 仅允许使用每个字符固定大小(8x8 像素)的字体
 直接写到显示屏上,无需微控制器中的缓冲(需要消耗较少的ram空间资源)
 
- U8g2
-  像素点点阵 OLED其实就是一个M x n 的像素点阵,想显示什么就得把具体位置的像素点亮起来。对于每一个像素点,有可能是1点亮,也有可能是0点亮 
-  坐标系 在坐标系中,左上角是原点,向右是X轴正方向,向下是Y轴正方向。 
回调函数
这里只有使用IIC驱动OLED的代码示例
软件IIC
//延时和GPIO初始化 回调函数
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){switch(msg){case U8X8_MSG_GPIO_AND_DELAY_INIT:	// called once during init phase of u8g2/u8x8delay_init();//初始化延时函数 IIC_Init();//软件IIC的IO初始化break;							// can be used to setup pinscase U8X8_MSG_DELAY_NANO:			// delay arg_int * 1 nano secondbreak;case U8X8_MSG_DELAY_100NANO:		// delay arg_int * 100 nano seconds__NOP();break;case U8X8_MSG_DELAY_10MICRO:		// delay arg_int * 10 micro secondsdelay_us(10*arg_int);break;case U8X8_MSG_DELAY_MILLI:			// delay arg_int * 1 milli seconddelay_ms(1*arg_int);break;case U8X8_MSG_DELAY_I2C:				// arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHzdelay_us(5*arg_int);break;							// arg_int=1: delay by 5us, arg_int = 4: delay by 1.25uscase U8X8_MSG_GPIO_D0:				// D0 or SPI clock pin: Output level in arg_int//case U8X8_MSG_GPIO_SPI_CLOCK:break;case U8X8_MSG_GPIO_D1:				// D1 or SPI data pin: Output level in arg_int//case U8X8_MSG_GPIO_SPI_DATA:break;case U8X8_MSG_GPIO_D2:				// D2 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D3:				// D3 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D4:				// D4 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D5:				// D5 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D6:				// D6 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_D7:				// D7 pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_E:				// E/WR pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_CS:				// CS (chip select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_DC:				// DC (data/cmd, A0, register select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_RESET:			// Reset pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_CS1:				// CS1 (chip select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_CS2:				// CS2 (chip select) pin: Output level in arg_intbreak;case U8X8_MSG_GPIO_I2C_CLOCK:		// arg_int=0: Output low at I2C clock pinPBout(8) = arg_int;break;							// arg_int=1: Input dir with pullup high for I2C clock pincase U8X8_MSG_GPIO_I2C_DATA:			// arg_int=0: Output low at I2C data pinPBout(9) = arg_int;break;							// arg_int=1: Input dir with pullup high for I2C data pincase U8X8_MSG_GPIO_MENU_SELECT:u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);break;case U8X8_MSG_GPIO_MENU_NEXT:u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);break;case U8X8_MSG_GPIO_MENU_PREV:u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);break;case U8X8_MSG_GPIO_MENU_HOME:u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);break;default:u8x8_SetGPIOResult(u8x8, 1);			// default return valuebreak;}return 1;
}//测试主程序
int main(void) {//初始化u8g2u8g2_t u8g2;                                                                              // a structure which will contain all the data for one displayu8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay);// init u8g2 structureu8g2_InitDisplay(&u8g2);                                                                      // send init sequence to the display, display is in sleep mode after this,u8g2_SetPowerSave(&u8g2, 0);                                                                  // wake up displaywhile (1){/* Begin of U8G2*/static int x = 30,y = 10;u8g2_ClearBuffer(&u8g2);u8g2_SetFont(&u8g2, u8g2_font_10x20_mf);u8g2_DrawStr(&u8g2, x,y,"Data");if(x >= 70){x = y = 0;}else{x++;y++;}u8g2_SendBuffer(&u8g2);/* End of U8G2 */}
}
硬件IIC
下面代码适用于stm32f1系列的引脚IIC
//为u8g2提供IIC的API接口的函数
uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{static uint8_t buffer[32];		/* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */static uint8_t buf_idx;uint8_t *data;u8 i;switch(msg){case U8X8_MSG_BYTE_INIT://Init the IIC GPIOIIC_Init();break;case U8X8_MSG_BYTE_START_TRANSFER://clear the indexbuf_idx = 0;break;case U8X8_MSG_BYTE_SEND:data = (uint8_t *)arg_ptr;while( arg_int > 0 ){buffer[buf_idx++] = *data;data++;arg_int--;}break;case U8X8_MSG_BYTE_END_TRANSFER:IIC_Start();IIC_Send_Address_Write(0x78);//write your oled address in herefor(i = 0; i < buf_idx; i++){IIC_Send_Byte(buffer[i]);}IIC_Stop();break;case U8X8_MSG_BYTE_SET_DC:/* ignored for i2c */break;default:return 0;}return 1;
}//为u8g2提供延时函数接口的函数
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{switch(msg){case U8X8_MSG_GPIO_AND_DELAY_INIT:delay_init();	    //延时函数初始化break;case U8X8_MSG_DELAY_MILLI:delay_ms(arg_int);break;case U8X8_MSG_GPIO_I2C_CLOCK:		break;							case U8X8_MSG_GPIO_I2C_DATA:			break;default:	return 0;}return 1; // command processed successfully.
}//测试主程序
void TestIO_Init(void){GPIO_InitTypeDef  GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	 		//使能端口时钟GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;	    		//端口配置GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 	//下拉输入GPIO_Init(GPIOA, &GPIO_InitStructure);	  		//根据设定参数初始化
}int main(void){NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	//设置系统中断优先级分为2u8g2_t u8g2;                                                                              // a structure which will contain all the data for one displayu8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8x8_gpio_and_delay);// init u8g2 structureu8g2_InitDisplay(&u8g2);                                                                      // send init sequence to the display, display is in sleep mode after this,u8g2_SetPowerSave(&u8g2, 0);                                                                  // wake up displaywhile (1){/* Begin of U8G2*/static int x = 30,y = 10;u8g2_ClearBuffer(&u8g2);u8g2_SetFont(&u8g2, u8g2_font_10x20_mf);u8g2_DrawStr(&u8g2, x,y,"Data");if(x >= 70){x = y = 0;}else{x++;y++;}u8g2_SendBuffer(&u8g2);/* End of U8G2 */}
}