#ifndef _usart_H
#define _usart_H
#include "
STM32f10x.h"
#include <stdio.h>
void usart_init(void);
#endif
/***********************************************************************************************/
#include "usart.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO¶Ë¿Ú½á¹¹Ì嶨Òå
USART_InitTypeDef USART_InitStructure; //USART´®¿Ú½á¹¹Ì嶨Òå
NVIC_InitTypeDef NVIC_InitStructure; //ÖжÏÓÅÏȼ¶½á¹¹Ì嶨Òå
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //A¶Ë¿Ú¼ÓʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //¸´Óù¦ÄܼÓʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //50MHzƵÂÊ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //PA.9¹Ü½Å
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //¸´ÓÃÍÆÃâÊä³öģʽ
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO³õʼ»¯
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //PA.10¹Ü½Å
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOA
tiNG; //¸¡¿ÕģʽÊäÈ룻
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO³õʼ»¯
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_Tx | USART_Mode_Rx; //·¢ËͺͽÓÊÕͬʱʹÄÜ
USART_Init(USART1,&USART_InitStructure); //USART1³õʼ»¯
USART_Cmd(USART1,ENABLE); //USART1ʹÄÜ
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //ÆôÓýÓÊÕÖжÏʹÄÜ
USART_ClearFlag(USART1,USART_FLAG_TC); //Çå³þ·¢ËÍÍê³É±êÖ¾
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //ÓÅÏȼ¶·Ö×é
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //ÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //ÏìÓ¦ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //ʹÄÜ
NVIC_Init(&NVIC_InitStructure); //NVIC³õʼ»¯
}
/***********************************************************************************************/
void USART1_IRQHandler(void)
{
u8 res;
USART_ClearFlag(USART1,USART_FLAG_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=Bit_RESET)
{
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==Bit_RESET);
}
}
一周热门 更多>