library function for timer/counter module

PLease tell me the library function for timer/counter module sothat i can give external clock pulse as
counter .
its an urgent one…

thanks in advance

I don’t know about a library function, but you could do it with something like:

TCCR1B = 0x07; // timer1 clock = external clock source on pin T1, rising edge

Connect your external clock to mega168 pin 9 (a.k.a. PD5/T1, a.k.a. arduino pin 5). Every rising edge of your external clock will increment the timer1 count register TCNT1 by one.

  • Ben

Yes i hav programmed as you suggested.but i am using AVr ATmega8

#define TCNT0 0x32

TCNT0=0x7;

void setup();
{
// portd4 as clock input
}
void loop()
{}
like this . but it shows error. Can u just mention me with a simple example in atmega8?

thanks for ur valuable reply

I’m not really familiar enough with the Arduino environment yet to know what you can and can’t do, but avr/io.h defines all of the mega8 register locations you need. You shouldn’t have to do anything like:

#define TCNT0 0x32 // this line is unnecessary!!!

By looking at the mega8 datasheet, I can see that your code should look something like this:

void setup()
{
TCNT0 = 0;
TCCR0 = 0x07; // External clock source on T0 pin. Clock on rising edge.
}

void loop
{
// connect your clock input to PD4
// on a regular basis check the register TCNT0 to see how many clock pulses have occurred?
// for example:

if (TCNT0 > 100)
do something fun;
}

  • Ben

sir,
Thankyou for your valuable suggestion.Now i using winavr.I hav compiled a program with no errors.
but one problem . i cant use simulator debugging , as i already selected AVR simulator as platform and
ATmega8 as device. It shows runtime rerror .Can i able to debug step by step in winavr without connecting the programmer( hardware).

reply me

For the timer mode, there’s the FrequencyTimer2 library in the playground. I don’t think there is a library yet for the counter.

Bens, I’m glad to see you’re sticking around. I can’t wait to see what libraries you come up with.

sir,
Thankyou for your valuable suggestion.Now i using winavr.I hav compiled a program with no errors.
but one problem . i cant use simulator debugging , as i already selected AVR simulator as platform and
ATmega8 as device. It shows runtime rerror .Can i able to debug step by step in winavr without connecting the programmer( hardware).

I’m not sure I fully understand. Are you still using the Arduino IDE or are you now bypassing that in favor of straight WinAVR? Do you have an AVR ISP programmer?

  • Ben

Bens, I’m glad to see you’re sticking around. I can’t wait to see what libraries you come up with.

This seems like a great community; I’m very happy to be here, share some of the things I’ve learned, and pick up some of the things that are new to me!

  • Ben