[RISC-V MCU 应用开发]

【RISC-V MCU CH32V103测评】+ UART打印输出

[复制链接]
553|1
手机看帖
扫描二维码
随时随地手机跟帖
昱枫|  楼主 | 2020-11-1 08:13 | 显示全部楼层 |阅读模式
【RISC-V MCU CH32V103测评】+ UART打印输出


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

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

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

#elif (DEBUG == DEBUG_UART2)
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

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

#elif (DEBUG == DEBUG_UART3)
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

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

#endif

  USART_InitStructure.USART_BaudRate = baudrate;
  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_Tx;

#if (DEBUG == DEBUG_UART1)
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);

#elif (DEBUG == DEBUG_UART2)
  USART_Init(USART2, &USART_InitStructure);
  USART_Cmd(USART2, ENABLE);

#elif (DEBUG == DEBUG_UART3)
  USART_Init(USART3, &USART_InitStructure);
  USART_Cmd(USART3, ENABLE);

#endif
}

/*******************************************************************************
* Function Name  : _write
* Description    : Support Printf Function
* Input          : *buf: UART send Data.
*                  size: Data length
* Return         : size: Data length
*******************************************************************************/
int _write(int fd, char *buf, int size)
{
  int i;

  for(i=0; i<size; i++)
  {
#if (DEBUG == DEBUG_UART1)
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    USART_SendData(USART1, *buf++);
#elif (DEBUG == DEBUG_UART2)
    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
    USART_SendData(USART2, *buf++);
#elif (DEBUG == DEBUG_UART3)
    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
    USART_SendData(USART3, *buf++);
#endif
  }

  return size;
}



int main(void)
{
        u16 i;
        Delay_Init();
    USART_Printf_Init(115200);
    printf("SystemClk:%d\r\n",SystemCoreClock);
ledInit();
printf("SystemClk:%d\r\n",SystemCoreClock);

        //setledOn();
        printf("System 1\r");
        intioledPort();
        printf("System 2");
        intioledPort();
        //OLED_Init();
        //OLED_ON();
        printf("System 3");
    //OLED_ShowString(0,0,disp1,16);
    //OLED_ShowString(0,2,disp2,16);
    printf("System 4");
    clrledOff();
    //p1 = disp1;
    //p2 = disp2;
    printf("System 5");
while(1)
    {

        //OLED_Clear();
        //OLED_ShowString(0,0,(p1+i),16);
        //OLED_ShowString(0,2,(p2+i),16);
        i++;

        if(i>=16)
        {
            printf("System %d\r",i);
            i=0;
        }
        setledOn();
        Delay_Ms(100);
        clrledOff();
        Delay_Ms(100);
    }
}
测试效果

2020-11-01_080658.jpg


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

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

使用特权

评论回复

相关帖子

zeshoufx| | 2020-11-2 08:45 | 显示全部楼层
谢谢分享【UART打印输出】

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

53

主题

674

帖子

4

粉丝