返回列表 发新帖我要提问本帖赏金: 30.00元(功能说明)

[AT32F405] 【AT-START-F405测评】-2- 硬件I2C驱动OLED

[复制链接]
 楼主| YangTwo 发表于 2024-5-15 20:53 | 显示全部楼层 |阅读模式
上一篇测评【【AT-START-F405测评】-1- I2C扫描设备】使用硬件I2C检测到了总线上的OLED设备,本次使用硬件I2C来驱动这个常见的显示设备。

OLED移植的关键在于下面函数的移植:
  1. void OLED_WR_Byte(uint8_t dat,uint8_t mode)
  2. {       

  3.         i2c_status_type i2c_status;
  4.         uint8_t buff[2] = {0};
  5.         if(mode){
  6.                 buff[0] = 0x40;
  7.                 buff[1] = dat;
  8.                 i2c_status = i2c_master_transmit(&hi2cx, 0x78, buff, 2, 0xFFFFFFF);
  9.                 if(i2c_status != I2C_OK){
  10.                         printf("\r\nerror send %d", i2c_status);
  11.                 }
  12.         }else{
  13.                 buff[0] = 0x00;
  14.                 buff[1] = dat;
  15.                 i2c_status = i2c_master_transmit(&hi2cx, 0x78, buff, 2, 0xFFFFFFF);
  16.                 if(i2c_status != I2C_OK){
  17.                         printf("\r\nerror send %d", i2c_status);
  18.                 }
  19.         }

  20. }
其中i2c_master_transmit()是雅特力重新封装的一个便于使用的I2C发送函数,更简洁有好。

主函数中先初始化I2C与OLED,然后扫描I2C设备,最后把扫描结果显示在OLED屏幕上。

  1. int main(void)
  2. {
  3.   

  4.   /* initial system clock */
  5.   system_clock_config();

  6.   /* config nvic priority group */
  7.   nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

  8.   /* at board initial */
  9.   at32_board_init();
  10.         uart_print_init(115200);

  11.   hi2cx.i2cx = I2Cx_PORT;

  12.   /* i2c config */
  13.   i2c_config(&hi2cx);
  14.         printf("i2c scan example: aim to find the i2c device on the bus\r\n");
  15.        
  16.         main_i2c_scan(I2C_DEVICE_General_ADDRESS_MIN, I2C_DEVICE_General_ADDRESS_MAX);
  17.         OLED_Init();
  18.         OLED_Clear();
  19.   while(1)
  20.   {
  21.                 static uint32_t i = 0;
  22.     OLED_ShowString(0,0,"   AT-START-F405",12, 1);
  23.                 OLED_ShowString(0,12,"Scaning I2C Device...",12, 1);
  24.                 OLED_ShowString(0,24,"I2C Slave 8 bit Addr: ",12, 1);
  25.                 OLED_ShowString(0,36,i2cScanResult,12, 1);
  26.                 OLED_Refresh();
  27.                 printf("\r\n inside while loop %d\r\n", i++);
  28.                 delay_ms(1000);
  29.   }
  30.        
  31.   
  32. }

实物结果:
946296644b047129c2.png

打赏榜单

ArteryMCU 打赏了 30.00 元 2024-06-07
理由:[F405开发板评测活动]内容优质

呐咯密密 发表于 2024-5-21 13:24 | 显示全部楼层
巨喜欢硬件的IIC
问天少年 发表于 2024-5-23 16:01 | 显示全部楼层
硬件IIC给赞了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

27

主题

110

帖子

1

粉丝

27

主题

110

帖子

1

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