[CW32F030系列] 【CW32F030CxTx StartKit测评】 串口输出

[复制链接]
2434|22
 楼主| tlled 发表于 2022-7-2 10:36 | 显示全部楼层 |阅读模式
    在上篇文章将内部时钟设置为48M后,下面学习下串口1的输出。


    一、串口硬件电路


    1.1、串口端口
    电路图有关串口1的部分,使用的PA8和PA9管脚。
    002.png
    1.2、USB转串口部分
    开发板上自带了一个USB转串口模块,在使用的时候需要将VDDU连接到单片机的电源上,否则PC找不到USB,同时将TX和RX连接到相应的PA9和PA8引脚。
    001.png


    二、程序部分

    2.1、usart.c
    这里将串口1的时钟源改成48M。
  1. #include "cw32f030.h"
  2. #include "cw32f030_uart.h"
  3. #include "cw32f030_rcc.h"
  4. #include "cw32f030_gpio.h"
  5. #include "usart.h"

  6. int fputc(int ch, FILE *f)
  7. {
  8.   USART_SendData_8bit(CW_UART1, (uint8_t)ch);

  9.   while (USART_GetFlagStatus(CW_UART1, USART_FLAG_TXE) == RESET);

  10.   return ch;
  11. }
  12.   
  13. void uart_init(uint32_t bound)
  14. {
  15.         GPIO_InitTypeDef GPIO_InitStructure;
  16.         USART_InitTypeDef USART_InitStructure;
  17.         
  18.         RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOA, ENABLE);
  19.   RCC_APBPeriphClk_Enable2(RCC_APB2_PERIPH_UART1, ENABLE);
  20.         
  21.   PA08_AFx_UART1TXD();                     
  22.   PA09_AFx_UART1RXD();                    

  23.   GPIO_InitStructure.Pins = GPIO_PIN_8;
  24.   GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  25.   GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  26.   GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
  27.    
  28.   GPIO_InitStructure.Pins = GPIO_PIN_9;
  29.   GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  30.   GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
  31.         
  32.         USART_InitStructure.USART_BaudRate = bound;
  33.         USART_InitStructure.USART_Over = USART_Over_16;
  34.         USART_InitStructure.USART_Source = USART_Source_PCLK;
  35.         USART_InitStructure.USART_UclkFreq = 48000000;
  36.         USART_InitStructure.USART_StartBit = USART_StartBit_FE;
  37.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  38.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  39.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  40.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  41.         USART_Init(CW_UART1, &USART_InitStructure);         
  42. }

    2.2、usart.h
   
  1. #ifndef __USART_H
  2. #define __USART_H
  3. #include "stdio.h"        

  4. void uart_init(uint32_t bound);

  5. #endif



    2.3、main.c

  1. #include "main.h"
  2. #include "cw32f030_flash.h"
  3. #include "led.h"
  4. #include "delay.h"
  5. #include "usart.h"


  6. void rcc_config(void)
  7. {
  8.         uint8_t res = 0U;
  9.         RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_FLASH, ENABLE);   //打开FLASH时钟
  10.         RCC_HCLK_OUT();                                                         //通过PA04观察HCLK频率
  11.         RCC_HSE_Enable( RCC_HSE_MODE_OSC, 16000000, RCC_HSE_DRIVER_NORMAL, RCC_HSE_FLT_CLOSE ); //开启外部高速时钟HSE,实际频率需要根据实际晶体频率进行填写
  12.                
  13.         RCC_PLL_Enable( RCC_PLLSOURCE_HSEOSC, 16000000, RCC_PLL_MUL_3 );     //开启PLL,PLL时钟来源为HSE
  14.         FLASH_SetLatency(FLASH_Latency_2);    //频率大于24M需要配置FlashWait=2
  15.         res = RCC_SysClk_Switch( RCC_SYSCLKSRC_PLL );                        //切换系统时钟到PLL
  16.         if( res == 0x0U )                                                    //切换系统时钟成功
  17.         {
  18.                 RCC_HSI_Disable();                                               //切换时钟到PLL后关闭源时钟HSI
  19.         }
  20. }

  21. int32_t main(void)
  22. {
  23.                 rcc_config();
  24.                 delay_init();
  25.                 uart_init(115200);        
  26.     init_led();
  27.                

  28.     while (1)
  29.     {
  30.                                 led1_tog();
  31.                                 printf("\r\nCW32F030 UART Printf Example\r\n");
  32.                                 delay_ms(100);
  33.     }
  34. }

    2.4、软件配置
    需要在软件上选择下面选项
    003.png

    三、程序运行
    串口输出打印信息。
004.png    
   
   


uytyu 发表于 2022-7-9 12:15 | 显示全部楼层
CW32F030CxTx 性能还可以的。   
xiaoyaozt 发表于 2022-7-9 14:44 | 显示全部楼层
使用dma了吗  
hotcool 发表于 2022-7-21 17:47 | 显示全部楼层
这次评测不是给了两块板子
selongli 发表于 2022-8-19 20:39 | 显示全部楼层
是不是使用中断了?   
jackcat 发表于 2022-8-19 20:52 | 显示全部楼层
程序跑飞的影响很大   
iamaiqiyi 发表于 2022-8-19 22:38 | 显示全部楼层
超出了数组范围了吗   
robincotton 发表于 2022-8-20 12:35 | 显示全部楼层
这个就是printf响应的。   
wengh2016 发表于 2022-8-20 19:29 | 显示全部楼层
是不是使用了printf呢??
wengh2016 发表于 2022-8-20 21:01 | 显示全部楼层
你看看,代码有问题。   
yujielun 发表于 2022-8-21 20:49 | 显示全部楼层
可以超频操作的吗   
averyleigh 发表于 2022-9-8 08:23 | 显示全部楼层
printf发送数据吗?怎么配置?
updownq 发表于 2022-9-8 09:08 | 显示全部楼层
可以看串口输出信息
soodesyt 发表于 2022-9-8 12:32 | 显示全部楼层
iar中怎么设置使用printf函数
tabmone 发表于 2022-9-8 14:37 | 显示全部楼层
C语言的库函数 吧      
jtracy3 发表于 2022-9-8 19:18 | 显示全部楼层
单片机如何使用printf函数
tifmill 发表于 2022-9-10 19:04 | 显示全部楼层
printf()和scanf()函数怎么用的?
pmp 发表于 2022-9-10 20:56 | 显示全部楼层
对Printf 的格式输出不支持吗
uytyu 发表于 2022-9-11 11:56 | 显示全部楼层
使用printf函数需要设置什么?
sdCAD 发表于 2022-9-11 12:50 | 显示全部楼层
keil 中printf 函数怎么用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

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