本帖最后由 challenger 于 2017-4-5 20:58 编辑
stm32F4,GPS模块接在串口3上,定时器7控制串口3中断(原子哥例程),GPRS模块接在串口2上(改了原子哥串口3的配制代码,变成串口2接GPRS模块),本来也是定时器7控制中断,被我改为定时器5控制中断。GPRS与GPS两个程序分开运行都正常。可是合并后,只能运行一个GPRS,怎么回事?是中断优先级没设置好,或是定时器7与定时器5中的哪个变量重复使用了,运行异常?
这是串口2配制,复制串口3,更改的:
__align(8) u8 USART2_TX_BUF[USART2_MAX_SEND_LEN]; //·¢Ëí»o3å,×î′óUSART2_MAX_SEND_LEN×Ö½ú
#ifdef USART2_RX_EN //èç1ûê1Äüá˽óêÕ
u8 USART2_RX_BUF[USART2_MAX_RECV_LEN];
u16 USART2_RX_STA=0;
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//½óêÕμ½êy¾Y
{
res =USART_ReceiveData(USART2);
if((USART2_RX_STA&(1<<15))==0) //½óêÕíêμÄò»Åúêy¾Y,»1ûóD±»′|àí,Ôò2»Ôù½óêÕÆäËûêy¾Y
{
if(USART2_RX_STA<USART2_MAX_RECV_LEN) //»1¿éòÔ½óêÕêy¾Y
{
//TIM_SetCounter(TIM7,0); //¼ÆêyÆ÷Çå¿Õ
TIM_SetCounter(TIM5,0); //¼ÆêyÆ÷Çå¿Õ
if(USART2_RX_STA==0)
//TIM_Cmd(TIM7, ENABLE); //ê1Äü¶¨ê±Æ÷5
TIM_Cmd(TIM5, ENABLE); //ê1Äü¶¨ê±Æ÷5
USART2_RX_BUF[USART2_RX_STA++]=res; //¼Ç¼½óêÕμ½μÄÖμ
}else
{
USART2_RX_STA|=1<<15; //Ç¿ÖƱê¼Ç½óêÕíê3é
}
}
}
}
#endif
//3õê¼»ˉIO ′®¿ú2
//bound:2¨ìØÂê
void usart2_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //ê1ÄüUSART2ê±Öó RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
USART_DeInit(USART2); //¸′λ′®¿ú2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2¸′óÃÎaUSART2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3¸′óÃÎaUSART2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA2oíGPIOA33õê¼»ˉ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉGPIOA2£¬oíGPIOA3
USART_InitStructure.USART_BaudRate = bound; //2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1; //ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No; //ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); //3õê¼»ˉ′®¿ú2
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //¿aÆôÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
TIM5_Int_Init(100-1,8400-1); //10msÖD¶Ï
USART2_RX_STA=0; //Çåáã
TIM_Cmd(TIM5, DISABLE); //1رն¨ê±Æ÷5
}
//′®¿ú2,printf oˉêy
//è·±£ò»′η¢Ëíêy¾Y2»3¬1yUSART2_MAX_SEND_LEN×Ö½ú
void u2_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART2_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)USART2_TX_BUF); //′Ë′η¢Ëíêy¾YμÄ3¤¶è
for(j=0;j<i;j++) //Ñ-»··¢Ëíêy¾Y
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);//μè′yéÏ′Î′«êäíê3é
USART_SendData(USART2,(uint8_t)USART2_TX_BUF[j]); //·¢Ëíêy¾Yμ½′®¿ú2
}
}
这是串口3配制:
u16 USART3_RX_STA=0;
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//½óêÕμ½êy¾Y
{
res =USART_ReceiveData(USART3);
if((USART3_RX_STA&(1<<15))==0)//½óêÕíêμÄò»Åúêy¾Y,»1ûóD±»′|àí,Ôò2»Ôù½óêÕÆäËûêy¾Y
{
if(USART3_RX_STA<USART3_MAX_RECV_LEN) //»1¿éòÔ½óêÕêy¾Y
{
TIM_SetCounter(TIM7,0);//¼ÆêyÆ÷Çå¿Õ
if(USART3_RX_STA==0)
TIM_Cmd(TIM7, ENABLE); //ê1Äü¶¨ê±Æ÷7
USART3_RX_BUF[USART3_RX_STA++]=res; //¼Ç¼½óêÕμ½μÄÖμ
}else
{
USART3_RX_STA|=1<<15; //Ç¿ÖƱê¼Ç½óêÕíê3é
}
}
}
}
#endif
//3õê¼»ˉIO ′®¿ú3
//bound:2¨ìØÂê
void usart3_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //ê1ÄüGPIOBê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//ê1ÄüUSART3ê±Öó
USART_DeInit(USART3); //¸′λ′®¿ú3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //GPIOB11¸′óÃÎaUSART3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //GPIOB10¸′óÃÎaUSART3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOB11oíGPIOB103õê¼»ˉ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉGPIOB11£¬oíGPIOB10
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú3
USART_Cmd(USART3, ENABLE); //ê1Äü′®¿ú
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇàÕ¼óÅÏ輶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
TIM7_Int_Init(1000-1,8400-1); //100msÖD¶Ï
USART3_RX_STA=0; //Çåáã
TIM_Cmd(TIM7, DISABLE); //1رն¨ê±Æ÷7
}
//′®¿ú3,printf oˉêy
//è·±£ò»′η¢Ëíêy¾Y2»3¬1yUSART3_MAX_SEND_LEN×Ö½ú
void u3_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART3_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)USART3_TX_BUF);//′Ë′η¢Ëíêy¾YμÄ3¤¶è
for(j=0;j<i;j++)//Ñ-»··¢Ëíêy¾Y
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //μè′yéÏ′Î′«êäíê3é
USART_SendData(USART3,(uint8_t)USART3_TX_BUF[j]); //·¢Ëíêy¾Yμ½′®¿ú3
}
以下是定时器7与定时器5的代码,原来两个模块接的都是定时器7,我把TIM7,直接变成TIM5,分开运行都正常。合并成一个工程后,发现调用函数,两个一起,只能运行一个模块。我注释任意一个函数,留下一个函数,都能正常运行。请哪位帮我指点指点,究竟哪儿出了问题?感谢!
extern vu16 USART3_RX_STA; //¶¨ê±Æ÷7ÅäoÏ′®¿ú3¿ØÖÆGPS
//¶¨ê±Æ÷7ÖD¶Ï·tÎñ3ìDò
void TIM7_IRQHandler(void)
{
if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET)//êǸüDÂÖD¶Ï
{
USART3_RX_STA|=1<<15; //±ê¼Ç½óêÕíê3é
TIM_ClearITPendingBit(TIM7, TIM_IT_Update ); //Çå3yTIM7¸üDÂÖD¶Ï±êÖ¾
TIM_Cmd(TIM7, DISABLE); //1رÕTIM7
}
}
void TIM7_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);//TIM7ê±Öóê1Äü
//¶¨ê±Æ÷TIM73õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM7ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇàÕ¼óÅÏ輶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
//////////////////////////////////////////////////////////////////////////////////
extern vu16 USART2_RX_STA; // ¶¨ê±Æ÷5 ÅäoÏ′®¿ú2,¿ØÖÆGPRS
void TIM5_SetARR(u16 period) //¶¨ê±Æ÷5
{
TIM_SetCounter(TIM5,0);//¼ÆêyÆ÷Çå¿Õ
TIM5->ARR&=0x00; //ÏèÇåԤװÔØÖüÆúÖμÎa0
TIM5->ARR|= period; //¸üDÂԤװÔØÖüÆúÖμ
}
//¶¨ê±Æ÷5ÖD¶Ï·tÎñ3ìDò
void TIM5_IRQHandler(void) //GPRS
{
static u8 i=0;
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) //êǸüDÂÖD¶Ï
{
TIM_ClearITPendingBit(TIM5, TIM_IT_Update ); //Çå3yTIM5¸üDÂÖD¶Ï±êÖ¾
if(Scan_Wtime!=0)//à¶Ñàé¨ÃèÄ£ê½
{
i++;
if(i==Scan_Wtime)
{
i=0;
Scan_Wtime = 0;
USART2_RX_STA|=1<<15; //±ê¼Ç½óêÕíê3é
TIM_Cmd(TIM5, DISABLE); //1رն¨ê±Æ÷5
TIM5_SetARR(100-1); //ÖØDÂéèÖÃÎa10msÖD¶Ï
}
}
else//·Çà¶Ñàé¨ÃèÄ£ê½
{
USART2_RX_STA|=1<<15; //±ê¼Ç½óêÕíê3é
TIM_Cmd(TIM5, DISABLE); //1رÕTIM5
}
}
}
//í¨óö¨ê±Æ÷ÖD¶Ï3õê¼»ˉ
//Õaàïê¼ÖÕÑ¡ÔñÎaAPB1μÄ2±¶£¬¶øAPB1Îa36M
//arr£o×Ô¶ˉÖØ×°Öμ¡£
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM5_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);//TIM5ê±Öóê1Äü
//¶¨ê±Æ÷TIM73õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM5ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
//NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn ;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ; //ÇàÕ¼óÅÏ輶0
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ; //ÇàÕ¼óÅÏ輶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
一周热门 更多>