Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

XMEGA crystal 32 KHz is not working for RTC
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Triplett

Bascom Member



Joined: 28 Feb 2010
Posts: 52
Location: Maryland

usa.gif
PostPosted: Tue Mar 14, 2017 11:33 am    Post subject: XMEGA crystal 32 KHz is not working for RTC Reply with quote

Does anyone know why the real time clock is not working?

The code below works if either of the internal 32 KHz oscillators is selected.
I hope to use the 32 KHz crystal. Do I need to insert a bitwait somewhere?

Code:

' The XM32A4U has a 16MHz crystal connected to XTAL1 and XTAL2.
' A 32Khz crystal is connected to TOSC1 and TOSC2.

$regfile = "xm32A4Udef.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 40
$framesize = 40

' Set up a 16 MHz system using a 16 MHz external crystal.
Config OSC = Enabled, _
EXTOSC = Enabled, _
PLLOSC = Enabled, _
RANGE = 12MHZ_16MHZ, _
STARTUP = XTAL_16KCLK, _
PLLSOURCE = Extclock, _
PLLMUL = 1
Config Sysclock = Pll

Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

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

' The following clocks should work 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 PortE.1 = Output ' LED

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

Enable Interrupts

Do
Loop

Sectic:
Toggle PortE.1 ' Toggle LED.
Print Time$
Return
 


(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
Meister

Bascom Member



Joined: 27 May 2010
Posts: 314

PostPosted: Tue Mar 14, 2017 6:17 pm    Post subject: Reply with quote

Could you post a code that works on internal oscillators and a code that doesn't work? (Maybe with a conditional compilation flag?)
I would test it on an XMega128A1 Xplained board that has the 32khz crystal on-board?
Back to top
View user's profile
Triplett

Bascom Member



Joined: 28 Feb 2010
Posts: 52
Location: Maryland

usa.gif
PostPosted: Tue Mar 14, 2017 10:34 pm    Post subject: Reply with quote

If curious to run it on a machine with no crystal, replace [Rtc = 1KHZ_32KHZ_CRYSTOSC] with [Rtc = 1KHZ_INT32KHZ_RCOSC]. Be sure to change the $regfile declaration to the correct XMEGA that you happen to be using, of course. It runs great with no crystal, but I want the 32 KHz crystal to work.

The board I am using has a 32 KHz crystal. It is a simple breakout board with an ATxmega32A4U. When using the breakout board, I have successfully activated the 32 KHz crystal as the RTC clock source in C, and it runs fine. It was straightforward. First, set bits in the XOSCCTRL register. Then, enable the oscillator using OSC.CTRL, and then finally do the C equivalent of a "bitwait" to see that the oscillator is ready and stable before moving on. I am wondering how to make these same things happen using the 32 KHz crystal in BASCOM.

It is probably something simple and, if you will excuse the pun, basic.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5024
Location: Holland

blank.gif
PostPosted: Tue Mar 14, 2017 10:50 pm    Post subject: Reply with quote

in your config osc, the 32KHZOSC=enabled is missing.

EDIT : that is for the internal 32 khz osc. so that is not the problem.

_________________
Mark


Last edited by albertsm on Tue Mar 14, 2017 11:22 pm; edited 1 time in total
Back to top
View user's profile Visit poster's website
Meister

Bascom Member



Joined: 27 May 2010
Posts: 314

PostPosted: Tue Mar 14, 2017 10:57 pm    Post subject: Reply with quote

This does not work:
Code:
Config Osc = Enabled , _
Extosc = Disabled , _
PLLOSC = Enabled, _
RANGE = 12MHZ_16MHZ, _
STARTUP = XTAL_16KCLK, _
Pllsource = Extclock , _
Pllmul = 1 , _
32khzosc = Enabled' <--------------------------Added acc. to Mark
Config Sysclock = Pll
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Clock = Soft , Rtc = 1khz_32khz_crystosc , Gosub = Sectic
 


This works (blinking LED):
Code:
Config Osc = Enabled , Pllosc = Enabled , Pllmul = 16
'Config OSC = Enabled, _
'Extosc = Disabled , _
'PLLOSC = Enabled, _
'RANGE = 12MHZ_16MHZ, _
'STARTUP = XTAL_16KCLK, _
'PLLSOURCE = Extclock, _
'PLLMUL = 1
Config Sysclock = Pll
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Clock = Soft , Rtc = 1khz_32khz_crystosc , Gosub = Sectic
Code:
 
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5024
Location: Holland

blank.gif
PostPosted: Tue Mar 14, 2017 11:28 pm    Post subject: Reply with quote

i checked the PDF and all needed for the RTC to run is done by the selection.
Clk_rtcctrl is set, and Rtc_status.0 is checked for busy. after that the other regs are written.

so since Meister demos that it works, the problem is most likely not that the RTC does not work, but that the whole program will not work due to osc. config.

_________________
Mark
Back to top
View user's profile Visit poster's website
Triplett

Bascom Member



Joined: 28 Feb 2010
Posts: 52
Location: Maryland

usa.gif
PostPosted: Wed Mar 15, 2017 12:05 am    Post subject: Reply with quote

Very strange.

Meister's code doesn't work for me. Are we using the same processor? Mine is the ATXMEGA32A4U.
Could the 32 KHz external crystal work differently from one xmega to another?

Let's look at a simpler test case.

Scrap the code that also enables the external 16 MHz crystal oscillator.
Just run the processor on the default 2 MHz system clock.
Continue trying to make the RTC clock work using the 32 KHz crystal.

Code:

$regfile = "xm32A4Udef.dat"
$crystal = 2000000
$hwstack = 64
$swstack = 40
$framesize = 40

Config Osc = Enabled, 32KHzosc = Disabled ' Disable the internal 32 KHz RC oscillator (the default).
Config Sysclock = 2MHz
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

'Config Clock = Soft , Rtc = 1khz_32khz_crystosc , Gosub = Sectic ' 32 KHz crystal oscillator for RTC (doesn't work)
Config Clock = Soft , Rtc = 1KHZ_INT32KHZ_RCOSC , Gosub = Sectic ' 32 KHz RC oscillator for RTC (does work)

Config PortE.1 = Output ' LED
Config Priority = Static , Vector = Application , Lo = Enabled
Enable Interrupts

Do
Loop

Sectic:
Toggle PortE.1 ' Toggle LED.
Print Time$
Return
 


When I use the commented out line for Config Clock, the 32 KHz crystal oscillator does not work for RTC.
When I use the alternate line for Config Clock using the 32 KHz RC oscillator, it works OK for the RTC.

Output on COM1 shows HH:MM:SS time with values incremented each second.
Back to top
View user's profile
enniom

Bascom Member



Joined: 20 Oct 2009
Posts: 472

PostPosted: Wed Mar 15, 2017 1:51 am    Post subject: Reply with quote

Quote:
Table 7-4. RTC Clock Source

RTCSRC[2:0] Group Configuration Description

000 ULP 1 kHz from internal 32 kHz ULP
001 TOSC 1.024 kHz from 32.768 kHz Crystal Oscillator on TOSC
010 RCOSC 1.024 kHz from internal 32.768 kHz RC Oscillator
011 - Reserved
100 - Reserved
101 TOSC32 32.768 kHz from 32.768 kHz Crystal Oscillator on TOSC
110 - Reserved
111 - Reserved


The XMEGA A Manual shows the options for configuring the RTC. I have not researched whether all XMEGA have all these capabilities, but I have successfully used this config statement that uses the Ultra Low Power Internal Oscillator:
Code:
Config Clock = Soft , Rtc = 1khz_int32khz_ulp , Gosub = Sectic       'select internal 1 KHz clock from the 32KHz ULP internal oscillator
 


This config also works on the XMEGA E Series.

The ULP consumes very little power ... so there must be a compelling reason to use on external crystal/oscillator.

E
Back to top
View user's profile
Triplett

Bascom Member



Joined: 28 Feb 2010
Posts: 52
Location: Maryland

usa.gif
PostPosted: Wed Mar 15, 2017 3:26 am    Post subject: Reply with quote

We have a low-power application where nothing less than quartz timing accuracy will be sufficient. In the application, there will be conditions when the system will need to track time that has passed during a reduced power state where power consumption is kept as low as can be accomplished while tracking the elapsed time with quartz accuracy. Of course, this requirement can be met using an external low-power RTC that uses a watch crystal. It looks as if the function can be supported by the xmegas directly, so there should be no need to include an extra RTC module - if I can get it working.

For most users, the requirements would be different. It would be sufficient to use the internal RC oscillator for driving the RTC. Ours is not that case.

So, the mystery remains. In BASCOM, how is it possible to have the ATxmega32A4U RTC take its timing reference using the 32 KHz external crystal? I know it would be possible to resort to directly manipulating configuration registers, but I would prefer to avoid that, and apply BASCOM statements, if possible. I have been reading the data sheets. I can start experimenting with stuffing values into registers, and reading status bits. I am just not sure how to do that in the most elegant way using BASCOM. Any adult supervision would be greatly appreciated.

This is a bit of a tangent, but did anyone notice that in the simplified example, when I had disabled the internal 32 KHz RC oscillator, it still worked?
Back to top
View user's profile
Meister

Bascom Member



Joined: 27 May 2010
Posts: 314

PostPosted: Wed Mar 15, 2017 9:26 am    Post subject: Reply with quote

@Triplett:
Quote:
Are we using the same processor? Mine is the ATXMEGA32A4U.

As I told in my first post, I have an XMega128A1U Xplained board. So one could see if there might be a different outcome with the ATxmega32A4U that Triplett is using. Sorry that I did not include the CPU definition in my code snippets.
Best regards, Meister
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5024
Location: Holland

blank.gif
PostPosted: Wed Mar 15, 2017 9:59 am    Post subject: Reply with quote

within series (A for example) all hardware is the same. That is why atmel/microchip has 1 manual for the series where everything is explained. And a generic datasheet for the chip.
The errata section for the 32U4 reveals some problems. just check with your revision.

The data sheet also shows that there are even 2 new modes which i did not noticed before. I will add those :
32.768 kHz from internal 32.768 kHz RC oscillator
External Clock from TOSC1


Did you add caps to the xtal?

beside problem with revisions of chips, it should not matter that Meister used a different chip. it is one from the same serie.
but exactly what kind of hardware do you use?

_________________
Mark
Back to top
View user's profile Visit poster's website
Triplett

Bascom Member



Joined: 28 Feb 2010
Posts: 52
Location: Maryland

usa.gif
PostPosted: Wed Mar 15, 2017 12:28 pm    Post subject: Reply with quote

I do not understand caps. In the Config Clock, is the [Rtc = 1khz_32khz_crystosc] case sensitive?

The breakout board is from MattairTech, the MT-DB-X4. It is a good board, gold plated.

The datasheet is here:

http://www.mattairtech.com/docs/MT-DB-X4/MT-DB-X4_User_Guide.pdf

It would be easiest for us if I mark where all the jumpers are set.
I will attach a JPG of Justin's schematic with all jumper settings marked in green.
If a jumper has no green marking, it is open.
Back to top
View user's profile
Triplett

Bascom Member



Joined: 28 Feb 2010
Posts: 52
Location: Maryland

usa.gif
PostPosted: Wed Mar 15, 2017 12:41 pm    Post subject: Reply with quote

DUH

Capacitors are on crystal.

Shocked
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1803

blank.gif
PostPosted: Wed Mar 15, 2017 12:47 pm    Post subject: Reply with quote

Triplett wrote:
Code:

$regfile = "xm32A4Udef.dat"
$crystal = 2000000
$hwstack = 64
$swstack = 40
$framesize = 40

Config Osc = Enabled, 32KHzosc = Disabled ' Disable the internal 32 KHz RC oscillator (the default).
Config Sysclock = 2MHz
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8

Config Clock = Soft , Rtc = 1khz_32khz_crystosc , Gosub = Sectic ' 32 KHz crystal oscillator for RTC (doesn't work)

Config PortE.1 = Output ' LED
Config Priority = Static , Vector = Application , Lo = Enabled
Enable Interrupts

Do
Loop

Sectic:
Toggle PortE.1 ' Toggle LED.
Print Time$
Return
 

I suggest you check your hardware, including the used 32768Hz crystals, maybe use some capacitors like in the data sheet.
If available, check with an oscilloscope if there's oscillation on the TOSC-pins.

Doing a simulation in AVR-Studio4 shows with above code:
- clock source RTC is running with configured crystal oscillator on TOSC
- RTC is connected to above clock source, overflow occurs at &h03FF with low level enabled
- manually setting OVFIF executes the appropriate vector, then Bascom's internal code and then Sectic

Everything at code level is in good order, thus it has to be a hardware fault.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5024
Location: Holland

blank.gif
PostPosted: Wed Mar 15, 2017 12:54 pm    Post subject: Reply with quote

caps is short for capacitor. i mean the ones from 6pf
i wonder which C code was able to run this when the jumpers are set the way you indicated?
IMO the jumpers do not route the xtal to the right pins. Have a look in the PDF of the processor.
The xtal pins are PR0 and PR1. you connect only the 16 mhz xtal.
but you must route the 32 KHz xtal to these pins.
I did not read the manual, but i guess there will be some instruction to set the jumpers to the 32 KHz xtal.
Do notice that this micro does not have 2 different osc. but you must make a choice. so either you use the 16 mhz as you have done now with the jumpers in green.
or you select the other one but then it runs on 32 khz.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
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