I am experimenting with timers using TCCRIB and TCNT1 registers. When I use Serial.Print to read the value ot TCNT1 registers or TCNT1L and TCNT1H seperately, the value of TCNT1 does not go beyond 255, while TCNT1H remains zero. The code I am using is as under, I am using Mega 2560, the basic program was to make Pin 13 blink at 1/20 sec . Can some one help:-
include<avr/io.h>
void setup(){
DDRB = DDRB | B1000000; // set digital 13 as OUTPUT
Serial.begin(9600);
what AWOL meant was that you should use the CODE tags when you post some code.
You can click up there on the third button from the right on the last row of message formating buttons (it has a number sign - # - on it) and then put all your code inside the tags.
Something like this
Would you be so kind to tell which variable is suitable?
I have tried a lot of types but I could not make it work.
You can read my code below. What I am doing is that I connect a signal generator on the INT0 pin and I wish to measure the time between falling edges and later communicate this. This actually works but only until I reach the 8 bit limit. The value of period will never go above 255 although according to the datasheet it should be possible to read the value of TCNT1 simply with 1 command.
I also tried reading TCNT1L and TNCT1H separately but I ended up same as the Original Poster.
Could you please give me a hint here to which variable type I really need or what I should do to get the 16 bit value from the TCNT1 register?
Thank you!
int ledPin = 13; // LED connected to digital pin 13
int INTPin = 2;
int FlagPin = 5;
unsigned int period = 0;
ISR(INT0_vect){
period=TCNT1;
if (period > 255) PORTD^=1<<FlagPin; // This flag will somehow never trigger
TCNT1=0;
}
void TIMER1_init()
{
//Init Timer 1 to normal mode prescaler = 8 -> 500ns
TCNT1=0;
TCCR1B=0x02; //Set prescaler to 8 & start counter
}
void INT_init() {
EICRA=0x02; //1<<ISC0; //Set Pin change IT for INT0 to falling edge
EIMSK=0x01; //1<<INT0; //Enable INT0 interrupt
}
void setup() {
Serial.begin(1000000);
pinMode(ledPin, OUTPUT);
pinMode(3, OUTPUT);
pinMode(INTPin, INPUT);
pinMode(FlagPin, OUTPUT);
INT_init();
TIMER1_init();
}
void loop()
{}