[mw_shl_code=c,true]#include "usart.h"
void USART1_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); //?ò??PE?±??
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); //?ò???®???±??
//Tx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //?è??????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //IO·?×?????50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //?è??????????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //?è??????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
USART_InitStructure.USART_BaudRate = 9600;
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(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE);
}
int fputc(int ch ,FILE *f)
{
USART_SendData(USART2, (unsigned char)ch);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
return(ch);
}
void USART_Send(uint8_t *f)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
USART_SendData(USART2, *f);
f++;
}
void USART_Send_bate(uint8_t bate)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
USART_SendData(USART2, bate);
}
需要勾选use MicroLIB 然后主程序可以直接使用printf输出[/mw_shl_code]
|