Go Down

Topic: sleep unil defined interrupt from keypad (Read 148 times) previous topic - next topic

wenzela

I'm trying to get my sam to sleep until I press a button on the keypad, which will interrupt the sleep and wake it up. I'm using the attachInterrupt command (may be the problem) to a interrupt the code running a function which figures out which button is being pressed and interrupted the program. The interrupts work when the code is running without sleeping.

Now I'm trying to add a sleep to it so that it'll: sleep, interrupt with a push of a button, determine what button is being pushed. I'm trying to simply call upon __WFI, but it's waking up immediately after it falls asleep every time. I'm thinking that it's getting an event from elsewhere, causing it to wake up.

Does anyone have any imput on waking up a DUE from defined interrupts?


Here's the pinout for the keypad: http://www.avr-asm-tutorial.net/avr_en/keypad/keyboard.gif with columns plugged into 41,43,45 and rows into 42,44,46,46

Code: [Select]

#define NO_KEY -999

volatile int key_pressed = NO_KEY;

void setup() {
  Serial.begin(115200);
}


void key_poll()
{
  Serial.println("key_poll");

 detachInterrupt(43);
 detachInterrupt(45);
 detachInterrupt(47);
 
 int trigger_Pin=0;
 int from_Pin = 0;
 if (digitalRead(43) == LOW)
   trigger_Pin = 43;
 if (digitalRead(45) == LOW)
   trigger_Pin = 45;
 if (digitalRead(47) == LOW)
   trigger_Pin = 47;
 
 if (trigger_Pin!= 0)
 {
   
   
   for(int i = 42; i<=48; i+=2)
   {
     digitalWrite(i, HIGH);
     delayMicroseconds(25);
     if(digitalRead(trigger_Pin)==HIGH)
     {
       from_Pin = i;
     }
     digitalWrite(i, LOW);
     
   }
   from_Pin = (from_Pin - 42)/2;
   trigger_Pin = (trigger_Pin - 41)/2;
   int Pin = from_Pin * 3 + trigger_Pin;
   key_pressed = Pin;
   //Serial.println(Pin);
 }
 

 pinMode(42, OUTPUT);
 pinMode(44, OUTPUT);
 pinMode(46, OUTPUT);
 pinMode(48, OUTPUT);
 digitalWrite (42, LOW);
 digitalWrite (44, LOW);
 digitalWrite (46, LOW);
 digitalWrite (48, LOW);

 pinMode(43, INPUT_PULLUP);
 pinMode(45, INPUT_PULLUP);
 pinMode(47, INPUT_PULLUP);

 

 delayMicroseconds(50000);
 attachInterrupt(digitalPinToInterrupt(43), &key_poll, FALLING);
 attachInterrupt(digitalPinToInterrupt(45), &key_poll, FALLING);
 attachInterrupt(digitalPinToInterrupt(47), &key_poll, FALLING);
 return;
}

void loop()
{
  Serial.println(getKey());
  delay(500);
}

int getKey()
{
  //reset key_pressed
  key_pressed = NO_KEY;
  //set up pins
  //row output LOW
  pinMode(42, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(48, OUTPUT);
  digitalWrite (42, LOW);
  digitalWrite (44, LOW);
  digitalWrite (46, LOW);
  digitalWrite (48, LOW);
 
  pinMode(43, INPUT_PULLUP);
  pinMode(45, INPUT_PULLUP);
  pinMode(47, INPUT_PULLUP);
 
  delayMicroseconds(25);
   
  //attach interrupts
  //interrupt col pins
  attachInterrupt(digitalPinToInterrupt(43), &key_poll, LOW);
  attachInterrupt(digitalPinToInterrupt(45), &key_poll, LOW);
  attachInterrupt(digitalPinToInterrupt(47), &key_poll, LOW);
 
  Serial.println("entering wait mode");
  //put it sleep
  SUPC->SUPC_SR;
  __WFI();
  Serial.println("left wait mode");

  //check
  return key_pressed;
}

wenzela

I added another check, to see if Serial was waking up my processor. It was.

I added an led to a pwm pin and turned up on/off with the processors wake state. When the Serial code was still in, the light flashed on and off. Without the Serial code, it stays off. The interrupts are not interrupting the sleep.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy