高级会员

- 积分
- 602
- 金钱
- 602
- 注册时间
- 2015-7-11
- 在线时间
- 94 小时
|

楼主 |
发表于 2016-8-24 09:37:38
|
显示全部楼层
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //PA9-TX PA10-RX
USART_InitStructure.USART_BaudRate = 115200;
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;
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_USART1);//PA9-TX
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);//PA10-RX
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1,&USART_InitStructure);
/*USART1 interrupt Enable*/
USART_ITConfig(USART1 ,USART_IT_RXNE ,ENABLE ); //??????
/* Enable USART */
USART_Cmd(USART1, ENABLE);
void USART1_IRQHandler(void)
{
uint8_t data;
//USART_ClearFlag(USART1,USART_FLAG_TC); //ÕaÖÖ·½ê½oÃÏñ¿éòÔ
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearFlag (USART1 ,USART_FLAG_RXNE);
data = USART_ReceiveData(USART1);
/* Read one byte from the receive data register */
printf("receive data:%02x\r\n",data);//16½øÖÆêä3ö
// USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //¿éòÔ
// if(data==0x36)
// stop_flag=0;
}
USART_Data_Parser(data);
}
再自行 配置usart1的中断优先级 |
|