Go Down

Topic: help on input and output on Arduino Due using registers (Read 23 times) previous topic - next topic


Today at 12:40 am Last Edit: Today at 04:51 pm by ale1284 Reason: adding code
Hi guys,

I'm new to this forum, but I used Arduino on a basic level in the past. I have recently starting to use the registers in Arduino Due to get faster read and writes. I have been reading and following different topics on the Arduino due forum regarding how to program Arduino Due using the registers and I would like to post the code I'm using so you can let me know if I'm declaring the input and outputs correctly. thanks in advance for any suggestions you may have are more than welcome!!

FYI. The code compiles

Code: [Select]
*  input output test v1.0
*  Pin D2 on Arduino DUE has been configure as input
*  Pin D13 On Arduino DUE has been configure as output
*  On this example we have 1 input( D9 / PC21) and 1 output (D13/PB27), the output will 
*  change its state based on the input state. 

//define variables
uint32_t pin13 = (1 << 27);      // mask for pin 13; equals BIT 27 in port B. 
uint32_t pin9 = (1 << 21);       // mask for pin 9; equals BIT 27 in port C.

void setup() {
  // put your setup code here, to run once:
  //We need to associate clock with Port C so we can read input, see section 31.5.8 of the Datasheet

   // I also found this other way to associate the clock on Port C
  //if ((PMC->PMC_PCSR0 & (0x01u << ID_PIOC)) != (0x01u << ID_PIOC)) PMC->PMC_PCER0 = PMC->PMC_PCSR0 | 0x01u << ID_PIOC;   // Enable PIO C clock
   /* settiing up D13 as output */
   //pinMode(pin13, OUTPUT);     //setting pin13 as output using arduino pinMode 
  //REG_PIOB_OER = pin13;        // set PORTB pin 13 (LED) as output (Output enable register = OER)
   pin13 =  PIOB->PIO_OER & PIO_PB27;      // set PORTB pin 13 as outout (Output enable register = OER) but this one uses arrow operator to 
  /*Setting up D9 as input*/
   //pinMode(pin9, INPUT);         //setting pin9 as input using arduino pinMode 
  //REG_PIOC_PDSR = pin9;        // set PORTB pin 2 as input using PDSR (PDSR = Pin Data Status Register)
  pin9 = PIOC->PIO_PDSR & PIO_PDSR_P21; // set PORTB pin 2 as input (PDSR = Pin Data Status Register), see section 31.5.8


void loop() {
  // put your main code here, to run repeatedly:
  // This routine checks the Pin Data Register's status
  //and sets the state of the LED accordingly.
  if ((PIOC->PIO_PDSR & PIO_PC21) == PIO_PC21)
    PIOB -> PIO_SODR = PIO_SODR_P27;  //Set Ouput Data Register
    PIOB -> PIO_CODR = PIO_CODR_P27;  //Clear Output Data Register

Go Up