News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

32.768khz clock code

Started by RGV250, Jun 23, 2024, 08:41 PM

Previous topic - Next topic

RGV250

Hi,
I have a dev board that has a 32.768khz xtal. I just wondered if there were any examples of a clock code for it. I know for real purposes I would be better off with a clock module but this is just for dev purposes.

Bob

SeanG_65


John Lawton

The oscillator configuration to use that crystal will probably be device specific. What device are you using?

Sorry I don't have any clock code.

John

trastikata

I am not sure if I understand the question. If you intent using the 32.768KHz clock as clock source for the MCU, then you will have to prorate all timing in the codes since Positron doesn't support this clock.

John Lawton

I thought that Bob was trying to simulate a digital clock in software, so the fact that Positron may not support that xtal/osc frequency doesn't matter that much.

J

RGV250

#5
Hi,
Apologies for a badly worded question, John has what I am looking for right. I have a Microstick plus board and and am looking at trying all the options out in Positron. It plugs directly in to the Microstick 2 board so the device could be one of several.
The device will use the internal oscillator and the 32.768khz xtal is connected to SOSCO/SOSCI.

I have just noticed in the datasheet there is RTCC (Real Time Clock & Calendar) which I will need to study.

This looks like what I need, should not be to much of a problem now :-)
https://onlinedocs.microchip.com/pr/GUID-50382BB8-F2BB-4032-8B69-CF704EA78B8A-en-US-1/index.html?GUID-FF91A83E-BA7F-4B21-8A4C-1407360422D5

Bob

RGV250

Hi,
I came across some code I must have got from the old forum dated 2016 which is exactly what I need, just got to test it and post it in case anyone else is interested.
The annoying thing is the PIC16(L)F19197 has Vbat to maintain in a power loss but cannot find a PIC24 that has.

Bob

RGV250

Hi,
I found something from the old forum, it is strange that there is RTCC.inc but no sample for it. If you do need to use the internal clock with 32.768khz xtal here is a demo for PIC24 but should be OK for 18 series.
'****************************************************************
'*  Name    : RTCC_Demo.BAS                                     *
'*  Author  : Bobby Garrett                                     *
'*  Date    : 11/07/2024                                        *
'*  Version : 1.0                                               *
'*  Notes   : 32.768khz xtal connected to SOSCI/SOSCO           *
'*          :                                                   *
'****************************************************************

Device = 24HJ128GP502
Declare Xtal = 79.23

' Configure for internal 7.37MHz oscillator with PLL
' OSC pins are general purpose I/O
'
    Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
    Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
    Config FGS = GWRP_OFF, GCP_OFF
    Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
    Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
    Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
    Config FPOR = FPWRT_PWR128, ALTI2C_OFF
    Config FICD = ICS_PGD1, JTAGEN_OFF
   
PLL_Setup(43, 2, 2, $0300)                   ' Configure the Oscillator to operate the device at 79.23MHz

'------------------------------------------------
' USART1 declares
' Defult settings for Microstick Plus board
    Declare Hserial_Baud = 19200     
         
    Declare HRSOut1_Pin = PORTB.10           ' Select which pin is to be used for TX with USART1   
    PPS_Output(cOut_Pin_RP10, cOut_Fn_U1TX)  ' Make Pin RB10 U1TX
' Not used in this example
    Declare HRSIn1_Pin = PORTB.11            ' Select which pin is to be used for RX with USART1   
    PPS_Input(cIn_Pin_RP11, cIn_Fn_U1RX)     ' Make Pin RB11 U1RX
'------------------------------------------------   

    Include "RTCC_24.inc"                    ' Load the RTCC procedures into the program
'
' Create some variables for the demo
'
    Dim RTCC_Second As Byte
    Dim RTCC_Minute As Byte
    Dim RTCC_Hour As Byte

    Dim RTCC_WeekDay As Byte
    Dim RTCC_Day As Byte
    Dim RTCC_Month As Byte
    Dim RTCC_Year As Byte
   
    Dim Led1 As PORTB.12                   ' For seconds

    RTCC_Init()                            ' Initialise the RTCC peripheral (must be placed after PLL_Setup)

    RTCC_SetTime(10,53, 0)                 ' Set the time
    RTCC_SetDate(11, 6, 24)                ' Set the date (Month starts at 0)
    RTCC_SetWeekDay(4)                     ' Set the week day (Sunday = 0)

    While         
        High Led1   
   
        RTCC_GetTime(RTCC_Hour, RTCC_Minute, RTCC_Second)
        RTCC_GetDate(RTCC_Day, RTCC_Month, RTCC_Year)
        RTCC_WeekDay = RTCC_GetWeekDay()

        HRSOut "Time = "
        If RTCC_Hour < 10 Then HRSOut "0"    'Print preceeding zero.
        HRSOut Dec RTCC_Hour,":"
       
        If RTCC_Minute < 10 Then HRSOut "0"  'Print preceeding zero.
        HRSOut Dec RTCC_Minute, ":"
       
        If RTCC_Second < 10 Then HRSOut "0"  'Print preceeding zero.
        HRSOut Dec RTCC_Second, 13

        HRSOut "Date = ", RTCC_GetDayName(RTCC_WeekDay), " ",_
                          RTCC_GetDateName(RTCC_Day), " ",_
                          RTCC_GetMonthName(RTCC_Month),_
                          " 20", Dec2 RTCC_Year, 13
        DelayMS 512

        Low Led1
        DelayMS 500
    Wend         
             

top204

At that time, I was becoming very disillusioned with the compilers because of the lack of 'any' support from "them", and the lack of any funds for the constant work I was doing on them.

I did create a few demos for the PIC24 RTCC library code I wrote, and below is one of them:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate the Real Time Clock Calendar procedures for PIC24H devices
' This program demonstrates the alarm feature
'
' Written for the Positron16 BASIC compiler by Les Johnson
'
    Device = 24HJ128GP502                   ' Tell the compiler what device to compile for
    Declare Xtal = 79.23                    ' Tell the compiler what frequency the device is operating at (in MHz)

    Declare Hserial_Baud = 115200           ' UART1 baud rate
    Declare Hrsout1_Pin = PORTB.3           ' Select which pin is to be used for TX with USART1

    Include "RTCC_24.inc"                   ' Load the RTCC procedures into the program
'
' Create some variables for the demo
'
    Dim RTCC_Second  As Byte
    Dim RTCC_Minute  As Byte
    Dim RTCC_Hour    As Byte

    Dim RTCC_WeekDay As Byte
    Dim RTCC_Day     As Byte
    Dim RTCC_Month   As Byte
    Dim RTCC_Year    As Byte
'
' Create day values
'
    Symbol cSunday = 0
    Symbol cMonday = 1
    Symbol cTuesday = 2
    Symbol cWednesday = 3
    Symbol cThursday = 4
    Symbol cFriday = 5
    Symbol cSaturday = 6
'
' Create month values
'
    Symbol cJanuary = 0
    Symbol cFebruary = 1
    Symbol cMarch = 2
    Symbol cApril = 3
    Symbol cMay = 4
    Symbol cJune = 5
    Symbol cJuly = 6
    Symbol cAugust = 7
    Symbol cSeptember = 8
    Symbol cOctober = 9
    Symbol cNovember = 10
    Symbol cDecember = 11

'-------------------------------------------------------------------------------------
' The main program starts here
' The RTCC is set with a time and date
' The Alarm is set to go off 10 seconds after the RTCC has started
'
Main:
    Setup()                                 ' Setup the program and peripherals

    RTCC_SetDate(23, cFebruary, 14)         ' Set the date (Day, Month, Year)
    RTCC_SetWeekDay(cSunday)                ' Set the week day
    RTCC_SetTime(14, 0, 0)                  ' Set the time (Hour, Minute, Second)

    RTCC_SetAlarm(cSunday, 23, cFebruary, 14, 0, 9) ' Set the alarm (Weekday, Day, Month, Year, Hour, Minute, Second)

    RTCC_ActivateAlarm(cAlarmMaskHours, 1)  ' Activate the alarm and select an Alarm mask to compare hours, minutes and seconds
'
' Read and transmit the time and date
'
    Do
        RTCC_GetTime(RTCC_Hour, RTCC_Minute, RTCC_Second)
        RTCC_GetDate(RTCC_Day, RTCC_Month, RTCC_Year)
        RTCC_WeekDay = RTCC_GetWeekDay()
        HRSOutLn "Time = ", Dec RTCC_Hour,":", Dec RTCC_Minute, ":",Dec RTCC_Second

        HRSOutLn "Date = ", RTCC_GetDayName(RTCC_WeekDay), " ",_
                            RTCC_GetDateName(RTCC_Day), " ",_
                            RTCC_GetMonthName(RTCC_Month),_
                            " 20", Dec2 RTCC_Year
        DelayMS 1000
    Loop

'-------------------------------------------------------------------------------------
' Setup the program and peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    OSC_PLLDIV(43)                          ' Configure the Oscillator to operate the device at 79.23MHz
    PPS_Output(cOut_Pin_RP3, cOut_Fn_U1TX)  ' Make Pin RB3 U1TX

    RTCC_Init()                             ' Initialise the RTCC peripheral
EndProc

'-------------------------------------------------------------------------------------
' Interrupt handler for the RTCC Alarm
' Input     : None
' Output    : For this demo, a message is sent serially when the interrupt triggers
' Notes     : None
'
Isr RTCCInterrupt
    HRSOut "\r************** Alarm Has Triggered ****************\r"
    IFS3bits_RTCIF = 0                      ' Clear the RTCC flag
EndIsr

'---------------------------------------------------------------------------------------
' Configure for internal 7.37MHz oscillator with PLL on a PIC24HJ128GP502 device
' OSC pins are general purpose I/O
'
    Config FBS = BWRP_WRPROTECT_OFF
    Config FSS = SWRP_WRPROTECT_OFF
    Config FGS = GWRP_OFF
    Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
    Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSECME
    Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
    Config FPOR = FPWRT_PWR128, ALTI2C_OFF
    Config FICD = ICS_PGD1, JTAGEN_OFF

I'm sure I also wrote an RTCC include file for the 18F devices that have the peripheral, but cannot find it on my drives, and the old forum was totally wiped by "you know who". Even thinking the name makes me cringe with anger at my own stupidity for the trust I had in some people!