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

Timer1 external oscillator for RTC on PIC18F47j13
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
HaveAchat



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

Timer1 external oscillator for RTC on PIC18F47j13
PostPosted: Thu Jan 23, 2020 9:25 pm     Reply with quote

Ladies and Gentlemen,

I am trying to implement the RTC on a PIC18F67J13. I have an external 32.768kHz xtal connected to C0 and C1. Startup code below from the wizard.
First was to wink the LED in the main loop... that worked (baby step 1)
I then comment the winking in the main loop and added the toggle in the interrupt. (baby step 2)
Usually this tactic works but this time I am lost. (The baby has fallen flat on his face).


I have changed the crystal (two types) and changed the caps from 10pf to 20pf and there is no sign of oscillation at the crystal terminals and no indication the RTC is running.

Sorry for the code in the text but I am new at this!!!!

Any assistance getting the baby back on his feet would be appreciated.
Code:

#include <main_01.h>
   //RTCC
   rtc_time_t wizardTempTime;

#INT_RTC
void  RTC_isr(void) {
   output_toggle(LED);
}

void main() {
   //RTCC
   setup_rtc(RTC_ENABLE , 0);
   wizardTempTime.tm_year = 20;
   wizardTempTime.tm_mon = 1;
   wizardTempTime.tm_mday = 24;
   wizardTempTime.tm_wday = 5;
   wizardTempTime.tm_hour = 13;
   wizardTempTime.tm_min = 54;
   wizardTempTime.tm_sec = 46;
   rtc_write(&wizardTempTime);
   setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 0);
   wizardTempTime.tm_year = 0x00;
   wizardTempTime.tm_mon = 1;
   wizardTempTime.tm_mday = 1;
   wizardTempTime.tm_wday = 1;
   wizardTempTime.tm_hour = 0;
   wizardTempTime.tm_min = 0;
   wizardTempTime.tm_sec = 0;
   rtc_alarm_write(&wizardTempTime);


   enable_interrupts(INT_RTC);
   enable_interrupts(GLOBAL);

   while(TRUE){

      //Example blinking LED program
//!      output_low(LED);
//!      delay_ms(DELAY);
//!      output_high(LED);
      delay_ms(DELAY);

      //TODO: User Code
   }

}


+++++++++++++++++++++++
Code block added per Ttelmah.

- Forum Moderator
+++++++++++++++++++++++
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 12:49 am     Reply with quote

You carefully don't show your fuses or chip configuration.
Generally, when posting code:
1) You need to always show the chip configuration (#fuses, and #clock),
and.
2) Use the forum code buttons.

I'd suggest, the secondary oscillator is not setup:

#fuses SOSC_HIGH, RTCOSC_T1

This says that the RTC is going to be fed off the T1 oscillator, and to select
the higher power mode for the secondary oscillator.

Start with the high power setting, you can select SOSC_LOW, once
everything is working.

Understand the wizard is thick. It will accept what you tell it, even if
this is impossible. It relies on you making sensible settings, and this means
reading the data sheet and examples.
HaveAchat



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 2:58 am     Reply with quote

Thanks for the speedy reply Ttelmah,
Yes I understand about the fuses but I forgot!
The original header had no fuses identified but the addition did not change my situation.
I decided to run the wizard again and asked for all the fuses to be added but still my oscillator does not run!

I am assuming the crystals are OK and the 10pf caps are correct.

Any insight would be appreciated as I just can't see what is missing.

Code:
#include <18F47J13.h>
#device ADC=16

#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES SOSC_HIGH                //High-power SOSC circuit is selected
#FUSES NOCLOCKOUT               //I/O function on OSC2
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES DSWDTOSC_INT             //DSWDT uses INTRC as reference clock
#FUSES RTCOSC_T1                //RTCC uses Secondary Oscillator as reference source
#FUSES DSBOR                    //BOR enabled in Deep Sleep
#FUSES NODSWDT                  //Deep Sleep Watchdog Timer disabled
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES ADC12                    //ADC is 12-bits
#FUSES MSSPMSK7                 //MSSP uses 7 bit Masking mode
#FUSES WPFP                     //Write/Erase Protect Page Start/End Location, set to last page or use WPFP=x to set page
#FUSES NOWPCFG                  //Configuration Words page is not erase/write-protected
#FUSES WPDIS                    //All Flash memory may be erased or written
#FUSES WPEND                    //Flash pages WPFP to Configuration Words page are write/erase protected



#use delay(internal=32MHz)

#define LED PIN_D2
#define DELAY 1000
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 6:44 am     Reply with quote

hmm.. I don't see a fuse called 'PUT' ( Power Up Timer ).
While this may not be a problem, I always use it...to be sure the PIC is 'organised' beforemain() is started.

Wizards aren't too smart ! ALWAYS confirm settings, 'their' defaults are never what YOU want or need..... Sad

Also be sure to compile in 'release' mode NOT 'debug'.... another 'gotcha'...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 7:10 am     Reply with quote

What is your CCS compiler version ? Look at the top of the .LST file.
It will be a 4-digit number (only), such as 4.065, 5.047, 5.092, etc.

Who are the manufacturers and what are the part numbers of your
watch crystals ?

Is your PIC an 'F' or an 'LF' chip ?

Do you have a capacitor on the Vddcore pin ?

What is your PIC Vdd ?

Did you build the board yourself, or did you buy it ? If you bought it, list
the manufacturer and part number.
HaveAchat



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 4:46 pm     Reply with quote

Thanks for the support, I value your time and effort.

I tried the PUT fuse but was rejected by the compiler.

Compiler version 5.071

F series PIC

I designed and manufactured the PCB and yes I am nervous at this stage, so many potential problems. I have assembled three units and all act the same.

Yes all caps are as per the Microchip design guidelines.
I will have a close look at track layout. The PCB is track only without a ground fill. I do this on the first prototype as it easier for cut and link tracks at this early design phase and yes, provides other potential instabilities!

Vdd is 3.1V it was to provide a battery charge solution but that is currently problematic.
I will move it back to 3.3V which will allow full power from the LoRa module.

The crystal is an Epson Toyco MC-164 32.768kHz
The second crystal is from my bench junk pile so not sure of its lineage.

The PCB supports a WiFi modem, Bluetooth modem, LoRa module, 2 eeproms and a ISP to serial converter.
All that stuff works as I have ported my development stuff from a PIC18F87J11. Mind you reallocating pins was a novel experience, that is a different issue....
Over the weekend I will assemble another PCB with a power supply, PIC and xtal only. I will also source a known good Xtal and I will post my results.
Once again thanks for the support.
HaveAchat



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 4:58 pm     Reply with quote

A quick question for temtronic.

I am a bit handicapped here as I am really a hardware engineer who has slipped into the dark side of programming and really struggle sometimes with the simplest of things.

How do I know if I am in 'release' mode?
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 5:19 pm     Reply with quote

OK, I'm assuming you're using MPLAB......
IF so, find the 'Project' tab, click to open, scroll down to the 'Build Configuration' (about 1/2 way down).
The factory default setting is 'debug' but you need to change this to 'release' before you compile the program to then download into the PIC.
After MPLAB v8.8x? you were allowed to change the default. Microchip did this at my request as I never use 'debug'. That's what the Real World is for. If you try to run a 'debug' version it won't work as expected....

Apparently I'm an oddity as I never use simulators or debuggers or ICDs. Maybe a hangover from my Teletype days ??

Jay
HaveAchat



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

PostPosted: Fri Jan 24, 2020 8:27 pm     Reply with quote

Jay,
Thanks for the reply.
I am using the ccs IDC so I can stay calm and relaxed with what I am doing.

I used to use MPLAB back in the days when assembler was the go but the Ccs C IDC has spoilt me rather badly e.g. new processor and no timer 1 oscillation!

I have a feeling we are of a similar vintage, CR came before LF for a reason!
The younger readers will think we are quite strange.

Thanks
John
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Jan 25, 2020 1:19 am     Reply with quote

Do you have a part number or data sheet link for the crystal you are
using?. You say it is an Epson MC164, but this number doesn't lead to
a crystal on the Epson site....

The capacitor values needed will depend on how your board is laid out,
and the crystal specifications. Describe how the secondary oscillator is
actually built.

These newer generation chips are actually more critical of the crystal
than their older brethren. In particular they won't drive crystals that
need a high load capacitance.
If you have a ground plane round the crystal pins, you may actually have
too much capacitance, with no external capacitors used.....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 25, 2020 1:41 am     Reply with quote

I was able to make it work with an 18F27J13. The watch crystal part
number is ECS-.327-12.5-13. I used two 22pf disk capacitors with this crystal.
I didn't change the program very much. I did add a repeat parameter to
the alarm so I could see the LED blink several times. This shows the
INT_RTC interrupt is working.

Test program:
Code:

#include <18F27J13.h>
#fuses INTRC_IO, NOWDT
#fuses SOSC_HIGH, RTCOSC_T1
#use delay(clock=4M)

rtc_time_t wizardTempTime;

#INT_RTC
void RTC_isr(void)
{
output_toggle(PIN_A0);
}

//==============================
void main()
{
setup_rtc(RTC_ENABLE, 0);

wizardTempTime.tm_year = 20;
wizardTempTime.tm_mon  = 1;
wizardTempTime.tm_mday = 24;
wizardTempTime.tm_wday = 5;
wizardTempTime.tm_hour = 13;
wizardTempTime.tm_min  = 54;
wizardTempTime.tm_sec  = 46;
rtc_write(&wizardTempTime);

setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 10);

wizardTempTime.tm_year = 0x00;
wizardTempTime.tm_mon  = 1;
wizardTempTime.tm_mday = 1;
wizardTempTime.tm_wday = 1;
wizardTempTime.tm_hour = 0;
wizardTempTime.tm_min  = 0;
wizardTempTime.tm_sec  = 0;
rtc_alarm_write(&wizardTempTime);


enable_interrupts(INT_RTC);
enable_interrupts(GLOBAL);

while(TRUE);
}


The board is running at 3.3 volts. It has a 20 MHz crystal on the board
but I'm not using it. I'm using the PIC's internal oscillator at 4 MHz.
You can't see the crystal's capacitors because they are SMD caps on the
bottom side. The caps you can see are for the watch crystal.
Also, there is a 10uF tantalum cap to GND on pin 6, which is the VCAP pin.

Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Jan 25, 2020 2:26 am     Reply with quote

Well done PCM.
You were obviously mirroring my thinking that there must be something
wrong with the hardware involved. It should work!... Very Happy
HaveAchat



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

PostPosted: Sat Jan 25, 2020 6:22 am     Reply with quote

PCM, what a sterling effort. If you were around here I would come and pay homage.

I have my system working and here is a note to hopefully help the community.
A few stupid mistakes, one was not fully understanding the program command and the other was me forgetting 20 years of working in the field of measurement!!!!!!!!! yes 20 years of bench experience repairing and calibrating electronic test equipment, how embarrassing.

The first issue is the '0' in setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 0); the wizard default.
What does it mean???????? Its obvious now!!!! If I change it to 10 the alarm will be issued 10 times at one second intervals and 0 means no alarms!
I still love the wizard.

So, I would run the code, see no change on the LED and suspect the oscillator is not running or not allocated correctly. To test this I used my CRO on the oscillator pins, about 0.6VDC on one and about 2VDC on the other. To me that indicated an inverting drive and was stumped that it did not oscillate.
What do you think happened when I changed the CRO probe to X10??????

Yep, X1 it stops and X10 away it went, blink, bling, blink!

The CRO probe capacitance killed the oscillator, as simple as that. Numpty here assumed the oscillator was not working all the time.

This does mean the 20pF caps are a bit high and I will have sharpen my pencil and do some serious design around this oscillator for the production version.

Many thanks for the assistance given in the efforts to resolve my issue and a special note to PCM, to take the time to knock up a real piece of hardware to help resolve the problem is just unbelievable, you do deserve special thanks.

My penitents for this dumb mistake is to become more involved in this community and hopefully help when I have some experience to share.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 25, 2020 7:27 am     Reply with quote

Always 'fun' to learn by doing !! Just remember the only reason 'we' know stuff is 'we' have already made your booboos !!! Very Happy

Be very,very careful about using the 'wizard' ! It's been programmed by someone else who decided HIS configurations should be YOUR defaults !!!

I don't use wizards, can't trust them, so I create a separate 'fuses' file with EVERY fuse, 1 per line, with comment on the end. I name it ,say 46k22base.fuz, then #include it. 46k22 is the pic type, base means a complete set of fuse that will run at 1Hz LED program, fuz is short for fuses. While most woud say .c, I consider that to be running code not 'setup' stuff.

I also copy the previous version of the program, then edit/compile/test/save lke pgm1.c, pgm2.c,...pgm100.c. This allows me to go back to previous versions as I can't type and if you make 3 changes, gee WHICH one made it fail ?? OK, so I'll have 100+ versions BUT HD space is unlimited and when I complete a project, I delete all but the 1st and 3 last versions.

I've spent 1/2 my life with PICs so I need some tricks to help me !

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Jan 25, 2020 7:41 am     Reply with quote

As a comment, MicroChip do have an application note giving a recommended
design for the PCB round this. Look at AN1798. Note the use of a top side
ground pour, and no traces on the bottom side. Note the comments about
cleaning. This is vital.

On your scope, you can only test using the SOSCO pin, not SOSCI. On
SOSCO it'll normally work, but SOSCI will kill it.

The crystals are critical on ESR, as I said, couldn't find the Epson crystal
you mention, a data sheet for this would be really useful!...

Glad you have it going now. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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