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 CCS Technical Support

PWM frequency falls in 4 times after sleep.

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



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PWM frequency falls in 4 times after sleep.
PostPosted: Wed Apr 20, 2016 1:44 am     Reply with quote

Why PWM frequency falls in 4 times after sleep instruction (at the end of code)?
When I comment these strings frequency does not change.

Code:

#include <12F1501.h>
#device ADC=8

#FUSES WDT                      //Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(internal=8000000)
#define RAND_MAX  255
#INCLUDE <STDLIB.H>


int16 pause , delay_sec;
int d1, d2, d3, d4;
int work_counter=1;
void main()
{
   setup_wdt(WDT_16S);      //~16,0 s reset
   setup_adc_ports(sAN1);
   setup_adc(ADC_CLOCK_DIV_64);
   setup_timer_2(T2_DIV_BY_64,63,16);  // 5kHz 8bit PWM @ 8MHz    (0-255)
   setup_PWM1(PWM_enabled|PWM_OUTPUT);
   setup_PWM2(PWM_enabled|PWM_OUTPUT);
   setup_PWM3(PWM_enabled|PWM_OUTPUT);
   setup_PWM4(PWM_enabled|PWM_OUTPUT);
   set_adc_channel(1);
   srand(142);
   while(TRUE)
   {     
      delay_ms(1);
      pause=read_adc();
         
      srand(work_counter);
      d1=rand();
      d2=rand();
      d3=rand();
      d4=rand();
     
      delay_sec=rand();            // ãåíåðèðóåì ñëó÷àéíîå âðåìÿ ðàáîòû ìîòîðîâ îò 0 äî 255 ñåê
     
      set_PWM1_Duty(d1); 
      set_PWM2_Duty(d2);
      set_PWM3_Duty(d3); 
      set_PWM4_Duty(d4); 
     
      for (int a=0; a<delay_sec;a++) // èñïîëíÿåì ñëó÷àéíîå âðåìÿ ðàáîòû ìîòîðîâ îò 0 äî 255 ñåê ñ ðåñòàðòîì âà÷äîãà
      {restart_wdt();
      delay_ms(1000);
      }
     
      set_PWM1_Duty(0); //off
      set_PWM2_Duty(0); //off
      set_PWM3_Duty(0); //off
      set_PWM4_Duty(0); //off
     // setup_PWM1(PWM_disabled);
     // setup_PWM2(PWM_disabled);
     // setup_PWM3(PWM_disabled);
     // setup_PWM4(PWM_disabled);
      restart_wdt();
     
      for (int i=0; i<pause; i++) //óõîäèì â sleep íà âðåìÿ time*16 sec
      sleep();
      work_counter++;
   }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Apr 20, 2016 2:39 am     Reply with quote

Clipped code section, comments inline:
Code:

      //restart_wdt(); - sleep automatically restarts the WDT
     
      for (int i=0; i<pause; i++) //óõîäèì â sleep íà âðåìÿ time*16 sec
      {
         sleep();
         delay_cycles(1); //sleep, should always be followed by a NOP
         //Not having this can cause slight problems if things like an
         //interrupt triggers a wake - safer to have it.
      }
      setup_oscillator(OSC_NORMAL | OSC_8MHZ);
      work_counter++;

If you look at the 'sleep' entry in the data sheet, the first thing done when entering sleep, is to clear the WDT, so you don't need to restart the wdt before calling this.
The oscillator 'wakes' in a default mode. On the boot, the compiler automatically sets it up to your requested 8MHz. After sleep it is back to the default mode, so you need to reset it.
40inD



Joined: 30 Jul 2007
Posts: 112
Location: Moscow, Russia

View user's profile Send private message

PostPosted: Wed May 25, 2016 11:58 pm     Reply with quote

I have one more problem.
The real PWM frequency is 488 HZ. But timer2 is configured for 5kHz (as says PICWizard). See code above.
I think it's wrong.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu May 26, 2016 1:25 am     Reply with quote

Forget wizards....

8MHz.

Fcy = 2MHz

/64 = 31250Hz

/64 (PR2=63 -> /64) = 488.28125Hz.

For 5KHz, you need Fcy/400

setup_timer_2(T2_DIV_BY_8,49,16); (50*8=400)

Which would give 0-199 for the PWM range

Or using the same divider

setup_timer_2(T2_DIV_BY_8,63,16);

Which would give 3906.25Hz.

However I've just gone and used the wizard for this chip, and it does not give 5KHz for the settings you have (It give 1952Hz). So it is forgetting the initial /4 built into the chip, but doesn't give your figure....

It only gives 5KHz for your settings, if you have not changed the master clock figure from the default 20MHz.
So, fault in it (*4), and fault in your settings (*2.5). GIGO.
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