user79640: I am trying to write a delay function in C, which should take 0.5 seonds then carry on. I am aware that there are libraries for this purpouse, I want to make it myself.
#include <avr/interrupt.h>
#include <avr/io.h>
#define bitset(var,bitno) ((var) |= (1 << (bitno)))

int volatile time_unit;

ISR(TIMER0_COMPA_vect) {

  time_unit += 1;
}

void wait_func(){
  TCNT0 = 0;
  time_unit = 0;
  while (time_unit < 31);
}


int main(void){
  DDRD = 1;
   cli();
  bitset(TCCR0A, WGM01);
  bitset(TCCR0B, CS00);
  bitset(TCCR0B, CS02);
  OCR0A = 0xFF;
  bitset(TIMSK0, OCIE0A);
  sei();

  while(1){
    PIND = 0;
    wait_func();
    PIND = 1;
    wait_func();
  };
}

The timer is in CTC- Mode 1024 prescaler, counting 31*255 (OCR0A = 0xFF) times should give about 0.5 seconds at a base clockrate of 16MHz of the Arduino Atmega328p. The code works, but it turns the LED on and of at a rate of 1 second, (1 sec on 1 sec off). I do not understand why.

user70020: 
PIND = 1; toggles the state of bit 0 of PORTD.
PIND = 0; does nothing at all; no toggling of any pins.

So you're simply just not toggling as often as you think.  Your delay itself is otherwise fine.  Change PIND = 0; to  PIND = 1; and it will work as expected.  Having done that you can get rid of one set of PIND = 1; and wait_func(); call.
Alternately, if you want to follow the current form of the code, set and clear the bit on PORTD directly:
while(1){
  PORTD &= ~1; // or &= 0xFE
  wait_func();
  PORTD |=  1; 
  wait_func();
}

For what it's worth:

The semicolon after your while loop does nothing.
The time_unit should probably be an unsigned char / uint8_t.
Unless you're running really tight on code space, it's probably best not to rely on the prior state of the timer hardware registers.


79640: <p>I am trying to write a delay function in C, which should take 0.5 seonds then carry on. I am aware that there are libraries for this purpouse, I want to make it myself.</p>
<pre><code>#include &lt;avr/interrupt.h&gt;
#include &lt;avr/io.h&gt;
#define bitset(var,bitno) ((var) |= (1 &lt;&lt; (bitno)))

int volatile time_unit;

ISR(TIMER0_COMPA_vect) {

  time_unit += 1;
}

void wait_func(){
  TCNT0 = 0;
  time_unit = 0;
  while (time_unit &lt; 31);
}


int main(void){
  DDRD = 1;
   cli();
  bitset(TCCR0A, WGM01);
  bitset(TCCR0B, CS00);
  bitset(TCCR0B, CS02);
  OCR0A = 0xFF;
  bitset(TIMSK0, OCIE0A);
  sei();

  while(1){
    PIND = 0;
    wait_func();
    PIND = 1;
    wait_func();
  };
}
</code></pre>
<p>The timer is in CTC- Mode 1024 prescaler, counting 31*255 (OCR0A = 0xFF) times should give about 0.5 seconds at a base clockrate of 16MHz of the Arduino Atmega328p. The code works, but it turns the LED on and of at a rate of 1 second, (1 sec on 1 sec off). I do not understand why.</p>
 70020: <ul>
<li><code>PIND = 1;</code> toggles the state of bit 0 of <code>PORTD</code>.</li>
<li><code>PIND = 0;</code> does nothing at all; no toggling of any pins.</li>
</ul>
<p>So you're simply just not toggling as often as you think.  Your delay itself is otherwise fine.  Change <code>PIND = 0;</code> to  <code>PIND = 1;</code> and it will work as expected.  Having done that you can get rid of one set of <code>PIND = 1;</code> and <code>wait_func();</code> call.</p>
<p>Alternately, if you want to follow the current form of the code, set and clear the bit on <code>PORTD</code> directly:</p>
<pre class="lang-c prettyprint-override"><code>while(1){
  PORTD &amp;= ~1; // or &amp;= 0xFE
  wait_func();
  PORTD |=  1; 
  wait_func();
}
</code></pre>
<p>For what it's worth:</p>
<ul>
<li>The semicolon after your while loop does nothing.</li>
<li>The <code>time_unit</code> should probably be an <code>unsigned char</code> / <code>uint8_t</code>.</li>
<li>Unless you're running really tight on code space, it's probably best not to rely on the prior state of the timer hardware registers.</li>
</ul>
