在掌握了GPIO口的使用方法后,可以用GPIO口来模拟I2C通讯来驱动OLED屏。 该显示屏是一款0.91寸的单色显示屏,只需2个GPIO口即可完成显示驱动任务,它开发板的连接关系为: SCL---PA1 SDA--- PA4 对这2个引脚的配置程序为: GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 定义引脚输出高低电平的语句为: #define OLED_SCLK_Clr() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET) #define OLED_SCLK_Set() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET) #define OLED_SDIN_Clr() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET) #define OLED_SDIN_Set() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET) OLED屏的初始化函数为:
- voidOLED_Init(void)
- {
- Write_IIC_Command(0xAE); //display off
- Write_IIC_Command(0x40);//--set start line address
- Write_IIC_Command(0xb0);//Set Page Start Address for Page Addressing Mode,0-7
- Write_IIC_Command(0xc8);//Set COM Output Scan Direction
- Write_IIC_Command(0x81);//--set contrast control register
- Write_IIC_Command(0xff);
- Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
- Write_IIC_Command(0xa6);//--set normal display
- Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
- Write_IIC_Command(0x1F);
- Write_IIC_Command(0xd3);//-set display offset
- Write_IIC_Command(0x00);//-not offset
- Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
- Write_IIC_Command(0xf0);//--set divide ratio
- Write_IIC_Command(0xd9);//--set pre-charge period
- Write_IIC_Command(0x22);
- Write_IIC_Command(0xda);//--set com pins hardware configuration
- Write_IIC_Command(0x02);
- Write_IIC_Command(0x8d);//--set DC-DC enable
- Write_IIC_Command(0x14);
- Write_IIC_Command(0xdb);//--set vcomh
- Write_IIC_Command(0x49);//0x20,0.77xVcc
- Write_IIC_Command(0xaf);//--turn on oled panel
- }
复制代码
实现字节数据发送的函数为:
- voidWrite_IIC_Byte(unsigned char IIC_Byte)
- {
- unsigned char i;
- unsigned char m,da;
- da=IIC_Byte;
- OLED_SCLK_Clr();
- for(i=0;i<8;i++)
- {
- m=da;
- m=m&0x80;
- if(m==0x80) OLED_SDIN_Set();
- else
- OLED_SDIN_Clr();
- Delay_Us(2);
- da=da<<1;
- OLED_SCLK_Set();
- Delay_Us(2);
- OLED_SCLK_Clr();
- Delay_Us(2);
- }
- }
复制代码
实现字符显示的函数为:
- voidOLED_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);
- }
- }
复制代码
实现图1显示效果的主程序为:
- int main(void)
- {
- SystemClock_Config(CPU_CLK_160M);
- printf("enter mainrn");
- HAL_Init();
- GPIO_Init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"W806-Kit TEST",16);
- OLED_ShowString(0,2,"OLED DISPLAY",16);
- while (1);
- return 0;
- }
复制代码
图1 OLED 屏显示效果 在程序下载后,其结果如图2所示。
图2下载结果 有了OLED屏的显示功能,再遇到显示问题就无需依赖串口了!
|