我想做一个通过SIM卡的短信功能把当前位置发送给其他人,单个功能都尝试了,但是放在一起始终调不通。sim卡用的是串口3,GPS用的是串口2,sim卡功能正常,但是串口2始终获取不到定位信息,单个跑GPS程序可以获取定位信息
网上看了可能是双串口问题,但是也没能解决,希望有志之士能给予解答一下,以下是我的串口初始化和中断处理
[mw_shl_code=c,true]//串口2
u16 USART2_RX_STA=0;
void USART2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)//ÊǸüÐÂÖжÏ
{
USART2_RX_STA|=1<<15; //±ê¼Ç½ÓÊÕÍê³É
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //Çå³ýTIM7¸üÐÂÖжϱêÖ¾
TIM_Cmd(TIM2, DISABLE); //¹Ø±ÕTIM7
}
}
#endif
//³õʼ»¯IO ´®¿Ú3
//bound:²¨ÌØÂÊ
void usart2_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ʹÄÜGPIOBʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //ʹÄÜUSART2ʱÖÓ
USART_DeInit(USART2); //¸´Î»´®¿Ú3
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOB11¸´ÓÃΪUSART3
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOB10¸´ÓÃΪUSART3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2; //GPIOB11ºÍGPIOB10³õʼ»¯
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ËÙ¶È50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»¯GPIOB11£¬ºÍGPIOB10
USART_InitStructure.USART_BaudRate = bound; //²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //×Ö³¤Îª8λÊý¾Ý¸ñʽ
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_Init(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú3
USART_Cmd(USART2, ENABLE); //ʹÄÜ´®¿Ú
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //¿ªÆôÖжÏ
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
TIM2_Int_Init(100-1,8400-1); //10msÖжÏ
USART2_RX_STA=0; //ÇåÁã
TIM_Cmd(TIM2, DISABLE); //¹Ø±Õ¶¨Ê±Æ÷7
}[/mw_shl_code]
[mw_shl_code=c,true]//串口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); //¼ÆÊýÆ÷Çå¿Õ
if(USART3_RX_STA==0)
TIM_Cmd(TIM7, ENABLE); //ʹÄܶ¨Ê±Æ÷7
USART3_RX_BUF[USART3_RX_STA++]=res; //¼Ç¼½ÓÊÕµ½µÄÖµ
}else
{
USART3_RX_STA|=1<<15; //Ç¿ÖƱê¼Ç½ÓÊÕÍê³É
}
}
}
}
#endif
//³õʼ»¯IO ´®¿Ú3
//bound:²¨ÌØÂÊ
void usart3_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //ʹÄÜGPIOBʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //ʹÄÜUSART3ʱÖÓ
USART_DeInit(USART3); //¸´Î»´®¿Ú3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //GPIOB11¸´ÓÃΪUSART3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //GPIOB10¸´ÓÃΪUSART3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOB11ºÍGPIOB10³õʼ»¯
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ËÙ¶È50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
GPIO_Init(GPIOB,&GPIO_InitStructure); //³õʼ»¯GPIOB11£¬ºÍGPIOB10
USART_InitStructure.USART_BaudRate = bound; //²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //×Ö³¤Îª8λÊý¾Ý¸ñʽ
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
USART_Cmd(USART3, ENABLE); //ʹÄÜ´®¿Ú
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //¿ªÆôÖжÏ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇÀÕ¼ÓÅÏȼ¶2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
TIM7_Int_Init(100-1,8400-1); //10msÖжÏ
USART3_RX_STA=0; //ÇåÁã
TIM_Cmd(TIM7, DISABLE); //¹Ø±Õ¶¨Ê±Æ÷7
USART_ClearFlag(USART3, USART_FLAG_TC);
}[/mw_shl_code]
[mw_shl_code=c,true]//定时器初始化
extern vu16 USART3_RX_STA;
//¶¨Ê±Æ÷7ÅäÖÃԤװÔØÖÜÆÚÖµ
void TIM7_SetARR(u16 period)
{
TIM_SetCounter(TIM7,0);//¼ÆÊýÆ÷Çå¿Õ
TIM7->ARR&=0x00; //ÏÈÇåԤװÔØÖÜÆÚֵΪ0
TIM7->ARR|= period; //¸üÐÂԤװÔØÖÜÆÚÖµ
}
//¶¨Ê±Æ÷7ÖжϷþÎñ³ÌÐò
void TIM7_IRQHandler(void)
{
static u8 i=0;
if(TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET) //ÊǸüÐÂÖжÏ
{
TIM_ClearITPendingBit(TIM7, TIM_IT_Update ); //Çå³ýTIM7¸üÐÂÖжϱêÖ¾
if(Scan_Wtime!=0)//À¶ÑÀɨÃèģʽ
{
i++;
if(i==Scan_Wtime)
{
i=0;
Scan_Wtime = 0;
USART3_RX_STA|=1<<15; //±ê¼Ç½ÓÊÕÍê³É
TIM_Cmd(TIM7, DISABLE); //¹Ø±Õ¶¨Ê±Æ÷7
TIM7_SetARR(100-1); //ÖØÐÂÉèÖÃΪ10msÖжÏ
}
}
else//·ÇÀ¶ÑÀɨÃèģʽ
{
USART3_RX_STA|=1<<15; //±ê¼Ç½ÓÊÕÍê³É
TIM_Cmd(TIM7, DISABLE); //¹Ø±ÕTIM7
}
}
}
//ͨÓö¨Ê±Æ÷Öжϳõʼ»¯
//ÕâÀïʼÖÕÑ¡ÔñΪAPB1µÄ2±¶£¬¶øAPB1Ϊ36M
//arr£º×Ô¶¯ÖØ×°Öµ¡£
//psc£ºÊ±ÖÓÔ¤·ÖƵÊý
void TIM7_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);//TIM7ʱÖÓʹÄÜ
//¶¨Ê±Æ÷TIM7³õʼ»¯
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM7ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ; //ÇÀÕ¼ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×ÓÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
}
extern vu16 USART2_RX_STA;
////¶¨Ê±Æ÷7ÅäÖÃԤװÔØÖÜÆÚÖµ
void TIM2_SetARR(u16 period)
{
TIM_SetCounter(TIM2,0);//¼ÆÊýÆ÷Çå¿Õ
TIM2->ARR&=0x00; //ÏÈÇåԤװÔØÖÜÆÚֵΪ0
TIM2->ARR|= period; //¸üÐÂԤװÔØÖÜÆÚÖµ
}
//¶¨Ê±Æ÷7ÖжϷþÎñ³ÌÐò
void TIM2_IRQHandler(void)
{
static u8 i=0;
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //ÊǸüÐÂÖжÏ
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //Çå³ýTIM7¸üÐÂÖжϱêÖ¾
if(Scan_Wtime!=0)//À¶ÑÀɨÃèģʽ
{
i++;
if(i==Scan_Wtime)
{
i=0;
Scan_Wtime = 0;
USART2_RX_STA|=1<<15; //±ê¼Ç½ÓÊÕÍê³É
TIM_Cmd(TIM2, DISABLE); //¹Ø±Õ¶¨Ê±Æ÷7
TIM2_SetARR(100-1); //ÖØÐÂÉèÖÃΪ10msÖжÏ
}
}
else//·ÇÀ¶ÑÀɨÃèģʽ
{
USART2_RX_STA|=1<<15; //±ê¼Ç½ÓÊÕÍê³É
TIM_Cmd(TIM2, DISABLE); //¹Ø±ÕTIM7
}
}
}
//ͨÓö¨Ê±Æ÷Öжϳõʼ»¯
//ÕâÀïʼÖÕÑ¡ÔñΪAPB1µÄ2±¶£¬¶øAPB1Ϊ36M
//arr£º×Ô¶¯ÖØ×°Öµ¡£
//psc£ºÊ±ÖÓÔ¤·ÖƵÊý
void TIM2_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//TIM7ʱÖÓʹÄÜ
//¶¨Ê±Æ÷TIM7³õʼ»¯
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //ʹÄÜÖ¸¶¨µÄTIM7ÖжÏ,ÔÊÐí¸üÐÂÖжÏ
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇÀÕ¼ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×ÓÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
}[/mw_shl_code]
一周热门 更多>