Sign in to follow this  
Followers 0
nirbar11

Interrupts

4 posts in this topic

Hello all,

Im using the MSP430 connecting to a device which outputs 4 bits sequence (parallel) every once in a while  and I want to get an interrupt for each new input sequence in port 1.

The input sequence voltage level is 3.6V and the MSP powered from the micro-USB (connected to the PC).

I also connected the GND of the device to the GND of the MSP.

this is my code :

 

void setup(){
Serial.begin(9600);

pinMode(P1_2,INPUT); 
pinMode(P1_3,INPUT); 
pinMode(P1_4,INPUT);
pinMode(P1_5,INPUT);

attachInterrupt(P1,intFunc,CHANGE); 

};

void intFunc(){

Serial.print("this is an interrupt - the value of P3.1 is :");
Serial.println(digitalRead(P1_3));

}


void loop()
{
delay(2000);
Serial.println("the is the main routine");
}

Share this post


Link to post
Share on other sites

Hi @nirbar11 and welcome to 43oh

I am not sure I understand your objectives but here is something to get you started...

  • Don't use pin names of the form P1_x in Energia as this has been deprecated
  • Don't put print statements (or other slow to execute code) inside of interrupts - flag the interrupt and handle it elsewhere
  • You will probably want to avoid using delay() in your code
  • Note that if using a G2 LaunchPad that P1.0 and P1.6 are attached to LEDs via jumpers that can be pulled

Here is some example code that detects buttons being pushed (no debouncing).

volatile bool pin5Pushed = false;
volatile bool pin6Pushed = false;
volatile bool pin7Pushed = false;
volatile bool pin8Pushed = false;

void setup()
{
  Serial.begin(9600);
  
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  
  attachInterrupt(5,interrupt5,FALLING);
  attachInterrupt(6,interrupt6,FALLING);
  attachInterrupt(7,interrupt7,FALLING);
  attachInterrupt(8,interrupt8,FALLING);
}

void loop()
{
  if (pin5Pushed == true){
    Serial.println("Pin 5 pushed");
    pin5Pushed = false;
  }
  if (pin6Pushed == true){
    Serial.println("Pin 6 pushed");
    pin6Pushed = false;
  }
  if (pin7Pushed == true){
    Serial.println("Pin 7 pushed");
    pin7Pushed = false;
  }
  if (pin8Pushed == true){
    Serial.println("Pin 8 pushed");
    pin8Pushed = false;
  }
}

void interrupt5(){
  pin5Pushed = true;
}
void interrupt6(){
  pin6Pushed = true;
}
void interrupt7(){
  pin7Pushed = true;
}
void interrupt8(){
  pin8Pushed = true;
}

And finally, avoid double posting.

Rei Vilo likes this

Share this post


Link to post
Share on other sites

Hey.. Thanks for the above.

regarding interrupts again, how should i handle the situation when I want to execute a specific procedure only when i get a specific input sequence. for example,  I want to execute the procedure only if the input sequence is 0010.
in this case I can't configure 4 pins with attachinterrupt() and send the program to the specific procedure. Can I use attachinterrupt() for the whole port? or any other suggestion?

Thank you

 

Nir

Share this post


Link to post
Share on other sites

You could use attachInterrupt() to detect when the pin of interest has changed (e.g. in your example when the sequence changes from xx0x to xx1x).  At that point you could use digitalRead() to determine the status of the other pins.  However, digitalRead() is relatively slow. 

If you are willing to give up compatibility with other processors then the register containing the input values for the port can be read quickly and directly.  For example, the following reads and prints the input register for port 1 and then masks out and prints G2 LaunchPad pin 5 (P1.3) which is attached to switch S2 on the G2 LaunchPad.

void setup()
{
  Serial.begin(9600);
  pinMode(5, INPUT_PULLUP);       // G2 LaunchPad push button
}

void loop()
{
  Serial.println(P1IN);           // Input register port 1
  Serial.println(P1IN & BIT3);    // Pin 5 is P1.3
  delay(100);
}

The GPIO registers are described in the Family User Guides for TI microcontrollers - see SLAU144J for the x2xx family.

It might be cleaner just to do this in CCS without using Energia.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0