Author Topic: Atmega8 with rtc crystal  (Read 3030 times)

0 Members and 1 Guest are viewing this topic.

Offline ramin110Topic starter

  • Contributor
  • !
  • Posts: 23
  • Country: ir
Atmega8 with rtc crystal
« on: January 16, 2016, 06:14:03 pm »
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 :wtf: . what is my problem?  :scared:

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>>8);

   /*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);
   }
}








« Last Edit: January 16, 2016, 06:49:48 pm by ramin110 »
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17817
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Atmega8 with rtc crystal
« Reply #1 on: January 16, 2016, 06:17:38 pm »
Have you measured the crystal signal ? if there is a 10% error you should spot it easily. otherwise it's something in your code.
 

Online wraper

  • Supporter
  • ****
  • Posts: 16866
  • Country: lv
Re: Atmega8 with rtc crystal
« Reply #2 on: January 16, 2016, 06:23:08 pm »
Wait, are you making the timer with a delay function?  :palm:
 

Offline ramin110Topic starter

  • Contributor
  • !
  • Posts: 23
  • Country: ir
Re: Atmega8 with rtc crystal
« Reply #3 on: January 16, 2016, 07:15:28 pm »
I am beginner in microcontroller and I think i can't use delay? right :o, i just changed the code and using avr timer. it's working. i think kinda  :-DMM


int main(void)
{
   DDRB = (1<<PB0);
   
   //for Baud rate = 300bps*/
   USARTInit(6);   
   
   
   OCR1A = 31;

   TCCR1B |= (1 << WGM12);
   // Mode 4, CTC on OCR1A

   TIMSK |= (1 << OCIE1A);
   //Set interrupt on compare match

   TCCR1B |= (1 << CS12) | (1 << CS10);
   // set prescaler to 1024 and start the timer


   sei();
   // enable interrupts


   
   
   while(1)
   {
      //LED On
      //PORTB = (1<<PB0);
      //_delay_ms(1000);
      
      //LED Off
      //PORTB = (0<<PB0);
      
//      count++;
      
   //   char str[16];
   
   //   itoa(micros(), str, 10);
      
   //   for(unsigned int i = 0; i < strlen(str); i++)
   //      USARTWriteChar(str);
   
   //    USARTWriteChar(0b1101);

      
      
      //_delay_ms(10);
   }
   
   
   
}



ISR (TIMER1_COMPA_vect)
{
   count++;
   
      char str[16];
   
      itoa(count, str, 10);
   
      for(unsigned int i = 0; i < strlen(str); i++)
         USARTWriteChar(str);
   
       USARTWriteChar(0b1101);
}
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17817
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Atmega8 with rtc crystal
« Reply #4 on: January 16, 2016, 07:17:18 pm »
The problem with delays is that you can't do anything else with the processor because it's locked into the time delay.

If you use an interupt on a timer it means you can do something for the 99.9999995 of the time and then act when the interupt fires.
 

Online wraper

  • Supporter
  • ****
  • Posts: 16866
  • Country: lv
Re: Atmega8 with rtc crystal
« Reply #5 on: January 16, 2016, 07:54:59 pm »
The problem with delays is that you can't do anything else with the processor because it's locked into the time delay.

If you use an interupt on a timer it means you can do something for the 99.9999995 of the time and then act when the interupt fires.
Also delay function is not accurate at all. All it does is burning CPU cycles without doing anything useful to get approximate delay. On top of that, doing something between delays (Sending data to USART), distracts the timing even more. Remember that your clock frequency is very low, so anything you do will take a hell a lot of cycles. Also slow clock by itself makes delay function less accurate rather than with higher clock.
« Last Edit: January 16, 2016, 08:03:49 pm by wraper »
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17817
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Atmega8 with rtc crystal
« Reply #6 on: January 16, 2016, 07:58:38 pm »
If you need higher processing speed then maybe the best thing is to use the clock chip to drive just the counter if your chip suports it so that the main CPU core can run at 1-20MHz. Or if not you can use a logic gate to make a crystal oscilator with the clock crystal and use it's output to drive an interupt enabled pin. I did this to make a pulse generator to drive an old mechanical clock, more because I could not make my uC work with the crystal but it immediately occured to me that I could harness the power of the uC to do anything else as well as the timer function.
 

Offline ramin110Topic starter

  • Contributor
  • !
  • Posts: 23
  • Country: ir
Re: Atmega8 with rtc crystal
« Reply #7 on: January 16, 2016, 08:00:16 pm »
thanks a lot people  for useful information and your time. ;D

 :-/O
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf