#include "CS.h"
#include "delay.h"
#include "led.h"
//PA6 ¶¨Ê±Æ÷ÈýͨµÀÒ»
// TIM_ICInitTypeDef TIM_ICInitStructure;
void cs_init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPD; //ÏÂÀ­ÊäÈë
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //TRIGÊä³ö
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=arr;
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
// TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter=0; //²»ÅäÖÃÂ˲¨Æ÷
TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising; //ÉÏÉýÑز¶»ñ
TIM_ICInitStructure.TIM_ICPrescaler=TIM_CKD_DIV1; //ÊäÈë²»·ÖƵ
TIM_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI; //Ó³Éäµ½TI1
TIM_ICInit(TIM3,&TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //ÏÈÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //´ÓÕ¼ÓÅÏȼ¶
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE); //ÔÊÐí¸üÐÂÖжÏ
LED0=1;
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
LED1=1;
TIM_Cmd(TIM3,ENABLE);
}
u8 sta=0; //8λµÄ²¶»ñ״̬ ÐéÄâsta¼Ä´æÆ÷
u16 val; //¼Ç¼Ï½µÑص½À´Ê±cntµÄÖµ
void TIM2_IRQHandler()
{
if(0==(sta&0x80)) //»¹Ã»Óгɹ¦²¶»ñ
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //·¢Éú¸üÐÂÖжÏ
{TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
if(sta&0x40) //ÒѾ­²¶»ñµ½¸ßµçƽ£¬¿ªÊ¼µÈ´ýµÍµçƽµÄµ½À´
{
if(0x3f==(sta&0x3f)) //sta¼Ä´æÆ÷ÒѾ­¼ÆÂú
{
sta=sta|0x80; //×î¸ßλÖÃ1
val=0xffff;
}
else //sta¼Ä´æÆ÷»¹Î´¼ÆÂú
{
sta++; //CNT¼ÆÂúÒ»´Îsta¼Ä´æÆ÷¼ÓÒ»
}
}
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC1)!=RESET) //·¢ÉúµÄÊDz¶»ñÖжÏ
{
if(sta&0x40) //ÒѾ­²¶»ñµ½¸ßµçƽ¿ªÊ¼µÈ´ýµÍµçƽµÄµ½À´£¨Õâ´Îµ½µÄ¾ÍÊǵ͵çƽ²¶»ñ£©
{
sta=sta|0x80; //µÍµçƽµ½´ï£¬×î¸ßλÖÃÒ»
val=TIM_GetCapture1(TIM3); //²¶»ñ½áÊø£¬»ñÈ¡CNTµÄÖµ
TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_High); //ÉèÖÃΪÉÏÉýÑز¶»ñ£¨×¼±¸ÏÂÒ»´Î£©
}
else //»¹Î´²¶»ñµ½¹ý¸ßµçƽ£¬ÕâÊǵÚÒ»´Î²¶»ñµ½£¨Õâ´Î¾ÍÊǸߵçƽ£©
{
sta=0; //
val=0; //¸ßµçƽµ½´ï¼ÆÊýÆ÷STAÇåÁ㣬¿ªÊ¼¼Æʱ
TIM_SetCounter(TIM3,0); //CNTÇåÁã
sta=sta|0x40; //±ê¼Ç²¶»ñµ½Á˸ߵçƽһ´Î
TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_Low); //ÉèÖÃΪϽµÑز¶»ñ
}
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
这个初始化一直卡死在 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); 这个地方(我使用的led灯检测出来的,两个灯默认都是亮的,原本灯赋值1会灭掉,但是只有LED0灭了)
,找不到原因,我以为是没清更新中断标志位,但在服务函数中清了好多次,还是这样,一直卡在这里》》》》求教,帮顶也行!!!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
顶一下
一周热门 更多>