CONFIG CLOCK

(Difference between revisions)
Jump to: navigation, search
(Created page with "= <span class="f_Header">Action</span> = Configures the timer to be used for the TIME$ and DATE$ variables. <span style="font-family: Arial;"> </span> <span style="fon...")
 
(See also)
Line 248: Line 248:
 
= <span class="f_Header">See also</span> =
 
= <span class="f_Header">See also</span> =
  
[[TIME$]]&nbsp;,&nbsp;[[DATE$]]&nbsp;,&nbsp;[[CONFIG DATE|CONFIG DATE]],&nbsp;[[MEMORY USAGE|Memory usage]],&nbsp;[[DATETIME|Date and Time Routines]]
+
[[TIME$]]&nbsp;,&nbsp;[[DATE$]]&nbsp;,&nbsp;[[CONFIG DATE|CONFIG DATE]],&nbsp;[[Memory_usage|Memory usage]],&nbsp;[[DATETIME|Date and Time Routines]]
  
 
<span style="font-family: Arial;">&nbsp;</span>
 
<span style="font-family: Arial;">&nbsp;</span>

Revision as of 17:23, 21 February 2013

Contents

Action

Configures the timer to be used for the TIME$ and DATE$ variables.

 

 

Syntax

CONFIG CLOCK = soft | USER [, GOSUB = SECTIC] [,RTC=rtc] [,RTC32=rtc32]

 

[,RTC=rtc] [,RTC32=rtc32] is only for ATXMEGA devices.

 

Remarks

Soft

Use SOFT for using the software based clock routines.

You need to add an ENABLE INTERRUPTS statement to your code since the SOFT mode uses the timer in interrupt mode. The timer interrupt is enabled automatic but the global interrupt you need to enable yourself. While the compiler could enable the global interrupt automatic, you would not have control anymore when it is enabled when using multiple interrupts.

In general you enable global interrupts after all interrupts are setup.

 

 

Use USER to write/use your own code in combination with an I2C clock chip for example.

Sectic

This option allows to jump to a user routine with the label sectic.

 

Since the interrupt occurs every second you may handle various tasks in the sectic label. It is important that you use the name SECTIC and that you return with a RETURN statement from this label.

 

The usage of the optional SECTIC routine will use 30 bytes of the hardware stack. This option only works with the SOFT clock mode. It does not work in USER mode. [, GOSUB = SECTIC] is only for SOFT mode.

RTC

This option is only available for processors with an RTC (XMEGA).

This option sets the RTC  clock source.

 

Valid parameters are :

1KHZ_INT32KHZ_ULP      1 kHz from internal 32 kHz ULP

1KHZ_32KHZ_CRYSTOSC    1 kHz from 32 kHz Crystal Oscillator on TOSC

1KHZ_INT32KHZ_RCOSC    1 kHz from internal 32 kHz RC Oscillator

32KHZ_32KHZ_CRYSTOSC   32 kHz from 32 kHz Crystal Oscillator on TOSC

 

The 1KHz clocks will load the PER register with 1000-1 and the 32 KHz clock will load PER with a value of 32768-1.

The overflow mode is used and you can use the compare overflow if required.

 

Do not forget to enable the 32 KHz oscillator and the interrupts as shown in the Xmega example.

RTC32

This option is available for few XMEGA chips. You can use it instead of the RTC. In fact when a processor has an RTC32, it does not have an RTC.

You can not use both RTC and RTC32 together.

RTC32 only accepts one value : 1KHZ_32KHZ_CRYSTOSC

This also means that you must use/connect an external 32 KHz crystal.

 

When you use the RTC32, the battery back register VBAT_CTRL is initialized and setup.

 

 

When you use the CONFIG CLOCK directive the compiler will DIM the following variables automatic : _sec , _min , _hour, _day , _month , _year

The variables TIME$ and DATE$ will also be dimensioned. These are special variables since they are treated different. SeeTIME$ and DATE$.

 

The _sec, _min and other internal variables can be changed by the user too.

But of course changing their values will change the DATE$/TIME$ variables.

 

The compiler also creates an ISR that gets updates once a second. This works for AVR chips which can be asynchronously clocked from the TOSC1/2 pins.

TOSC1 = Timer Oscillator Pin 1

TOSC2 = Timer Oscillator Pin 2

 

For example the Timer/Counter 2 of an ATMEGA16 can be used as a Real Time Counter (RTC). The Timer/Counter 2 will then be asynchronously clocked from the TOSC Pin's. The Timer/Counter 2 can NOT be used for other tasks when configured in asynchronous mode.

 

Notice.jpg
Notice that you need to connect a 32768 Hz crystal in order to use the timer in async mode, the mode that is used for the clock timer. You also need to enable interrupts because of the interrupt service routine.

 

When you choose the USER option, only the internal variables are created (like _sec , _min , _hour....). With the USER option you need to write the clock code yourself (so the USER need to update for example the System Second or Secofday).

 

Config Clock = User                                         'Use USER to write/use your own code
 
You also need to include the following labels:
 
Getdatetime:                                               'needed for Config Clock = User
Return
 
Settime:                                                   'needed for Config Clock = User
Return

 

See the datetime.bas example that shows how you can use a DS1307 clock chip for the date and time generation.

 

With ATXMEGA there are devices with 16-Bit RTC like ATXMEGA128A1 and 32-Bit RTC like ATXMEGA256A3B or ATXMEGA256A3BU.

 

ATXMEGA with 16-Bit RTC:

Can be used with one of the two internal RC oscillator options or external 32.768kHz crystal oscillator
The internal 32 kHz Ultra Low Power (ULP) is a very low power clock source, and it is not designed for high accuracy.
If you want to use the internal 32Khz RC oscillator you need to enable it with config osc

Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled  

 

ATXMEGA with 32-Bit RTC (for example ATXMEGA256A3B or ATXMEGA256A3BU):

An external 32.768kHz crystal oscillator must be used as the clock source
The 32-Bit RTC is combined with a Battery Backup System

 

 

Numeric Values to calculate with Date and Time:

 

SecOfDay: (Type LONG) Seconds elapsed since Midnight. 00:00:00 start with 0 to 85399 at 23:59:59.
SysSec: (Type LONG) Seconds elapsed since begin of century (at 2000-01-01!). 00:00:00 at 2000-01-01 start with 0 to 2147483647 (overflow of LONG-Type) at 2068-01-19 03:14:07
DayOfYear: (Type WORD) Days elapsed since first January of the current year.
First January start with 0 to 364 (365 in a leap year)
SysDay: (Type WORD) Days elapsed since begin of century (at 2000-01-01!). 2000-01-01 starts with 0 to 36524 at 2099-12-31
DayOfWeek: (Type Byte) Days elapsed since Monday of current week. Monday start with 0 to Sunday = 6

 

 

With the numeric type calculations with Time and date are possible. Type 1 (discrete Bytes) and 2 (Strings) can be converted to an according numeric value. Than Seconds (at SecOfDay and SysSec) or Days (at DayOfYear, SysDay), can be added or subtracted. The Result can be converted back.

 

 

 

 

See also

TIME$ , DATE$ , CONFIG DATEMemory usageDate and Time Routines

 

 

 

ASM

The following ASM routines are called from datetime.lib

_soft_clock. This is the ISR that gets called once per second.

 

 

 

Example


'-----------------------------------------------------------------------------------------
'name : megaclock.bas
'copyright : (c) 1995-2005, MCS Electronics
'purpose : shows the new TIME$ and DATE$ reserved variables
'micro : Mega103
'suited for demo : yes
'commercial addon needed : no
'-----------------------------------------------------------------------------------------
 
$regfile = "m103def.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
 
'With the 8535 and timer2 or the Mega103 and TIMER0 you can
'easily implement a clock by attaching a 32768 Hz xtal to the timer
'And of course some BASCOM code
 
'This example is written for the STK300 with M103
Enable Interrupts
 
'[configure LCD]
$lcd = &HC000 'address for E and RS
$lcdrs = &H8000 'address for only E
Config Lcd = 20 * 4 'nice display from bg micro
Config Lcdbus = 4 'we run it in bus mode and I hooked up only db4-db7
Config Lcdmode = Bus 'tell about the bus mode
 
'[now init the clock]
Config Date = Mdy , Separator = / ' ANSI-Format
 
Config Clock = Soft 'this is how simple it is
'The above statement will bind in an ISR so you can not use the TIMER anymore!
'For the M103 in this case it means that TIMER0 can not be used by the user anymore
 
'assign the date to the reserved date$
'The format is MM/DD/YY
Date$ = "11/11/00"
 
'assign the time, format in hh:mm:ss military format(24 hours)
'You may not use 1:2:3 !! adding support for this would mean overhead
'But of course you can alter the library routines used
 
Time$ = "02:20:00"
 
'---------------------------------------------------
 
'clear the LCD display
Cls
 
Do
Home 'cursor home
Lcd Date$ ; " " ; Time$ 'show the date and time
Loop
 
'The clock routine does use the following internal variables:
'_day , _month, _year , _sec, _hour, _min
'These are all bytes. You can assign or use them directly
_day = 1
'For the _year variable only the year is stored, not the century
End
 
Xmega Sample
'----------------------------------------------------------------
' (c) 1995-2010, MCS
' xm128-RTC.bas
' This sample demonstrates the Xmega128A1 RTC
'-----------------------------------------------------------------
 
$regfile = "xm128a1def.dat"
$crystal = 32000000
$hwstack = 64
$swstack = 64
$framesize = 64
 
Config Portb = Output
 
'First Enable The Osc Of Your Choice , make sure to enable 32 KHz clock or use an external 32 KHz clock
Config Osc = Enabled , 32mhzosc = Enabled , 32khzosc = Enabled
' For the CLOCK we use the RTC so make sure the 32 KHZ osc is enabled!!!
 
'configure the systemclock
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
 
Config Com1 = 19200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
 
Open "COM1:" For Binary As #1
 
Config Clock = Soft , Rtc = 1khz_int32khz_ulp ' we select the internal 1 KHz clock from the 32KHz internal oscillator
'the following clocks can be used to clock the RTC
' 1KHZ_INT32KHZ_ULP 1 kHz from internal 32 kHz ULP
' 1KHZ_32KHZ_CRYSTOSC 1 kHz from 32 kHz Crystal Oscillator on TOSC
' 1KHZ_INT32KHZ_RCOSC 1 kHz from internal 32 kHz RC Oscillator
' 32KHZ_32KHZ_CRYSTOSC 32 kHz from 32 kHz Crystal Oscillator on TOSC
 
 
Config Priority = Static , Vector = Application , Lo = Enabled ' the RTC uses LO priority interrupts so these must be enabled !!!
Enable Interrupts ' as usual interrupts must be enabled
 
Do
Print Time$ ' print the time
Waitms 1000
Loop
 
'TO USE THE SECTIC in the sample you must use GOSUB=SECTIC in CONFIG CLOCK !!!
 
Sectic:
Toggle Portb 'optional toggle some leds when using the gosub=sectic option
Return

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language