hi everyone
I want to build a precise timer with atmega8 microcontroller and external 32.768khrz watch crystal. I set all fuse correctly for external crystal and etc. everything works fine but my timer has a offset of -1 second in each 9 seconds
. what is my problem?
please help
hfuse = 0xc9
lfuse = 0xe9
code:
#ifndef F_CPU
#define F_CPU 32768UL
#endif
#include <util/delay.h>
#include <avr/io.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
//This function is used to initialize the USART
//at a given UBRR value
void USARTInit(uint16_t ubrr_value)
{
//Set Baud rate
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>
;
/*Set Frame Format
>> Asynchronous mode
>> No Parity
>> 1 StopBit
>> char size 8
*/
UCSRC=(1<<URSEL)|(3<<UCSZ0);
//Enable The receiver and transmitter
UCSRB=(1<<RXEN)|(1<<TXEN);
}
//This fuction writes the given "data" to
//the USART which then transmit it via TX line
void USARTWriteChar(char data)
{
//Wait untill the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
//Now write the data to USART buffer
UDR=data;
}
unsigned int count = 0;
int main(void)
{
DDRB = (1<<PB0);
//for Baud rate = 300bps*/
USARTInit(6);
while(1)
{
//LED On
//PORTB = (1<<PB0);
//_delay_ms(1000);
//LED Off
//PORTB = (0<<PB0);
count++;
char str[16];
itoa(count, str, 10);
for(unsigned int i = 0; i < strlen(str); i++)
USARTWriteChar(str
);
USARTWriteChar(0b1101);
_delay_ms(1000);
}
}