打印
[CW32F030系列]

【CW32F030CxTx StartKit测评】 串口输出

[复制链接]
859|22
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tlled|  楼主 | 2022-7-2 10:36 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
    在上篇文章将内部时钟设置为48M后,下面学习下串口1的输出。


    一、串口硬件电路


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


    二、程序部分

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

int fputc(int ch, FILE *f)
{
  USART_SendData_8bit(CW_UART1, (uint8_t)ch);

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

  return ch;
}
  
void uart_init(uint32_t bound)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOA, ENABLE);
  RCC_APBPeriphClk_Enable2(RCC_APB2_PERIPH_UART1, ENABLE);
        
  PA08_AFx_UART1TXD();                     
  PA09_AFx_UART1RXD();                    

  GPIO_InitStructure.Pins = GPIO_PIN_8;
  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
   
  GPIO_InitStructure.Pins = GPIO_PIN_9;
  GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
  GPIO_Init(CW_GPIOA, &GPIO_InitStructure);
        
        USART_InitStructure.USART_BaudRate = bound;
        USART_InitStructure.USART_Over = USART_Over_16;
        USART_InitStructure.USART_Source = USART_Source_PCLK;
        USART_InitStructure.USART_UclkFreq = 48000000;
        USART_InitStructure.USART_StartBit = USART_StartBit_FE;
        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_Rx | USART_Mode_Tx;
        USART_Init(CW_UART1, &USART_InitStructure);         
}

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

void uart_init(uint32_t bound);

#endif



    2.3、main.c

#include "main.h"
#include "cw32f030_flash.h"
#include "led.h"
#include "delay.h"
#include "usart.h"


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

int32_t main(void)
{
                rcc_config();
                delay_init();
                uart_init(115200);        
    init_led();
               

    while (1)
    {
                                led1_tog();
                                printf("\r\nCW32F030 UART Printf Example\r\n");
                                delay_ms(100);
    }
}

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

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


使用特权

评论回复
沙发
uytyu| | 2022-7-9 12:15 | 只看该作者
CW32F030CxTx 性能还可以的。   

使用特权

评论回复
板凳
xiaoyaozt| | 2022-7-9 14:44 | 只看该作者
使用dma了吗  

使用特权

评论回复
地板
hotcool| | 2022-7-21 17:47 | 只看该作者
这次评测不是给了两块板子

使用特权

评论回复
5
selongli| | 2022-8-19 20:39 | 只看该作者
是不是使用中断了?   

使用特权

评论回复
6
jackcat| | 2022-8-19 20:52 | 只看该作者
程序跑飞的影响很大   

使用特权

评论回复
7
iamaiqiyi| | 2022-8-19 22:38 | 只看该作者
超出了数组范围了吗   

使用特权

评论回复
8
robincotton| | 2022-8-20 12:35 | 只看该作者
这个就是printf响应的。   

使用特权

评论回复
9
wengh2016| | 2022-8-20 19:29 | 只看该作者
是不是使用了printf呢??

使用特权

评论回复
10
wengh2016| | 2022-8-20 21:01 | 只看该作者
你看看,代码有问题。   

使用特权

评论回复
11
yujielun| | 2022-8-21 20:49 | 只看该作者
可以超频操作的吗   

使用特权

评论回复
12
averyleigh| | 2022-9-8 08:23 | 只看该作者
printf发送数据吗?怎么配置?

使用特权

评论回复
13
updownq| | 2022-9-8 09:08 | 只看该作者
可以看串口输出信息

使用特权

评论回复
14
soodesyt| | 2022-9-8 12:32 | 只看该作者
iar中怎么设置使用printf函数

使用特权

评论回复
15
tabmone| | 2022-9-8 14:37 | 只看该作者
C语言的库函数 吧      

使用特权

评论回复
16
jtracy3| | 2022-9-8 19:18 | 只看该作者
单片机如何使用printf函数

使用特权

评论回复
17
tifmill| | 2022-9-10 19:04 | 只看该作者
printf()和scanf()函数怎么用的?

使用特权

评论回复
18
pmp| | 2022-9-10 20:56 | 只看该作者
对Printf 的格式输出不支持吗

使用特权

评论回复
19
uytyu| | 2022-9-11 11:56 | 只看该作者
使用printf函数需要设置什么?

使用特权

评论回复
20
sdCAD| | 2022-9-11 12:50 | 只看该作者
keil 中printf 函数怎么用

使用特权

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

本版积分规则

125

主题

689

帖子

6

粉丝