stm32 - USART 与 STM32F373VCt6
问题描述
我是新来的。我刚开始用STM32F373VCT6学习STM32F373。我使用 CMSIS 来配置 UART1。我的代码似乎没有任何错误。但是当我使用 PL2303 转换串行 USB 以与 PC 连接时。我什么都没有收到。
这是我的代码。谁能帮我找出你的错误?
/* Includes ------------------------------------------------------------------*/
#include "stm32f37x.h"
#include"main.h"
uint8_t ledVal = 0;
static __IO uint32_t TimingDelay;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BSRR_VAL 0x0003
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
/* Private function prototypes -----------------------------------------------*/
uint8_t SendChar (uint8_t ch);
void GPIO_Config(void);
void USART1_Config(void);
uint8_t GetChar (void);
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f37x.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f37x.c file
*/
//SystemInit();
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* Configure PC0 and PC1 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Config();
USART1_Config();
if (SysTick_Config(SystemCoreClock / 1000))
while (1);
while (1)
{
/* Set PC0 and PC1 */
GPIO_WriteBit(GPIOC , GPIO_Pin_13 , (ledVal) ? Bit_SET : Bit_RESET);
ledVal = 1 - ledVal;
SendChar('f');
Delay(250);
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void GPIO_Config(void) {
// PC4 configuration (TX)
RCC->AHBENR |= 1 << 19; // enable GPIOC clock
GPIOC->MODER |= 2 << (4*2); // GPIO_Mode_AF
GPIOC->OTYPER |= 1 << (4*1); // GPIO_OType_OD
GPIOC->OSPEEDR |= 3 << (4*2); // GPIO_Speed_50MHz
GPIOC->PUPDR &= ~(3 << (4*2)); // GPIO_PuPd_NOPULL
GPIOC->AFR[0] |= 7 << (4*4); // AF7
// PC5 configuration (RX)
GPIOC->MODER |= 2 << (5*2); // GPIO_Mode_AF
GPIOC->AFR[0] |= 7 << (5*4); // AF7
}
/* Configuring USART1 */
void USART1_Config(void)
{
RCC->APB2ENR |= RCC_APB2ENR_USART1EN|RCC_APB2ENR_SYSCFGEN; // Enable USART1 clock
USART1->BRR = 72000000/115200;
USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16
USART1->CR1 &= ~USART_CR1_M; // Word length = 8 bits
USART1->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0); // one stop bit
USART1->CR1 &= ~USART_CR1_PCE; // No parity
USART1->CR1 |= USART_CR1_UE; // USART enable
USART1->CR1 |= USART_CR1_RE; // Receiver enable
USART1->CR1 |= USART_CR1_TE; // Transmitter enable
}
uint8_t SendChar (uint8_t ch)
{
while (!(USART1->ISR & USART_ISR_TXE));
USART1->TDR = (ch & 0xFF);
return (ch);
}
uint8_t GetChar (void)
{
while (!(USART1->ISR & USART_ISR_RXNE));
return ((uint8_t)(USART1->RDR & 0xFF));
}
/* Delay fuction */
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/* Decrement */
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
TimingDelay --;
}
解决方案
RCC
缺少操作后的延迟
系列的勘误表STM32F373
说,
2.1.2 RCC 外设时钟使能后的延迟
说明 应考虑 RCC 外设时钟使能和有效外设使能之间的延迟,以便管理外设对寄存器的读/写。此延迟取决于外设映射。
如果外设映射到 AHB:在硬件寄存器上设置时钟使能位后延迟 2 个 AHB 时钟周期。
如果外设映射到 APB:在硬件寄存器上设置时钟使能位后,延迟为 2 个 APB 时钟周期。
解决方法
有时在需要外设读/写寄存器之前启用外设时钟。
对于 AHB 外设,插入两个虚拟读取到外设寄存器。
对于 APB 外设,插入一个虚拟读取到外设寄存器。
如果没有此延迟,RCC 使能操作之后的一次(或两次)读取可能会返回 0,而不管寄存器值如何,或者外设可能会忽略前两次写入。
库函数RCC_AHBPeriphClockCmd()
可能已经包含此延迟,但请检查 Stdperiph 源以确定。
如果可能,我通常会在开始时对 RCC 访问进行分组,从我将首先使用的外围设备开始。
RCC->AHBENR |= 1 << 19; // enable GPIOC clock // (1)
RCC->APB2ENR |= RCC_APB2ENR_USART1EN|RCC_APB2ENR_SYSCFGEN; // (2)
GPIOC->MODER = whatever; // (3)
// ...
USART1->BRR = divisor;
这样,(2)中的读-修改-写操作将在启用GPIOC
(1)和使用它(3)之间引入足够的延迟。
推荐阅读
- php - 在哪里可以找到 Laravel 中定义的语言函数?
- python - Matplotlib 分组条形图显示为堆叠
- swift - 为什么我不能在 SwiftUI init 中为 @State var 赋值?
- android - 如何在运行时使用带有参数的 FragmentFactory?
- c# - 有没有办法让 WinSCP 会话在主表单中保持打开状态并从另一个表单访问同一会话?
- javascript - 我如何根据日期(或任何元素相似的元素)在javascript中合并数组中的两个元素
- c++ - C++ 双精度
- javascript - 向对象键添加引号
- binary - 使用 IEEE 754 浮点到二进制
- drools - Drools [测试场景]:日期类型的加载字段