芯片使用: 用TPM1的CH0和CH1通道产生中央对齐的PWM波,分别控制2个NMOS,当PWM为高的时候MOS关断,PWM为低的时候MOS导通。 初始化程序:(PE自动生成)周期为2ms void TPM1_Init(void) { (void)getReg8(TPM1C0SC); /* Channel 0 int.flag clearing (first part) */ /*TPM1C0SC: CH0F=0,CH0IE=1,MS0B=1,MS0A=0,ELS0B=1,ELS0A=0,??=0,??=0 */ setReg8(TPM1C0SC, 0x68U); /*Int. flag clearing (2nd part) and channel 0 contr. registersetting */ setReg16(TPM1C0V, 0x3E80U); /*Compare 0 value setting */ (void)getReg8(TPM1C1SC); /* Channel 1 int.flag clearing (first part) */ /*TPM1C1SC: CH1F=0,CH1IE=1,MS1B=1,MS1A=0,ELS1B=1,ELS1A=0,??=0,??=0 */ setReg8(TPM1C1SC,0x68U); /*Int. flag clearing (2nd part) and channel 1 contr. registersetting */ setReg16(TPM1C1V, 0x3E80U); /*Compare 1 value setting */ /*TPM1SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */ setReg8(TPM1SC, 0x00U); /*Stop and reset counter */ setReg16(TPM1MOD, 0x3E80U); /*Period value setting */ (void)getReg8(TPM1SC); /* Overflow int.flag clearing (first part) */ /*TPM1SC: TOF=0,TOIE=1,CPWMS=1,CLKSB=0,CLKSA=1,PS2=0,PS1=0,PS0=0 */ setReg8(TPM1SC, 0x68U); /*Int. flag clearing (2nd part) and timer control register setting */ } TPM1调用程序: if (PTBES_PTBES1==0) //如果是捕获下降沿,则此时的hal信号为高电平 { if(duty>50) { TPM1C1SC= 0x00; TPM1C1SC=0x64; TPM1C1V=(duty-DEAD_TIME); //设置死区时间16*0.125=2us; } else { TPM1C1SC = 0x00; TPM1C1SC=0x68; TPM1C1V=16000; } TPM1C0SC= 0x00; *Channel 0 int. flag clearing (first part) */ TPM1C0SC = 0x68; /*Int. flag clearing (2nd part) and channel 0 contr. registersetting */ TPM1C0V=duty; //???????????????????? //设置死区时间,主动续流 TPM1SC= 0x00; /*Stop and reset counter */ TPM1MOD =16000; /* Period valuesetting */ (void)(TPM1SC == 0); /* Overflow int.flag clearing (first part) */ TPM1SC = 0x68; //中央对齐,66.5us } if(PTBES_PTBES1==1) //如果是捕获上升沿,则此时的hal信号为低电平 { if(duty>50) { TPM1C0SC =0x00; TPM1C0SC=0x64; TPM1C0V=(duty-DEAD_TIME); //设置死区时间16*0.125=2us; } else { TPM1C0SC= 0x00; TPM1C0SC=0x68; TPM1C0V=16000; } TPM1C1SC =0x00; /* Channel 0 int.flag clearing (first part) */ TPM1C1SC = 0x68; /*Int. flag clearing (2nd part) and channel 0 contr. registersetting */ TPM1C1V = duty; //???????????????????? TPM1SC = 0x00; /* Stop and resetcounter */ TPM1MOD =16000; /* Period valuesetting */ (void)(TPM1SC == 0); /* Overflow int.flag clearing (first part) */ TPM1SC = 0x68; //中央对齐,66.5us,15KHz调速频率 } 问题描述: 上电瞬间捕获2个通道的波形如下: file:///C:/Users/aaronbo/AppData/Local/Temp/msohtmlclip1/01/clip_image001.giffile:///C:/Users/aaronbo/AppData/Local/Temp/msohtmlclip1/01/clip_image003.jpg 如图中红圈所示:上电时存在同时为低电平1ms左右,之后为互补的PWM波形。 疑惑的问题: 1、 如何避免这个同时为低的电平 2、 在程序中可以看到,程序交替的改变通道CH0和CH1的工作模式。不断的初始化TPM1及其2个通道的工作模式,是否会引起TPM1工作不正常,会带来什么不良后果 3、 看程序中对TPM1的使用是否有妥之处。
|