CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

RTCC with dsPIC33FJ128GP802

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

RTCC with dsPIC33FJ128GP802
PostPosted: Mon Dec 11, 2017 8:09 am     Reply with quote

I use dsPIC33FJ128GP802 in the 28-Pin SPDIP package with internal oscillator PLL for main CPU activities.
I attached an external crystal 32.768 Hz between pins SOSCO/T1CK/CN0/PMA1/RA4 and SOSCI/RP4(1)/CN1/PMBE/RB4 and I would like to use the internal RTCC of the dsPIC33FJ128GP80.

For an unknown reason the RTCC does not work.

I have tried next test code:
Code:
#include <33FJ128GP802.h>
#device ICSP=3
//#device ICD=3

#FUSES FRC_PLL                  //Internal Fast RC oscillator with PLL
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NORBS                    //No Boot RAM defined
#FUSES NOWRTSS                  //Secure segment not write protected
#FUSES NOSSS                    //No secure segment
#FUSES NORSS                    //No secure segment RAM
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES PUT128                   //Power On Reset Timer value 128ms
#FUSES NOALTI2C1                //I2C1 mapped to SDA1/SCL1 pins
#FUSES NOJTAG                   //JTAG disabled

#use delay(internal=80mhz, AUX:crystal=32768)
#pin_select U1TX=PIN_B15
#pin_select U1RX=PIN_B14
#use rs232(baud=9600,UART1)

void main()
{

rtc_time_t read_clock;
setup_rtc(RTC_ENABLE,0);         //enables internal RTCC

 while(TRUE){
   
    rtc_read(&read_clock);        //reads clock value from RTCC
    printf("\r%02u/%02u/20%02u %02u:%02u:%02u\r\n",read_clock.tm_mon,read_clock.tm_mday,read_clock.tm_year,read_clock.tm_hour,read_clock.tm_min,read_clock.tm_sec);
    delay_ms(1000);
 }

}


The dsPIC33FJ128GP802 is connected to a TLL-RS232 USB adapter and I use RealTerminal serial monitor software to see info on PC.
I can see the data on PC screen, but RTCC does not change the seconds.
I am aware that I do not set the time with the above code and I do not care, I just want to see the seconds changing, the 32768 oscillator running and it doesn’t.
I have tried with only the crystal attached to the PIC pins, and then I have tried to add some capacitors: one test with 22pF, one test with 15pF and one with 10pF. It does not oscillate. I looked with the oscilloscope to the crystal pins and there is no oscillation signal. I have used 1:100 single ended probe and also 1:20 differential probe.

Do you have any suggestion to debug the 32768 oscillator and RTCC settings?
Ttelmah



Joined: 11 Mar 2010
Posts: 12953

View user's profile Send private message

PostPosted: Mon Dec 11, 2017 9:14 am     Reply with quote

Have you actually tested that the secondary oscillator will physically run OK?.
Set this to run the CPU, instead of the master oscillator, and do a basic 'flash an LED' test running at 32.768KHz.
What compiler version?.
Have checked on the current compiler and the code does correctly enable the oscillator, but it is quite possible that an older release had this wrong.

There is a problem with your settings though. You are enabling the _high power_ Aux oscillator on the pins. The Aux oscillator requires a clock rate not under 3.5MHz.....

The Aux oscillator must be off for the secondary low power oscillator to be used. You have to feed the Aux connection from Fosc, to use the RTC. Aux:clock, not Aux:crystal
jeremiah



Joined: 20 Jul 2010
Posts: 978

View user's profile Send private message

PostPosted: Mon Dec 11, 2017 8:13 pm     Reply with quote

If you just want to check if your oscillator can run, you can also test out the timer1 input. Basically just call setup_timer1() with a TMR_EXTERNAL_RTC option. This should cause the oscillator to start oscillating:

Code:

#include <33FJ128GP802.h>

#FUSES NOPR
#FUSES FRC_PLL                  //Internal Fast RC oscillator with PLL
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NORBS                    //No Boot RAM defined
#FUSES NOWRTSS                  //Secure segment not write protected
#FUSES NOSSS                    //No secure segment
#FUSES NORSS                    //No secure segment RAM
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES PUT128                   //Power On Reset Timer value 128ms
#FUSES NOALTI2C1                //I2C1 mapped to SDA1/SCL1 pins
#FUSES NOJTAG                   //JTAG disabled

#use delay(internal=80mhz)

void main()
{

 setup_timer1(TMR_EXTERNAL_RTC, 32768);
 while(TRUE){}

}


I know this doesn't do anything with the RTC in particular but it can be a quick way to test your secondary oscillator. Run that and then check to see if the crystal is oscillating. If it isn't you either have a hardware problem or your version of the compiler has a bug.
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

PostPosted: Tue Dec 12, 2017 3:53 am     Reply with quote

It works.
My confusion was with „Aux oscillator“. I though it refers to the 32.768 KHz crystal, the secondary oscillator and must be set in #use delay beside the internal clock. But actually we do not need to refer to that 32768 clock in #use delay. It os enough to specify the clock of the CPU, in my case internal as #use delay(internal=80mhz). The 32768 clock is started and set with the setup_rtc(RTC_ENABLE,0);
The “Aux:” I just deleted.

So when do we have to use the “Aux: clock”, in what situation, for what oscillator?

How accurate can that RTCC be?
Do you have any idea in terms of seconds deviation per day/week/month?
What improvement can be done to make it more accurate?

Here is my working code, based on “ex_rtcc.c” example from “C:\Program Files (x86)\PICC\Examples”.
My compiler version is 5.070. I did not add any ceramic capacitors to the 32.768 KHz crystal. It is just the crystal connected to the pins SOSCO/T1CK/CN0/PMA1/RA4 and SOSCI/RP4(1)/CN1/PMBE/RB.

Code:
#include <33FJ128GP802.h>
#device ICSP=3

#FUSES FRC_PLL                  //Internal Fast RC oscillator with PLL
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NORBS                    //No Boot RAM defined
#FUSES NOWRTSS                  //Secure segment not write protected
#FUSES NOSSS                    //No secure segment
#FUSES NORSS                    //No secure segment RAM
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES PUT128                   //Power On Reset Timer value 128ms
#FUSES NOALTI2C1                //I2C1 mapped to SDA1/SCL1 pins
#FUSES NOJTAG                   //JTAG disabled

#use delay(internal=80mhz)

#pin_select U1TX=PIN_B15
#pin_select U1RX=PIN_B14
#use rs232(baud=9600,UART1)

int8 get_number()
{
  char first,second;

  do {
    first=getc();
  } while ((first<'0') || (first>'9'));
  putc(first);
  first-='0';

  do {
    second=getc();
  } while (((second<'0') || (second>'9')) && (second!='\r'));
  putc(second);

  if(second=='\r')
    return(first);
  else
    return((first*10)+(second-'0'));
}

void set_clock(rtc_time_t &date_time)
{
   printf("\r\nPress ENTER after 1 digit answers.");
   printf("\r\nYear 20: ");
   date_time.tm_year=get_number();
   printf("\r\nMonth: ");
   date_time.tm_mon=get_number();
   printf("\r\nDay: ");
   date_time.tm_mday=get_number();
   printf("\r\nWeekday 1-7: ");
   date_time.tm_wday=get_number();
   printf("\r\nHour: ");
   date_time.tm_hour=get_number();
   printf("\r\nMin: ");
   date_time.tm_min=get_number();
   date_time.tm_sec=0;

   printf("\r\n\n");
}

void main()
{

   rtc_time_t write_clock, read_clock;
   setup_rtc(RTC_ENABLE,0);         //enables internal RTCC
   set_clock(write_clock);
   rtc_write(&write_clock);         //writes new clock setting to RTCC

 while(TRUE){
   
    rtc_read(&read_clock);        //reads clock value from RTCC
    printf("\r%02u/%02u/20%02u %02u:%02u:%02u\r\n",read_clock.tm_mon,read_clock.tm_mday,read_clock.tm_year,read_clock.tm_hour,read_clock.tm_min,read_clock.tm_sec);
    delay_ms(1000);
 }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 12953

View user's profile Send private message

PostPosted: Tue Dec 12, 2017 6:12 am     Reply with quote

On a lot of chips the Aux feeds things like the USB. On this chip it just feeds the audio DAC. It's designed to allow you to have things running at particular speeds or multiples. So (for instance) if you needed to match a speed for playing a DVD for example, you could feed this off a frequency that gives an exact multiple of what you want, or even off other parts of the hardware.
It uses the same pins as the low power oscillator, but if using an oscillator requires a crystal between 3.5 and 10Mhz. It can be fed off an external clock, the internal Fosc, or it's own oscillator. Selecting 'crystal' says to do the latter, which automatically disables the low power oscillator.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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


Powered by phpBB © 2001, 2005 phpBB Group