pulse width measurement atmega
I've been reading your code, and you are reading and writing 16 bits registers in a wrong way. Like I say in my previous post, when you read or write a 16 bits register, you must do in a particular order (Page 92 of the ATmega16 datasheet).
1) If you write a 16 bits register, you must first write the higher 8 bits, and then write the lower 8 bits.
2) If you read a 16 bits register, you first read the lower 8 bits, and then read the higher 8 bits.
TCNT1 is a 16 bits register, so you must follow this procedure to read and write in it. This, when you write C code, in most cases is solved by the compiler. You have two choices, read the hexa code produced and find if they do in the proper way, and/or change your code to make two 8 bits read/write operations.
Here, you are reading the TCNT1 register in a wrong way:
Code:
width=TCNT1H;
width=(width<<8);
width=width|TCNT1L;
You must, first read the lower 8 bits, and then the higher 8 bits.
Here, you write the CNNT1 register in a proper way:
Code:
TCNT1H=0x00;
TCNT1L=0x00;
Correct, and try again.
Regards