USART with STM32F373VCt6

时间:2018-05-12 05:54:52

标签: stm32

我是新来的。我刚开始用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 --;
}

1 个答案:

答案 0 :(得分:1)

缺少RCC操作后的延迟

STM32F373系列的勘误表说,

  

2.1.2 RCC外设时钟使能后的延迟

     

描述   RCC外设时钟使能和有效外设使能之间的延迟   应该考虑到管理外设对寄存器的读/写操作。   此延迟取决于外设映射。

     

如果在AHB上映射外设:时钟使能位后的延迟为2个AHB时钟周期   设置在硬件寄存器上。

     

如果外设映射在APB上:延迟是在硬件寄存器上设置时钟使能位后的2个APB时钟周期。

     

变通方法

     
      
  1. 有时在需要外设读/写寄存器之前使能外设时钟。

  2.   
  3. 对于AHB外设,将两个虚拟读取插入外设寄存器。

  4.   
  5. 对于APB外设,将虚拟读取插入外设寄存器。

  6.   

如果没有此延迟,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)之间引入足够的延迟。