I am only used to program ATmega uC's so I'm new to direct port access on the arduion due. I did some research but I couldn't quiet find an example for checking a digital I/O Pin for HIGH or LOW. I want to check pin 51 on the due for HIGHs and LOWs. The ATmega equivalent would be e. g. if(PINA&(1<<PINA51)) or if(~PINA&(1<<PINA51)).
I'm guessing something like if(PIO_PDSR&(1<<P0)) from page 645 in the SAM3X8E datasheet.
Since pin 51 is PC12 (See Graynomad pinout diagram), you'll have to read PIOC->PIO_PDSR bit 12. Beforehand, you have to power PIOC so that you can read input state on any PIOC pin.
An example sketch:
void setup() {
PMC->PMC_PCER0 |= PMC_PCER0_PID13; // PIOC power ON to read PIOC input level
}
void loop() {
while (PIOC->PIO_PDSR & PIO_PDSR_P12) { // As long as pin 51 (PC12) is High
//Do something
}
}
Not quite. The input registers only have 8 bits. On the Arduino MEGA 2650, for example, the look-up table to translate Arduino pin numbers to Port and Bit are found in "hardware/arduino/avr/variants/mega/pins_arduino.h"
In the table it says that Pin 51 is Bit 2 of Port B so the code to read it would be more like:
I am only used to program ATmega uC’s so I’m new to direct port access on the arduion due. I did some research but I couldn’t quiet find an example for checking a digital I/O Pin for HIGH or LOW. I want to check pin 51 on the due for HIGHs and LOWs. The ATmega equivalent would be e. g. if(PINA&(1<<PINA51)) or if(~PINA&(1<<PINA51)).
I’m guessing something like if(PIO_PDSR&(1<<P0)) from page 645 in the SAM3X8E datasheet.
Then all you have to do is figure out how to read a pin with the information provided. digitalRead() punts on that question, calling the function PIO_Get().
The meanings of the fields in the pin descriptor might be helpful. You can find that struct defined in Arduino.h:
/* Types used for the tables below */
typedef struct _PinDescription
{
Pio* pPort ;
uint32_t ulPin ;
uint32_t ulPeripheralId ;
EPioType ulPinType ;
uint32_t ulPinConfiguration ;
uint32_t ulPinAttribute ;
EAnalogChannel ulAnalogChannel ; /* Analog pin in the Arduino context (label on the board) */
EAnalogChannel ulADCChannelNumber ; /* ADC Channel number in the SAM device */
EPWMChannel ulPWMChannel ;
ETCChannel ulTCChannel ;
} PinDescription ;
Thanks for the tip, I found some interesting threads by searching for digitalReadDirect!
But I'm not quiet sure how to use this, do I put the inline code into the setup() or do I need to include a library?
I also need it only for one pin, so to safe some time can I write like :
bool myState = !!(g_APinDescription[12].pPort -> PIO_PDSR & g_APinDescription[12].ulPin); because Pin51 = PC12 and then I check the pin like: if(myState==true)?
The 'g_APinDescription' table is indexed by ARDUINO pin number so for Arduino Pin 51 you would use 51, not 12: bool myState = !!(g_APinDescription[51].pPort->PIO_PDSR & g_APinDescription[51].ulPin);
And I already gave you the contents of that table entry (above) so you can avoid the two look-ups: bool myState = !!(PIOC->PIO_PDSR & PIO_PC12);