Hello again, I am currently trying to set up the internal RTCC module but I am not entirely sure how to connect the external SOSC and how to use this with the internal FRC oscillator as the primary oscillator source at the same time. According to the dspic30 datasheet it should be connected like in my attachment. I don't have access to any 18-22 pf capacitors, closest value I have are 47 pf. I have tried connecting the oscillator directly to the SOSCI and SOSCO pins and I don't really see any change. I've also been looking around the forums and google but I can't really find any concrete information on this module. I have used the reference manual, the learning to fly the pic book, and the dspic33f example for reference but I can't seem to get this module running correctly in any way. I also read stimulus files could be used in order to simulate the crystal for mplabsim but I can't find any information on creating these files.
Here's my code so far:
Hello spritepr,
If you do not have the correct capacitors the crystal will refuse to oscillate. Since you only have 47pF caps available you could put a couple in series to create a 23pF capacitor. You'd need a total of 4 of the 47pF caps.
Also, be sure to check the data sheet for the actual value of the load capacitance (CL). The value of your external capacitors will be determined by this number. There is a formula available in the oscillator section of the Microchip data sheets.
I have used the example code available to set up the RTCC and it worked fine. Stick with that as a start and get the right caps in place.
I hope that this helps.
ES
Hi, thanks for the reply, I looked at the product page and saw that CL should be 12.5 pF. According to the equation if I assume CS is 0 with the equivalent 23 pF capacitors for C1 and C2 would give me a CL of about 12 pF. Would this be good enough for the crystal to start oscillating? Also is there any way to determine what the value of CS is? Attached are the product specifications for the crystal as well as a screenshot of the CL equation.
I connected the oscillator with the calculated CL values and it seems to be working but it's counting way too fast. I have set up a 100 ms delay between each print and every time the values are printed it goes up by one second hence 1 second is being counted every 100 ms. Any suggestions on why this may be happening? Incorrect CL values maybe?
Current code:
Main:
RTCC:
http://www.microchip.com/forums/m569665-print.aspx
Here's my code so far:
main:
#include "p33fj64mc802.h"
#include "HardwareProfile.h"
#include "RtccInit.h"
#define LED1 LATBbits.LATB15
#define LED_1_TRIS TRISBbits.TRISB15
//Macros for Configuration Fuse Registers:
//Invoke macros to set up device configuration fuse registers.
//The fuses will select the oscillator source, power-up timers, watch-dog
//timers etc. The macros are defined within the device
//header files. The configuration fuse registers reside in Flash memory.
// External Oscillator
//_FOSCSEL(FNOSC_PRI); // Primary (XT, HS, EC) Oscillator
//_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: XT Crystanl
Internal FRC Oscillator
_FOSCSEL(FNOSC_FRC); // FRC Oscillator
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: Disabled
_FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software
void pps_config()
{
// Unlock Registers
asm volatile ( "disi #6 \n"
"mov #OSCCON, w1 \n"
"mov #0x46, w2 \n"
"mov #0x57, w3 \n"
"mov.b w2, [w1] \n"
"mov.b w3, [w1] \n"
"bclr OSCCON, #6 \n");
// UART1TX and RX pins:
RPOR5bits.RP11R = 3; // Assign U1TX peripheral to pin RP11
RPINR18bits.U1RXR = 10; // Assign pin RP10 to U1RX peripheral
// Lock Registers
asm volatile ( "disi #6 \n"
"mov #OSCCON, w1 \n"
"mov #0x46, w2 \n"
"mov #0x57, w3 \n"
"mov.b w2, [w1] \n"
"mov.b w3, [w1] \n"
"bclr OSCCON, #6 \n");
// Set TRIS bits
LED_1_TRIS = 0;
}
int main (void)
{
pps_config();
Init_UART();
init_Timer1();
init_RTCC(0x011,0x0331,0x0412,0x4400); // pass the values in the form:
// (Year, Month Day, Weekday Hours, Minutes Seconds)
while(1)
{
Read_RTCC(); // read and print the date and time registers.
delay_ms(100);
LED1=~LED1;
}
}// Main
Rtccinit.c:
#include "p33fj64mc802.h"
#include "RtccInit.h"
#include "uart_module.h"
#include "timer_delay.h"
#include <stdio.h>
int Year, MonDat, WkHr, MinSec;
void init_RTCC(int aYEAR, int aMONDAT, int aWKHR, int aMINSEC)
{
// Enables the OSCON write and set
//_SOSCEN =1;
asm volatile ("mov #OSCCON,W1");
asm volatile ("mov.b #0x46, W2"); // unlock sequence
asm volatile ("mov.b #0x57, W3");
asm volatile ("mov.b #0x02, W0"); // SOSCEN =1
asm volatile ("mov.b W2, [W1]");
asm volatile ("mov.b W3, [W1]");
asm volatile ("mov.b W0, [W1]");
//_RTCWREN = 1; // unlock setting
asm volatile("disi #5");
asm volatile("mov #0x55, w7");
asm volatile("mov w7, NVMKEY");
asm volatile("mov #0xAA, w8");
asm volatile("mov w8, NVMKEY");
asm volatile("bset RCFGCAL, #13"); // RTCWREN =1;
asm volatile("nop");
asm volatile("nop");
RCFGCALbits.RTCEN = 0; // disable the clock
// set aMON/DAT/aYEAR aWK HR:aMIN:SEC
RCFGCALbits.RTCPTR = 3; // start the sequence
RTCVAL = aYEAR; // YEAR
RTCVAL = aMONDAT; // MONTH-1/DAY-1
RTCVAL = aWKHR; // WEEKDAY/HOURS
RTCVAL = aMINSEC; // MINUTES/SECONDS
// lock and enable
RCFGCALbits.RTCEN = 1; // start the clock
RCFGCALbits.RTCWREN = 0; // lock settings
} // init RTCC
// Function to read the time and date registers
void Read_RTCC(void)
{
while(RCFGCALbits.RTCSYNC==1); //wait for RTCSYNC bit to become ‘0’
RCFGCALbits.RTCPTR = 3; //points to the Year register
// Read RTCC values:
Year =RTCVAL;
MonDat =RTCVAL;
WkHr =RTCVAL;
MinSec =RTCVAL;
printf("The RTCC register values are: YEAR:%X MONDAT:%X WKHR:%X MINSEC:%X\n",Year,MonDat,WkHr,MinSec);
} // Read RTCC
![](http://www.microchip.com/forums/thumb.axd/150_71119/9E1B39EF18B64F238121CB7E9B40219D.jpg)
![](http://www.microchip.com/forums/thumb.axd/150_71119/9E1B39EF18B64F238121CB7E9B40219D.jpg)
Hello spritepr,
If you do not have the correct capacitors the crystal will refuse to oscillate. Since you only have 47pF caps available you could put a couple in series to create a 23pF capacitor. You'd need a total of 4 of the 47pF caps.
Also, be sure to check the data sheet for the actual value of the load capacitance (CL). The value of your external capacitors will be determined by this number. There is a formula available in the oscillator section of the Microchip data sheets.
I have used the example code available to set up the RTCC and it worked fine. Stick with that as a start and get the right caps in place.
I hope that this helps.
ES
LowVoltBrain
Hello spritepr,
If you do not have the correct capacitors the crystal will refuse to oscillate. Since you only have 47pF caps available you could put a couple in series to create a 23pF capacitor. You'd need a total of 4 of the 47pF caps.
Also, be sure to check the data sheet for the actual value of the load capacitance (CL). The value of your external capacitors will be determined by this number. There is a formula available in the oscillator section of the Microchip data sheets.
I have used the example code available to set up the RTCC and it worked fine. Stick with that as a start and get the right caps in place.
I hope that this helps.
ES
Hi, thanks for the reply, I looked at the product page and saw that CL should be 12.5 pF. According to the equation if I assume CS is 0 with the equivalent 23 pF capacitors for C1 and C2 would give me a CL of about 12 pF. Would this be good enough for the crystal to start oscillating? Also is there any way to determine what the value of CS is? Attached are the product specifications for the crystal as well as a screenshot of the CL equation.
<message edited by spritepr on Thursday, March 31, 2011 9:22 PM>
![](http://www.microchip.com/forums/thumb.axd/150_71119/9E1B39EF18B64F238121CB7E9B40219D.jpg)
I connected the oscillator with the calculated CL values and it seems to be working but it's counting way too fast. I have set up a 100 ms delay between each print and every time the values are printed it goes up by one second hence 1 second is being counted every 100 ms. Any suggestions on why this may be happening? Incorrect CL values maybe?
Current code:
Main:
#include "p33fj64mc802.h"
#include "HardwareProfile.h"
#include "RtccInit.h"
#define LED1 LATBbits.LATB15
#define LED_1_TRIS TRISBbits.TRISB15
//Macros for Configuration Fuse Registers:
//Invoke macros to set up device configuration fuse registers.
//The fuses will select the oscillator source, power-up timers, watch-dog
//timers etc. The macros are defined within the device
//header files. The configuration fuse registers reside in Flash memory.
// External Oscillator
//_FOSCSEL(FNOSC_PRI); // Primary (XT, HS, EC) Oscillator
//_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: XT Crystanl
Internal FRC Oscillator
_FOSCSEL(FNOSC_FRC); // FRC Oscillator
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: Disabled
_FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software
void pps_config()
{
// Unlock Registers
asm volatile ( "disi #6 \n"
"mov #OSCCON, w1 \n"
"mov #0x46, w2 \n"
"mov #0x57, w3 \n"
"mov.b w2, [w1] \n"
"mov.b w3, [w1] \n"
"bclr OSCCON, #6 \n");
// UART1TX and RX pins:
RPOR5bits.RP11R = 3; // Assign U1TX peripheral to pin RP11
RPINR18bits.U1RXR = 10; // Assign pin RP10 to U1RX peripheral
// Lock Registers
asm volatile ( "disi #6 \n"
"mov #OSCCON, w1 \n"
"mov #0x46, w2 \n"
"mov #0x57, w3 \n"
"mov.b w2, [w1] \n"
"mov.b w3, [w1] \n"
"bclr OSCCON, #6 \n");
// Set TRIS bits
LED_1_TRIS = 0;
}
int main (void)
{
pps_config();
Init_UART();
init_Timer1();
init_RTCC(0x0011,0x0401,0x0515,0x2500); // pass the values in the form:
// (Year, Month Day, Weekday Hours, Minutes Seconds)
while(1)
{
Read_RTCC(); // read and print the date and time registers.
delay_ms(100); // print timestamp each second
LED1=~LED1;
}
}// Main
![](http://www.microchip.com/forums/thumb.axd/150_71119/9E1B39EF18B64F238121CB7E9B40219D.jpg)
RTCC:
#include "p33fj64mc802.h"
#include "RtccInit.h"
#include "uart_module.h"
#include "timer_delay.h"
#include <stdio.h>
int Year, MonDat, WkHr, MinSec;
void init_RTCC(int aYEAR, int aMONDAT, int aWKHR, int aMINSEC)
{
// Enables the OSCON write and set
//_SOSCEN =1;
asm volatile ("mov #OSCCON,W1");
asm volatile ("mov.b #0x46, W2"); // unlock sequence
asm volatile ("mov.b #0x57, W3");
asm volatile ("mov.b #0x02, W0"); // SOSCEN =1
asm volatile ("mov.b W2, [W1]");
asm volatile ("mov.b W3, [W1]");
asm volatile ("mov.b W0, [W1]");
//_RTCWREN = 1; // unlock setting
asm volatile("disi #5");
asm volatile("mov #0x55, w7");
asm volatile("mov w7, NVMKEY");
asm volatile("mov #0xAA, w8");
asm volatile("mov w8, NVMKEY");
asm volatile("bset RCFGCAL, #13"); // RTCWREN =1;
asm volatile("nop");
asm volatile("nop");
RCFGCALbits.RTCEN = 0; // disable the clock
// set aMON/DAT/aYEAR aWK HR:aMIN:SEC
RCFGCALbits.RTCPTR = 3; // start the sequence
RTCVAL = aYEAR; // YEAR
RTCVAL = aMONDAT; // MONTH-1/DAY-1
RTCVAL = aWKHR; // WEEKDAY/HOURS
RTCVAL = aMINSEC; // MINUTES/SECONDS
// lock and enable
RCFGCALbits.RTCEN = 1; // start the clock
RCFGCALbits.RTCWREN = 0; // lock settings
} // init RTCC
// Function to read the time and date registers
void Read_RTCC(void)
{
while(RCFGCALbits.RTCSYNC==1); //wait for RTCSYNC bit to become ‘0’
RCFGCALbits.RTCPTR = 3; //points to the Year register
// Read RTCC values:
Year =RTCVAL;
MonDat =RTCVAL;
WkHr =RTCVAL;
MinSec =RTCVAL;
printf("The RTCC register values are: YEAR:%X MONDAT:%X WKHR:%X MINSEC:%X\n",Year,MonDat,WkHr,MinSec);
} // Read RTCC
![](http://www.microchip.com/forums/thumb.axd/150_71119/9E1B39EF18B64F238121CB7E9B40219D.jpg)
http://www.microchip.com/forums/m569665-print.aspx