| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			thomas1234
 
 
  Joined: 27 Oct 2014 Posts: 1
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| stupid problem with pwm | 
			 
			
				 Posted: Mon Oct 27, 2014 12:35 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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 
			
			 
			 
			
			
			 
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Oct 27, 2014 1:43 pm     | 
				     | 
			 
			
				
  | 
			 
			
				* 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: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Oct 27, 2014 2:13 pm     | 
				     | 
			 
			
				
  | 
			 
			
				and of course the obvious
 
 
does a 1Hz LED program work ??
 
 
jay | 
			 
		  | 
	
	
		  | 
	
	
		
			Jim90
 
 
  Joined: 27 Apr 2013 Posts: 55
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| stupid problem with pwm | 
			 
			
				 Posted: Wed Nov 05, 2014 11:29 am     | 
				     | 
			 
			
				
  | 
			 
			
				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: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Wed Nov 05, 2014 12:04 pm     | 
				     | 
			 
			
				
  | 
			 
			
				| hmm.. the OP hasn't said which PIC he's using..... | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Nov 06, 2014 1:18 am     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| stupid problem with pwm | 
			 
			
				 Posted: Thu Nov 06, 2014 8:16 am     | 
				     | 
			 
			
				
  | 
			 
			
				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: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Nov 06, 2014 8:22 am     | 
				     | 
			 
			
				
  | 
			 
			
				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: 1968 Location: Norman, OK 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Nov 06, 2014 2:04 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Nov 07, 2014 2:22 am     | 
				     | 
			 
			
				
  | 
			 
			
				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. | 
			 
		  | 
	
	
		  | 
	
	
		 |