正在尝试激活并利用sTM32F303K8上的usart1外围设备。正在VisualGDB中使用stdperiph运行它。但是,我无法使用中断模式开始使用USART。即使我探究别针,它们都保持沉默。我想念什么。花了很多天了。
#include <stm32f30x_gpio.h>
#include <stm32f30x_rcc.h>
#include "stm32f30x_usart.h"
#include "stm32f30x_misc.h"
void SysTick_Handler()
{
msTicks++;
}
void setSysTick()
{
if (SysTick_Config(SystemCoreClock / 1000))
{
while (1) {}
}
}
void Delay()
{
int i;
for (i = 0; i < 1000000; i++)
asm("nop");
}
void init_peripherals(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 | GPIO_PinSource10, GPIO_AF_7);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART_puts(USART_TypeDef* usar_tx, volatile char* str)
{
while (*str)
{
while (USART_GetFlagStatus(usar_tx, USART_FLAG_TC) == RESET) {}
USART_SendData(usar_tx, *str);
str++;
}
}
void USART1_IRQHandler(void)
{
if (USART1->ISR & USART_ISR_RXNE)
{
const char ch = USART_ReceiveData(USART1);
if (ch != 'n' && ch != 'r')
{
rcvd_str[cnt++] = ch;
}
else
{
rcvd_str[cnt] = 'r';
cnt = 0;
str_buildup = true;
}
}
}
int main()
{
setSysTick();
init_peripherals();
char* strr = "Hello Worldn";
USART_puts(USART1, strr);
return 0;
}
预期的结果是在串行终端中像实现一样的印刷。我还不能到达那里。
一旦中断到达后,它将开始在上述代码处理程序中相应地执行其处理程序,正在检查终止字符并将字符存储在字符串中,但在离开中断处理程序之前,需要清除中断。NVIC初始化后,再次添加USART_CMD并错过了NVIC_INIT检查这些。
gpio_pinsourcex不应是bitmask。尽管很难知道哪些操作不需要被拆卸