本帖最后由 lbb_1112 于 2016-3-18 11:20 编辑
我写的中断接收数据,然后查询发送不能正常工作,求高手指点:
void NVIC_Configuration(void);
u8 i,rt;
u8 data1[3]={0,0,0};
int main()
{
u8 i;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
USART1_init1();
NVIC_Configuration();
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_Cmd(USART1,ENABLE);
while(1)
{
if(rt==1)
{
for(i=0;i<3;i++)
{
USART_SendData(USART1, data1);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)!=SET);
}
rt=0;
}
else
{
__nop();
}
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
// USART_ClearITPendingBit(USART1, USART_IT_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{for(i=0;i<3;i++)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
data1=USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
if(i==2)
{i=0;
rt=1;
}
}
}
初始化串口程序:
void USART1_init1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART1);//¸´Î»´®¿Ú1;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9ΪUSART1_TX£¬Ð´³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //ÍÆÍ츴ÓÃÊä³ö£»
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
}
|