DAC in Arduino Due

i am very new at C language and Arduino, i am kind of struggling and really need someone to help me.

i want to convert a external digital signal to analog using DAC1 feature in Due.

i am questioning my way to write the code, could some one tell me what is the mistake in my sketch or help me to find a code for it.

const int digitalInputPin = 26;
void setup() {
pinMode (DAC1,OUTPUT);
pinMode (digitalInputPin,INPUT);
Serial.begin(9600);

}

void loop() {

int b;

b = digitalRead (digitalInputPin);// Read pin 26

analogWriteResolution(12); //12 bits resolution
analogWrite(DAC1, b); // output values of varible b at the Digial to analog converter
delayMicroseconds(14);
Serial.println (b);

while(0){ // loop goes forever
}

}

any help would be so much appreciated,
M,

Hello m_lab85,

I just made a brief test with your code but simulating the digital input, and got in my oscilloscope a 13 seconds ramp from 0.6v to 2.8v at the DAC1 pin which means to me that your code should run OK. A caveat regarding DAC Due outputs: do not connect any load directly to the DAC pins of the Arduino Due. Use an amplification circuit between the DAC pin and the load as is shown here: http://arduino.cc/en/Tutorial/SimpleAudioPlayer

Here my test code:

const int digitalInputPin = 26;
void setup() {
  pinMode (DAC1,OUTPUT);
  pinMode (digitalInputPin,INPUT);
  Serial.begin(9600);
  
}

void loop() {
 
int b;

 for(int i=0; i<4096; i++)
 {
 b = digitalRead (digitalInputPin);// Read pin 26
 
  analogWriteResolution(12); //12 bits resolution
 analogWrite(DAC1, i); // output values of varible b at the Digial to analog converter
 delayMicroseconds(14);
   Serial.println (b);
 }
while(0){                     // loop goes forever
  }

}

I hope this helps. Regards.

thank thank you so much Palliser for verifying the code. with your help,i feel i am still on track.
i hope you don't mind if i ask another question,

i wrote a code to convert an analog signal to digital on Due. the results ,i am getting on the oscilloscope, of the ADC as shown in Frequency_5Hz (attached) for analog square wave = 1v at the pin( A1). fre = 5Hz.
and by increasing the frequency to 50Hz at scope Frequency_50Hz, the ADC seems like it can't catch up with the input signal.
the code i wrote is:

const int InputPin = A1;
const int OutPin = 28;

void setup()
{
pinMode(InputPin,INPUT);
pinMode(OutPin,OUTPUT);
analogReadResolution(12); // 12-bit!
Serial. begin(9600);
}
void loop() {

int a;
a = analogRead(InputPin);
digitalWrite(OutPin,a);
Serial.println(a);
while(0){
}
}

a confusing point is that in Frequency_5Hz i can see that ADC is responding but not correctly. i think that i should see a series of binaries not a plus looks similar to the input signal.

Best Wishes,

M,