美好的一天。 我试图从Stm32 std库运行UART示例脚本,似乎它不起作用。 我使用这样的电路板:
通过USART1上传固件。 USART初始化代码:
#include <stm32f10x.h>
#include <misc.h>
volatile char received_string[MAX_STRLEN+1];
void Delay(__IO uint32_t nCount) {
while(nCount--) {
}
}
void init_USART1(uint32_t baudrate){
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = baudrate;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART_puts(USART_TypeDef* USARTx, volatile char *s){
while(*s){
while( !(USARTx->SR & 0x00000040) );
USART_SendData(USARTx, *s);
*s++;
}
}
int main(void) {
init_USART1(9600); // initialize USART1 @ 9600 baud
USART_puts(USART1, "Init complete! Hello World!\r\n"); // just send a message to indicate that it works
while (1){
/*
* You can do whatever you want in here
*/
}
}
void USART1_IRQHandler(void){
// check if the USART1 receive interrupt flag was set
if( USART_GetITStatus(USART1, USART_IT_RXNE) ){
static uint8_t cnt = 0; // this counter is used to determine the string length
char t = USART1->DR; // the character from the USART1 data register is saved in t
// check if the received character is not the LF character (used to determine end of string)
// or the if the maximum string length has been been reached
if( (t != '\n') && (cnt < MAX_STRLEN) ){
received_string[cnt] = t;
cnt++;
}
else{ // otherwise reset the character counter and print the received string
cnt = 0;
USART_puts(USART1, received_string);
}
}
}
USART1端口正在使用NVIC启用。
答案 0 :(得分:0)
您可以尝试将USART_puts()函数更改为:
void USART_puts(USART_TypeDef* USARTx, volatile char *s){
while(*s)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);// Wait the set of TXE
USART_SendData(USART1, *(s++));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);// Wait the end of transmit
}
}
答案 1 :(得分:0)
我正在像您一样使用Stdpriph方法。我的代码有效。你能给它一个试用伴侣吗?
#include <stm32f10x.h>
#include <stdio.h>
#include <stm32f10x_usart.h>
#include <stm32f10x_gpio.h>
#include <stm32f10x_rcc.h>
static unsigned int LEDState = 0;
static void USART_SendString(USART_TypeDef* USARTx, char* s)
{
while(*s)
{
while(!USART_GetFlagStatus(USARTx, USART_FLAG_TC));
USART_SendData(USARTx, *s);
s++;
}
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
if((char)USART_ReceiveData(USART1) == '1'){
LEDState = 2;
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
}
if((char)USART_ReceiveData(USART1) == '0')
LEDState = 1;
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
int main(){
SystemInit();
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//----------------------------------------------------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
//GPIO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA pin 0 output push pull
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM3_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM3_InitStructure.TIM_Prescaler = 50 ; //Prescaler degeri.
TIM3_InitStructure.TIM_CounterMode = TIM_CounterMode_Up; //Timer yukari sayacak sekilde ayarlandi.
TIM3_InitStructure.TIM_Period = 48000;//Period degeri.
TIM3_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//Bölme orani 1 yani geçersiz-kapali.
TIM_TimeBaseInit(TIM3, &TIM3_InitStructure);//Ayarlari yükle
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); // Kesmeyi aktif ediyoruz.
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE);*/
while(1)
{
char temp= USART_ReceiveData(USART1);
/*if(LEDState == 2) {
GPIO_SetBits(GPIOC, GPIO_Pin_8);
USART_SendString(USART1, "LED On\r\n");
LEDState = 0;
}
if(LEDState == 1) {
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
USART_SendString(USART1, "LED Off\r\n");
LEDState = 0;
}
}*/
}
}