Go Down

Topic: Arduino Zero Reading output pin (Read 723 times) previous topic - next topic

Newking

Hi Everyone!

I was wondering if anyone would happen to know how the Arduino Zero could read an output pin to find out what its current status would happen to be. I've read that for AVR arduinos like the UNO digitalRead() would still work and that for the Duemilanove it's a matter of reading the PIO_ODSR register for the appropriate pin.

My goal is such that once a timer is over, on the next pwm output pulse(controlled by Adafruit_Timer on pin 12) I would read a value. However to detect this pulse I would like to read the output pin itself as I need to ensure that it was raised high.

As I am still relatively new to large datasheets like the SAMD21G, I was wondering if anyone would have any tips on this.

As this is more of a theoretical question than a debugging one, I figure my code is not needed but if it is, just let me know!

MartinL

#1
Mar 13, 2018, 09:50 am Last Edit: Mar 13, 2018, 02:44 pm by MartinL
Hi Newking,

EDIT: See below...

Unfortunately, it's not a case of simply reading the state of digital pin. The pin configuration register (PINCFG) in the SAMD21 switches the pin between GPIO and a selected peripheral, for example sercom, ADC, timer, etc...

If you're using a PWM timer to generate the output pulses, then the peripheral overrides the GPIO, so it's not possible to use digitalRead() to read back the pin's value.

An easier way to test the value of the pin, would be to connect your PWM output to a neighbouring GPIO input on your board and use digitalRead() to test it.

An alternative way would be to interrogate the timer's count, counter compare and overflow registers. The value of the count registers relative to the others would inform you as to whether the PWM output was high or low. Or you could keep track of the timer's output state using its interrupt service routine.

Exactly how you'd implement this depends on how your timer is set-up: whether it's single slope PWM, dual slope PWM, signal polarity, etc...

Newking

Hi Martin,

Thanks for the quick reply and the answer. Now knowing that it's not possible to directly read the pin output, I'll most likely just read the output on a second pin. I'm still getting used to accessing registers and understanding them from the datasheet so I might follow your alternative later once I feel more comfortable with it.


MartinL

#3
Mar 13, 2018, 02:38 pm Last Edit: Mar 13, 2018, 02:59 pm by MartinL
Hi Newking,

My apologies, I had a rather belated look a the SAMD21 datasheet, actually I'm wrong.

It is possible to read back the pin value with the it set to a peripheral, provided that the INEN (Input Enable) bit in the pin cofiguration (PINCFG) register.

Here's the line from the datasheet:

Quote
The peripheral multiplexer value in
PMUXn is ignored. Writing '1' to this bit enables the peripheral selection in PMUXn to control the pad. In this configuration, the physical pin state may still be read from the Data Input Value register (IN) if PINCFGn.INEN is set.
The INEN bit for digital pin D12 can be set with the following line:

Code: [Select]
PORT->Group[PORTA].PINCFG[19].bit.INEN = 1;     // Enable the input enable bit
Note that pin D12 is port pin PA19. It can also be set by configuring the pin to an output with the Arduino pinMode() function.

To check the input on D12 just use the following:

Code: [Select]
if (digtialRead(12) == HIGH) 
{
  //...
}

or alternatively...

Code: [Select]
if (REG_PORT_IN0 & PORT_PA19) 
{
  //...
}

Go Up