//代码
#include "stm32f10x.h"
#include "usart.h"
#include <stdio.h>
int main(void)
{
Usart_Init(115200);
while (1)
{
printf("
I am in while loop
");
}
}
//usart.c
#include "usart.h"
#include "stm32f10x.h"
#include <stdio.h>
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
void Usart_Init(unsigned int bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//rxd PA10 浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
printf("
USART is now ok!
");
}
//usart.h
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
void Usart_Init(unsigned int bound);
#endif
不知道为什么波特率会减半 比如我这里设置的115200波特率 下载到板子上打开串口调试工具 要调到57600波特率才能正常接收信息
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我记成F4了,从你的图,应该是F1的
从你的图上看,你选的是6倍PLL,但我看不到PLLSource选的是什么。
一周热门 更多>