gpt4 book ai didi

c - STM32F303 USART配置

转载 作者:行者123 更新时间:2023-11-30 16:57:14 25 4
gpt4 key购买 nike

我对 ARM 很陌生,正在尝试配置 U​​ART。我使用的主板是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
}

最佳答案

while(USART_ISR_TXE==1); 

USART_ISR_TXE是一个已定义的常量 ( #define USART_ISR_TXE (1 << 7) ),它与 ​​ISR 没有任何共同点自行注册。您只是将 2^7 与 1 进行比较。

关于c - STM32F303 USART配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39642198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com