- //OLED管脚初始化
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pins =GPIO_Pins_4|GPIO_Pins_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_OD;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
然后将OLED基本的拉高拉低操作映射到这两个端口,通过宏定义实现,如下
- //-----------------OLED端口定义----------------
- #define OLED_SCL_Clr() GPIO_ResetBits(GPIOA,GPIO_Pins_4)
- #define OLED_SCL_Set() GPIO_SetBits(GPIOA,GPIO_Pins_4)
- #define OLED_SDA_Clr() GPIO_ResetBits(GPIOA,GPIO_Pins_6)
- #define OLED_SDA_Set() GPIO_SetBits(GPIOA,GPIO_Pins_6)
接下来就是直接使用OLED的库函数点亮屏幕,显示字符了
整个main.c内容如下
- #include "at32f4xx_gpio.h"
- #include "at32f4xx_usart.h"
- #include "oled.h"
- /*delay variable*/
- static __IO uint32_t fac_us;
- static __IO uint32_t fac_ms;
- /*delay macros*/
- #define STEP_DELAY_MS 50
- void Delay_init()
- {
- /*Config Systick*/
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
- fac_us = SystemCoreClock / (1000000U);
- fac_ms = fac_us * (1000U);
- }
- void Delay_ms(u16 nms)
- {
- u32 temp;
- while(nms)
- {
- if(nms > STEP_DELAY_MS)
- {
- SysTick->LOAD = (u32)(STEP_DELAY_MS * fac_ms);
- nms -= STEP_DELAY_MS;
- }
- else
- {
- SysTick->LOAD = (u32)(nms * fac_ms);
- nms = 0;
- }
- SysTick->VAL = 0x00;
- SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
- do
- {
- temp = SysTick->CTRL;
- }while( (temp & 0x01) && !(temp & (1<<16)) );
- SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
- SysTick->VAL = 0X00;
- }
- }
- //串口初始化,根据原理图使用的是USART1,PA9为TX,PA10为RX
- void UART1_Init(uint32_t bound)
- {
- GPIO_InitType GPIO_InitStructure;
- USART_InitType USART_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_USART1,ENABLE);
- /* Configure the UART1 TX pin */
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /*Configure UART param*/
- USART_StructInit(&USART_InitStructure);
- USART_InitStructure.USART_BaudRate = bound;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
- USART_INTConfig(USART1, USART_INT_RDNE, ENABLE);
- USART_Cmd(USART1, ENABLE);
- }
- int main(void)
- {
- int i;
-
- //LED2管脚初始化
- GPIO_InitType GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOD, ENABLE);
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pins =GPIO_Pins_13;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- //OLED管脚初始化
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pins =GPIO_Pins_4|GPIO_Pins_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_OD;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- Delay_init();
- UART1_Init(115200);
-
- OLED_Init();//初始化OLED
- OLED_ColorTurn(0);//0正常显示,1 反色显示
- OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
-
- while(1)
- {
- GPIO_SetBits(GPIOD,GPIO_Pins_13);
- Delay_ms(200);
- GPIO_ResetBits(GPIOD,GPIO_Pins_13);
- Delay_ms(200);
- USART_SendData(USART1,'A');
-
- for(i=0;i<8;i++)
- OLED_ShowChinese(i*16,2,i,16);
- for(i=0;i<3;i++)
- OLED_ShowChinese(i*16,4,i+8,16);
- i=48;
- OLED_ShowChar(i,4,'A',16);
- i=i+8;
- OLED_ShowChar(i,4,'T',16);
- i=i+8;
- OLED_ShowChar(i,4,'3',16);
- i=i+8;
- OLED_ShowChar(i,4,'2',16);
- i=i+8;
- OLED_ShowChar(i,4,'F',16);
- i=i+8;
- OLED_ShowChar(i,4,'4',16);
- i=i+8;
- OLED_ShowChar(i,4,'0',16);
- i=i+8;
- OLED_ShowChar(i,4,'7',16);
- }
- }
编译烧录后效果如下所示