MSP432 Interrupt With Encoder Count
up vote
1
down vote
favorite
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
c interrupt sensor encoder msp432
add a comment |
up vote
1
down vote
favorite
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
c interrupt sensor encoder msp432
2
Indenting your code will greatly improve readability.
– Nic3500
Nov 20 at 1:14
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
c interrupt sensor encoder msp432
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
c interrupt sensor encoder msp432
c interrupt sensor encoder msp432
edited Nov 20 at 1:13
Nic3500
3,40481829
3,40481829
asked Nov 20 at 0:10
crazydude
112
112
2
Indenting your code will greatly improve readability.
– Nic3500
Nov 20 at 1:14
add a comment |
2
Indenting your code will greatly improve readability.
– Nic3500
Nov 20 at 1:14
2
2
Indenting your code will greatly improve readability.
– Nic3500
Nov 20 at 1:14
Indenting your code will greatly improve readability.
– Nic3500
Nov 20 at 1:14
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384417%2fmsp432-interrupt-with-encoder-count%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Nov 20 at 4:31
lod
923612
923612
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384417%2fmsp432-interrupt-with-encoder-count%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Indenting your code will greatly improve readability.
– Nic3500
Nov 20 at 1:14