msp432快速入门第十一节之编码器

(一)编码器介绍

首先可以在man手册中查看TAx的功能(function),发现他不支持直连编码器,然后看了下timer32,发现timer32也不支持,不过好消息是发现TAx定时器支持输入捕获,那我们只能使用输入捕获来计算编码器的数值了,如果是步方向编码器可以直接拿来用了,如果是正交编码器需要使用D触发器鉴相,具体电路可以百度搜索,我自己使用的是正交编码器,利用74HC74D双D触发器来鉴相,这样可以直接给两个电机使用了。
在这里插入图片描述

(二)移植输入捕获程序

仿照TI的官方例子进行配置输入捕获:

(1)配置TAx定时器

/* Timer_A Continuous Mode Configuration Parameter */
const Timer_A_ContinuousModeConfig Encoder_continuousModeConfig =
{
        TIMER_A_CLOCKSOURCE_SMCLK,           // SMCLK Clock Source
        TIMER_A_CLOCKSOURCE_DIVIDER_24,       // SMCLK/24 = 500KHz
        TIMER_A_TAIE_INTERRUPT_DISABLE,      // Disable Timer ISR
        TIMER_A_SKIP_CLEAR                   // Skup Clear Counter
};

/* Timer_A Capture Mode Configuration Parameter */
const Timer_A_CaptureModeConfig Encoder_captureModeConfig0 =
{
        TIMER_A_CAPTURECOMPARE_REGISTER_1,        // CC Register 1
        TIMER_A_CAPTUREMODE_RISING_EDGE,          // Rising Edge
        TIMER_A_CAPTURE_INPUTSELECT_CCIxA,        // CCIxA Input Select
        TIMER_A_CAPTURE_ASYNCHRONOUS,             // Asynchronoused Capture
        TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE,  // Enable interrupt
        TIMER_A_OUTPUTMODE_OUTBITVALUE            // Output bit value
};


// 初始化编码器
void Encoder_Init(void)
{
	//编码器计数部分
	//引脚复用
    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P5, GPIO_PIN6,
            GPIO_PRIMARY_MODULE_FUNCTION);
    //配置模式
    Timer_A_initCapture(TIMER_A2_BASE, &Encoder_captureModeConfig0);
	//配置连续捕获
    Timer_A_configureContinuousMode(TIMER_A2_BASE, &Encoder_continuousModeConfig);
	//配置中断
    Interrupt_enableInterrupt(INT_TA2_N);
	
	//开始捕获
    Timer_A_startCounter(TIMER_A2_BASE, TIMER_A_CONTINUOUS_MODE);
	//方向部分
	GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1,GPIO_PIN7);
}

(2)配置中断

/* TA0 ISR */
void TA2_N_IRQHandler(void)
{
	encoder0_cnt ++;
	Timer_A_clearCaptureCompareInterrupt(TIMER_A2_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_1);	
}

(3)主函数读取

    while(1)
    {
		Read_Encoder();
		oled_int16(0,0,encoder0_val);
		delay_ms(100);
    }

(4)烧录后查看现象

在这里插入图片描述
可以看到轮子朝一个方向转时数值为正,另外一个方向为负,而且随着速度变快数值会变化,说明成功了。

(三)优化支持双轮速度读取

双轮的话我们就需要多通道中断判断了,在TI给出的函数中我没有发现可以在中断中查看定时器中断源(中断通道)的办法,所以需要查手册来看看是哪个寄存器代表中断源,而且这个寄存器一定是只读的,然后我就在手册找到了:
在这里插入图片描述
所以直接读取这个寄存器就可以查看中断通道源了,还有需要注意初始化时需要分别初始化两个通道,不可以使用 | 来初始化多通道,这是一个小问题,其他都还好。

克隆空白工程请使用 git , 如果你觉得还不错欢迎点亮 star !

git clone https://github.com/YGZone/Design-a-car-with-MSP432P401R.git

更多资料可以转步我的个人网站 www.eestr.com ( https://www.eestr.com)或者 Github[https://github.com/YGZone], 欢迎访问

  • 10
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 42
    评论
MSP432 低功耗高性能并存10.1 Digital I/O Introduction The digital I/O features include: • Independently programmable individual I/Os • Any combination of input or output • Individually configurable interrupts for ports (available for certain ports only) • Independent input and output data registers • Individually configurable pullup or pulldown resistors • Wake-up capability from ultra-low power modes (available for certain ports only) • Individually configurable high drive I/Os (available for certain I/Os only) Devices within the family may have up to eleven digital I/O ports implemented (P1 to P10 and PJ). Most ports contain eight I/O lines; however, some ports may contain less (see the device-specific data sheet for ports available). Each I/O line is individually configurable for input or output direction, and each can be individually read or written. Each I/O line is individually configurable for pullup or pulldown resistors. Certain ports have interrupt and wake-up capability from ultra-low power modes (see device specific data sheet for ports with interrupt and wake-up capability). Each interrupt can be individually enabled and configured to provide an interrupt on a rising or falling edge of an input signal. All interrupts are fed into an encoded Interrupt Vector register, allowing the application to determine which sub-pin of a port has generated the event. Individual ports can be accessed as byte-wide ports or can be combined into half-word-wide ports. Port pairs P1 and P2, P3 and P4, P5 and P6, P7 and P8, and so on, are associated with the names PA, PB, PC, PD, and so on, respectively. All port registers are handled in this manner with this naming convention. The main exception are the interrupt vector registers, for example, interrupts for ports P1 and P2 must be handled through P1IV and P2IV, PAIV does not exist. When writing to port PA with half-word operations, all 16 bits are written to the port. When writing to the lower byte of port PA using byte operations,
要使用MSP432读取编码器,你可以遵循以下步骤: 1. 连接编码器:将编码器的输出引脚连接到MSP432的GPIO引脚。通常,编码器有两个输出信号(A和B),分别对应于正向和反向旋转。 2. 配置GPIO引脚:使用MSP432的GPIO库函数,将连接编码器的引脚配置为输入模式。确保在代码中指定正确的引脚号码和端口。 3. 设置中断:为了实现编码器的读取,你可以配置GPIO引脚的中断功能。当编码器信号发生变化时,中断将被触发。 4. 编写中断处理程序:在中断处理程序中,你可以读取编码器的状态并计算旋转方向和步数。这通常涉及到读取A和B信号的状态并进行比较。 下面是一个使用MSP432读取编码器的示例代码片段: ```C #include <ti/devices/msp432p4xx/driverlib/driverlib.h> #define ENCODER_A_PIN GPIO_PIN0 #define ENCODER_B_PIN GPIO_PIN1 void encoderInterruptHandler(void) { static int encoderState = 0; int a = GPIO_getInputPinValue(GPIO_PORT_P1, ENCODER_A_PIN); int b = GPIO_getInputPinValue(GPIO_PORT_P1, ENCODER_B_PIN); if (a == b) { encoderState++; } else { encoderState--; } // Do something with the encoder state... GPIO_clearInterruptFlag(GPIO_PORT_P1, ENCODER_A_PIN | ENCODER_B_PIN); } int main(void) { // 初始化MSP432 GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, ENCODER_A_PIN | ENCODER_B_PIN); GPIO_interruptEdgeSelect(GPIO_PORT_P1, ENCODER_A_PIN | ENCODER_B_PIN, GPIO_HIGH_TO_LOW_TRANSITION); GPIO_clearInterruptFlag(GPIO_PORT_P1, ENCODER_A_PIN | ENCODER_B_PIN); GPIO_enableInterrupt(GPIO_PORT_P1, ENCODER_A_PIN | ENCODER_B_PIN); Interrupt_enableInterrupt(INT_PORT1); while (1) { // 主循环 } } #pragma vector=PORT1_VECTOR __interrupt void Port1_ISR(void) { if (P1IFG & (ENCODER_A_PIN | ENCODER_B_PIN)) { encoderInterruptHandler(); } } ``` 这只是一个简单的示例,你可以根据你的编码器和应用程序的要求进行修改。确保根据MSP432的文档和编码器的规格说明进行正确的配置和连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YGZone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值