新手入门
- 积分
- 11
- 金钱
- 11
- 注册时间
- 2016-7-22
- 在线时间
- 2 小时
|
6金钱
GPIO初始化函数
void GPIOInit()
{
GPIO_InitTypeDef GPIOAInit,GPIOEInit;//PA9 PA10分别用于串口TX RX PE9用于LED
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,ENABLE);//使能外设时钟
GPIOAInit.GPIO_Mode = GPIO_Mode_AF_PP;
GPIOAInit.GPIO_Pin = GPIO_Pin_9;
GPIOAInit.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOAInit);
GPIOAInit.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIOAInit.GPIO_Pin = GPIO_Pin_10;
GPIOAInit.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIOAInit);
GPIOEInit.GPIO_Mode = GPIO_Mode_Out_PP;
GPIOEInit.GPIO_Pin = GPIO_Pin_9;
GPIOEInit.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIOEInit);
GPIO_SetBits(GPIOE,GPIO_Pin_9);
}
串口初始化
void UARTInit()
{
USART_InitTypeDef USART1Init;
USART1Init.USART_BaudRate = 9600;
USART1Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART1Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART1Init.USART_Parity = USART_Parity_No;
USART1Init.USART_StopBits = USART_StopBits_1;
USART1Init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART1Init);
}
中断初始化
void NVICInit()
{
NVIC_InitTypeDef NVICUART;
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVICUART.NVIC_IRQChannel = USART1_IRQn;
NVICUART.NVIC_IRQChannelCmd = ENABLE;
NVICUART.NVIC_IRQChannelPreemptionPriority = 1;
NVICUART.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVICUART);
}
主函数
int main(void)
{
SystemInit();
GPIOInit();
UARTInit();
NVICInit();
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
while(1)
{
if(Receive)//Receive为全局变量初始值为0
{
USART1->DR = Receive;
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET);
Receive = 0;
}
}
}
中断服务函数
void USART1_IRQHandler()
{
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
{
Receive = USART1->DR;
GPIO_ResetBits(GPIOE,GPIO_Pin_9);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
|
|