概述
高级定时器和通用定时器在基本定时器的基础上引入了外部引脚,通过外部引脚可以实现输入捕获和输出比较的功能。高级定时器和通用定时器相比增加了可编程死区互补输出、重复计数器带刹车功能,这些功能在电机控制方面发挥了很大作用。
以下是使用APM8S007的STIM配置为死区互补输出。 #define Deadtime 100
#define STMR_PEROID_VAL (SYSCLK/1/2000 - 1) // 周期值=系统时钟/分频/频率*2(计数类型选择中心对齐所以需要乘2) - 1
FOUT_S05 = 0xA; // AH--05
FOUT_S06 = 0x9; // AL--06
FOUT_S07 = 0x8; // BH--P07
FOUT_S10 = 0x7; // BL--P10
FOUT_S11 = 0x6; // CH--P11
FOUT_S12 = 0x5; // CL--P12
P0_MD1 &= ~((0x3<<2) | (0x3<<4) | (0x3<<6));
P1_MD0 &= ~((0x3<<0) | (0x3<<2) | (0x3<<4));
P0_MD1 |= ((0x1<<2) | (0x1<<4) | (0x1<<6));
P1_MD0 |= ((0x1<<0) | (0x1<<2) | (0x1<<4));
// STMR0和STMR1互补配置:STMR0和STMR1为互补模式: STMR1的周期和占空比由STMR0控制,其他行为STMR1自行控制
STMR_CON0 &= ~ ((0x3<<0)|(0x3<<2)|(0x3<<4));
STMR_CON0 |= ((0x2<<0)|(0x2<<2)|(0x2<<4)); // STMR0和STMR1为互补模式
STMR_DTEN |= ((0x1<<0) | (0x1<<1) |(0x1<<2)|(0x1<<3)); // STMR0/STMR1死区使能
STMR_EDGESEL &=~((0x1<<0) | (0x1<<1)); // STMR1 PWM下降沿触发STMR0死区事件
STMR_EDGESEL |= ((0x1<<2)| (0x1<<3)|(0x1<<4)|(0x1<<5));
STMR_DTDAT &=~((0x0<<0)|(0x0<<1)|(0x0<<2)|(0x0<<3)|(0x0<<4)|(0x0<<5)); // STMR1 PWM死区时间内输出0
STMR_DTCON |= ((0x6<<0) |(0x7<<3)); // STMR0无死区,STMR1向下或向上计数时比较点-死区时间
STMR01_DT = Deadtime << 0;
STMR23_DT = Deadtime << 0;
STMR45_DT = Deadtime << 0;
STMR0_PSC = (0x0<<0); // 不分频
STMR1_PSC = (0x0<<0);
STMR2_PSC = (0x0<<0);
STMR3_PSC = (0x0<<0);
STMR4_PSC = (0x0<<0);
STMR5_PSC = (0x0<<0);
STMR0_PRH = (((STMR_PEROID_VAL >> 8) & 0xFF)<<0); // 周期高八位寄存器
STMR0_PRL = (((STMR_PEROID_VAL >> 0) & 0xFF)<<0); // 周期低八位寄存器
STMR0_CMPAH = ((((STMR_PEROID_VAL/2) >> 8) & 0xFF)<<0); // 比较值A点高八位寄存器
STMR0_CMPAL = ((((STMR_PEROID_VAL/2) >> 0) & 0xFF)<<0); // 比较值A点低八位寄存器
STMR2_PRH = (((STMR_PEROID_VAL >> 8) & 0xFF)<<0); // 周期高八位寄存器
STMR2_PRL = (((STMR_PEROID_VAL >> 0) & 0xFF)<<0); // 周期低八位寄存器
STMR2_CMPAH = ((((STMR_PEROID_VAL/2) >> 8) & 0xFF)<<0); // 比较值A点高八位寄存器
STMR2_CMPAL = ((((STMR_PEROID_VAL/2) >> 0) & 0xFF)<<0); // 比较值A点低八位寄存器
STMR4_PRH = (((STMR_PEROID_VAL >> 8) & 0xFF)<<0); // 周期高八位寄存器
STMR4_PRL = (((STMR_PEROID_VAL >> 0) & 0xFF)<<0); // 周期低八位寄存器
STMR4_CMPAH = ((((STMR_PEROID_VAL/2) >> 8) & 0xFF)<<0); // 比较值A点高八位寄存器
STMR4_CMPAL = ((((STMR_PEROID_VAL/2) >> 0) & 0xFF)<<0); // 比较值A点低八位寄存器
STMR_PWMVALA |= ((0x1<<0)|(0x1<<2)|(0x1<<4)); // 计数CNT大于等于比较值A,PWM输出1,小于输出0
STMR_PWMVALA &= ~((0x1<<1)|(0x1<<3)|(0x1<<5));
STMR_PWMEN |= ((0x1<<0)|(0x1<<1)|(0x1<<2)|(0x1<<3)|(0x1<<4)|(0x1<<5)); // PWM输出使能
STMR_CNTMD |= ((0x1<<0)|(0x1<<1)|(0x1<<2)|(0x1<<3)|(0x1<<4)|(0x1<<5)); // 选择连续计数模式
STMR_LOADEN |= ((0x1<<0)|(0x1<<1)|(0x1<<2)|(0x1<<3)|(0x1<<4)|(0x1<<5)); // 自动装载使能
STMR_CNTCLR |= ((0x1<<0)|(0x1<<1)|(0x1<<2)|(0x1<<3)|(0x1<<4)|(0x1<<5)); // 计数清零
STMR_CNTTYPE |= ((0x1<<0)|(0x1<<1)|(0x1<<2)|(0x1<<3)|(0x1<<4)|(0x1<<5)); // 计数类型选择中心对齐
// STIMER1配置 // 不分频
// 计数使能
STMR_CNTEN |= ((0x1<<0)|(0x1<<1)|(0x1<<2)|(0x1<<3)|(0x1<<4)|(0x1<<5)); // 计数使能
|