usart.h
#ifndef _USART_H
#define _USART_H
#include "stm32f10x.h"
#include "stdio.h"
void Usart_Init(unsigned long int Rate);
int fputc(int ch, FILE *f);
#endif
pbdata.h
#ifndef _PBDATA_H
#define _PBDATA_H
#include "stm32f10x.h"
void RCC_Init(unsigned char PLL);
#endif
usart.c
#include "usart.h"
void Usart_Init(unsigned long int Rate)
{
unsigned short int M,F;
float temp;
temp=(float)(72000000)/(16*Rate);
M=temp;
F=(temp-M)*16;
USART1->BRR=(M<<4)+F;
USART1->CR1|=1<<13;
USART1->CR1|=0X03<<2;
GPIOA->CRH&=0XFFFFF00F;
GPIOA->CRH|=0X000004b0;
RCC->APB2ENR|=1<<2;
RCC->APB2ENR|=1<<14;
RCC->APB2ENR|=0x01;
}
int fputc(int ch, FILE *f)
{
USART1->DR=(unsigned char)ch;
while(!(USART1->SR&(1<<6)));
return ch;
}
pbdata.c
#include "pbdata.h"
void RCC_Init(unsigned char PLL)
{
RCC->CR|=1<<16;
while(!(RCC->CR&(1<<17)));
RCC->CFGR|=1<<10;
RCC->CFGR|=(PLL-2)<<18;
RCC->CFGR|=1<<16;
FLASH->ACR=0X32;
RCC->CR|=1<<24;
while(!(RCC->CR&(1<<25)));
RCC->CFGR|=0X02;
while((RCC->CFGR&0X08)!=0X08);
}
main.c
#include "stm32f10x.h"
#include "pbdata.h"
#include "usart.h"
int main(void)
{
RCC_Init(9); //系统时钟初始化
Usart_Init(115200); //串口初始化
printf("1234567");
while(1);
}
此帖出自
小平头技术问答
好的,我晚上试一下,谢啦
一周热门 更多>