大家帮我看看 有问题吗?下面是初始化函数
/**********************************************************
函数名称:初始化IO 串口1
函数功能:
出口参数:bound:波特率
***********************************************************/
void Uart1_Init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //配置GPIOA和USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //端口映射选择
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/*配置串口1的TX*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //最高输出速率50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //AF复用
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //PP推挽式
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure); // void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
/*配置串口1的RX*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //OD Open-Drain 开漏
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure); // void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
/*配置串口1的模式*/
USART_InitStructure.USART_BaudRate = bound;
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); // void USART_Init(USART_TypeDef *USARTx,)
USART_Cmd(USART1, ENABLE); // void USART_Cmd(USART_TypeDef *USARTx, FunctionalState NewState)
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启中断
USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //开启中断
USART_Cmd(USART1, ENABLE); //使能串口
}
下面是中断处理函数。
/**********************************************************
函数名称:串口1中断服务函数
函数功能:
出口参数:
***********************************************************/
void USART1_IRQHandler(void) // 串口1中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 接收中断(接收到的数据必须是0x0d 0x0a结尾)
//if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
Res =USART_ReceiveData(USART1); //(USART1->DR);
buffreadtourat1[len_buffreadtourat1]=Res;
len_buffreadtourat1++;
if((buffreadtourat1[len_buffreadtourat1-1]==0x29)||(len_buffreadtourat1>100))
{
len_buffreadtourat1=0;
Flag_Allow_Send_Net=1;
}
}
}
时间不定会死机 有时候几十分钟 有时候 几个小时
最终进入 硬件中断里面
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
一般我查看都是从这里进去的
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
{
uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
ITStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_GET_IT(USART_IT));
/* The CTS interrupt is not available for UART4 and UART5 */
if (USART_IT == USART_IT_CTS)
{
assert_param(IS_USART_1236_PERIPH(USARTx));
}
/* Get the USART register index */
usartreg = (((uint8_t)USART_IT) >> 0x05);
/* Get the interrupt position */
itmask = USART_IT & IT_MASK;
itmask = (uint32_t)0x01 << itmask;
if (usartreg == 0x01) /* The IT is in CR1 register */
{
itmask &= USARTx->CR1;
}
else if (usartreg == 0x02) /* The IT is in CR2 register */
{
itmask &= USARTx->CR2;
}
else /* The IT is in CR3 register */
{
itmask &= USARTx->CR3;
}
bitpos = USART_IT >> 0x08;
bitpos = (uint32_t)0x01 << bitpos;
bitpos &= USARTx->SR;
if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return bitstatus;
}
其他定时器的优先级 都很低
//?ù±??¨?±?÷6??????????
//?????±????????APB1??2±?????APB1??36M
//arr??×?????×°????
//psc???±???¤·?????
//?????????????¨?±?÷3!
void TIM6_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //?±??????
TIM_TimeBaseStructure.TIM_Period = arr; //?è???????????ü??????×°????????×?????×°???????÷???????? ??????5000??500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //?è??????×÷??TIMx?±?????????????¤·????? 10Khz??????????
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //?è???±??·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?ò??????????
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //?ù??TIM_TimeBaseInitStruct?????¨????????????TIMx???±???ù??????
TIM_ITConfig( TIM6,TIM_IT_Update|TIM_IT_Trigger,ENABLE);//?????¨?±?÷6?ü????·?????
TIM_Cmd(TIM6, ENABLE); //????TIMx???è
NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn; //TIM6????
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15; //??????????2??
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //????????3??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??±?????
NVIC_Init(&NVIC_InitStructure); //?ù??NVIC_InitStruct?????¨???????????????èNVIC?????÷
}
//?ù±??¨?±?÷3??????????
//arr??×?????×°????
//psc???±???¤·?????
//?????????????¨?±?÷3!
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //?±??????
TIM_TimeBaseStructure.TIM_Period = arr; //?è???????????ü??????×°????????×?????×°???????÷???????? ??????5000??500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //?è??????×÷??TIMx?±?????????????¤·????? 10Khz??????????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //?è???±??·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?ò??????????
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //?ù??TIM_TimeBaseInitStruct?????¨????????????TIMx???±???ù??????
TIM_ITConfig( TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE);//?????¨?±?÷6?ü????·?????
TIM_Cmd(TIM3, ENABLE); //????TIMx???è
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM6????
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 14; //??????????2??
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //????????3??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??±?????
NVIC_Init(&NVIC_InitStructure); //?ù??NVIC_InitStruct?????¨???????????????èNVIC?????÷
}
不知道怎么回事 大家帮我看看啊?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>