[AT32F405] 【AT-START-F405测评】--4.点亮OLED

[复制链接]
 楼主| dirty123 发表于 2024-5-15 23:04 | 显示全部楼层 |阅读模式
本帖最后由 dirty123 于 2024-5-15 23:06 编辑

      本篇讲述AT-START-F405开发板点亮OLED.
一.硬件准备
      这里准备了128*64分辨率ssd1306 OLED屏。引脚连接如下
OLED       开发板
VCC          VDD
GND         GND
SCL           PB8
SDA          PB7


二.代码准备
1.引脚初始化
  1. void oled_gpio_init(void)
  2. {
  3.     gpio_init_type gpio_init_struct;

  4.     /* enable the led clock */
  5.     crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

  6.     /* set default parameter */
  7.     gpio_default_para_init(&gpio_init_struct);

  8.     /* configure the led gpio */
  9.     gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  10.     gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  11.     gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  12.     gpio_init_struct.gpio_pins =  OLED_SCLK_Pin|OLED_SDIN_Pin;
  13.     gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  14.     gpio_init(OLED_SDIN_GPIO_Port, &gpio_init_struct);

  15.    
  16. }
2.I2C写数据/命令
  1. /**********************************************
  2. // IIC Write Command
  3. **********************************************/
  4. void Write_IIC_Command(uint8_t IIC_Command)
  5. {
  6.     IIC_Start();
  7.     Write_IIC_Byte(0x78);            //Slave address,SA0=0
  8.     IIC_Wait_Ack();        
  9.     Write_IIC_Byte(0x00);                        //write command
  10.     IIC_Wait_Ack();        
  11.     Write_IIC_Byte(IIC_Command);
  12.     IIC_Wait_Ack();        
  13.     IIC_Stop();
  14. }
  15. /**********************************************
  16. // IIC Write Data
  17. **********************************************/
  18. void Write_IIC_Data(uint8_t IIC_Data)
  19. {
  20.     IIC_Start();
  21.     Write_IIC_Byte(0x78);                        //D/C#=0; R/W#=0
  22.     IIC_Wait_Ack();        
  23.     Write_IIC_Byte(0x40);                        //write data
  24.     IIC_Wait_Ack();        
  25.     Write_IIC_Byte(IIC_Data);
  26.     IIC_Wait_Ack();        
  27.     IIC_Stop();
  28. }
  29. void OLED_WR_Byte(unsigned dat,unsigned cmd)
  30. {
  31.     if(cmd)
  32.     {
  33.         Write_IIC_Data(dat);  
  34.     }
  35.     else
  36.     {
  37.         Write_IIC_Command(dat);               
  38.     }
  39. }


3.寄存器初始化
  1. //初始化SSD1306                                            
  2. void OLED_Init(void)
  3. {         
  4.    
  5.     oled_gpio_init();

  6.     delay_sec(1);
  7.     OLED_WR_Byte(0xAE,OLED_CMD);//--display off
  8.     OLED_WR_Byte(0x02,OLED_CMD);//---set low column address
  9.     OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
  10.     OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  
  11.     OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
  12.     OLED_WR_Byte(0x81,OLED_CMD); // contract control
  13.     OLED_WR_Byte(0xFF,OLED_CMD);//--128   
  14.     OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap //--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  15.     OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
  16.     OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
  17.     OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
  18.     OLED_WR_Byte(0xC0,OLED_CMD);//Com scan direction//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  19.     OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
  20.     OLED_WR_Byte(0x00,OLED_CMD);//
  21.    
  22.     OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
  23.     OLED_WR_Byte(0x80,OLED_CMD);//
  24.    
  25.     OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
  26.     OLED_WR_Byte(0x05,OLED_CMD);//
  27.    
  28.     OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
  29.     OLED_WR_Byte(0xF1,OLED_CMD);//
  30.    
  31.     OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
  32.     OLED_WR_Byte(0x12,OLED_CMD);//
  33.    
  34.     OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
  35.     OLED_WR_Byte(0x30,OLED_CMD);//
  36.    
  37.     OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
  38.     OLED_WR_Byte(0x14,OLED_CMD);//
  39.    
  40.     OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
  41. }
4.设计UI界面显示
  1. int main(void)
  2. {
  3.     system_clock_config();
  4.     at32_board_init();
  5.     uart_print_init(115200);

  6.     /* output a message on hyperterminal using printf function */
  7.     printf("usart printf example: retarget the c library printf function to the usart\r\n");
  8.         
  9.    
  10.     OLED_Init();
  11.     OLED_Clear();
  12.     OLED_ShowStringCN(0,0,"雅特力 & 21ic",1);
  13.     OLED_ShowStringCN(0,16,"AT-START-F405",1);
  14.         OLED_ShowStringCN(0,32,"bbs.21ic.com/",1);
  15.     OLED_Refresh();   
  16.         
  17.     while(1)
  18.     {
  19.         printf("usart printf counter: %u\r\n",time_cnt++);
  20.         delay_sec(1);
  21.     }
  22. }

三.测验
      编译烧录后,按复位键,可以看到OLED按设计显示如下。
oled显示.jpg


      至此实现AT-START-F405驱动oled显示。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

135

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部