我想用定时器1的PA.8输出占空比为50%,频率为500K的方波信号。结果我按下面的配置,500K的信号是出来了,但出几个方波接着就一个宽一点的低电平,不正常。这是怎么回事?是我的配置哪里有问题吗?
//¶¨Ê±Æ÷2ÅäÖÃ
void SetupTIMBASE(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//ÅäÖÃϵͳʱÖÓ10MS
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷2Ä£¿éʱÖÓ(¶¨Ê±Æ÷2µÄʱÖÓÔÚAPB1µÄ36M×ÜÏßÉÏ)
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 20000; //Èç¹ûCCRx>=APR,ÔòFREquency=TIM2CLK/APR 1M/20000=50HZ
TIM_TimeBaseStructure.TIM_Prescaler = 35; //TIM2CLK/£¨35+1£©=1MHz 1000 000 /20000
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //²»·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //¼ÆÊýģʽÏòÉÏ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //ʹÄÜÖжÏ
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE); //ʹÄÜTIM2Ä£¿é
}
//Çý¶¯²½½øµç»ú»òÕßËÅ·þµç»úµÄÂö³åÊä³öPWM³õʼ»¯
void Time1_pwm_setup(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM1, GPIOA clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA, ENABLE);
/* GPIOA Configuration: Channel 1 Output */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//51200*3000/60=2.56MHz ×î´óϸ·ÖµÄʱºò51200¸öÂö³åһȦ£¬ÉèÖÃ×î¸ß3000תÿ·ÖÖÓµÄ×î¸ß£Ð£×£ÍƵÂÊ
//10000*3000/60=500K ×î´óϸ·ÖµÄʱºò10000¸öÂö³åһȦ£¬ÉèÖÃ×î¸ß3000תÿ·ÖÖÓµÄ×î¸ß£Ð£×£ÍƵÂÊ
//1/500K=2uS
//72MHzÏÂ144¸öÂö³åΪȡ·´Ò»´ÎΪ500K
/* TIM1 Peripheral Configuration */
TIM_DeInit(TIM1);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0x00; //Ô¤·ÖƵ72M£¯500K=144
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
TIM_TimeBaseStructure.TIM_Period = 143; //ԤװÔØÖµ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //²»·ÖƵ
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //¸ù¾ÝTIM_TIMBaseInitStruct ÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ TIM1 µÄʱ¼ä»ùÊýµ¥Î»
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM1Âö³å¿í¶Èµ÷ÖÆģʽ 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ʹÄÜÊä³ö±È½Ï״̬
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //ʹÄÜÊä³ö±È½ÏN ״̬
TIM_OCInitStructure.TIM_Pulse = 71; //ÉèÖÃÁË´ý×°È벶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ¡£ËüµÄÈ¡Öµ±ØÐëÔÚ0x0000 ºÍ0xFFFFÖ®¼ä¡£
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM1Êä³ö±È½Ï¼«ÐÔµÍ
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //TIM1Êä³ö±È½Ï N ¼«ÐÔµÍ
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //µ±MOE=0ÉèÖà TIM1Êä³ö±È½Ï¿ÕÏÐ״̬
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //µ±MOE=0ÖØÖà TIM1Êä³ö±È½Ï N ¿ÕÏÐ״̬
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //¸ù¾ÝTIM_OCInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ TIM1ͨµÀ1
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //ʹÄÜTIM1 OSSR״̬
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //ʹÄÜTIM1 OSSI ״̬
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //ʹÓÃËøµçƽ1
TIM_BDTRInitStructure.TIM_DeadTime = 0x75; //Ö¸¶¨ÁËÊä³ö´ò¿ªºÍ¹Ø±Õ״̬֮¼äµÄÑÓʱ¡£
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //ʹÄÜTIM1ɲ³µÊäÈë
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //TIM1ɲ³µÊäÈë¹Ü½Å¼«ÐÔ¸ß
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //×Ô¶¯Êä³ö¹¦ÄÜʹÄÜ
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); //ÉèÖÃɲ³µÌØÐÔ£¬ËÀÇøʱ¼ä£¬Ëøµçƽ£¬OSSI£¬OSSR ״̬ºÍAOE£¨×Ô¶¯Êä³öʹÄÜ£©
/* TIM1 counter enable */
TIM_Cmd(TIM1,ENABLE);
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1,ENABLE);
} |