Go Down

Topic: How to read port pin directly in DUE , Direct port manipulation. (Read 2693 times) previous topic - next topic

EJLED

hi, guys.

I want to use port pin of DUE as an input, i worked on mega there we write DDR and PINA to read the port A data. how do i write the same code in DUE.?

any sample code?

Thanks

ard_newbie



Hello EJLED,


Here is the most basic example I found of polling a PIO input, see attached schematic :

Code: [Select]

/*
  PIO with polling input
  Example  which uses a tactile switch that when pressed dims a normally lit onboard LED.
  The switch is configured as an input on PC12/DUE PIN N° 51 and the onboard LED is located on PB27/DUE PIN N°13.
*/


void setup() {

  //We need to associate clock with Port C so we can read input
  //A list of Peripheral ID's can be found on Page 37 in the datasheet.
 
  PMC->PMC_PCER0 = PMC_PCER0_PID13; // PIOC power on
 
  //Enable PB27 and PC12 (Peripheral Enable Register)
  PIOB->PIO_PER = PIO_PB27;
  PIOC->PIO_PER = PIO_PC12;
 
  //Set B27 as output (Output Enable Register)
  PIOB->PIO_OER = PIO_PB27;

  //Set C12 as input (Ouput Disable Register)
  PIOC->PIO_ODR = PIO_PC12;

  //Disable pull-up on both pins (Pull Up Disable Register)
  PIOB->PIO_PUDR = PIO_PB27;
  PIOC->PIO_PUDR = PIO_PC12;
}


void loop() {
  //Very simple polling routine that checks the Pin Data Register's status
  //and sets the state of the LED accordingly.

  if ((PIOC->PIO_PDSR & PIO_PC12) == PIO_PC12)
    PIOB->PIO_SODR = PIO_PB27;  //Set Ouput Data Register
  else
    PIOB->PIO_CODR = PIO_PB27;  //Clear Output Data Register
  delay(10);

}






EJLED

Thanks a lot ard_newbie. i am trying now.

can u tell me what are the basic procedure to setup a pin as a input.

The schematic u have attached it showes pull up register, does it read HIGH in normal operation?

EJLED

Hi, Thanks SeniorIceMan. i will upload skatch soon..

can u tell me what ths does (1u<<21) specially "1u" i know (1<<21) means it will shift 21left 21 bit. but what this (1u<<21) exactly do.?


Thank you so much for help me

EJ

EJLED

okay, thats nice. actually i did not know this stuff, thanks for sharing.

did u measure the clock of Arduino DUE? 84MHZ. how do we know the speed of core ? any code to evaluate the speed of DUE.?

danielciencia

This is too valid https://github.com/arduino/ArduinoCore-sam/blob/master/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_pio.h

Code: [Select]
void setup() {

  //We need to associate clock with Port C so we can read input
  //A list of Peripheral ID's can be found on Page 37 in the datasheet.
 
  //Enable PB27 and PC12 (Peripheral Enable Register)
  PIOB -> PIO_PER = PIO_PER_P27;
  PIOC -> PIO_PER = PIO_PER_P12;
 
  //Set B27 as output (Output Enable Register)
  PIOB -> PIO_OER = PIO_OER_P27;

  //Set C12 as input (Ouput Disable Register)
  PIOC -> PIO_ODR = PIO_ODR_P12;

  //Disable pull-up on both pins (Pull Up Disable Register)
  PIOB -> PIO_PUDR = PIO_PUDR_P27;
  PIOC -> PIO_PUDR = PIO_PUDR_P12;
}


void loop() {
  //Very simple polling routine that checks the Pin Data Register's status
  //and sets the state of the LED accordingly.

  if ((PIOC->PIO_PDSR & PIO_PC12) == PIO_PC12)
    PIOB -> PIO_SODR = PIO_SODR_P27;  //Set Ouput Data Register
  else
    PIOB -> PIO_CODR = PIO_CODR_P27;  //Clear Output Data Register
  delay(10);

}

Go Up