程序代码如下:#include "pbdata.h"
//#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
uint32_t uartreceive;
int main(void)
{
RCC_Configuration(); //ϵͳʱÖÓ³õʼ»¯
GPIO_Configuration(); //¶Ë¿Ú³õʼ»¯
USART_Configuration();//´®¿Ú³õʼ»¯
NVIC_Configuration(); //Öжϳõʼ»¯
GPIO_ResetBits(GPIOD,GPIO_Pin_7);
while(1);
}
void RCC_Configuration(void)
{
SystemInit();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//TX ¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//RX ¸¡¿ÕÊäÈë
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;//485_CS ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
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_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
中断处理如下:
void USART2_IRQHandler(void)
{
uartreceive = 0;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
static int k = 0;
// uartreceive=USART_ReceiveData(USART2);
uartreceive=USART2->DR;
GPIO_SetBits(GPIOD,GPIO_Pin_7);
delay_ms(1);
//USART_ClearITPendingBit(USART2, USART_IT_RXNE);
//USART_ClearFlag(USART2, USART_FLAG_RXNE);
USART_SendData(USART2,uartreceive);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
delay_ms(1);
delay_ms(1);
GPIO_ResetBits(GPIOD,GPIO_Pin_7);
}
} |