新手入门
- 积分
- 10
- 金钱
- 10
- 注册时间
- 2016-3-5
- 在线时间
- 5 小时
|
1金钱
自己按照视频写的串口程序。想打到给串口发数据,串口收到数据后,有将收到的数据重新发到电脑上。但是写好的程序。串口可以发,但是加上中断后。进入不到中断。以下是我的程序: 请指教
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2,&USART_InitStruct);
USART_Cmd(USART2,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
void USART2_IRQHandler()
{
float adc;
USART_SendData(USART2,'2');
if(USART_GetITStatus(USART2,USART_IT_RXNE)==1)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
adc=USART_ReceiveData(USART2);
USART_SendData(USART2,adc);
USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
}
}
int main()
{
usart_init();
USART_SendData(USART2,'1');//这个1可以发送。
while(1);
}
|
最佳答案
查看完整内容[请看2#楼]
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
你的这句话错了,细心点。是USART2_IRQn
|