[AT32L021] 【AT-START-L021测评】一行代码搞定串口打印

[复制链接]
 楼主| 南来之风 发表于 2024-12-23 13:20 | 显示全部楼层 |阅读模式
在上一篇GPIO点灯成功后,如何串口打印相应的信息来辅助后续调试?
答案是一行代码。
  1. uart_print_init(115200);
转到函数定义:
  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]  initialize uart
  3.   * @param  baudrate: uart baudrate
  4.   * @retval none
  5.   */
  6. void uart_print_init(uint32_t baudrate)
  7. {
  8.   gpio_init_type gpio_init_struct;

  9. #if defined (__GNUC__) && !defined (__clang__)
  10.   setvbuf(stdout, NULL, _IONBF, 0);
  11. #endif

  12.   /* enable the uart and gpio clock */
  13.   crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE);
  14.   crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE);

  15.   gpio_default_para_init(&gpio_init_struct);

  16.   /* configure the uart tx pin */
  17.   gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  18.   gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  19.   gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  20.   gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN;
  21.   gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  22.   gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);

  23.   gpio_pin_mux_config(PRINT_UART_TX_GPIO, PRINT_UART_TX_PIN_SOURCE, PRINT_UART_TX_PIN_MUX_NUM);

  24.   /* configure uart param */
  25.   usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
  26.   usart_transmitter_enable(PRINT_UART, TRUE);
  27.   usart_enable(PRINT_UART, TRUE);
  28. }
基于ARTERY 的L021 BSP可以方便的使用UART串口打印。

烧录下载:
737476768f17bee9c5.png

接下来增加I2C相关的测试代码。L021开发板上提供了Arduino接口,有SCL, SDA 分别是PB8与PB9.
这里借用网友的代码,来测试一下开发板能否检测到OLED这个I2C设备。
  1. **
  2.   * @}
  3.   */
  4. /* Scans for I2C client devices on the bus - that have an address within the
  5. *specified range [addr_min, addr_max]
  6. */
  7. void simple_i2c_scan(uint8_t addr_min, uint8_t addr_max) {
  8.   uint8_t client_address;
  9.   uint8_t client_addr_str[5] = { 0 };
  10.   static uint8_t IIC_Client_Num = 0;
  11.   printf("\r\n I2C Scan started from 0x%02X to 0x%02X:", addr_min, addr_max);
  12.   //LCD_ShowString(0,35,"I2C Client:",RED,WHITE,12,0);
  13.   for (client_address = addr_min; client_address <= addr_max; client_address++) {
  14.     printf("\r\n Scanning client address = 0x%02X", (int)client_address);

  15.     /* start the request reception process */

  16.     //I2C_Send7bitAddress(I2C1, client_address, I2C_Direction_Transmitter);
  17.     if ((i2c_status = i2c_master_transmit(&hi2cx, client_address, &client_address, 1, I2C_TIMEOUT)) == I2C_OK) {
  18.       printf("\r\n---- --> client (0x%02X) ACKED <-- ------ ", (int)client_address);
  19.       client_addr_str[0] = '0';
  20.       client_addr_str[1] = 'x';
  21.       if (client_address / 16 >= 10)
  22.         client_addr_str[2] = client_address / 16 - 10 + 'A';
  23.       else
  24.         client_addr_str[2] = client_address / 16 + '0';
  25.       if (client_address % 16 >= 10)
  26.         client_addr_str[3] = client_address % 16 - 10 + 'A';
  27.       else
  28.         client_addr_str[3] = client_address % 16 + '0';
  29.       client_addr_str[4] = '\0';
  30.       IIC_Client_Num++;

  31.       printf("Yes, I2C Device found. Device Addr: %s", client_addr_str);
  32.     } else {
  33.       continue;
  34.     }

  35.     delay_ms(50);
  36.   }  //<--for--loop>
  37.   printf("\r\n ----------------- I2C Scan ended -----------------");
  38. }


串口打印结果:

145686768f1c4a3bff.png

可见OLED被检测到了。
0x78 : 0'b 0111_1000
0x79:  0'b 0111_1001
对应的7位地址是相同的,0'b 011_1100,即0x3C。上面地址最后一位分别代表 写地址与读地址。


后面会继续来尝试驱动OLED,使用硬件I2C。




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

本版积分规则

69

主题

290

帖子

2

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