完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
听说VET6还要自己写模拟IIC?还有为什么编译成功 OLED显示不了。 OLED.C #include "OLED_I2C.h" #include "delay.h" #include "codetab.h" /** * @brief I2C_Configuration,初始化硬件IIC引脚 * @param 无 * @retval 无 */ void I2C_Configuration(void) {I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;//I2C必须开漏输出 GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C2);//使用I2C2 I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x0A;//主机的I2C地址,随便写的 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 400000;//400K I2C_Cmd(I2C2, ENABLE); I2C_Init(I2C2, &I2C_InitStructure); } /** * @brief I2C_WriteByte,向OLED寄存器地址写一个byte的数据 * @param addr:寄存器地址 * data:要写入的数据 * @retval 无 */ void I2C_WriteByte(uint8_t addr,uint8_t data) { while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C2, ENABLE);//开启I2C2 while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/ I2C_Send7bitAddress(I2C2, OLED_ADDRESS, I2C_Direction_Transmitter);//器件地址 -- 默认0x78 while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C2, addr);//寄存器地址 while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_SendData(I2C2, data);//发送数据 while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTOP(I2C2, ENABLE);//关闭I2C1总线 } /** * @brief WriteCmd,向OLED写入命令 * @param I2C_Command:命令代码 * @retval 无 */ void WriteCmd(unsigned char I2C_Command)//写命令 { I2C_WriteByte(0x00, I2C_Command); } /** * @brief WriteDat,向OLED写入数据 * @param I2C_Data:数据 * @retval 无 */ void WriteDat(unsigned char I2C_Data)//写数据 { I2C_WriteByte(0x40, I2C_Data); } /** * @brief OLED_Init,初始化OLED * @param 无 * @retval 无 */ void OLED_Init(void) { delay_ms(100); //这里的延时很重要 WriteCmd(0xAE); //display off WriteCmd(0x20); //Set Memory Addressing Mode WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7 WriteCmd(0xc8); //Set COM Output Scan Direction WriteCmd(0x00); //---set low column address WriteCmd(0x10); //---set high column address WriteCmd(0x40); //--set start line address WriteCmd(0x81); //--set contrast control register WriteCmd(0xff); //亮度调节 0x00~0xff WriteCmd(0xa1); //--set segment re-map 0 to 127 WriteCmd(0xa6); //--set normal display WriteCmd(0xa8); //--set multiplex ratio(1 to 64) WriteCmd(0x3F); // WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content WriteCmd(0xd3); //-set display offset WriteCmd(0x00); //-not offset WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency WriteCmd(0xf0); //--set divide ratio WriteCmd(0xd9); //--set pre-charge period WriteCmd(0x22); // WriteCmd(0xda); //--set com pins hardware configuration WriteCmd(0x12); WriteCmd(0xdb); //--set vcomh WriteCmd(0x20); //0x20,0.77xVcc WriteCmd(0x8d); //--set DC-DC enable WriteCmd(0x14); // WriteCmd(0xaf); //--turn on oled panel } /** * @brief OLED_SetPos,设置光标 * @param x,光标x位置 * y,光标y位置 * @retval 无 */ void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标 { WriteCmd(0xb0+y); WriteCmd(((x&0xf0)>>4)|0x10); WriteCmd((x&0x0f)|0x01); } /** * @brief OLED_Fill,填充整个屏幕 * @param fill_Data:要填充的数据 * @retval 无 */ void OLED_Fill(unsigned char fill_Data)//全屏填充 { unsigned char m,n; for(m=0;m<8;m++) { WriteCmd(0xb0+m); //page0-page1 WriteCmd(0x00); //low column start address WriteCmd(0x10); //high column start address for(n=0;n<128;n++) { WriteDat(fill_Data); } } } /** * @brief OLED_CLS,清屏 * @param 无 * @retval 无 */ void OLED_CLS(void)//清屏 { OLED_Fill(0x00); } /** * @brief OLED_ON,将OLED从休眠中唤醒 * @param 无 * @retval 无 */ void OLED_ON(void) { WriteCmd(0X8D); //设置电荷泵 WriteCmd(0X14); //开启电荷泵 WriteCmd(0XAF); //OLED唤醒 } /** * @brief OLED_OFF,让OLED休眠 -- 休眠模式下,OLED功耗不到10uA * @param 无 * @retval 无 */ void OLED_OFF(void) { WriteCmd(0X8D); //设置电荷泵 WriteCmd(0X10); //关闭电荷泵 WriteCmd(0XAE); //OLED休眠 } /** * @brief OLED_ShowStr,显示codetab.h中的ASCII字符,有6*8和8*16可选择 * @param x,y : 起始点坐标(x:0~127, y:0~7); * ch[] :- 要显示的字符串; * TextSize : 字符大小(1:6*8 ; 2:8*16) * @retval 无 */ void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize) { unsigned char c = 0,i = 0,j = 0; switch(TextSize) { case 1: { while(ch[j] != ' |