利用STC32G12K128 读取 KYTB-1503-1024角度编码器
Posted 卓晴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用STC32G12K128 读取 KYTB-1503-1024角度编码器相关的知识,希望对你有一定的参考价值。
STC32G正交编码编码器
01 角度编码器
一、前言
为了测试STC32G12K128高级PWM模块中的正交编码功能, 是以一款之前所使用的微型编码器产生正交脉冲输出信号帮助测试。 微型编码器的型号为 KYTB-1503 。 下面利用这款编码器测试STC32G的正交编码输入功能。
二、编码器
这是编码器接口功能定义。 旋转盖编码器,使用示波器测量它的A,B两个输出信号。 可以观察到A,B信号之间相差90°的相位。
正交编码信号的输入是从 PWMA的 通道1, 通道2 的正向输入端口进入单片机的, 在PDIP40 封装中, 通道1的正向输入为P1.0。 通道2 的正向输入端被移植到P5.4端口。
三、编程实现
根据 STC32G 手册中给出的历程, 编写的PWMA正交信号位置编码器初始化程序, 这是允许PWM中断语句。 在每一次中断时, 都会将计数器的数值幅值在全局变量中, 请注意,在这里是从CCR寄存器中获得的计数数值,而不是从CNT。 我的理解是在每次中断中,CCR都捕获了CNT的数值,因而它们是相同的。
#if PWMA_ENCODER == 1
PWMA_ENO = 0x0; // Disable all ENO
PWMA_PS = 0x0; // 00:PWM at P1
PWMA_PSCRH = 0x0; // Set prescaler = 0x0
PWMA_PSCRL = 0x0;
PWMA_CCMR1 = 0x21; // Set model as Input, Encoder, filter 4 clock
PWMA_CCMR2 = 0x21; // Set mode as input, Encode, Filter 4 clock
PWMA_SMCR = 0x03; // Encode Mode 3
PWMA_CCER1 = 0x55; // Input channel enable and polarity
PWMA_CCER2 = 0x55;
// PWMA_IER = 0x02; // Enable interrupt
PWMA_CR1 |= 0x01; // Enable counter
EA = 1;
g_ucPWMACntH = 0x0;
g_ucPWMACntL = 0x0;
#if PWMA_ENCODER
void PWMA_ISR() interrupt 26
if(PWMA_SR1 & 0x02)
g_ucPWMACntH = PWMA_CCR1H;
g_ucPWMACntL = PWMA_CCR1L;
PWMA_SR1 &= ~0x02;
#endif // PWMA_ENCODER
在主程序中, 每个150毫秒,读取和显示一次编码器的数值。 将这里的 CCR寄存器, 更换成 CNTR寄存器,输出数值是相同的。
经过测试,可以看到显示的数值会随着编码器旋转方向不同而上升或者下降。 至此验证了STC32G单片机通道A正交编码器的功能。
将STC32G中的PWMA初始化的程序复制到PWMB的初始化程序中, 验证了从 P2.0, P2.1 管脚输入旋转编码正交信号的计数功能。???至此,对于STC32G的PWM功能的使用, 就基本完成了。可以进行后期的应用程序的开发。
//==============================================================================
// ADVANCE PWM A and B
//------------------------------------------------------------------------------
#if ADVANCE_PWMA_EN
void AdvancePWMAInit(void)
#if PWMA_ENCODER == 1
PWMA_ENO = 0x0; // Disable all ENO
PWMA_PS = 0x0; // 00:PWM at P1
PWMA_PSCRH = 0x0; // Set prescaler = 0x0
PWMA_PSCRL = 0x0;
PWMA_CCMR1 = 0x21; // Set model as Input, Encoder, filter 4 clock
PWMA_CCMR2 = 0x21; // Set mode as input, Encode, Filter 4 clock
PWMA_SMCR = 0x03; // Encode Mode 3
PWMA_CCER1 = 0x55; // Input channe l enable and polarity
PWMA_CCER2 = 0x55;
// PWMA_IER = 0x02; // Enable interrupt
PWMA_CR1 |= 0x01; // Enable counter
// EA = 1;
g_ucPWMACntH = 0x0;
g_ucPWMACntL = 0x0;
#else
PWMA_ENO = PWMA_ENO1P_EN | (PWMA_ENO1N_EN<<1) | (PWMA_ENO2P_EN<<2) | (PWMA_ENO2N_EN<<3) |
(PWMA_ENO3P_EN<<4) | (PWMA_ENO3N_EN<<5) | (PWMA_ENO4P_EN<<6) | (PWMA_ENO4N_EN<<7);
PWMA_PS = 0x00; // PWMA pin selection, PWMA at P1
PWMA_PSCRH = (unsigned char)(PWMA_PSCR >> 8);
PWMA_PSCRL = (unsigned char)(PWMA_PSCR);
PWMA_ARRH = (unsigned char)(PWMA_PERIOD >> 8);
PWMA_ARRL = (unsigned char)(PWMA_PERIOD);
PWMA_IOAUX = 0x0;
PWMA_CR1 = 0x80;
PWMA_CR2 = 0x24;
PWMA_SMCR = 0x20;
PWMA_CCMR1 = 0x60;
PWMA_CCMR2 = 0x60;
PWMA_CCMR3 = 0x60;
PWMA_CCMR4 = 0x60;
PWMA_CCER1 = 0x55;
PWMA_CCER2 = 0x55;
PWMA_OISR = 0xaa;
PWMA_CCR1H = (unsigned char)(PWMA_PERIOD >> 10);
PWMA_CCR1L = (unsigned char)(PWMA_PERIOD >> 2);
PWMA_CCR2H = (unsigned char)(PWMA_PERIOD >> 10);
PWMA_CCR2L = (unsigned char)(PWMA_PERIOD >> 2);
PWMA_CCR3H = (unsigned char)(PWMA_PERIOD >> 10);
PWMA_CCR3L = (unsigned char)(PWMA_PERIOD >> 2);
PWMA_CCR4H = (unsigned char)(PWMA_PERIOD >> 10);
PWMA_CCR4L = (unsigned char)(PWMA_PERIOD >> 2);
PWMA_CNTRH = 0x0;
PWMA_CNTRL = 0x0;
PWMA_BKR = 0x80; // Enable output
PWMA_DTR = 0x0;
PWMA_CR1 |= 0x01;
#endif
#if PWMA_ENCODER
void PWMA_ISR() interrupt 26
if(PWMA_SR1 & 0x02)
g_ucPWMACntH = PWMA_CCR1H;
g_ucPWMACntL = PWMA_CCR1L;
PWMA_SR1 &= ~0x02;
#endif // PWMA_ENCODER
#endif // ADVANCE_PWM1_EN
//------------------------------------------------------------------------------
#if ADVANCE_PWMB_EN
void AdvancePWMBInit(void)
#if PWMB_ENCODER == 1
PWMB_ENO = 0x0; // Disable all ENO
PWMB_PS = 0x0; // 00:PWM at P1
PWMB_PSCRH = 0x0; // Set prescaler = 0x0
PWMB_PSCRL = 0x0;
PWMB_CCMR1 = 0x21; // Set model as Input, Encoder, filter 4 clock
PWMB_CCMR2 = 0x21; // Set mode as input, Encode, Filter 4 clock
PWMB_SMCR = 0x03; // Encode Mode 3
PWMB_CCER1 = 0x55; // Input channe l enable and polarity
PWMB_CCER2 = 0x55;
// PWMB_IER = 0x02; // Enable interrupt
PWMB_CR1 |= 0x01; // Enable counter
// EA = 1;
g_ucPWMBCntH = 0x0;
g_ucPWMBCntL = 0x0;
#else
PWMB_ENO = PWMB_ENO1P_EN | (PWMB_ENO2P_EN<<2) | (PWMB_ENO3P_EN<<4) | (PWMB_ENO4P_EN << 6);
PWMB_PS = 0x00; // PWMB pin selection: PWMB at P2
PWMB_PSCRH = (unsigned char)(PWMB_PSCR >> 8);
PWMB_PSCRL = (unsigned char)(PWMB_PSCR);
PWMB_ARRH = (unsigned char)(PWMB_PERIOD >> 8);
PWMB_ARRL = (unsigned char)(PWMB_PERIOD);
PWMB_IOAUX = 0x0;
PWMB_CR1 = 0x80;
PWMB_CR2 = 0x24;
PWMB_SMCR = 0x20;
PWMB_CCMR1 = 0x60;
PWMB_CCMR2 = 0x60;
PWMB_CCMR3 = 0x60;
PWMB_CCMR4 = 0x60;
PWMB_CCER1 = 0x55;
PWMB_CCER2 = 0x55;
PWMB_OISR = 0xaa;
PWMB_CCR5H = (unsigned char)(PWMB_PERIOD >> 10);
PWMB_CCR5L = (unsigned char)(PWMB_PERIOD >> 2);
PWMB_CCR6H = (unsigned char)(PWMB_PERIOD >> 10);
PWMB_CCR6L = (unsigned char)(PWMB_PERIOD >> 2);
PWMB_CCR7H = (unsigned char)(PWMB_PERIOD >> 10);
PWMB_CCR7L = (unsigned char)(PWMB_PERIOD >> 2);
PWMB_CCR8H = (unsigned char)(PWMB_PERIOD >> 10);
PWMB_CCR8L = (unsigned char)(PWMB_PERIOD >> 2);
PWMB_CNTRH = 0x0;
PWMB_CNTRL = 0x0;
PWMB_BKR = 0x80; // Enable output
PWMB_DTR = 0x0;
PWMB_CR1 |= 0x01;
#endif // PWMB_ENCODER
#if PWMB_ENCODER
void PWMB_ISR() interrupt 27
if(PWMB_SR1 & 0x02)
g_ucPWMBCntH = PWMB_CCR5H;
g_ucPWMBCntL = PWMB_CCR5L;
PWMB_SR1 &= ~0x02;
#endif // PWMB_ENCODER
#endif // ADVANCE_PWMB_EN
※ 总 结 ※
本文测试了STC32G中的 PWMA,PWMB模块的正交编码器的功能。
■ 相关文献链接:
以上是关于利用STC32G12K128 读取 KYTB-1503-1024角度编码器的主要内容,如果未能解决你的问题,请参考以下文章