最近在做电机控制的项目,因为牵扯到电机的PI控制,所以想用到编码盘,但编码盘和PWM控制似乎都只能用TMR1来做,但似乎有冲突。附上程序编码盘初始化:
void
tiM1_Init(void)
{
TIM1_CR1=0x00; //up count,interrupt not stop count
TIM1_IER=0x00;
TIM1_PSCRH=0x0; //PSC 10000 hz
TIM1_PSCRL=0x0;
TIM1_ARRH=0xFF; //这里步数从-32767—32767
TIM1_ARRL=0xFF;
TIM1_CCMR1=0x01;
TIM1_CCMR2=0x01;
TIM1_CCER1=0x22;
TIM1_SMCR=0x03;
TIM1_CR1=0x01; //counter enable
}
PWM初始化
void PwmTIM1(void)
{
TIM1_EGR = 0x01;//初始化TIM1 TIM1时基初始化
TIM1_EGR |= 0x20;//重新初始化TIM1
TIM1_CR1 = 0x80;//边沿对齐,向上计数,带缓冲***************
TIM1_RCR = 0x01;//重复计数器***************************************
TIM1_PSCRH = 0; //预分频 设置PWM频率
TIM1_PSCRL = 0;
TIM1_ARRH = 0x00; //设定重装载值
TIM1_ARRL = 150;
TIM1_CCER1 = 0x55;//高/低电平有效(CCiP),使能PWM输出(CCiE)使能互补输出 (CCiNE),互补高/低电平有效(CCiNP)
TIM1_CCMR1 = 0X6C;
TIM1_CCER2 = 0x55;//高/低电平有效(CCiP),使能PWM输出(CCiE)使能互补输出 (CCiNE),互补高/低电平有效(CCiNP)
TIM1_CCMR2 = 0x6C;//PWM模式1 通道2PWM输出
TIM1_CCMR3 = 0x6C;//PWM模式1 通道3PWM输出
TIM1_BKR = 0x80;//主使能
TIM1_DTR = 0x12;//插入2us死区时间
TIM1_CR1_CEN = 0x01;//计数使能
}
其中TIM1_ARRH 和TIM1_ARRL两个寄存器是重复定义的,不知道大家有其他办法么,我使用的芯片是STM8S105,望大神帮忙解答