Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ATXMEGA32A4U Real clock

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
matjazs

Bascom Member



Joined: 08 Nov 2016
Posts: 109

PostPosted: Sun Jul 20, 2025 12:57 pm    Post subject: ATXMEGA32A4U Real clock Reply with quote

Hello.
I'm trying to configure real time on my ATXMEGA32A4U. It's not working.

It's delayed by about 3 seconds every minute.

I have:
ATXMEGA32A4U
external 16MHz crystal oscillator and configured internal 32KHz oscillator.
Any idea what's wrong?

This is my example.
Code:
'*******************************************************************************
' File Name:       CLK_XM32-TST.bas
' Hardware PCB:    GSM800L-PLC
' Date:            2025-07
' Controller:      ATXMEGA32A3U
' LCD:             128x64 - glcd-ST7567.lib"
' WiFi: ESP8266 Firmware "guio_esp8266.ino"
'
' GUI-O Ver v.1.0.101  2025-07
'*******************************************************************************
$regfile = "xm32a4udef.dat"
$crystal = 32000000
$hwstack = 150
$swstack = 150
$framesize = 200


Config Watchdog = 8000
Start Watchdog
Reset Watchdog

'-Oscilator --------------------------------------------------------------------
Config Osc = Disabled , Pllosc = Enabled , Extosc = Enabled , 32khzosc = Enabled , Range = 12mhz_16mhz , Xosc_sel_startup = Xtal_16kclk , Pllsource = Extclock , Plldiv2 = Disabled , Pllmul = 2
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1

Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled

Config Clock = Soft , Rtc = 1khz_int32khz_ulp , Gosub = Sectic       ' we select the internal 1 KHz clock from the 32KHz internal oscillator
Config Date = Dmy , Separator = -

'-------------------------------------------------------------------------------
Config Submode = New

'-EEPROM XMEGA------------------------------------------------------------------
Config Eeprom = Mapped
'-------------------------------------------------------------------------------
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Com3 = 57600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Com5 = 57600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

'--Open all UARTS---------------------------------------------------------------
Open "COM1:" For Binary As #1                               'Bluetooth HC06
Open "COM3:" For Binary As #3                               'GSM modem 800L
Open "COM5:" For Binary As #5                               'WiFi  ESP8266

Config Portb.3 = Output
Config Print0 = Portb.3 , Mode = Set

Config Serialin = Buffered , Size = 200
Config Serialin2 = Buffered , Size = 255
Config Serialin4 = Buffered , Size = 200
'-PIN---------------------------------------------------------------------------
'-------------------------------------------------------------------------------
Config Porta.7 = Output                                     'LED Display
Ledd Alias Porta.7
Set Ledd

'-LCD ST-7567-------------------------------------------------------------------
$lib "glcd-ST7567.lib"
Config Vport0 = A , Vport2 = C
Config Graphlcd = 128x64sed , Si = Port2.4 , Sclk = Port0.6 , A0 = Port2.5 , Rst = Port2.7 , Cs1 = Port2.6       'Veliki

Const Negative_lcd = 0
Const Mirror_y = 1
Const Mirror_x = 0
Dim N As Byte
'N = 35                                                      'mali
N = 50                                                      'Veliki
Cls
Initlcd
Lcdcontrast N

'------------------------------------------------------------------------------
Time$ = "14:00:00"

Date$ = "20-07-25"
Enable Interrupts

'------------------------------------------------------------------------------
Main:
Do
Reset Watchdog

Loop

End
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
Sectic:
Reset Watchdog

    Setfont Font5x5
    Lcdat 7 , 1 , Time$
    Lcdat 7 , 58 , Date$

Return

'------------------------------------------------------------------------------
'-GLCD-------------------------------------------------------------------------
$include "font5x5.font"
$include "font5x12.font"
'------------------------------------------------------------------------------


(BASCOM-AVR version : 2.0.8.7 )
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 630
Location: Cleveland, OH

usa.gif
PostPosted: Sun Jul 20, 2025 5:42 pm    Post subject: Reply with quote

I don’t know what the issue is, but I have a thought or two that you could try.

For initial testing remove all of the code except for the Clocks, Interrupts, and Display.

Remove all of the USART (serial port) code.
Remove the Open… and the Config Serialin…
REMOVE the Watchdog code
Remove the EEPROM code.

If you have a floating input on one of the serial input pins it could be continually interrupting.
The Sectic uses the Lo ISR priority.
The ISR driven, buffered, serial input uses the MED priority, so it could be continually interrupting the program, and not allowing the Sectic to run as you think it should.

Make sure that none of your serial communications input pins are floating, (not connected to anything).

Although it is not the cause for your problem, you could make the Sectic ISR much shorter if you simply set a flag in the routine, and then actually update the GLCD in the Main Loop, (and reset the flag). Since you do not change the Font being used, you also can call the SetFont once, and not every time you update the GLCD.

Once the clock is working, then add back your other code, one part at a time.

You could also flash an LED at one flash per second, within your Main Loop, to make sure that the serial communications are not tying up the micro's processing time.
The Sectic is very short, and an LED that flashes using Waitms to flash on and off should flash at about 1 / sec.
If the processor is continually running ISRs, then the Main Loop and Lo priority ISRs don't get to run very much processor time, and the LED will also flash very slowly.
(See Config Priority and the Roundrobin mode, to allow Sectic to fire, (but that isn't the best way to fix this problem.))


JC
Back to top
View user's profile Visit poster's website
matjazs

Bascom Member



Joined: 08 Nov 2016
Posts: 109

PostPosted: Sun Jul 20, 2025 6:47 pm    Post subject: Reply with quote

JC I changed it as you suggested, but nothing changed.

I think, there is something wrong with the parameter settings.

Code:
Config Osc = Disabled , Pllosc = Enabled , Extosc = Enabled , 32khzosc = Enabled , Range = 12mhz_16mhz , Xosc_sel_startup = Xtal_16kclk , Pllsource = Extclock , Plldiv2 = Disabled , Pllmul = 2
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1

Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled

Config Clock = Soft , Rtc = 1khz_int32khz_ulp , Gosub = Sectic  
 




Code:
'*******************************************************************************
' File Name:       CLK_XM32-TST.bas
' Hardware PCB:    GSM800L-PLC
' Date:            2025-07
' Controller:      ATXMEGA32A3U
' LCD:             128x64 - glcd-ST7567.lib"
' WiFi: ESP8266 Firmware "guio_esp8266.ino"
'
' GUI-O Ver v.11.07  2021-08
'*******************************************************************************
$regfile = "xm32a4udef.dat"
$crystal = 32000000
$hwstack = 150
$swstack = 150
$framesize = 200


'Config Watchdog = 8000
'Start Watchdog
'Reset Watchdog

'-Oscilator --------------------------------------------------------------------
Config Osc = Disabled , Pllosc = Enabled , Extosc = Enabled , 32khzosc = Enabled , Range = 12mhz_16mhz , Xosc_sel_startup = Xtal_16kclk , Pllsource = Extclock , Plldiv2 = Disabled , Pllmul = 2
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1

Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled

Config Clock = Soft , Rtc = 1khz_int32khz_ulp , Gosub = Sectic       ' we select the internal 1 KHz clock from the 32KHz internal oscillator
Config Date = Dmy , Separator = -

'-------------------------------------------------------------------------------
Config Submode = New

'-EEPROM XMEGA------------------------------------------------------------------
'Config Eeprom = Mapped
'-------------------------------------------------------------------------------
'Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
'Config Com3 = 57600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
'Config Com5 = 57600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

'--Open all UARTS---------------------------------------------------------------
'Open "COM1:" For Binary As #1                               'Bluetooth HC06
'Open "COM3:" For Binary As #3                               'GSM modem 800L
'Open "COM5:" For Binary As #5                               'WiFi  ESP8266

'Config Portb.3 = Output
'Config Print0 = Portb.3 , Mode = Set

'Config Serialin = Buffered , Size = 200
'Config Serialin2 = Buffered , Size = 255
'Config Serialin4 = Buffered , Size = 200
'-PIN---------------------------------------------------------------------------
'-------------------------------------------------------------------------------
Config Porta.7 = Output                                     'LED Display
Ledd Alias Porta.7
Set Ledd

'-LCD ST-7567-------------------------------------------------------------------
$lib "glcd-ST7567.lib"
Config Vport0 = A , Vport2 = C
Config Graphlcd = 128x64sed , Si = Port2.4 , Sclk = Port0.6 , A0 = Port2.5 , Rst = Port2.7 , Cs1 = Port2.6       'Veliki

Const Negative_lcd = 0
Const Mirror_y = 1
Const Mirror_x = 0
Dim N As Byte
'N = 35                                                      'mali
N = 50                                                      'Veliki
Cls
Initlcd
Lcdcontrast N
'------------------------------------------------------------------------------
'Reset Watchdog
'Wait 3

'------------------------------------------------------------------------------
Time$ = "19:00:00"
Date$ = "20-07-25"
Enable Interrupts
Setfont Font5x5
'------------------------------------------------------------------------------
Main:
Do
'Reset Watchdog
    Lcdat 7 , 1 , Time$
    Lcdat 7 , 58 , Date$
Loop

End
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
Sectic:
'Reset Watchdog

    'Setfont Font5x5
    'Lcdat 7 , 1 , Time$
    'Lcdat 7 , 58 , Date$

Return

'------------------------------------------------------------------------------
'-GLCD-------------------------------------------------------------------------
$include "font5x5.font"
$include "font5x12.font"
'------------------------------------------------------------------------------
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2166

netherlands.gif
PostPosted: Sun Jul 20, 2025 7:48 pm    Post subject: Reply with quote

Start with the Xmega sample provided by Mscelec, see if this works.
_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2338

blank.gif
PostPosted: Tue Jul 22, 2025 9:55 am    Post subject: Re: ATXMEGA32A4U Real clock Reply with quote

matjazs wrote:
Code:
Config Clock = Soft , Rtc = 1khz_int32khz_ulp , Gosub = Sectic

From the help:
Quote:
The internal 32 kHz Ultra Low Power (ULP) is a very low power clock source, and it is not designed for high accuracy.

Try the option:
Code:
Rtc = 1KHZ_INT32KHZ_RCOSC

The following Microchip document talks about 1% accuracy for the 32kHzRC-oscillator, which - 14 minutes per day - is rather useless for a proper time-keeping device:
https://onlinedocs.microchip.com/oxy/GUID-E67844AC-6EF1-4149-B445-44B52E1F1DC2-en-US-2/GUID-0C5D02AE-E09D-4DB5-89F7-1448DD1C7F3E.html?hl=rc32kcal

Anyway, 5% as the reported 3 out of 60 seconds seems a bit much, the 32kHzRC can be calibrated via OSC_RC32KCAL, however a simple external 32768Hz crystal will be more accurate.

Normally calibration is done already at startup, but if you're curious you can compare the content of OSC_RC32KCAL against the value in the signature row.
See:
Quote:
calibration byte from the signature row

from:
https://ww1.microchip.com/downloads/en/Appnotes/XMEGA-Internal-RC-Oscillator-Calibration-00002644A.pdf
Back to top
View user's profile
matjazs

Bascom Member



Joined: 08 Nov 2016
Posts: 109

PostPosted: Tue Jul 22, 2025 5:36 pm    Post subject: Reply with quote

Quote:
MWS

Try the option:
Code:
Code:
Rtc = 1KHZ_INT32KHZ_RCOSC


Thank you. Now is 1 second per minute delay.
Although it is not exact, it is better.

The time in my application is not that important, but I would like to be as accurate as possible on the LCD screen.
There is no room on my PCB for e.g. DS2331.

Every time I connect to the device (GUI-O), I set the time to the exact value.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2338

blank.gif
PostPosted: Tue Jul 22, 2025 8:46 pm    Post subject: Reply with quote

matjazs wrote:
Thank you. Now is 1 second per minute delay.
Although it is not exact, it is better.

As already said, alter (increase) OSC_RC32KCAL.
You can look up this register in the processor's Dat-file.
OSC_RC32KCAL is a 8bit-register, it should work if your code in startup looks like that:
Code:
Dim RCCal As Byte
RCCal = OSC_RC32KCAL
RCCal = RCCal + 10
OSC_RC32KCAL = RCCal

May work also:
Code:
OSC_RC32KCAL = OSC_RC32KCAL + 10
Back to top
View user's profile
matjazs

Bascom Member



Joined: 08 Nov 2016
Posts: 109

PostPosted: Fri Jul 25, 2025 11:48 am    Post subject: Reply with quote

Quote:

MWS
May work also:

Code:
OSC_RC32KCAL = OSC_RC32KCAL + 10


Thank you. I changed this on:

Code:
OSC_RC32KCAL = OSC_RC32KCAL + 7


I think 10 is too fast.
Back to top
View user's profile
matjazs

Bascom Member



Joined: 08 Nov 2016
Posts: 109

PostPosted: Fri Jul 25, 2025 11:52 am    Post subject: Reply with quote

Quote:

MWS
May work also:

Code:
OSC_RC32KCAL = OSC_RC32KCAL + 10


Thank you. I changed this on:

Code:
OSC_RC32KCAL = OSC_RC32KCAL + 7


I think 10 is too fast.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2338

blank.gif
PostPosted: Fri Jul 25, 2025 4:48 pm    Post subject: Reply with quote

matjazs wrote:
Thank you. I changed this on:

Code:
OSC_RC32KCAL = OSC_RC32KCAL + 7


I think 10 is too fast.

Sure, it was only an example value.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum