初中生
最后登录1970-1-1
在线时间 小时
注册时间2021-8-14
|
实验是12_USART实验 1 USART收发
我对着源程序敲,一共需要敲程序的代码文件有四个(根据LED程序移植过来),一个是main.c,一个是bsp_debug_usart.c,一个是bsp_debug_usart.h, 一个是stm32f4xx.it.c
其中main是主程序
bsp是USART配置和相关收发程序
stm324xx_it.c是中断程序
现在问题是,中断程序可以正常运行,可以正常显示发出去的信息
但是
- Usart_SendString( DEBUG_USART,"这是一个串口中断接收回显实验\n");
复制代码 这个函数发给调试助手的是一堆乱码。- [21:49:44.265]收←◆炴樉瀹為獙\n杩欐槸涓€涓覆鍙d腑鏂帴鏀跺洖鏄惧疄楠孿n寤舵椂
复制代码 我看网上的debug贴说这是内部的晶振设置有问题,但是我也不太清楚哪里有问题。
源码是没错的,我怕自己敲错没找出来还替换了配套的官方程序(上面讲的四个文件)到我的工程里,再次下载还是不行,但是我用官方的程序又可以正常发送和正常显示。下面是我的源代码
- /*bsp_debug_usart.c
- * _______________#########_______________________
- * ______________############_____________________
- * ______________#############____________________
- * _____________##__###########___________________
- * ____________###__######_#####__________________
- * ____________###_#######___####_________________
- * ___________###__##########_####________________
- * __________####__###########_####_______________
- * ________#####___###########__#####_____________
- * _______######___###_########___#####___________
- * _______#####___###___########___######_________
- * ______######___###__###########___######_______
- * _____######___####_##############__######______
- * ____#######__#####################_#######_____
- * ____#######__##############################____
- * ___#######__######_#################_#######___
- * ___#######__######_######_#########___######___
- * ___#######____##__######___######_____######___
- * ___#######________######____#####_____#####____
- * ____######________#####_____#####_____####_____
- * _____#####________####______#####_____###______
- * ______#####______;###________###______#________
- * ________##_______####________####______________
- *
- * [url=home.php?mod=space&uid=41770]@brief[/url] : 函数定义,重定向printf函数到USART端口,中断接收模式
- * @Version : 1.0
- * @Autor : hgospodar
- * @Date : 2021-08-28 16:29:50
- * @LastEditors : hgospodar
- * @LastEditTime : 2021-09-08 20:41:42
- */
- #include "./usart/bsp_debug_usart.h"
- static void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- // 嵌套向量中断控制器选择
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- // 配置USART为中断源
- NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ;
- // 抢断优先级为1
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void Debug_USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_AHB1PeriphClockCmd(DEBUG_USART_RX_GPIO_CLK|DEBUG_USART_TX_GPIO_CLK, ENABLE);
- RCC_APB2PeriphClockCmd(DEBUG_USART_CLK, ENABLE);
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_PIN;
- GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_PIN;
- GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
- GPIO_PinAFConfig(DEBUG_USART_RX_GPIO_PORT, DEBUG_USART_RX_SOURCE, DEBUG_USART_RX_AF);
- GPIO_PinAFConfig(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_SOURCE, DEBUG_USART_TX_AF);
- USART_InitStructure.USART_BaudRate = DEBUG_USART_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_Rx | USART_Mode_Tx;
- USART_Init(DEBUG_USART, &USART_InitStructure);
- NVIC_Configuration();
- USART_ITConfig(DEBUG_USART, USART_IT_RXNE, ENABLE);
- USART_Cmd(DEBUG_USART, ENABLE);
- }
- /**
- * @brief 发送一个字符
- * @param {USART_TypeDef} *pUSARTx 发送的USART端口
- * @param {uint8_t} ch 发送的字符
- * [url=home.php?mod=space&uid=82536]@return[/url] {*}
- * @author hgospodar
- */
- void Usart_SendByte(USART_TypeDef *pUSARTx, uint8_t ch)
- {
- USART_SendData(pUSARTx, ch);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- /**
- * @brief 发送字符串
- * @param {USART_TypeDef} *pUSARTx 发送端口
- * @param {char} *str 发送的字符串地址
- * @return {*}
- * @author hgospodar
- */
- void Usart_SendString(USART_TypeDef *pUSARTx, char *str)
- {
- unsigned int k = 0;
- do
- {
- Usart_SendByte(pUSARTx, *(str + k));
- k++;
- } while (*(str + k)!='\0');
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET)
- {
- /* code */
- }
- }
- /**
- * @brief 发送一个16位数
- * @param {USART_TypeDef} *pUSARTx 发送端口
- * @param {uint16_t} ch 发送的16位数
- * @return {*}
- * @author hgospodar
- */
- void Usart_SendHalfWord(USART_TypeDef *pUSARTx, uint16_t ch)
- {
- uint8_t temp_h, temp_l;
- temp_h = (ch&0xff00)>>8;
- temp_l = ch&0xff;
- USART_SendData(pUSARTx, temp_h);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- USART_SendData(pUSARTx, temp_l);
- while(USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- int fputc(int ch, FILE *f)
- {
- while(USART_GetFlagStatus(DEBUG_USART, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(DEBUG_USART);
- }
- int fget(FILE *f)
- {
- while(USART_GetFlagStatus(DEBUG_USART, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(DEBUG_USART);
- }
复制代码- /*bsp_debug_usart.h
- * _______________#########_______________________
- * ______________############_____________________
- * ______________#############____________________
- * _____________##__###########___________________
- * ____________###__######_#####__________________
- * ____________###_#######___####_________________
- * ___________###__##########_####________________
- * __________####__###########_####_______________
- * ________#####___###########__#####_____________
- * _______######___###_########___#####___________
- * _______#####___###___########___######_________
- * ______######___###__###########___######_______
- * _____######___####_##############__######______
- * ____#######__#####################_#######_____
- * ____#######__##############################____
- * ___#######__######_#################_#######___
- * ___#######__######_######_#########___######___
- * ___#######____##__######___######_____######___
- * ___#######________######____#####_____#####____
- * ____######________#####_____#####_____####_____
- * _____#####________####______#####_____###______
- * ______#####______;###________###______#________
- * ________##_______####________####______________
- *
- * @brief : USART1的宏定义,函数声明
- * @Version : 1.0
- * @Autor : hgospodar
- * @Date : 2021-08-28 16:29:24
- * @LastEditors : hgospodar
- * @LastEditTime : 2021-08-28 17:21:30
- */
- #ifndef __DEBUG_USART_H
- #define __DEBUG_USART_H
- #include "stm32f4xx.h"
- #include <stdio.h>
- #define DEBUG_USART USART1
- #define DEBUG_USART_CLK RCC_APB2Periph_USART1
- #define DEBUG_USART_BAUDRATE 115200
- #define DEBUG_USART_RX_GPIO_PORT GPIOA
- #define DEBUG_USART_RX_GPIO_CLK RCC_AHB1Periph_GPIOA
- #define DEBUG_USART_RX_PIN GPIO_Pin_10
- #define DEBUG_USART_RX_AF GPIO_AF_USART1
- #define DEBUG_USART_RX_SOURCE GPIO_PinSource10
- #define DEBUG_USART_TX_GPIO_PORT GPIOA
- #define DEBUG_USART_TX_GPIO_CLK RCC_AHB1Periph_GPIOA
- #define DEBUG_USART_TX_PIN GPIO_Pin_9
- #define DEBUG_USART_TX_AF GPIO_AF_USART1
- #define DEBUG_USART_TX_SOURCE GPIO_PinSource9
- #define DEBUG_USART_IRQHandler USART1_IRQHandler
- #define DEBUG_USART_IRQ USART1_IRQn
- void Debug_USART_Config(void);
- void Usart_SendByte(USART_TypeDef *pUSARTx, uint8_t ch);
- void Usart_SendString(USART_TypeDef *pUSARTx, char *str);
- void Usart_SendHalfWord(USART_TypeDef *pUSARTx, uint16_t ch);
- #endif // __USART1_H
- //
复制代码- /*main.c
- * @Description: 循环点亮LED
- * @Version: 1.0
- * @Autor: hgospodar
- * @Date: 2021-08-20 17:15:28
- * @LastEditors : hgospodar
- * @LastEditTime : 2021-09-08 19:44:59
- * @FilePath: \05_新建工程_库函数版\User\main.c
- */
- /*
- * _______________#########_______________________
- * ______________############_____________________
- * ______________#############____________________
- * _____________##__###########___________________
- * ____________###__######_#####__________________
- * ____________###_#######___####_________________
- * ___________###__##########_####________________
- * __________####__###########_####_______________
- * ________#####___###########__#####_____________
- * _______######___###_########___#####___________
- * _______#####___###___########___######_________
- * ______######___###__###########___######_______
- * _____######___####_##############__######______
- * ____#######__#####################_#######_____
- * ____#######__##############################____
- * ___#######__######_#################_#######___
- * ___#######__######_######_#########___######___
- * ___#######____##__######___######_____######___
- * ___#######________######____#####_____#####____
- * ____######________#####_____#####_____####_____
- * _____#####________####______#####_____###______
- * ______#####______;###________###______#________
- * ________##_______####________####______________
- */
- #include "stm32f4xx.h"
- #include "./usart/bsp_debug_usart.h"
- /**
- * @description: 延时函数
- * @param {__IO u32} nCount 定时计数(相对大小)
- * @return {*}
- * @author: hgospodar
- */
- void Delay(__IO u32 nCount);
- /**
- * @description: 主函数
- * @param {*}
- * @return {int} 返回值
- * @author: hgospodar
- */
- int main(void)
- {
- Debug_USART_Config();
- Usart_SendString(DEBUG_USART, "这是一个串口中断接收回显实验\n");
- printf("这是一个串口中断接收回显实验\n");
- while(1)
- {
- Usart_SendString(DEBUG_USART, "延时\n");
- Delay(10000000);
- }
- }
- void Delay(__IO uint32_t nCount)
- {
- for (;nCount != 0; nCount--);
- }
复制代码
|
|