RTC problem

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Hertz
Posts: 18
Joined: 28 Aug 2007 13:35

RTC problem

#1 Post by Hertz » 13 Oct 2008 11:14

Hi there

I am working with the RTC and the only problem left is why the RTC is not running.

See code further down. I have managed to set the date/time/dayofweek, but when I try to read them, they are the same as when it was set. I'm aware of that the alarm is not set, but shouldn't the RTC be running anyway?

Code: Select all


// Union to access rtcc registers
typedef union tagRTCC {
    struct {
        unsigned char sec;
        unsigned char min;
        unsigned char hour;
        unsigned char dayofweek;
        unsigned char day;
        unsigned char month;
        unsigned char year;
    } components;
    struct {
        unsigned int prt00;
        unsigned int prt01;
        unsigned int prt10;
        unsigned int prt11;
    } ports;
} RTCC;



/* Enable Write's to RTC Configuration Register
 * This cannot be directly done by setting RTCWREN bit in RTCCFG
 * The special unlock sequence would set RTCWREN bit for the first time
 * Once this unlock sequence has been executed, RTCWREN bit can be set or reset directly
 */

void RTCCUnlock()
{
    asm {
        disi #5
        mov #0x55, w7        // write 0x55 and 0xAA to
        mov w7, NVMKEY       // NVMKEY to disable
        mov #0xAA, w8        // write protection
        mov w8, NVMKEY
        BSET RCFGCAL, #13    // set the RTCWREN bit
        nop
        nop
    }
}

void OSCCONWriteEnable() {
    OSCCON = OSCCON;  // Let the linker use the value
    asm {
        disi  #7
        mov   #@OSCCON, W1
        mov   #0x02, W0
        mov   #0x46, W2
        mov   #0x57, W3
        mov.b W2, [W1]
        mov.b W3, [W1]
        mov.b W0, [W1]
    }
}

// Input is binary
// encoding in RTC registers are BCD

void RTC_Set(unsigned char year,
             unsigned char month,
             unsigned char day,
             unsigned char hour,
             unsigned char min,
             unsigned char sec,
             unsigned char dayofweek) {

    RTCC time;

    time.components.year = Dec2Bcd(year);
    time.components.month = Dec2Bcd(month);
    time.components.day = Dec2Bcd(day);
    time.components.hour = Dec2Bcd(hour);
    time.components.min = Dec2Bcd(min);
    time.components.sec = Dec2Bcd(sec);
    time.components.dayofweek = Dec2Bcd(dayofweek);

    OSCCONWriteEnable();
    RTCCUnlock();      // includes RTCWREN = 1

    // Start setting time and date

    RCFGCALbits.RTCEN = 0;	// bit15 - disable RTC

    RCFGCALbits.RTCPTR0 = 1;
    RCFGCALbits.RTCPTR1 = 1;

    RTCVAL = time.ports.prt11;
    RTCVAL = time.ports.prt10;
    RTCVAL = time.ports.prt01;
    RTCVAL = time.ports.prt00;

    RCFGCALbits.RTCEN = 1;	// bit15 enable RTC

  	RCFGCALbits.RTCWREN = 0;	// Lock the RTCC
}


void RTC_Get() {

    RTCC time;

    RCFGCALbits.RTCPTR0 = 1;
    RCFGCALbits.RTCPTR1 = 1;

    time.ports.prt11 = RTCVAL;
    time.ports.prt10 = RTCVAL;
    time.ports.prt01 = RTCVAL;
    time.ports.prt00 = RTCVAL;  // <-- Never changes WHY????

    // Here we should get time one more time to ensure, that there's no roll over
}




void Timer1Int() org 0x1A{// Timer1 address in the interrupt vector table
    IFS0 = IFS0 & 0xFFF7;   // Interrupt flag reset
    RTC_Get();
}

void InitTimer() {

    const unsigned long Sec = 19530;

    // Init the timerlist and flag

    // Init Timer 1
    PR1   = Sec;            // Interrupt period is 10000 clocks
    T1CON = 0x8030;         // Timer1 enabled (internal clock divided by 256)
    IPC0  = IPC0 | 0x1000;  // Priority level is 1
    IEC0  = IEC0 | 0x0008;  // Timer1 interrupt enabled
}



void main(){

    RTC_SET(9,10,13, 11,15,0, 1);

    InitTimer();
    while(1) asm nop;       // Endless loop
}


anton
Posts: 807
Joined: 23 Sep 2004 09:16
Location: South-Africa
Contact:

#2 Post by anton » 13 Oct 2008 11:29

Hi Hertz

I've got the RTC to run, but my code is in Pascal. You can find the topic here
http://www.mikroe.com/forum/viewtopic.php?t=16605 and then compare it to your code.

Anton
Another proud user of LV 24-33A Development System and mikroPascal PRO for dsPIC :)
PortA not working? Add CMCON := 7; PortD not working? Add ADCON1 := 6;
To paste code on the forum, please use the [b] Code [/b] button !! ;)

Hertz
Posts: 18
Joined: 28 Aug 2007 13:35

#3 Post by Hertz » 13 Oct 2008 12:00

Thanks - I have allready used that code in my first version.

I'm wondering if my testboard (LV24-33) do not have a 32 KHz clock crystal - anybody knows???

I raise the question in the hardware section also.

norbie
Posts: 160
Joined: 06 May 2006 22:40
Location: Vista, California

#4 Post by norbie » 21 Nov 2008 22:15

Hi,
I'm wondering if my testboard (LV24-33) do not have a 32 KHz clock crystal - anybody knows???
No the LV24-33 does not have any crystals whatsoever, except for the ICD and programmer.
The crystals are installed on the MCU Card and unless you put a 32KHz Crystal across the appropriate pin with the right capacitor there will be no 32KHz clock.

Cheers
Cheers,
Norbert

You don't need to know the answer, you just need to know where to find it!

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”