PIC24F - cannot reprogram reference clock output. - Page 1
Forum section: electronics / general

author: alj
author_rank: Contributor
timestamp: March 31, 2020, 08:47:50 pm
content: Hi Everybody!I'm trying to make programmable clock divider based on PIC24F08KL301 .I have external clock supplied to pin 7 and I have divided clock coming in out of pin 18.the "rate" consists from 3 lower bits which represent division, with 4th bit representing external relay switching XTAL.Everything works great except that i cannot change clock division. I e i can initially set it but consequent writes to REFOCONbits.RODIV have no effect.Also if I disable output (REFOCONbits.ROEN = 0;) then enable it again - it does not get re-enabled, there is no clock in the output, where it was there prior to disabling.Maybe there is a config that prevents me from changing reference clock reprogramming? Did not find anything in dox. Errata does not have anything related to this issue.my project sources:https://gitlab.com/aleynikov/daccontrol/-/tree/Forum/DACcontrol.Xinit is done in DACmain.c lines 86 - 93clock change is done in dac.c line 74What am I doing wrong and how can I change divisor?Any help is greatly appreciated.

author: TomS_
author_rank: Frequent Contributor
timestamp: April 01, 2020, 07:00:15 am
content: In some PIC24s there may be two things at play:One of the configuration registers may have a bit which determines how many times certain other registers are able to be changed. Things like peripheral pin assignments and oscillator config can be limited to a single configuration between resets.If that bit is set to allow multiple reconfigurations then you may need to perform an unlock sequence sequence each time you want to change them (this is meant to prevent inadvertant changes during operation). There are some built-in macros in xc24 that allow you to unlock the OSCCON register, which then also allows things like the PPS configuration to be changed. It's something along the lines of __builtin_osccon_unlock or thereabouts. You could check the xc24 manual for details.I don't have access to specifics right now so going by memory, but look into those two things and see how you get on.

author: alj
author_rank: Contributor
timestamp: April 01, 2020, 10:25:49 am
content: I read all dox left and right. There is FCKSM1 configuration bit which unlocks writing to OSCCON, but I'm not even touching it.General PIC24F dox says i can write to REFOCONbits.RODIV as many times as i like without even disabling REFOCONbits.ROEN (although it is recommended to disable then enable after change). But.... i cannot even do that alone (disable then enable). What is really weird is that if i wasnt allowed to modify REFOCON then i should not be able to disable it, but i can enable then disable and then I cannot reenable it anymore.This is chip datasheethttp://ww1.microchip.com/downloads/en/DeviceDoc/30001037c.pdfpage 101 says about FCKSM1page 102 says about REFOCONHere is general oscillator guidehttp://ww1.microchip.com/downloads/en/devicedoc/39700c.pdffrom 6:13 ..... "This postscaler can be changed as needed at run time. It is recommended that theclock generator be disabled prior to changing the RODIV bits to ensure the smoothest speedtransition."Chip errata:http://ww1.microchip.com/downloads/en/DeviceDoc/80000534e.pdf... it is amazing that they mislabeled chip pins in the doc. Cost me some lost time and PCB cutting.And this chip does not have PPS so it seems. I'm almost ready to redo PCB and try it on some other PIC (or maybe get different DIP20 chip with PPS and make it pin-compatible) but what if (as most oftenly happens) this is my screwup and that will not fix the issue.....

author: TomS_
author_rank: Frequent Contributor
timestamp: April 01, 2020, 11:11:00 pm
content: Hm yes, tricky situation. I would also post on the Microchip forum itself. Dont get me wrong, there are some very helpful people here on the EEVblog forum and I have had some of my own questions answered here, but you may find more focussed and specialised help over there.

author: TomS_
author_rank: Frequent Contributor
timestamp: April 02, 2020, 10:55:28 pm
content: Not sure what else. I read through the docs and it seems straight forward enough, but Ive never used anything like this personally so other than going right back to basics (although your code doesnt look overly complex) I feel fresh out of ideas.The *only* thing I would try is using the OSCCON unlock sequence and see if that does anything at all. It doesnt seem like it will from what the docs say, but sometimes you never know. Could be a new erratta item.Final resort, perhaps make a support request to Microchip themselves and see if you can get any joy directly from them.

author: alj
author_rank: Contributor
timestamp: April 18, 2020, 10:46:02 pm
content: I figured out the issue.1. When changing divisor need to cycle ROEN off and on.2. When you enable output you cannot disable it immediately, THis is what caused the problem so it seem. I removed code which was setting divisor 2-nd time right away (and cycling ROEN off and on) and problem went away. However this issue does not happen if clock source is system clock, i e only with external clock source.

author: TomS_
author_rank: Frequent Contributor
timestamp: April 21, 2020, 08:50:33 am
content: Good to know you got it sorted.I suppose there's nothing in the manual to suggest you had to do that...