USART1配置普通收发模式,发送完后就进入接收中断 ,发送几个数据就进入几次。USART1 刚开始重映射用了PB6 PB7搞了一天都还是不是,用复用PA10,PA9也是这问题。上代码:
void GpioCon**(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_GPIOA
| RCC_APB2PERIPH_GPIOB
| RCC_APB2PERIPH_GPIOC
| RCC_APB2PERIPH_GPIOF
| RCC_APB2PERIPH_AFIO , ENABLE); //打开外设时钟
//TXD1
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9 ;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//RXD1
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10 ;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Usart1Con**(void)
{
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
USART_ClockInitType USART_ClockInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2PERIPH_USART1, ENABLE);
USART_ClockStructInit(&USART_ClockInitStructure);
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
/*Configure UART param*/
USART_StructInit(&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(USART1, &USART_InitStructure);
USART_INTConfig(USART1, USART_INT_RDNE, ENABLE); //开启接收中断
//USART_ClearFlag(USART1, USART_INT_RDNE); //清除USARTx的挂起标志
USART_Cmd(USART1, ENABLE); //使能USART
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_SEND_DATA(u8 * data ,u8 datalen)
{
u8 t =0;
if(USART1_TXOK_FLAG == 1)
{
for(t=0;t<datalen;t++)
{
USART_SendData(USART1, data[t]); //向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TDE)!=SET); //等待发送结束
}
USART1_TXOK_FLAG = 0;
}
}
void USART1_IRQHandler(void)
{
u8 u8InChar;
static u8 sezi_cnt = 0;
if(USART_GetITStatus(USART1,USART_INT_RDNE) != RESET )
{
u8InChar = USART_ReceiveData(USART1);
SYS_LED_TURN;
DomeTxCnt[0]++;
/****************************************************************************************************/
//PollReceHandle(u8InChar); //并机通讯接收处理
/****************************************************************************************************/
//USART_INTConfig(USART1, USART_INT_RDNE, DISABLE);
}
} |