//代码
#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波特率才能正常接收信息
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
原子哥 我用正点原子的例程直接烧进去不会出现这种现象 我自己写的这个就要波特率减半 是主时钟不对么
是的
目前的代码 单片机设置的115200电脑上要设置成57600才能正常显示
http://pan.baidu.com/s/1hs5UHM0
求解
还有一种可能,你的系统初始化认为你的晶体是16M的,但你的晶体是8M的,也会出现这个问题,去程序里检查一下P、N、M三个参数。
一周热门 更多>