初级会员

- 积分
- 65
- 金钱
- 65
- 注册时间
- 2014-4-19
- 在线时间
- 0 小时
|
5金钱
#include "delay.h"
#include "usart.h"
#include "sys.h"
int main()
{
u32 i;
DELAY_init(72);
usart_init(9600);
printf("abcdefg");
for(i=0;i<8;i++)
{
USART_SendData(USART1,'A');
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
}
#include "usart.h"
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
void usart_init(u32 BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
//GPIO配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //接收
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
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);
//USART配置
USART_InitStructure.USART_BaudRate = BaudRate;
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_Tx | USART_Mode_Rx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
|
|