文章标题

来源:互联网 发布:淘宝网红店铺排名 编辑:程序博客网 时间:2024/05/22 14:17

include “USART1.h”

//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB

if 1

pragma import(__use_no_semihosting)

//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef¡¯ d in stdio.h. */
FILE __stdout;
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ
void _sys_exit(int x)
{
x = x;
}
//Öض¨Òåfputcº¯Êý
int fputc(int ch, FILE *f)
{
while(!((USART1->ISR)&(1<<7))){}
USART1->TDR=ch;
return (ch);
}

endif

/* USART³õʼ»¯ */
void USART1_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);  //ʹÄÜGPIOAµÄʱÖÓRCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//ʹÄÜUSARTµÄʱÖÓ/* USART1µÄ¶Ë¿ÚÅäÖà */GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);//ÅäÖÃPB6³ÉµÚ¶þ¹¦ÄÜÒý½Å TXGPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);//ÅäÖÃPB7³ÉµÚ¶þ¹¦ÄÜÒý½Å  RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOB, &GPIO_InitStructure);/* USART1µÄ»ù±¾ÅäÖà */USART_InitStructure.USART_BaudRate = baud;              //²¨ÌØÂÊ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);                             //ʹÄÜUSART1/* USART1µÄNVICÖжÏÅäÖà */NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStruct.NVIC_IRQChannelPriority = 0x02;NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStruct);

}

//=============================================================================
//ÎļþÃû³Æ£º
//¹¦ÄܸÅÒª£ºUSART1ÖжϺ¯Êý
//²ÎÊý˵Ã÷£ºÎÞ
//º¯Êý·µ»Ø£ºÎÞ
//=============================================================================
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_SendData(USART1,USART_ReceiveData(USART1));
while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}

}

void USART1_SendBuf(uint8_t *pBuf, uint32_t u32Len)
{
while(u32Len–)
{
while(!USART_GetFlagStatus(USART1,USART_FLAG_TXE));
USART_SendData(USART1,*pBuf++);
}
}

uint8_t USART1_ReciverBuf(void)
{
while(!USART_GetFlagStatus(USART1,USART_FLAG_RXNE));
return USART_ReceiveData(USART1);
}

原创粉丝点击