Ok, now the loop takes 12.5 us. So every 12.5us, a new value is put into the dac and outputted. I think I can reduce this period by smarter bitsetting, but I don't know how to do best. Ideally I want to stick to the pins I use now, 30-45, so not an unique port. I expect the for loop to take most of the time.
//Definieer digitalwrite en digitalread-direct
inline void digitalWriteDirect(int pin, boolean val){
if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
else g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}
inline int digitalReadDirect(int pin){
return !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);
}
int update = 8;
//trim DC output to 0, depends on resistor specs and board layout
int offset = 23;
//Halfway ideal value 2^16 / 2 + the offset adjustment
int zero = 32768+offset;
//2^16 / 20 volts is value per volt
float scale = 3276.8;
//DAC reference, what should the output be? In volts.
float input = 0.1;
//Define the value which is send to the DAC, with scaling and offset.
int value = 0; //Value to DAQ;
void setup() {
//Define DAC PINS
pinMode(update, OUTPUT);
digitalWrite(update, LOW);
pinMode(A0, INPUT);
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
pinMode(36, OUTPUT);
pinMode(37, OUTPUT);
pinMode(38, OUTPUT);
pinMode(39, OUTPUT);
pinMode(40, OUTPUT);
pinMode(41, OUTPUT);
pinMode(42, OUTPUT);
pinMode(43, OUTPUT);
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
//Put value out to DAC
//Determine integer to DAC
value = round(zero+input*scale);
//Put DAC pins high
for(int x = 30; x < 46; x++){digitalWriteDirect(x, bitRead(value,x-30)); }
//Update DAC output value
digitalWriteDirect(update, HIGH);
digitalWriteDirect(update, HIGH);
//Fix outpur value
digitalWriteDirect(update, LOW);
}