本帖最后由 jinglixixi 于 2020-6-17 23:06 编辑
1.使用GPIO口模拟I2C接口 在MM32 eMiniBoard开发板的例程中提供了一个LED灯闪烁程序,通过它我们可以学习控制GPIO口定义输出口及输出高低电平信号的方法, 据此可用GPIO口来模拟I2C接口以控制该接口形式的OLED显示屏,该OLED显示屏具有占用引脚少,显示信息量较多的特点。 在引脚的分配上,用PB6来连接OLED屏的SCK引脚,用PB7来连接OLED屏的SDA引脚,为此其输出高低的定义为: #define OLED_SCLK_Set() GPIO_SetBits(GPIOB,GPIO_Pin_6) //PB13 #define OLED_SCLK_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_6)
#define OLED_SDIN_Set() GPIO_SetBits(GPIOB,GPIO_Pin_7) //PB14 #define OLED_SDIN_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_7)
相应的OLED显示屏的初始化函数为: - void OLED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- OLED_SCLK_Set();
- OLED_SDIN_Set();
- delay_ms(800);
- OLED_WR_Byte(0xAE,OLED_CMD);//--display off
- OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
- OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
- OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
- OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
- OLED_WR_Byte(0x81,OLED_CMD); // contract control
- OLED_WR_Byte(0xFF,OLED_CMD);//--128
- OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
- OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
- OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
- OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
- OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
- OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
- OLED_WR_Byte(0x00,OLED_CMD);//
- OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
- OLED_WR_Byte(0x80,OLED_CMD);//
- OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
- OLED_WR_Byte(0x05,OLED_CMD);//
- OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
- OLED_WR_Byte(0xF1,OLED_CMD);//
- OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
- OLED_WR_Byte(0x12,OLED_CMD);//
- OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
- OLED_WR_Byte(0x30,OLED_CMD);//
- OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
- OLED_WR_Byte(0x14,OLED_CMD);//
- OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
- }
复制代码2. OLED屏显示驱动 在完成功能引脚定义及初始化的基础下,要实现具体的字符及数值的显示,需相关函数的配合,关键的函数为: - void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
- {
- unsigned char c=0,i=0;
- c=chr-' ';
- if(x>Max_Column-1){x=0;y=y+2;}
- if(Char_Size ==16)
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
- }
- else {
- OLED_Set_Pos(x,y);
- for(i=0;i<6;i++)
- OLED_WR_Byte(F6x8[c][i],OLED_DATA);
- }
- }
- uint32_t oled_pow(uint8_t m,uint8_t n)
- {
- uint32_t result=1;
- while(n--)result*=m;
- return result;
- }
-
- void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2)
- {
- uint8_t t,temp;
- uint8_t enshow=0;
- for(t=0;t<len;t++)
- {
- temp=(num/oled_pow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
- continue;
- }else enshow=1;
-
- }
- OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
- }
- }
- void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
- {
- unsigned char j=0;
- while (chr[j]!='\0')
- { OLED_ShowChar(x,y,chr[j],Char_Size);
- x+=8;
- if(x>120){x=0;y+=2;}
- j++;
- }
- }
复制代码3.显示功能的测试 为了测试OLED屏的显示功能,其主函数如下: - int main(void)
- {
- delay_init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"MM32L073 TEST",16);
- OLED_ShowString(0,2,"OLED Display",16);
- OLED_ShowString(0,4,"jinglixixi",16);
- OLED_ShowString(0,6,"2020.5.22",16);
- while(1);
- }
复制代码
相应的显示效果如图1 所示,后面就可以为其添加具体的应用功能。图1 显示效果
4.温度测量与显示 在例程中存在一个利用A/D转换测量片内温度的程序,参考该程序的设计,我们可以将其改造成一个利用OLED屏来显示温度值。 实现图2显示效果的主程序如下: - int main(void)
- {
- u16 ADCVAL;
- float Temp;
- u32 Tem;
- delay_init();
- uart_initwBaudRate(9600);
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"MM32L073 TEST",16);
- OLED_ShowString(0,2,"Temp Display",16);
- ADC1_SingleChannel( ADC_Channel_10);
- while(1)
- {
- ADCVAL=Get_Adc_Average(ADC_Channel_10,5);
- Temp=27.0+(ADCVAL-1800)/5.96;
- Tem=Temp;
- printf("Temp=%.2f \r\n",Temp);
- OLED_ShowString(0,6,"Temp= C",16);
- OLED_ShowNum(40,6,Tem,4,16);
- delay_ms(1000);
- }
- }
复制代码
图2 显示效果
|