• AVR Freaks

Hot!PIC24F RTCC module.

Author
TER
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/04/15 03:59:38
  • Location: 0
  • Status: offline
2019/07/11 06:16:36 (permalink)
0

PIC24F RTCC module.

Hello everybody! I'am using pic24fj64gb202. I work with RTCC module. I configure the registers of the module (code below). I use internal oscilator FRC. Everything works as it should, but it works faster. For the test, I set the alarm so that it works after one minute after the power is turned on, the alarm  triggers after 55 seconds. 5 sikundas earlier. Why is it so work, please help who can. THANKS.
 

#include "rtcc.h"
 

unsigned char year=0;
unsigned char month=0;
unsigned char day=0;
int week=0;
unsigned char hour=0;
unsigned char min=0;
unsigned char sec=0;
unsigned char alarm_month=0;
unsigned char alarm_day=0;
int alarm_week=0;
unsigned char alarm_hour=0;
unsigned char alarm_min=0;
unsigned char alarm_sec=0;
extern unsigned char AlarmStopMonth;
extern unsigned char AlarmStopDay;
extern unsigned char AlarmStopHour;
extern unsigned char AlarmStopMin;
 

uint8_t RTCC_DecToBCD (uint8_t value)
{
return (((value / 10)) << 4) | (value % 10);
}
uint8_t RTCC_BCDToDec (uint8_t value)
{
return ((value >> 4) * 10) + (value & 0x0F);
}

void RTCC (void)
{
unsigned long int rtcbuildvalue;
NVMKEY=0x55;
NVMKEY=0xAA;
RCFGCALbits.RTCWREN=1;
RCFGCALbits.RTCPTR0=1;
RCFGCALbits.RTCPTR1=1;
RCFGCALbits.RTCEN=0;

rtcbuildvalue = RTCC_DecToBCD(year);
RTCVAL = rtcbuildvalue; // YEAR

rtcbuildvalue = ((uint16_t)(RTCC_DecToBCD(month)) << 8) | RTCC_DecToBCD(day);
RTCVAL = rtcbuildvalue; // MONTH-1/DAY-1

rtcbuildvalue=((uint16_t)(RTCC_DecToBCD(week)) << 8) | RTCC_DecToBCD(hour);
RTCVAL = rtcbuildvalue; // WEEKDAY/HOURS

rtcbuildvalue = ((uint16_t)(RTCC_DecToBCD(min)) << 8) | RTCC_DecToBCD(sec);
RTCVAL = rtcbuildvalue; // MINUTES/SECONDS

RTCPWCbits.RTCLK=1;
RTCPWCbits.PWCEN=1;
RCFGCALbits.CAL = 0b00000000;//0b01111111;
PMD3bits.RTCCMD=0;
RCFGCALbits.RTCEN=1;
RCFGCALbits.RTCWREN=0;
}

void RTCCALARM(void)
{

unsigned long int alarmbuildvalue;
ALCFGRPTbits.ALRMEN=0;
ALCFGRPTbits.AMASK =0b1001;
ALCFGRPTbits.ALRMPTR0=1;
ALCFGRPTbits.ALRMPTR1=1;

ALRMVAL = 0xFFFF;

alarmbuildvalue = ((uint16_t)(RTCC_DecToBCD(alarm_month)) << 8) | RTCC_DecToBCD(alarm_day);

ALRMVAL = alarmbuildvalue;

alarmbuildvalue=((uint16_t)(RTCC_DecToBCD(alarm_week)) << 8) | RTCC_DecToBCD(alarm_hour);

ALRMVAL = alarmbuildvalue;

alarmbuildvalue=((uint16_t)(RTCC_DecToBCD(alarm_min)) << 8) | RTCC_DecToBCD(alarm_sec);

ALRMVAL = alarmbuildvalue;

ALCFGRPTbits.CHIME=1;
ALCFGRPTbits.ARPT=255;

IEC3bits.RTCIE=1;
IPC15bits.RTCIP=3;
IFS3bits.RTCIF=0;

ALCFGRPTbits.ALRMEN=1;
}
void RTCCGET(void)
{
unsigned long int rtcvalue;

RCFGCALbits.RTCPTR0=1;
RCFGCALbits.RTCPTR1=1;
rtcvalue=RTCVAL;
year = RTCC_BCDToDec(rtcvalue);
rtcvalue=RTCVAL;
day = RTCC_BCDToDec(rtcvalue);
month = RTCC_BCDToDec(rtcvalue>>8);
rtcvalue=RTCVAL;
hour = RTCC_BCDToDec(rtcvalue);
week =RTCC_BCDToDec(rtcvalue>>8);
rtcvalue=RTCVAL;
sec = RTCC_BCDToDec(rtcvalue);
min = RTCC_BCDToDec(rtcvalue>>8);
}
void RTCC_ALARM_GET(void)
{
unsigned long int rtcalarmvalue;


ALCFGRPTbits.ALRMPTR0=1;
ALCFGRPTbits.ALRMPTR1=1;
rtcalarmvalue =ALRMVAL;
rtcalarmvalue =ALRMVAL;
alarm_day = RTCC_BCDToDec(rtcalarmvalue);
alarm_month = RTCC_BCDToDec(rtcalarmvalue>>8);
rtcalarmvalue =ALRMVAL;
alarm_hour = RTCC_BCDToDec(rtcalarmvalue);
alarm_week =RTCC_BCDToDec(rtcalarmvalue>>8);
rtcalarmvalue =ALRMVAL;
alarm_sec = RTCC_BCDToDec(rtcalarmvalue);
alarm_min =RTCC_BCDToDec(rtcalarmvalue>>8);
}
void AlarmStopRec (void){
alarm_month = AlarmStopMonth;
alarm_day = AlarmStopDay;
alarm_hour = AlarmStopHour;
alarm_min = AlarmStopMin;
alarm_sec=00;
//alarm_week = Day_of_Week(alarm_day,alarm_month,year);
RTCCALARM();
}
 
#1

2 Replies Related Threads

    TER
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/04/15 03:59:38
    • Location: 0
    • Status: offline
    Re: PIC24F RTCC module. 2019/08/07 04:58:39 (permalink)
    0
    help me please who can!!!
    #2
    dan1138
    Super Member
    • Total Posts : 3262
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC24F RTCC module. 2019/08/07 13:38:28 (permalink)
    0
    termin, post #1
    Hello everybody!
    I'am using pic24fj64gb202. I work with RTCC module.
     
    I configure the registers of the module (code below). I use internal oscillator FRC.
     
    Everything works as it should, but it works faster.
     
    For the test, I set the alarm so that it asserts one minute after the power is turned on, the alarm triggers after 55 seconds, 5 seconds early.
     
    Why is it so work, please help who can.

    THANKS.
    #include "rtcc.h"

    unsigned char year = 0;
    unsigned char month = 0;
    unsigned char day = 0;
    int week = 0;
    unsigned char hour = 0;
    unsigned char min = 0;
    unsigned char sec = 0;

    unsigned char alarm_month = 0;
    unsigned char alarm_day = 0;
    int alarm_week = 0;
    unsigned char alarm_hour = 0;
    unsigned char alarm_min = 0;
    unsigned char alarm_sec = 0;

    extern unsigned char AlarmStopMonth;
    extern unsigned char AlarmStopDay;
    extern unsigned char AlarmStopHour;
    extern unsigned char AlarmStopMin;

    uint8_t RTCC_DecToBCD (uint8_t value)
    {
        return (((value / 10)) << 4) | (value % 10);
    }

    uint8_t RTCC_BCDToDec (uint8_t value)
    {
        return ((value >> 4) * 10) + (value & 0x0F);
    }

    void RTCC (void)
    {
        unsigned long int rtcbuildvalue;
        NVMKEY = 0x55;
        NVMKEY = 0xAA;
        RCFGCALbits.RTCWREN = 1;
        RCFGCALbits.RTCPTR0 = 1;
        RCFGCALbits.RTCPTR1 = 1;
        RCFGCALbits.RTCEN = 0;

        rtcbuildvalue = RTCC_DecToBCD(year);
        RTCVAL = rtcbuildvalue; // YEAR

        rtcbuildvalue = ((uint16_t)(RTCC_DecToBCD(month)) << 8) | RTCC_DecToBCD(day);
        RTCVAL = rtcbuildvalue; // MONTH-1/DAY-1

        rtcbuildvalue = ((uint16_t)(RTCC_DecToBCD(week)) << 8) | RTCC_DecToBCD(hour);
        RTCVAL = rtcbuildvalue; // WEEKDAY/HOURS

        rtcbuildvalue = ((uint16_t)(RTCC_DecToBCD(min)) << 8) | RTCC_DecToBCD(sec);
        RTCVAL = rtcbuildvalue; // MINUTES/SECONDS

        RTCPWCbits.RTCLK = 1;
        RTCPWCbits.PWCEN = 1;
        RCFGCALbits.CAL = 0b00000000;//0b01111111;
        PMD3bits.RTCCMD = 0;
        RCFGCALbits.RTCEN = 1;
        RCFGCALbits.RTCWREN = 0;
    }

    void RTCCALARM(void)
    {
        unsigned long int alarmbuildvalue;

        ALCFGRPTbits.ALRMEN = 0;
        ALCFGRPTbits.AMASK = 0b1001;
        ALCFGRPTbits.ALRMPTR0 = 1;
        ALCFGRPTbits.ALRMPTR1 = 1;

        ALRMVAL = 0xFFFF;

        alarmbuildvalue = ((uint16_t)(RTCC_DecToBCD(alarm_month)) << 8) | RTCC_DecToBCD(alarm_day);

        ALRMVAL = alarmbuildvalue;

        alarmbuildvalue = ((uint16_t)(RTCC_DecToBCD(alarm_week)) << 8) | RTCC_DecToBCD(alarm_hour);

        ALRMVAL = alarmbuildvalue;

        alarmbuildvalue = ((uint16_t)(RTCC_DecToBCD(alarm_min)) << 8) | RTCC_DecToBCD(alarm_sec);

        ALRMVAL = alarmbuildvalue;

        ALCFGRPTbits.CHIME = 1;
        ALCFGRPTbits.ARPT = 255;

        IEC3bits.RTCIE = 1;
        IPC15bits.RTCIP = 3;
        IFS3bits.RTCIF = 0;

        ALCFGRPTbits.ALRMEN = 1;
    }

    void RTCCGET(void)
    {
        unsigned long int rtcvalue;

        RCFGCALbits.RTCPTR0 = 1;
        RCFGCALbits.RTCPTR1 = 1;
        rtcvalue = RTCVAL;
        year = RTCC_BCDToDec(rtcvalue);
        rtcvalue = RTCVAL;
        day = RTCC_BCDToDec(rtcvalue);
        month = RTCC_BCDToDec(rtcvalue>>8);
        rtcvalue = RTCVAL;
        hour = RTCC_BCDToDec(rtcvalue);
        week = RTCC_BCDToDec(rtcvalue>>8);
        rtcvalue = RTCVAL;
        sec = RTCC_BCDToDec(rtcvalue);
        min = RTCC_BCDToDec(rtcvalue>>8);
    }

    void RTCC_ALARM_GET(void)
    {
        unsigned long int rtcalarmvalue;

        ALCFGRPTbits.ALRMPTR0 = 1;
        ALCFGRPTbits.ALRMPTR1 = 1;
        rtcalarmvalue = ALRMVAL;
        rtcalarmvalue = ALRMVAL;
        alarm_day = RTCC_BCDToDec(rtcalarmvalue);
        alarm_month = RTCC_BCDToDec(rtcalarmvalue>>8);
        rtcalarmvalue = ALRMVAL;
        alarm_hour = RTCC_BCDToDec(rtcalarmvalue);
        alarm_week = RTCC_BCDToDec(rtcalarmvalue>>8);
        rtcalarmvalue = ALRMVAL;
        alarm_sec = RTCC_BCDToDec(rtcalarmvalue);
        alarm_min = RTCC_BCDToDec(rtcalarmvalue>>8);
    }

    void AlarmStopRec (void){
        alarm_month = AlarmStopMonth;
        alarm_day = AlarmStopDay;
        alarm_hour = AlarmStopHour;
        alarm_min = AlarmStopMin;
        alarm_sec = 00;
        //alarm_week = Day_of_Week(alarm_day,alarm_month,year);
        RTCCALARM();
    }



    termin, post #2
    help me please who can!!!

     
    What you have posted is an incomplete program so no useful help can be offered.
     
    To be able to offer useful help post a complete program that builds without errors or warning that shows the failing behavior.
     
    At a guess it seems likely that the clock source you have selected for the RTCC is running faster than you expect.
    #3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5