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

stupid problem with pwm

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



Joined: 27 Oct 2014
Posts: 1

View user's profile Send private message

stupid problem with pwm
PostPosted: Mon Oct 27, 2014 12:35 pm     Reply with quote

I face a stupid problem with pwm.

I need to have a pwm. This pwm need to be activate if a switch is on and deactivate when switch is off.

i used the following code, but something is going wrong:


Code:

while(PIN_B5)
{
setup_compare(PIN_B14, COMPARE_PWM | COMPARE_TIMER2);
set_pwm_duty(PIN_B14,10);

}


what is the problem ?
i use PCD compiler v4.09
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Oct 27, 2014 1:43 pm     Reply with quote

* you never setup or initialized timer2
* never setup the pwm
* you use wrong syntax in set_pwm duty

* v 4.09 is a dodgy old, muchly pirated version of the compiler

UPDATE compiler, and read the manual.
then you might be in better shape
temtronic



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

View user's profile Send private message

PostPosted: Mon Oct 27, 2014 2:13 pm     Reply with quote

and of course the obvious

does a 1Hz LED program work ??

jay
Jim90



Joined: 27 Apr 2013
Posts: 55

View user's profile Send private message

stupid problem with pwm
PostPosted: Wed Nov 05, 2014 11:29 am     Reply with quote

Actually the problem appear also to compiler 5.007

for example by using the example EX_PWM_PCD.C
Code:
void main(void)
{
int value=10;
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 0xFFC0);
setup_compare(1, COMPARE_PWM|COMPARE_TIMER2);
set_pwm_duty(1,value * (unsigned int16)64);
}


i receive the error message:
Invalid parameters to build in function ::Invalid Pin

at the line:
Code:
setup_compare(1, COMPARE_PWM|COMPARE_TIMER2);


I think that the problem is coming from the header file of the dspic33fj128GP802.h

beacuse for the dspic30F2010 everything is working fine.
temtronic



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

View user's profile Send private message

PostPosted: Wed Nov 05, 2014 12:04 pm     Reply with quote

hmm.. the OP hasn't said which PIC he's using.....
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Nov 06, 2014 1:18 am     Reply with quote

The answer is simple.
Nothing to do with the processor include file.
All to do with the data sheet, and the capabilities of the processor.

Does this PIC have a fixed OC1 pin for the compare function to use?.

Answer. No.

_You_ have to tell the compiler which of the relocatable peripheral pins you want to use for the peripheral, _before_ you try to use it.

The compiler is telling you what is wrong in it's error message 'Invalid pin' is pretty obvious.
Jim90



Joined: 27 Apr 2013
Posts: 55

View user's profile Send private message

stupid problem with pwm
PostPosted: Thu Nov 06, 2014 8:16 am     Reply with quote

this is not the case for me.

The problem appear at the line

Code:

setup_compare(1, COMPARE_PWM|COMPARE_TIMER2);



timer2,compare pwm,compare timer2 is provided by the microcontroller.

what is going wrong?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Nov 06, 2014 8:22 am     Reply with quote

It is the answer for you.

You are trying to use setup_compare, which uses the OC1 pin.

On chips with relocatable peripherals, you _have_ to tell the compiler which pin(s) to use, before using functions that use the peripherals.

Hence it is telling you that it doesn't have a valid pin setup for this function.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Thu Nov 06, 2014 2:04 pm     Reply with quote

Search this forum for the term pin_select for a number of examples.
Also see ex_pinselect and ex_pinselect2 in the examples folder.
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Nov 07, 2014 2:22 am     Reply with quote

As a further comment, you don't want to be continuously sitting in a loop re-configuring the peripheral. It only needs to be setup _once_ unless you are changing things.
So something like:
Code:

   if (input(PIN_B5))
   {
       setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
       set_pwm_duty(1,10);
       while (input(PIN_B5)) ; //wait for pin to release
       set_pwm_duty(1,0); //turn off PWM
   }


The first parameter in the compare function is the PWM unit number, _not_ a pin number.
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