本帖最后由 huzb11 于 2017-4-6 16:58 编辑
串口3接GPS模块,串口2接GPRS模块。单独运行串口3 GPRS程序,工作正常,单独运行串口2 GPS程序也正常,两个程序同时运行,发现GPRS运行正常,GPS数据都为0,有时按一下按键,又有数据,但是数据不能更新,不知是定时器之间配制冲突了,还是中断优先级配制的问题引起的。哪位帮我分析一下。
这是串口2配制,复制串口3,更改的:
__align(8) u8 USART2_TX_BUF[USART2_MAX_SEND_LEN];
#ifdef USART2_RX_EN
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)
{
res =USART_ReceiveData(USART2);
if((USART2_RX_STA&(1<<15))==0)
{
if(USART2_RX_STA<USART2_MAX_RECV_LEN)
{
//TIM_SetCounter(TIM7,0);
TIM_SetCounter(TIM5,0);
if(USART2_RX_STA==0)
//TIM_Cmd(TIM7, ENABLE);
TIM_Cmd(TIM5, ENABLE);
USART2_RX_BUF[USART2_RX_STA++]=res;
}else
{
USART2_RX_STA|=1<<15;
}
}
}
}
#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);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
USART_DeInit(USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
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(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM5_Int_Init(100-1,8400-1);
USART2_RX_STA=0;
TIM_Cmd(TIM5, DISABLE);
}
//′®¿ú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);
for(j=0;j<i;j++)
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
USART_SendData(USART2,(uint8_t)USART2_TX_BUF[j]);
}
}
这是串口3配制:
u16 USART3_RX_STA=0;
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
res =USART_ReceiveData(USART3);
if((USART3_RX_STA&(1<<15))==0)
{
if(USART3_RX_STA<USART3_MAX_RECV_LEN)
{
TIM_SetCounter(TIM7,0);//¼ÆêyÆ÷Çå¿Õ
if(USART3_RX_STA==0)
TIM_Cmd(TIM7, ENABLE);
USART3_RX_BUF[USART3_RX_STA++]=res;
}else
{
USART3_RX_STA|=1<<15;
}
}
}
}
#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);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
USART_DeInit(USART3); //¸′λ′®¿ú3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
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(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú3
USART_Cmd(USART3, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM7_Int_Init(1000-1,8400-1);
USART3_RX_STA=0;
TIM_Cmd(TIM7, DISABLE);
}
//′®¿ú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¼Ä′æ
}
|
|