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

clock switching at PIC24 does not work
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



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

View user's profile Send private message

PostPosted: Mon May 10, 2021 4:49 am     Reply with quote

I encourage all that run PICs on batteries to download and read Microchip's AN606 application note. While written around 1997, it is STILL a great 'primer' or 'how-to-do-it' reference. While not a lengthy document it is FULL of important information that anyone, everyone.. that runs a PIC from a battery should read.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon May 10, 2021 6:34 am     Reply with quote

Yes, and one key point made there is that it is actually more efficient in
power terms to run the chip at a slightly higher speed for shorter periods,
rather than lowering the clock rate.
andresteff



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

PostPosted: Mon May 10, 2021 1:01 pm     Reply with quote

Which chip of PIC18 do you mean?
Many years i use the PIC18LF1220.
One with the lowest current consumption i know.
Idd(max)=330µA@1Mhz/3V, but Fcy=Fosc/4!!!
The Pic24 from this thread is no worse!

I take the pic24 because of the trigger mode of the PWM mode. The PIC18 don't have this feature. One PWM pulse for a trigger event.
In this feature, i find a bug in the CCS compiler, for the init of the compare module. I will still post it.
Moreover i need the comparators.

Originally i would take the PIC16LF1619. This one have a hardware-limit-timer (HLT) and can produce also one PWM pulse for an trigger event.
But on these chip, i have find a hardware bug for the HLT!!! I have posted this on this forum.
The development department of microchip confirmed me that.
I think, all PICs with HLT, have this bug.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon May 10, 2021 11:41 pm     Reply with quote

OK. So you have a sensible reason to want this chip. Generally you can
generate single pulses from a trigger on the PIC16/18's, by using the CCP
as a CCP, rather than a PWM. So you program it to turn the pulse 'off'
after a defined time.
The low power PIC18's, are the ones they define as XLP, so chips like the
LF25K40. On this running on the LFINTOSC, you have a consumption of
just 0.4uA. Then with the low power optimised HFINTOSC, at 1MHz, just on
100uA.
andresteff



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

#use delay with 31k LPRC is wrong
PostPosted: Thu Jun 03, 2021 10:11 am     Reply with quote

@Ttelmah: help!
You had written me the code for the 31kHz LPRC-oscillator.
At that time I had tested the code with wdt, and the code ran well!
Now I have turned off the wdt, and find that the delay function does not work properly!

Below in the code i use the 32,5khz LPFRC and the 31k LPRC.
LPFRC: delay ok
LPRC: delay nok
Code:

#include <24F16KA301.h>
#device ADC=8       
#device ICSP=3

#use delay(internal=500000)

#fuses LPRC                      //Internal low power RC Oscillator
#fuses LPFRC_DIV              //Low-Power FRC oscillator with divide-by-N
#fuses LPRCLOW                //Low-Power FRC Low Power and Low Accuracy
#fuses CKSFSM                 //Clock Switching is enabled, fail Safe clock monitor is enabled

#fuses WDT_SW                    //No Watch Dog Timer, enabled in Software


#FUSES NOBROWNOUT                //No brownout reset
#fuses MCLR                      //Master Clear pin enabled
#fuses PUT                       //Power Up Timer
#fuses SOSC_DIGITAL              //SOSC pins set for Digital mode for use with external clock or Digital I/O


#define LED_gr                PIN_B4
#define LED_RT               PIN_B7   // Auch TX


#WORD OSCCON=getenv("SFR:OSCCON")
#WORD CLKDIV=getenv("SFR:CLKDIV")
#DEFINE NOSC 0b0101
#bit OSWEN=OSCCON.0

void change_toRC31K(void)
{
   //routine to switch oscillator to the RC at 31000Hz, on compiler not suporting this
#ASM
   //MOV.b NOSC, W0 //new value to load
   MOV.b #5, W0 //new value to load
   MOV #0x743,W1 //address to write to
   MOV #0x78, W2
   MOV #0x9A, W3 //preload unlock sequence
   DISI #5
   //write unlock
   MOV.b W2, [W1]
   mov.b W3, [W1]
   mov.b W0, [W1]
   
   MOV.b #1, W0 //new value to load OSWEN
   MOV #0x742,W1 //address to write to
   DISI #5
   MOV #0x46, W2
   MOV #0x57, W3 //preload unlock sequence
   //write unlock
   MOV.b W2, [W1]
   mov.b W3, [W1]   
   //Trigger switch
   mov.b W0, [W1]
#ENDASM   
   //Now need to wait for the RC oscillator to start
   delay_us(70); //wait for LPRC to start. Needs 70uSec. Time will be variable since
   //clock may switch at some point
   while (OSWEN!=0)
      ; //wait for switch to be successful
}


void main()
{
            //setup_wdt(WDT_8S);
            setup_wdt(WDT_OFF);
            restart_wdt();
           
            output_low (PIN_B9);     
            output_low (PIN_B0);       
            output_low (PIN_B1);       
            output_low (PIN_A3);
            output_low(PIN_A4);
            output_low(LED_RT);
            output_low(PIN_B4);
            output_high(PIN_B12);
            output_high(PIN_B8);
         
         
// LPFRC=31250kHz           
            setup_oscillator(OSC_LPFRC, 31250);     
            #use delay(clock=31250)
            delay_ms(100);
         
            output_high(LED_RT);
            delay_ms(1000);
            output_low(LED_RT);
            delay_ms(2000);
            restart_wdt();
           
           
 // LPRC=31kHz           
            change_toRC31K();
            #use delay(clock=31000)
            delay_ms(100);
           
             output_high(LED_RT); 
            delay_ms(1000);
            output_low(LED_RT);
            delay_ms(2000);
            restart_wdt();
           
             while(1){ restart_wdt();} // "wdt-loop", after 8s wdt time out
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Jun 03, 2021 12:18 pm     Reply with quote

Having the watchdog enabled, keeps the LPRC running. If it is not enabled
the LPRC physically stops. Look at the datasheet.
andresteff



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

PostPosted: Thu Jun 03, 2021 12:45 pm     Reply with quote

I look at the datasheet...
I switch off the watchdog and switch on the LPRC!
In the data sheet is that one of three "switches" must be selected to turn on the LPRC.
That should be ok, but the delay does not work properly.

DS39726A-page 38-19:

Since it serves the PWRT clock source, the LPRC oscillator is enabled at POR whenever the
on-board voltage regulator is disabled. After the PWRT expires, the LPRC oscillator will remain on
if any one of the following is true:
• The FSCM is enabled
• The WDT is enabled
• The LPRC Oscillator is selected as the system clock (COSC<2:0> = 101)
If none of the above is true, the LPRC will shut off after the PWRT expires.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Jun 04, 2021 1:22 am     Reply with quote

How do you 'switch on the LPRC'?.
You would have to set the LPRC as the primary clock. Remember we have
already found that if you select 31KHz, the compiler defaults to using the
faster oscillator not the LPRC.
andresteff



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

PostPosted: Mon Jun 07, 2021 2:06 pm     Reply with quote

my mistake.
I have set the Fuse.
"#fuses LPRCLOW //Low-Power FRC Low Power and Low Accuracy".
therefore the wrong delay.

A small error, however, in the description of the Fuse.
The low-high power mode applies only to the LPRC, not to the FRC!
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Jun 07, 2021 11:47 pm     Reply with quote

Well done finding this.
This sort of glitch in the nomenclature is why I always sit down and 'check'
what fuses actually 'are' after a selection. Sad
andresteff



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

bug fixes CCS
PostPosted: Mon Jun 14, 2021 3:46 am     Reply with quote

@Ttelmah:
How does ccs learn about the incorrect behavior of the 31kHz.
And also because of the wrong description of the fuses (High Power and High Accuracy).

Should I write to ccs directly?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Jun 14, 2021 5:08 am     Reply with quote

One word answer.
Yes.... Very Happy

The very top of the forum page says 'where'.
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 Previous  1, 2
Page 2 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