SAMD21 xplained Pro 4x4 keypad

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am new with Atmel Arm SAMD21 xplained Pro. I am trying to program a 4x4 keypad, for some reason the command does not work. any help will be appreciated.

see code below.

 

// Keypad4x4

//

// I've initialized the ports to connect to a 4x4 keypad like this:

//

// ROW1 PB00----X--X--X--X  <--every X is a button switch

// ROW2 PB01----X--X--X--X     that connects the row with 

// ROW3 PB06----X--X--X--X     a column

// ROW4 PB07----X--X--X--X

//              |  |  |  |

// COL1 PB02----+  |  |  |

// COL2 PB03-------+  |  |

// COL3 PB04----------+  |

// COL4 PB05-------------+

//

// Now set one of the rows low and check the 4 columns.

//

 

#include <atmel_start.h>

 

int main(void)

{

/* Initializes MCU, drivers and middleware */

atmel_start_init();

REG_PORT_DIRSET1=(1<<30);

/* Replace with your application code */

while (1) 

{

// Check ROW1

gpio_set_pin_level(ROW1, false);

if (gpio_get_port_level(COL1)==false)

{

REG_PORT_OUTTGL1=(1<<30);

}

if (gpio_get_port_level(COL2)==false)

{

// ROW1,COL2 button is pressed

}

if (gpio_get_port_level(COL3)==false)

{

// ROW1,COL3 button is pressed

}

if (gpio_get_port_level(COL4)==false)

{

// ROW1,COL4 button is pressed

}

gpio_set_pin_level(ROW1, true);

 

// Check ROW2

gpio_set_pin_level(ROW2, false);

if (gpio_get_port_level(COL1)==false)

{

// ROW2,COL1 button is pressed

}

if (gpio_get_port_level(COL2)==false)

{

// ROW2,COL2 button is pressed

}

if (gpio_get_port_level(COL3)==false)

{

// ROW2,COL3 button is pressed

}

if (gpio_get_port_level(COL4)==false)

{

// ROW2,COL4 button is pressed

}

gpio_set_pin_level(ROW2, true);

 

// Check ROW3

gpio_set_pin_level(ROW3, true);

if (gpio_get_port_level(COL1)==false)

{

// ROW3,COL1 button is pressed

}

if (gpio_get_port_level(COL2)==false)

{

// ROW3,COL2 button is pressed

}

if (gpio_get_port_level(COL3)==false)

{

// ROW3,COL3 button is pressed

}

if (gpio_get_port_level(COL4)==false)

{

// ROW3,COL4 button is pressed

}

gpio_set_pin_level(ROW3, true);

// Check ROW4

gpio_set_pin_level(ROW4, true);

if (gpio_get_port_level(COL1)==false)

{

// ROW4,COL1 button is pressed

}

if (gpio_get_port_level(COL2)==false)

{

// ROW4,COL2 button is pressed

}

if (gpio_get_port_level(COL3)==false)

{

// ROW4,COL3 button is pressed

}

if (gpio_get_port_level(COL4)==false)

{

// ROW4,COL4 button is pressed

}

gpio_set_pin_level(ROW4, true);

}

//

// Do what ever else you are doing here

//

}

Last Edited: Thu. May 10, 2018 - 08:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take a closer look at Row 3 and Row 4.  The row is set TRUE, checked, then set TRUE again.  Rows 1 and 2 are different - set FALSE, checked, returned to TRUE.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Still doesn't work after making the changes. i try to debug the code, as soon as i enter the debug mode the code has been executed without a key being pressed  ( see attach).

 

 

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code you have there in the debugger makes no sense (it's setting ROW1 high and then testing for low on each column and then then setting ROW1 high again).

What you had for ROW1 and ROW2 above makes some sense (set the row low and test for low on each column). But for this to work you need a pullup on each column, this can be from the internal pullup (configured with gpio_set_pin_pull_mode probably, I don't use ASF4).

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the code is executed without pressing a button, I would rather recommend you to check the hardware. Pull up/down resistors would help not having a non clear value.