[RISC-V MCU 应用开发] 【RISC-V MCU CH32V103测评】+ UART打印输出

[复制链接]
 楼主| 昱枫 发表于 2020-11-1 08:13 | 显示全部楼层 |阅读模式
【RISC-V MCU CH32V103测评】+ UART打印输出


1. uart 初始化
  1. void USART_Printf_Init(uint32_t baudrate)
  2. {
  3.   GPIO_InitTypeDef GPIO_InitStructure;
  4.   USART_InitTypeDef USART_InitStructure;

  5. #if (DEBUG == DEBUG_UART1)
  6.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);

  7.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  8.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  10.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  11. #elif (DEBUG == DEBUG_UART2)
  12.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  13.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  14.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  15.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  17.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  18. #elif (DEBUG == DEBUG_UART3)
  19.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  20.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  21.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  22.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  24.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  25. #endif

  26.   USART_InitStructure.USART_BaudRate = baudrate;
  27.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  28.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  29.   USART_InitStructure.USART_Parity = USART_Parity_No;
  30.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  31.   USART_InitStructure.USART_Mode = USART_Mode_Tx;

  32. #if (DEBUG == DEBUG_UART1)
  33.   USART_Init(USART1, &USART_InitStructure);
  34.   USART_Cmd(USART1, ENABLE);

  35. #elif (DEBUG == DEBUG_UART2)
  36.   USART_Init(USART2, &USART_InitStructure);
  37.   USART_Cmd(USART2, ENABLE);

  38. #elif (DEBUG == DEBUG_UART3)
  39.   USART_Init(USART3, &USART_InitStructure);
  40.   USART_Cmd(USART3, ENABLE);

  41. #endif
  42. }

  1. /*******************************************************************************
  2. * Function Name  : _write
  3. * Description    : Support Printf Function
  4. * Input          : *buf: UART send Data.
  5. *                  size: Data length
  6. * Return         : size: Data length
  7. *******************************************************************************/
  8. int _write(int fd, char *buf, int size)
  9. {
  10.   int i;

  11.   for(i=0; i<size; i++)
  12.   {
  13. #if (DEBUG == DEBUG_UART1)
  14.     while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  15.     USART_SendData(USART1, *buf++);
  16. #elif (DEBUG == DEBUG_UART2)
  17.     while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  18.     USART_SendData(USART2, *buf++);
  19. #elif (DEBUG == DEBUG_UART3)
  20.     while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
  21.     USART_SendData(USART3, *buf++);
  22. #endif
  23.   }

  24.   return size;
  25. }



  1. int main(void)
  2. {
  3.         u16 i;
  4.         Delay_Init();
  5.     USART_Printf_Init(115200);
  6.     printf("SystemClk:%d\r\n",SystemCoreClock);
  7. ledInit();
  8. printf("SystemClk:%d\r\n",SystemCoreClock);

  9.         //setledOn();
  10.         printf("System 1\r");
  11.         intioledPort();
  12.         printf("System 2");
  13.         intioledPort();
  14.         //OLED_Init();
  15.         //OLED_ON();
  16.         printf("System 3");
  17.     //OLED_ShowString(0,0,disp1,16);
  18.     //OLED_ShowString(0,2,disp2,16);
  19.     printf("System 4");
  20.     clrledOff();
  21.     //p1 = disp1;
  22.     //p2 = disp2;
  23.     printf("System 5");
  24. while(1)
  25.     {

  26.         //OLED_Clear();
  27.         //OLED_ShowString(0,0,(p1+i),16);
  28.         //OLED_ShowString(0,2,(p2+i),16);
  29.         i++;

  30.         if(i>=16)
  31.         {
  32.             printf("System %d\r",i);
  33.             i=0;
  34.         }
  35.         setledOn();
  36.         Delay_Ms(100);
  37.         clrledOff();
  38.         Delay_Ms(100);
  39.     }
  40. }
测试效果

2020-11-01_080658.jpg


串口是在缓存一定的数据量,才进行发数据
程序调试,当然希望直接发出串口数据来了

有没有在线调试功能?
随便一个接口程序有很多像
while( flag );
等待标志位,就不知道哪里状态不置位那就卡

zeshoufx 发表于 2020-11-2 08:45 | 显示全部楼层
谢谢分享【UART打印输出】
您需要登录后才可以回帖 登录 | 注册

本版积分规则

54

主题

679

帖子

4

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