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

24FV32KA301 PWM freq. not works

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



Joined: 05 Nov 2012
Posts: 4
Location: United States

View user's profile Send private message

24FV32KA301 PWM freq. not works
PostPosted: Mon Nov 05, 2012 8:19 pm     Reply with quote

How can I get to 320Khz PWM with this chip? The most I can get is 243hz and I had try many combinations, I use a 20Mhz external crystal.

Code:

#include <24FV32KA301.h>

#device ICD=TRUE
#INCLUDE <string.h>
#INCLUDE <ctype.h>

//Primary Oscillator with PLL
#FUSES NOWDT
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES WDT_NOSL                 //Watch Dog Timer, disabled during SLEEP
#FUSES NOBROWNOUT               //No brownout reset

//#use delay(crystal=20000000)

#use delay(internal=32Mhz)

//#pragma use rs232(UART1,baud=9600,xmit=PIN_B7,rcv=PIN_B2,parity=N,bits=8,ERRORS,RESTART_WDT,stream=COM1)
//#pragma use rs232(UART1,baud=9600,xmit=PIN_B7,rcv=PIN_B2,parity=N,bits=8,ERRORS,RESTART_WDT,stream=COM1)
//#pragma use rs232(UART2,baud=115200,xmit=PIN_B0,rcv=PIN_B1,parity=N,bits=8,ERRORS,RESTART_WDT,stream=COM2)

//#define BUFFER_SIZE 240
//#define BUFFER_SIZE3 70
//#define BUFFER_SIZE2 240
//BYTE buffer_1[BUFFER_SIZE];
//BYTE buffer_3[BUFFER_SIZE3];
//BYTE next_in_1 = 0;
//BYTE next_out_1 = 0;


// buffer_2[BUFFER_SIZE2];
//BYTE next_in_2 = 0;
//BYTE next_out_2 = 0;
//int tam=0;

/////////////////////////////////////////////////////////////////////////
//               //
/////////////////////////////////////////////////////////////////////////

void main() {

int16 duty;
int16 value=32768L;

duty = 0;

setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1,39);
setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER2);

    While(1){
       if(!input(PIN_B1)){
           delay_ms(330);
          if(!input(PIN_B1))
              value=value+1000;
        }
        if(!input(PIN_B2)){
         
           delay_ms(300);
           if(!input(PIN_B2))
           value=value-1000;
        }
        duty= value ;
       set_pwm_duty(1, duty);
     
    }
}

With this configuration I alway have 240 Hz, if I use external crystal, freq. is always 150 hz.
How can I modify freq. to obtain 320 Hz ?

setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1,39); no works period, i change value but always have 240 hz.
Thanks
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 3:09 am     Reply with quote

I'm confused, about what frequencies you are using and trying to generate.

Quote:
How can I get to 320Khz PWM with this chip? The most I can get is 243hz and I had try many combinations, I use a 20Mhz external crystal.

Code:

...

#use delay(internal=32Mhz)

setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1,39);

With this configuration I alway have 240 Hz, if I use external crystal, freq. is always 150 hz.
How can I modify freq. to obtain 320 Hz ?

setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1,39); no works period, i change value but always have 240 hz.


You're:-

1) Asking for 320kHz, then 320Hz.
2) Quoting 20MHz crystal then using 32Mz for delay.

Is your crystal working properly?
Does an LED flasher work at the correct frequency?
Seriously, are you wanting kHz or Hz?

Mike

EDIT And, of course, complier version No.
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 4:05 am     Reply with quote

Also:

You continually update the duty cycle, even if the value hasn't changed.

What is the maximum period with your clock and PWM frequency (equation 15-3 in the data sheet)?. Divider required is 49, and you have just 5.6bits of resolution. Maximum period value of just 50, but you are using 32767, and updating in thousands.....

Best Wishes
chinukis



Joined: 05 Nov 2012
Posts: 4
Location: United States

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 10:52 am     Reply with quote

Thanks for your help.

About frequency, I need 320 Hz,
I'm using 20MHz crystal,
Ver.4.134 CCS compiler
About 20Mhz and 32Mhz,
I use internal 32MHz because with 20 MHz I only get 150 Hz.

I follows equation and nothing, that is the reason for the period value. I was playing with numbers, because nothing seems to work.

The duty cycle is in a loop because I use input B1 and B2 to increase and decrease duty.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 12:05 pm     Reply with quote

Work on the KISS principle.

Remove surplus code (RS232 etc).

Start with a LED flasher. Confirm this works at the correct frequency.

Then try PWM at a fixed frequency and sensible fixed duty cycle (i.e. LESS than the period).

Learn to walk before attempting to run with push buttons.

Mike
chinukis



Joined: 05 Nov 2012
Posts: 4
Location: United States

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 1:58 pm     Reply with quote

Thanks for the tip

But dosent works

Code:
#include <24FV32KA301.h>

#device ICD=TRUE


//Primary Oscillator with PLL
#FUSES NOWDT
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES WDT_NOSL                 //Watch Dog Timer, disabled during SLEEP
#FUSES NOBROWNOUT               //No brownout reset

#use delay(crystal=20000000)

//////////////////////////////////

void main() {





setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER2);
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1,225);

set_pwm_duty(1, 122);
     
    While(1);

       
   
}



With this configuration, always have 150 Hz,
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 3:32 pm     Reply with quote

You are not actually turning on the primary oscillator. Since you have fail safe mode enabled, it is dropping back to the secondary, which is giving you 62.5KHz. 62500/(2*226) = 138Hz.

#fuses HS

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 3:37 pm     Reply with quote

Have you tried and tested a simple LED flasher?

What duty ratio are you getting?

What happens if you change your programmed duty ratio?

Mike
chinukis



Joined: 05 Nov 2012
Posts: 4
Location: United States

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 5:28 pm     Reply with quote

Hi,

Also i use #fuses HS and nothing

duty is not a problem, but you need to use more than 255,
i use it from 0 to 65535 to get 0 to 100 % duty cycle
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 5:55 pm     Reply with quote

We're trying to help you, but you're still not clearly answering the questions.

It's bed time here.

Bye

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 3:40 am     Reply with quote

chinukis wrote:


set_pwm_duty(1, 122L);



Seriously, take the hint, that has been given multiple times. Run a simple 'three line LED flasher', and verify the frequency of this, _before_ trying to use the PWM. You _must_ repeat _must_ get your clock working and know that it is working at the right rate, before trying to get the PWM going. You are trying to 'walk before you can crawl'.

Best Wishes
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