1. 主页
  2. 文档
  3. 双轮平衡小车
  4. 外设
  5. OLED

OLED

OLED原理图

用3.3V电压进行驱动,SCL,SDA是IIC通信的时钟线和信号线,RES高电平时重启模块,上电后默认低电平可以通过软件进行复位。

相关代码路径:HARDWARE/OLED/oled.c

参考OLED卖家提供的例程进行开发:

采用IIC通信协议,通过GPIO口进行模拟的IIC通信:

//-----------------OLED端口定义----------------#define OLED_SCLK_Clr() GPIO_ResetBits(GPIOG,GPIO_Pin_6)//SCL#define OLED_SCLK_Set() GPIO_SetBits(GPIOG,GPIO_Pin_6)#define OLED_SDIN_Clr() GPIO_ResetBits(GPIOG,GPIO_Pin_5)//DIN#define OLED_SDIN_Set() GPIO_SetBits(GPIOG,GPIO_Pin_5)#define OLED_RES_Clr() GPIO_ResetBits(GPIOG,GPIO_Pin_4)//RES#define OLED_RES_Set() GPIO_SetBits(GPIOG,GPIO_Pin_4)#define OLED_DC_Clr() GPIO_ResetBits(GPIOG,GPIO_Pin_3)//DC#define OLED_DC_Set() GPIO_SetBits(GPIOG,GPIO_Pin_3) 		     #define OLED_CS_Clr()  GPIO_ResetBits(GPIOG,GPIO_Pin_2)//CS#define OLED_CS_Set()  GPIO_SetBits(GPIOG,GPIO_Pin_2)

GPIO初始化:

GPIO_InitTypeDef  GPIO_InitStructure;	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);//使能PORTA~E,PORTG时钟  	//GPIO初始化设置GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化

底层的驱动代码卖家已经提供好了,包括OLED的初始化,字符串的显示,以及传送字节的通讯语句,所以如果我们要用其他GPIO口与OLED通信的话,只要改变OLED端口的定义,以及GPIO的初始化即可。

除了基本的显示函数之外,我们还封装了一些函数方便调用。

显示浮点数: OLED_ShowFloatNum

void OLED_ShowFloatNum(u8 x,u8 y,float num,u8 len,u8 size1){	u32 temp,i=1;	if(num<0){		num=-num;		OLED_ShowChar(x,y,'-',16);		x+=8;	}	temp=(u32)num;	while(temp>=10){		temp/=10;		i++;	}	OLED_ShowNum(x,y,(u32)num,i,size1);	OLED_ShowChar(x+i*8,y,'.',16);	temp=((u32)(num*1000))%1000;//保留两位小数	temp/=10;  OLED_ShowNum(x+size1/2*(i+1),y,temp,2,size1);}

也可以在OLED上显示其他模块的相关信息:

显示MPU的角度:OLED_ShowAngleKalman、OLED_ShowAngleDMP

void OLED_ShowAngleKalman(float angle_acc,float angle_kalman){		OLED_Clear();	OLED_ShowString(0,0,"ANGLE",24);	OLED_ShowChar(60,0,'(',24);	OLED_DrawCircle(80,6,5);	OLED_ShowChar(90,0,')',24);	OLED_ShowString(5,32,"ACC:",16);	OLED_ShowFloatNum(40,32,angle_acc,3,16);	OLED_ShowString(5,50,"KALMAN:",16);  	OLED_ShowFloatNum(65,50,angle_kalman,3,16);	OLED_Refresh();}void OLED_ShowAngleDMP(float angle_acc,float angle_dmp){		//OLED_Clear();	OLED_ShowString(0,0,"ANGLE",24);	OLED_ShowChar(60,0,'(',24);	OLED_DrawCircle(80,6,5);	OLED_ShowChar(90,0,')',24);	OLED_ShowString(5,32,"ACC:",16);	OLED_ShowFloatNum(40,32,angle_acc,3,16);	OLED_ShowString(5,50,"DMP:",16);  	OLED_ShowFloatNum(40,50,angle_dmp,3,16);	OLED_Refresh();}

显示电源电压:OLED_ShowVoltage

void OLED_ShowVoltage(float battery){		OLED_Clear();	OLED_ShowString(0,0,"BATTERY",24);	OLED_ShowFloatNum(5,32,battery,3,16);	OLED_ShowChar(50,32,'V',16);	OLED_Refresh();}

显示电机转速:OLED_ShowSpeed

void OLED_ShowSpeed(float left,float right){		OLED_Clear();	OLED_ShowString(0,0,"SPEED",24);	OLED_ShowString(5,32,"LEFT:",16);	OLED_ShowFloatNum(50,32,left,3,16);	OLED_ShowString(90,32,"r/s",16);	OLED_ShowString(5,50,"RIGHT:",16);  	OLED_ShowFloatNum(50,50,right,3,16);	OLED_ShowString(90,50,"r/s",16);	OLED_Refresh();}

显示温度(MCU、MPU):OLED_ShowTemperature

void OLED_ShowTemperature(float mpu,float mcu){	OLED_Clear();	OLED_ShowString(0,0,"TEMP",24);	OLED_ShowChar(60,0,'(',24);	OLED_DrawCircle(80,6,5);	OLED_ShowChar(90,0,'C',24);	OLED_ShowChar(102,0,')',24);	OLED_ShowString(5,32,"MPU:",16);	OLED_ShowFloatNum(40,32,mpu,3,16);	OLED_ShowString(5,50,"MCU:",16);  	OLED_ShowFloatNum(40,50,mcu,3,16);	OLED_Refresh();}
这篇文章对您有用吗?

我们要如何帮助您?

发表评论

邮箱地址不会被公开。 必填项已用*标注