Timers/counter

Hi all!

I have this piece of code,

#include<avr/io.h>
#include<avr/interrupt.h>                  
unsigned char cont;                        
unsigned char flag=0;            


void setup(){                        
      DDRD=0x02;            //pin 1 as output for led                  
      OCR0B=77;            //value which tcnt0 will compare                  
      TCCR0A=0b00000010; //activate clear timer on compare match
        TCCR0B=0b00000101; //prescaler 1024                        
      TIMSK0=0b00000100;  //Timer/counter Output Compare Match B Interrupt Enable                  
      SREG=0b10000000;      // global interrupt enable            
      cont=100;                              
}

ISR(TIMER0_COMPB_vect){                  
      cont--;                                    
      if (cont==0){                        
            flag=1;                              
            cont=100;
      }
}


void loop(){                  
      while(flag==1){                  
          PORTD=~PORTD;            
        flag=0;                        
        }      
}

it’s suppose to put led blinking, but it’s not working, what i am doing wrong?

regards

I'd mark flag as "volatile", but I don't have any h/w here to test.

what i am doing wrong?

Maybe running before you can walk?

bspirit

it's suppose to put led blinking, but it's not working, what i am doing wrong?

  1. We have to know where the LED is connected.

  2. We have to know what each of the steps in your sketch is attempting to achieve.

The information mentioned above is typically provided via 'comments'. I know it requires some effort to provide that information but why should you expect your helpers to do that work in addition to doing the troubleshooting that you request?

  1. You state that the program is 'not working'. We have to know what it is supposed to do, in this case how often should the LED blink. We also have to know what it actually does. Is the LED always ON, always OFF, dim? When a program does not work as expected what it does do can often provide a clue to the problem.
TIMSK0=0b00000100;

I think you have the wrong bit here.

Don

sorry for the lack of comments in the code!

but the problem is resolved, changed unsigned char cont to unsigned int cont and raised cont=2000, the led is blinking very fast that is impossible to see.

regards

Correction:

TIMSK0=0b00000100;
I think you have the wrong bit here.

I see that you are using Compare Match B, so this would be correct. I’m not sure about their interaction (between A and B), but I think you might have better luck starting out with Compare Match A.

Maybe running before you can walk?

Doesn’t everyone start with interrupts?

Don

the led is blinking very fast that is impossible to see.

That is what I meant by the LED being 'dim'!

Don

bspirit:

It's much better with the comments! Here's one more tip:

      DDRD=0x02;            //pin 1 as output for led
      OCR0B=77;            //value which tcnt0 will compare

These two arguments are bitmapped, so the binary radix is more appropriate.

You might want to go to http://web.alfredstate.edu/weimandn. Scroll down to the "Atmel ATmega Subsystem Diagrams" section and follow the [u]Reference Diagrams[/u] link. There's no diagram for Timer/Counter0 Compare Match B (yet), but the one for Compare Match A is pretty close.

Don

bspirit:

It's much better with the comments! Here's one more tip:

DDRD=0x02; //pin 1 as output for led OCR0B=77; //value which tcnt0 will compare

These two arguments are bitmapped, so the binary radix is more appropriate.

You might want to go to http://web.alfredstate.edu/weimandn. Scroll down to the "Atmel ATmega Subsystem Diagrams" section and follow the Reference Diagrams link. There's no diagram for Timer/Counter0 Compare Match B (yet), but the one for Compare Match A is pretty close.

Don

thanks for the tips and link, the link is very useful, and contains diagrams that help me a lot to understand, i'm newbie, my friends told me that the arduinos is good thing for beginners like me. i don't know what is meaning of "bitmapped", but i will reasearch about this.

regards

ps. english is not my language, correct me, if i wrote something wrong, so i could improve my english.

bspirit:

First of all I misspoke when I said that OCR0B is bitmapped - it’s not, but DDRD is.

i don’t know what is meaning of “bitmapped”

‘Bitmapped’ means that each bit within the byte has a specific meaning but the numerical value of all the bits taken together probably does not.

Think about how you came up with the statement DDRD=2;. Where did the number ‘2’ come from? Would DDR2=0b00000010; make more sense? If you don’t know the answers let me know and I will give you more information.

english is not my language, correct me, if i wrote something wrong, so i could improve my english.

Your English is quite understandable but if you want to make the grammar better you would rephrase ‘i don’t know what is meaning of “bitmapped”’ to ‘I don’t know the meaning of “bitmapped”’.

Don

You might want to look over a timer library that would save you all the gory details for setting up a fixed timed interrupt routine.

http://www.arduino.cc/playground/Main/MsTimer2

Lefty

lefty:

You might want to look over a timer library...

Somebody has to learn enough about programming to write these libraries. I got the impression that 'bsprit' might be one of those types.

Don

floresta: i wrote in two ways on purpose

DDRD=0x02; //pin 1 as output for led - here i could had written DDRD=0b00000010 TCCR0B=0b00000101; //prescaler 1024 and here i could had written TCCR0B=0x05

so if look it again another time, i could know that is possible to program in one way or another.

Somebody has to learn enough about programming to write these libraries. I got the impression that 'bsprit' might be one of those types.

Don

why you say that?

ps. for first impression i have, I'm really enjoying the forum, here have helpful people, it is pity not understand and write English well, making more difficult to reply well