野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 23130|回复: 2

TIM定时器的问题(定时器3产生4路不同方波)

[复制链接]
发表于 2014-3-11 20:47:41 | 显示全部楼层 |阅读模式
在我没有修改这个函数的时候,软件仿真是正常的,出现了四个不同的PWM方波,但是,我也只是修改了一下注释,调了一下语句的顺序,程序没有错,但是,没有方波出现了,怎么回事啊??
下面是我修改后的程序段:
/*
* º¯ÊýÃû£ºTIM3_Mode_Config
* ÃèÊö  £ºÅäÖÃTIM3Êä³öµ&AumlWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
* ÊäÈë  £ºÎÞ
* Êä³ö  £ºÎÞ
* µ÷Óà £ºÄÚ²¿µ÷ÓÃ
*/
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
/* PWMÐźŵçƽÌø±äÖµ */
u16 CCR1_Val = 500;        
u16 CCR2_Val = 375;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
/* -----------------------------------------------------------------------
    TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
    TIM3CLK = 72 MHz, Prescaler = 0x0, TIM3 counter clock = 72 MHz
    TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    TIM3 Frequency = 72 KHz.
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */
  /* Time base configuration */   
  TIM_TimeBaseStructure.TIM_Period = 999;       //µ±¶¨Ê±Æ÷´Ó0¼ÆÊýµ½999£¬¼´Îª1000´Î£¬ÎªÒ»¸ö¶¨Ê±ÖÜÆÚ
  TIM_TimeBaseStructure.TIM_Prescaler = 0;     //ÉèÖÃÔ¤·ÖƵ£º²»Ô¤·ÖƵ£¬¼´Îª72MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //ÏòÉϼÆÊýģʽ
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
  
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;     //ÅäÖÃÎ&ordfWMģʽ1£¨È«²¿Í¨µÀ¶¼ÓÐÊ¡ÂÔдÁË£©
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ ¸ß£¨È«²¿Í¨µÀ¶¼ÓÐÊ¡ÂÔдÁË£©

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ÓÃÓÚÑ¡ÔñÊä³ö±È½Ï״̬ʹÄÜ
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //Ìø±äÖµ   
TIM_OC1Init(TIM3, &TIM_OCInitStructure);//³õʼ»¯Í¨µÀ1  
  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//ʹÄÜԤװÔؼĴæÆ÷£¨OC1£©
  
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//ÓÃÓÚÑ¡ÔñÊä³ö±È½Ï״̬ʹÄÜ
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;//Ìø±äÖµ     
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);//³õʼ»¯Í¨µÀ2  
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);//ʹÄÜԤװÔؼĴæÆ÷£¨OC2£©
  
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//ÓÃÓÚÑ¡ÔñÊä³ö±È½Ï״̬ʹÄÜ
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //Ìø±äÖµ  
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);//³õʼ»¯Í¨µÀ3  
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);//ʹÄÜԤװÔؼĴæÆ÷£¨OC3£©

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//ÓÃÓÚÑ¡ÔñÊä³ö±È½Ï״̬ʹÄÜ
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //Ìø±äÖµ  
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);//³õʼ»¯Í¨µÀ4  
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);//ʹÄÜԤװÔؼĴæÆ÷£¨OC4£©
  
TIM_ARRPreloadConfig(TIM3, ENABLE); //ʹÄÜTIMxÔÚARRÉϵÄԤװÔؼĴæÆ÷   
}

回复

使用道具 举报

发表于 2014-3-11 20:51:36 | 显示全部楼层
这。。看得眼花,楼主可以尝试使用论坛的代码工具贴代码,这太难看看了。

建议使用source insight 软件或者文本比较软件来看看自己修改后的代码和野火原来的代码有什么区别
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-11 20:58:52 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-23 16:47 , Processed in 0.024797 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表