为什么LED不闪烁,串口也不一直发数据?求指导
#include "delay.h"
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_crc.h"
#include "system_stm32f10x.h"
#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void delay_ms(u16 time);
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data);
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str);
int main()
{
SystemInit();
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
USART_ClearFlag(USART1, USART_FLAG_TC);
//printf("
发送的信息:");
UART_PutStr(USART1, "hello world!");
delay_ms(500);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delay_ms(500);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//LED GPIO_Pin_A8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)//串口初始化函数
{
//串口参数初始化
USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
//初始化参数设置
USART_InitStructure.USART_BaudRate = 115200; //波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止字节
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//打开Rx接收和Tx发送功能
USART_Init(USART1,&USART_InitStructure); //初始化
USART_Cmd(USART1,ENABLE); //启动串口
}
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET){}
}
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)
{
while (0 != *str)
{
UART_PutChar(USARTx, *str);
str++;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>