Problem with unequal rise/falltime on triangular wave

Today, I buildt an 8-bit DAC (R2R ladder) and I tried to run a triangular waveform. The first impression was good, but as I checked the rise/falltime, it showed me these results: Rise:98us Fall:60us

The Problem is, that the rise/fall time should be equal.
The Code I use is:

void setup(){
  DDRD = B11111111;
}

void loop(){
  for ( int i = 0; i < 256; i++) {
    PORTD = i; 
  }
  for ( int i = 255; i >= 0; i--) {
    PORTD = i; 
  }
}

Please post a wiring diagram (not Fritzing), with labeled components and values. image posting guide

Here it is.

Westpol:
Today, I buildt an 8-bit DAC (R2R ladder) and I tried to run a triangular waveform. The first impression was good, but as I checked the rise/falltime, it showed me these results: Rise:98us Fall:60us

The Problem is, that the rise/fall time should be equal.
The Code I use is:

void setup(){

DDRD = B11111111;
}

void loop(){
 for ( int i = 0; i < 256; i++) {
   PORTD = i;
 }
 for ( int i = 255; i >= 0; i–) {
   PORTD = i;
 }
}

What I see here could be improved on:
void loop(){
for ( int i = 0; i < 256; i++) {
PORTD = i;
}
for ( int i = 255; i >= 0; i–) {
PORTD = i;
}

Each time through “loop” a NEW “int i” is created. Also, since your first “for” is testing for 256, the code has to process two bytes for the compare, where the second “for” only need a single byte compare.

Make “i” a global variable.

You might look at the actual instructions created and see if there are other changes that could be made.

Paul

Putting in a 255 instead of a 256 solved the Problem.

Thanks for the quick reply.

Glad it works. But, it’s still appropriate to look at the actual instructions created and see if there are a different set for each of the “for” statements.

Paul

I see you already solved the problem, but here is a slightly different method that should keep the waveform symetrical (except for the slight delays caused by interrupts running in the background);

void setup() {
  DDRD = B11111111;
}

void loop() {
  byte pattern = 0x00;
  while (1) {
    pattern ^= 0xFF;
    for (byte i = 255; i > 0; i--) {
      PORTD = i ^ pattern;
    }
  }
}

I tried both and they look the same. The only difference is, that yours is a bit slower with 5 kHz, but yours is easier to implement in the project, I will buildt, so I guess I'll use your design in the future.

Thanks for the suggestion