I am using a MSP432 for a project and I would like to know how to generate a interrupt when a incremental encoder has reached a specified count. The interrupt should stop a motor from moving in a particular direction.
Background: I'm using a incremental encoder to control a brush motor. As the brush motor moves left or right, it is mechanically connected to a incremental encoder that counts pulses or "clicks". The incremental encoder is effectively controlling the limit of motion of the motor. Ie, if the encoder reads 20 pulses in the right direction, the motor should stop. My project has two modes of operation controlled by a switch-case statement. The first is a routine mode, and the second is a mode where a user can control the motor with a joystick. Regardless of whichever mode the program is in, the motor should stop when the limit of motion has been reached.
Psedo Code:
Case: Routine Button Mode
{
// Motor executes right, left, right movement routine
digitalWrite(directionMotor,RIGHT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
if(encoder_count == Motion_Limit)
analogWrite(pwm_motor,0); // tell motor to stop
// change direction
digitalWrite(directionMotor,LEFT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
}
Case: Joystick_Control
{
while(analogRead<10) // Joystick is pushed to the left
{
digitalWrite(directionMotor,LEFT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
if(encoder_count == Motion_Limit)
analogWrite(pwm_motor,0); // tell motor to stop
}
while(analogRead>1000) // Joystick is pushed to the right
{
digitalWrite(directionMotor,RIGHT); // telling motor what direction to go
analogWrite(pwm_motor2,60); // telling motor to activate at 60% PWM
if(encoder_count == Motion_Limit)
analogWrite(pwm_motor,0); // tell motor to stop
}
} // end case statement
Again, no matter what mode of operation the program is in, it should stop when the count has been reached. Even when the motion limit has been reached, the program should still allow the joystick control to drive the motor away from the motion limit. That is, if count == 20 on the right limit, I can still drive the motor left. Essentially, the encoder should be tracking the motor at all moments in operation.
Questions: 1. How do I declare a interrupt on a MSP432? 2. Can I use a incremental encoder for as a interrupt? Most examples I've found use a button that outputs a high or low signal as a flag for a interrupt. I'm not sure I can do the same thing with a encoder
Your code looks a lot like Arduino code, to attach an interrupt you should use the Arduino attachInterrupt() function. If you are using a different high level support library then its documentation should include some interrupt examples.
As to the meat of your question.
Your incremental encoder should have two lines, one to indicate left motion, one to indicate right motion.
You will need to alter the global variable encoder_count up and down as indicated by these lines. This should definitely be done using an interrupt for each line. The interrupt should fire on the edge transition as specified in the encoder's datasheet. There is no difference between triggering on a button edge and an encoder edge (except that buttons are messy and need to be debounced, find a non-debounced example).
If it is very important to stop the motor exactly on the incremental count you can test the values in the incremental encoder increment/decrement interrupts and disable the motor if required.
However it is probably sufficient to do the test as part of your main loop. (Side note: remember you need to handle when the joystick is centered.)
I would also recommend creating a function to control the motor. This abstracts away the implementation details, allowing your main loop to focus on higher level functionality. Ensuring the motor control is always done by one function also allows you to guarantee that your limits are always applied.