【RISC-V MCU CH32V103测评】- 3:USART - 为什么试了三个串口工具都不行?
[复制链接]
本帖最后由 MianQi 于 2021-2-2 11:07 编辑
这次测评的对象是:USART,依据是:《CH32V103应用教程——USART》。
首先,文档看过以后,有一点不明白,在第4页,关于"void USART ReceiveWakeUpCmd",介绍说,“功能:确定USART是否处于静音模式,不明白“WakeUp”何以对应“静音”。
其次,接线:
用了6根双母口的跳线:
WCH-Link CH32V103C-RO-1v2
5v 5v
GND GND
SWCLK SWCLK
SWDIO SWDIO
TX PA3
RX PA2
代码:
usart.h
#ifndef __USART_H
#define __USART_H
#include "ch32v10x_conf.h"
void USARTx_CFG(void);
void USARTx_SendByte(USART_TypeDef* pUSARTx, uint8_t data);
void USARTx_SendStr(USART_TypeDef* pUSARTx, char* str);
void USART2_IRQHandler(void);
#endif
usart.c
#include "usart.h"
void USART2_IRQHandler(void)__attribute__((interrupt("WCH-Interrupt-fast")));
void USARTx_CFG(void) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
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_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_DeInit(USART2);
USART_InitStructure.USART_BaudRate = 115200;
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 | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USARTx_SendByte(USART_TypeDef* pUSARTx, uint8_t data) {
USART_SendData(pUSARTx, data);
while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
void USARTx_SendStr(USART_TypeDef* pUSARTx, char* str) {
uint8_t i = 0;
do {
USARTx_SendByte(pUSARTx, *(str + i));
i++;
} while(*(str + i) != '\n');
while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET);
}
void USART_IRQHandler(void) {
uint8_t ucTemp;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
ucTemp = USART_ReceiveData(USART2);
USART_SendData(USART2, ucTemp);
}
}
main.c
#include "debug.h"
#include "usart.h"
int main(void) {
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
USARTx_CFG();
USARTx_SendStr(USART2, "This is a test string.\n");
while(1);
}
上述代码,个别细节跟原文不一样,做了一点调整,但就是不做调整,依然是编译下载没有问题,却在Putty、Tera Term、VS Code三个串口工具中都没有显示,有谁知道这是为什么。
|