STM32F303 USART配置

时间:2016-09-22 14:44:00

标签: c microcontroller stm32 usart

我在ARM中很新,并尝试配置UART。我使用的董事会是一个 STM32F3发现。目前我只试图从USART1获取PA9的tx信号,rx的中断例程尚未写入。在我看来,当我使用示波器时,我应该在PA9上看到一个信号,但是PIN在上拉时始终保持3V。我使用参考手册进行配置,并按照描述初始化所有寄存器。你看到有什么错吗?到目前为止我的代码:

/*----------------------------------------------------------------------------
 * CMSIS-RTOS 'main' function template
 *---------------------------------------------------------------------------*/

#define osObjectsPublic                     // define objects in main module
#include "osObjects.h"                      // RTOS object definitions
#include "stm32f3xx.h"                  // Device header

/*
 * Defines
 */
 #define SYS_FREQUENCY 8000000L //8Mhz

 /*
  * Global Variables
    */

 long baudrate=9600;

 //----------------------------------------------------------------------------------------------------
 void initGPIO() {      
        // initialize peripherals here
        //Activate red LED Port
        RCC->AHBENR |= RCC_AHBENR_GPIOEEN;              //enable PORTE clock
        GPIOE->MODER |= GPIO_MODER_MODER9_0;            //PORTE9 (LED red) is output
 }

 void initUSART(long baudrate) {
     long baudratio=SYS_FREQUENCY/baudrate;

     //Clock
     RCC->AHBENR |= RCC_AHBENR_GPIOAEN;             //enable PORTA clock
     RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_SYSCFGEN;       //ENABLE USART1 Clock

     //AF
     GPIOA->AFR[1] = 0;
     GPIOA->AFR[0] = GPIO_AFRL_AFRL7 & (7U<<8);     //AF7 Configuration

     //TX (PA9)
     GPIOA->MODER |= GPIO_MODER_MODER9_1 ;                                                                      //Alternating function, for TX (PA9)
     GPIOA->OTYPER = 0;                                                                                                             //Push-Pull
     GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR9_0 | GPIO_OSPEEDER_OSPEEDR9_1;     //Fast Speed
     GPIOA->PUPDR |= GPIO_PUPDR_PUPDR9_0;                                                                           //Pull-Up

     //RX (PA)
     GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR10_0 | GPIO_OSPEEDER_OSPEEDR10_1; //Fast Speed

     //USART
     USART1->CR1 =0;                                                                                                                                        //Reset
     USART1->CR2 =0;                                                                                                                                        //Reset
     USART1->CR3 =0;                                                                                                                                        //Reset
     USART1->BRR = baudratio & 0xFFFF;                                                                                                  //set Baudrate to 9600
     USART1->CR1 |= (USART_CR1_TE |USART_CR1_TXEIE | USART_CR1_RXNEIE| USART_CR1_RE);       //TX, RX Enable, Interrupts Enable
     USART1->CR1 |=  USART_CR1_UE;                                                                                                          //Enable USART
 }

 void sendChar(char c) {
         while(USART_ISR_TXE==1);                           //Data transfered to shift register
         USART1->TDR = (c & 0xFF);
         GPIOE->BSRRL = GPIO_BSRR_BS_9;      //LED on -> BSRRL is Bit Set Reset Register -> write 1 -> high
     osDelay(50);                           
     GPIOE->BSRRH =  GPIO_BSRR_BS_9 ;    //LED off
     osDelay(50);
 }

 void sendXThread(void const *argument) {
     while(1) {
        osDelay(150);
        sendChar('X');
     }
 }

 osThreadDef(sendXThread,osPriorityNormal,1,0);

 //----------------------------------------------------------------------------------------------------

int main (void) {
  osKernelInitialize ();                    // initialize CMSIS-RTOS

    //----------------------------------------------------------------------------------------------------
    initGPIO();
    initUSART(baudrate);
    //----------------------------------------------------------------------------------------------------


  // create 'thread' functions that start executing,
  // example: tid_name = osThreadCreate (osThread(name), NULL);
    osThreadCreate(osThread (sendXThread),NULL);
  osKernelStart ();                         // start thread execution 
}

1 个答案:

答案 0 :(得分:0)

while(USART_ISR_TXE==1); 

USART_ISR_TXE是定义的常量(#define USART_ISR_TXE (1 << 7)),它与ISR寄存器本身没有共同之处。您只是将2 ^ 7与1进行比较。