SAMD21 Read if Input Pin is High or Low

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

Hello, 

I work with SAMD21. I set several GPIO pins to input. I want to read their state. First, I set which pins I want to be set as input, using REG_PORT_CLR1 and REG_PORT_CLR2.

For example, Pin 22 of Port B should be set as input. Then it receives the external signal. My question is how do I read if this pin is High or Low? Can I just say (Code is in C in Atmel Studio):

 

if (PORT_PB22){
    //do something
}

Or is there a special function which does the reading?

Thanks.

This topic has a solution.
Last Edited: Thu. Feb 14, 2019 - 07:05 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your ports are set up properly, you can do

   if (REG_PORT_IN1 & (1<<22))

(yeah, while the datasheet as PORTA and PORTB and so on, the .file definitions are all PORT0 and PORT1...  Because CMSIS, probably.)

 

Technically, I believe the preferred method is to use the structure-based definition, which turn out to be even more poorly named (IMO.  Because they have "PortGroup" and "Port" backward.)

if (PORT->Group[1].IN.reg & (1<<22))

 

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

You can generalise this with:

crt_pin = (PORT->Group[PPS_PIN / 32].IN.reg & (1u << (PPS_PIN % 32)));
/* crt_pin is 0 if the level is 0, non-zero if it's high
 PPS_PIN is the pin number which you want to look at, e.g. PA17
 Remenber to set up the pin as input and set 
 PORT->Group[PPS_PIN / 32].PINCFG[PPS_PIN % 32].bit.INEN = 1;
 as well as other bits in PINCFG as needed
 */

Jerry

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

Thank you both.