Due DAC port manipulation

Hey everyone,

I’d like to do some direct port manipulation on the due but don’t get the DAC working.
I wrote a little test program with a button and an LED. While the button is pushed the LED is lighting because of the DAC output. It works when I use analogWrite(). It doesn’t work with direct manipulation. What am I doing wrong?

I got inspiration from this thread: DAC free running mode - #9 by ard_newbie

  bool p12 = true;             // boolean to check input pin state
  uint16_t dacOutput;          // variable to store dac output
  int inputPin = 51;           // Pin which reads in TTL signal (94/C.12/D51)(PC12 on SAM3X8E)
  int outputPin = 66;          // DAC0 PB15
  
void setup() {
  // put your setup code here, to run once:

  // setup I/O
  pinMode(inputPin, INPUT);           // set pin51 = PC12 to input
  PMC->PMC_PCER0 |= PMC_PCER0_PID13;  // PIOC power ON to read PIOC input level
    
  // setup DAC
  pinMode(outputPin, OUTPUT);                   // set pin66 = PB15 as output
  analogWriteResolution(10);                    // set resolution to 10-bit (1024)
  DACC->DACC_CHER = DACC_CHER_CH0;              // enable channel 0 = DAC0 
  //DACC->DACC_CHER = (1<<DACC_CHER_CH0);
  PMC->PMC_PCER1 = PMC_PCER1_PID38;             // DACC power ON
  DACC->DACC_CR = DACC_CR_SWRST;                // Reset DACC
  //DACC->DACC_CR = (1<<DACC_CR_SWRST);
    
  // Hardware trigger disable = free running mode // no sleep betweeb conversions  // select channel 0 // zero seconds start up time // set DAC to max speed
  DACC->DACC_MR = DACC_MR_TRGEN_DIS | DACC_MR_SLEEP | DACC_MR_USER_SEL_CHANNEL0 | DACC_MR_STARTUP_0 | DACC_MR_MAXS;

//  DACC->DACC_MR &= ~(1<<DACC_MR_TRGEN_DIS);
//  DACC->DACC_MR &= ~(1<<DACC_MR_SLEEP);
//  DACC->DACC_MR &= ~(1<<DACC_MR_USER_SEL_CHANNEL0);
//  DACC->DACC_MR = (1<<DACC_MR_MAXS);
//  DACC->DACC_MR &= ~(1<<DACC_MR_STARTUP_0);
              
}

void loop() {
  // put your main code here, to run repeatedly:

  // button press
  p12 = !!(PIOC->PIO_PDSR & PIO_PDSR_P12); //read pin 12 on port C

  //if(digitalRead(inputPin) == LOW) {
  if(!p12) {
    dacOutput = 1000;
    //analogWrite(DAC0, dacOutput);
    DACC->DACC_CDR = dacOutput;
  }

   if(p12) {
    dacOutput = 0;
    //analogWrite(DAC0, dacOutput);
    DACC->DACC_CDR = dacOutput;
  }
}

If anyone is interested, it wasn’t working because of the 10-bit resolution. Using analogWriteResolution(12) and dacOutput = 4096 works!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.