对于STM32F413开发板来说,它就像一张可供你实现各种可能的白纸,你总是需要为它配上些什么才好使用。这里我们就为它配上一个使用IIC接口的OLED屏来进行人机交互, 其使用效果如图所示。
OLED屏显示效果图
OLED屏与STM32F413的连接关系为SCK接PA5,SDA接PA6,故程序的初始定义如下: - // STM32F413+IIC_OLED
- #include "main.h"
- #include "oledfont.h"
- #include "bmp.h"
- #define OLED_MODE 0
- #define SIZE 8
- #define XLevelL 0x00
- #define XLevelH 0x10
- #define Max_Column 128
- #define Max_Row 64
- #define Brightness 0xFF
- #define X_WIDTH 128
- #define Y_WIDTH 64
- #define OLED_SCLK_Clr() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)
- #define OLED_SCLK_Set() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)
- #define OLED_SDIN_Clr() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET)
- #define OLED_SDIN_Set() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET)
- #define OLED_CMD 0
- #define OLED_DATA 1
复制代码为了使OLED屏工作,其初始化的函数为: - // SSD1306
- void OLED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- OLED_SCLK_Set();
- OLED_SDIN_Set();
- Delay_1ms(800); //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
- }
复制代码制作上图的显示效果,其主函数的内容如下: - int main(void)
- {
- HAL_Init();
- /* Configure the system clock to 84 MHz */
- SystemClock_Config();
- /* -1- Enable GPIOA Clock (to be able to program the configuration registers) */
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /* -2- Configure PA05 IO in output push-pull mode to drive external LED */
- GPIO_InitStruct.Pin = GPIO_PIN_7; // LD2 PB7
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(2,0,"STM32F413",16);
- OLED_ShowString(2,2,"Nucleo-144",16);
- OLED_ShowString(2,4,"1.5MB Flash",16);
- OLED_ShowString(2,6,"320-KB SRAM",16);
- /* -3- Toggle PA05 IO in an infinite loop */
- while (1)
- {
- HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
- /* Insert delay 100 ms */
- HAL_Delay(100);
- }
- }
复制代码 |