中级会员
 
- 积分
- 255
- 金钱
- 255
- 注册时间
- 2014-2-2
- 在线时间
- 41 小时
|
#include "stm32f10x.h"
void Delay(unsigned int n);
int main(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
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_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART1);
USART_InitTypeDef USART_InitStructure;
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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
while(1)
{
USART_SendData(USART1, 0xaa);
Delay(2000);
GPIO_ResetBits(GPIOE, GPIO_Pin_2);
Delay(2000);
GPIO_SetBits(GPIOE, GPIO_Pin_2);
Delay(2000);
}
}
void USART1_IRQHander(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_SendData(USART1, USART_ReceiveData(USART1));
while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
}
void Delay(unsigned int n)
{
unsigned int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<1000; j++);
}
}
上面是我写的程序,单片机能正常发送数据,要是接收数据他不会进入我写的中断,反而进入了vectors_stm32f10x.c文件中的
void __attribute__ ((section(".after_vectors")))
Default_Handler(void)
{
while (1)
{
}
}
这个死循环,请问这是怎么回事,怎么解决?麻烦高手帮忙解决。。。。。
|
|