Real Time Clock

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone

I want to make a rtc with atmega8535 and a 32.768 kHz watch crystal.
I read the datasheet but i dont understand a lot.
The crystal will connect at C port at pins 6-7.
I use codevision and i want to ask this will be the settings?As shown at foto?

And then how i will take the time?
What i have to do?
Τhere are no ready library for this?[/img]

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want the time for security system that i want to make.
But i think i dont need rtc.
I want to put a movement sensor and work from 22:00-05:00.
Is this be possible without rtc?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you have to count the time somehow to know it's within that window so I would call that "real time" myself.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That kind of crystal/timer setup just gives you regular interrupts every second. How you use them is up to you. You can place code like this into your interrupt:

sec++;
     if(sec>59){min++;sec=0;};
     if(min>59){min=0;hour++;};
     if(hour>23){hour=0;};

What you do with the data is up to you - you can use the hour variable for your sensor.

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

These 32kHz watch crystals are only for battery powered applications. If your design is not battery powered then do not bother - use a regular timer with a regular quartz because using asynchronous peripherals is tricky.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your design is battery powered then the mega8535 is the possibly one of the very worst AVRs to choose to use. Do a Google search on the word "Picopower".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
These 32kHz watch crystals are only for battery powered applications.
Why do you say that? Surely the crystal does not care what the power source is.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Watch crystals DO care what the drive level is. When over driven, the frequency shifts and, if too high, can even damage the crystal.

Crystal manufacturers have recommendations for using the crystals with 5V logic. Often, a single resistor in series between the oscillator output pin and the capacitor+crystal node will do it. Other times, they suggest a voltage divider to get the drive amplitude down.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Why do you say that?

Because in any design that does not care about energy consumption it is much easier to run everything with one clock than to have two separate clock domains. You will not gain anything using two clock domains in such design. For example imagine debugging AVR with RTC running at 32kHz and F_CPU = RC running at 10MHz+-5%. The events that were supposed to take 1s will sometimes last 9.5e6 and sometimes 10.5e6 clocks. That is much more challenging than when IRQs always have constant and predictable load on CPU. IMHO.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is usually desirable for security oriented equipment to be self contained, and be able to run on backup battery supply for hours or days.

In that case, energy consumption is important.

Asynchronous clocks are a bit tricky, but the issues are mostly concerned with how to handle sleep modes other than IDLE. If you don't use a 'deeper' mode, there is no problem.

ATMEGA8535 is a current hog, compared to picopower devices. At 16 Mhz and 5 volts you're looking at 20 mA active mode current.

Assuming a battery capacity of 1 AH, it could run over a weekend on battery power without sleeping.

Slept in IDLE mode, that period could be extended up to 4 days.

Once you have the hardware set up, the next question is how to use it. As you said, there is no 'standard' time library, which has been a source of frustration for many.

If you really don't care about anything other than knowing when the time is between the hours of 10 PM and 5 AM, its pretty easy.

Configure Timer/Counter 2 to overflow at a 1 Hz rate (set prescaler to 128). Use the overflow interrupt to increment a counter, and mod by 86400 (the number of seconds in 1 day).

Arrange that the count will be zero at midnight, and that the motion detector is connected to an external interrupt. Then, that interrupt need only verify that count is either less than 18000 (5 AM) or greater than 72900 (10 PM).

Somehow you will need to initalize the count. Lets say you do this by pushing a button connected to interrupt 1, at 10 AM on the dot.

With all the work taking place at interrupt time, all main() needs to do is get the ball rolling, and then sleep... forever.

#include 
#include 
#include 

#define FOREVER 1

volatile long counter;

ISR(TIMER2_OVF_vect){
    counter++;
    counter %= 86400L;
}

ISR(INT1_vect){
    counter = 36000L; // 10 AM
    TIMSK |= 
}

ISR(INT0_vect){

    if( counter > 79200L || counter < 18000L ){
        // sound alarm, turn on lights, etc
    }
}

int main(){

    // configure TC2 
    ASSR = 1 << AS2; // enable asynch clock from 32768 Hz xtal
    TCCR2 = (1 << CS22) | (1 << CS20); // prescale by 128
    TIFR |= 1 << TOV2; // clear T2 overflow flag

    // enable interrupts
    TIMSK =  1 << TOIE2;
    GICR =  ( 1 << INT0 ) | ( 1 << INT1 );

    // set IDLE sleep mode
    set_sleep_mode(0);
    sleep_enable);

    // loop forever
    do{
        sleep_cpu();
    } while(FOREVER);

    return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Without a very accurate clock source or at least very accurate 32 kHz crystal and accurate handling of it, you'll lose time fairly quickly.

Assuming you want this to be running every night over a long period of time with accurate timestamps, then you definitely need an accurate source. You can get ready-made RTC chips that are temperature/voltage compensated and even then can still lose several seconds per month.

With an approximate clock, you could be quite out of sync fairly quickly.

Another option might be somehow syncing it with a known clock source, be that manual verification/adjustment periodically or something automatic, but that would probably require much more effort than buying an RTC built for the job. Plus then you can get the date taken care of for you as well.

I know the post is based on your desire to create an RTC out of the AVR, and if that's your primary purpose, then the help above is what you need, but if your primary purpose is to get an accurate clock source, then you might consider an RTC. If you can get samples from Maxim for free there's quite a few to choose.

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It does not really matter if you use a 32kHz watch crystal or a regular HF crystal. Both will keep reasonable time for months. The watch crystal will probably be slightly more accurate.

There is no need to have two crystals. One will work fine. e.g. ASSR or HF crystal.

Since you have a mega8535, there is little point in worrying about power consumption. If you had a PicoPower chip, you can sleep all day with just the watch crystal on the ASSR.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Watch crystals DO care what the drive level is.
But not the >>source<< of that drive level.
Quote:
For example imagine debugging AVR with RTC running at 32kHz and F_CPU = RC running at 10MHz+-5%. The events that were supposed to take 1s will sometimes last 9.5e6 and sometimes 10.5e6 clocks.
But isn't the whole point of having the RTC interrupts to drive things that need particular timing while the rest of the application that does not need accurate timing can go on at whatever pace it can get? And wouldn't such debugging problems also happen in any application that receive input from outside sources? I think that you are simply coming up with some contrived example where it might make a difference, then using it to justify never doing it.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The 32k clock is convenient, but you could do the same with timer interrupts and the system clock. With some calibration, you could get a reasonably accurate clock. Either way, without tcvc oscillator, you'll need some calibration if you need accuracy.

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
But isn't the whole point of having the RTC interrupts..

I simply underline there is little point playing with RTC when you can clock whole cpu with quartz. Then everything is accurate, not only clock but also UART, timers, adc sampling and banging with pins.
The expected performance (accuracy) does not depend on the frequency but specified parameters so it does not matter if quartz is 32kHz or 16MHz.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks all of you for the replies!
i tried some code with 8Mhz crystal and delays
but its not accurate.
it worked for 2 hours and lost 10 seconds.
i will try with the 8535 timers but i dont think i will make it!!!
Probably i will buy the Ds1307 to make a correct rtc!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Probably i will buy the Ds1307 to make a correct rtc!!!
It will be just as inaccurate as anything else!!! :-)

The accuracy depends on the clock, get an accurate clock and then you can use anything. (ie DS32KHz)

Look at the DS3231 (or DS3232), the first few lines of the datasheet will tell you the reason why you want to use it.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brutte wrote:
so it does not matter if quartz is 32kHz or 16MHz.

It's true that the clock cannot be more accurate than the quartz, but you won't be able to execute many instructions when the chip is clocked at 32KHz :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should be able to make it work at 8MHz with some calibration. It'll take some time and some testing, but essentially just set an interrupt every x cycles and increment a counter during that period. Whenever the counter hits a particular value, count that as 1 second.

Start with, for example, kicking the timer interrupt every 32000 cycles. This should give you ~ 250 hits per second. Each interrupt, increment a static counter. Once that counter hits 250, increment the 'seconds' value and handle whatever further time related things are needed.

Then run the test for a few hours and check whether the time is still accurate. If the clock is running fast, increment the counter maximum a few. If it's running slow, decrement it a few. You can play with the various variables to change the granularity and such until you get as much accuracy as you need.

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As John said, though, if you need accuracy, check out RTCs with built in TCXOs. They'll cost you $10 or so if you can't get them to send you a sample, but will easily save you that in calibration time and effort. Plus that way you'll be able to reserve all your clock cycles for whatever else you're doing without needed to worry about keeping the time-keep interrupts working in real time.

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
It's true that the clock cannot be more accurate than the quartz, but you won't be able to execute many instructions when the chip is clocked at 32KHz
The timer is run at 32kHz, not the whole chip.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Problem with ds1307 and atmega8535!!!
i make the connections 1,2 crystal 32.768
3,4 ground i dont use batery
5,6 SDA SCL (PINC.0 SDA, PINC.1 SCL)
7 ground and 8 vcc.
here the code

#include 
#include 
#include 
// I2C Bus functions
#include 
#include 
// DS1307 Real Time Clock functions
#include 

// Alphanumeric LCD functions
#include 

// Declare your global variables here

void main(void)
{
// Declare your local variables here
 unsigned char hour, m, sec, day, month, year, buff[17];
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P 
PORTC=0xFF;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

i2c_init();

// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTA Bit 0
// RD - PORTA Bit 1
// EN - PORTA Bit 2
// D4 - PORTA Bit 4
// D5 - PORTA Bit 5
// D6 - PORTA Bit 6
// D7 - PORTA Bit 7
// Characters/line: 16
lcd_init(16);
rtc_set_time(11,35,00); 
rtc_set_date(10,3,13); 
while (1)
      {
      // Place your code here
rtc_get_time(&hour,&m,&sec); 
rtc_get_date(&day,&month,&year);
sprintf(buff, "%02d:%02d:%02d", hour,m,sec);
lcd_gotoxy(0, 0);       // top LHC
lcd_puts(buff);          // write the formatted string
lcd_gotoxy(0,1);
sprintf(buff,"%02d:%02d:%02d ",day,month,year); 
lcd_puts(buff);
delay_ms(500);
lcd_clear();
      }
}

The result is 165:165:165
and other symbols...
Anyone help?
at codevision i make this choice

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I enable pull up resistors as you see and i have two
10k resistors across SDA , SCL and 5 volt.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to use the TWI dialogue for hardware I2C, you have selected the bit banged mode.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
The timer is run at 32kHz, not the whole chip.

I would hope so, but that cannot be the case when the CPU clock and timer are synchronous as was proposed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I enable TWI dialogue but with this code i have lot errors...
The crystal of the chip is 8mhz.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
i have lot errors...
Then post the errors, we can't see it from here. :-) of course if there are other issue with the code selecting the hadware TWI will not solve those.

Have you selected the correct chip etc. from the menu?

Quote:
The result is 165:165:165
Is this for the time? Does it change indicating that the clock is running? If not then you need to get the clock to run by writing to the clock halt bit CH in the seconds register (I think you need to clear it, long time since I used the chip).

And finally the number in the registers are in BCD, I have used this code with AS4 (ie %02x instead of %02d)

printf_P( PSTR( "Off time: %02x:%02x\n\n\r"),hh, mm);  	//hh and mm in BCD hex

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly