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

Help me with PWM

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



Joined: 10 Mar 2010
Posts: 10

View user's profile Send private message

Help me with PWM
PostPosted: Wed Mar 10, 2010 7:56 am     Reply with quote

Hi everybody !

I want to test PWM on CCP1 pin of PIC16F887A, this is my code :

Code:
#include <16F887.h>
#device *=16,ADC = 8
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
//==============================================================================

int duty = 25;

#int_ext
void interrupt_rb0(){ 
  duty = read_ADC(); 
  set_pwm1_duty(duty);
}
//==============================================================================

void main() { 
   
   set_tris_b(0x01);
   
   setup_adc(ADC_CLOCK_DIV_32);   // sample time = IC clock
   setup_adc_ports(sAN0);        // Set PIN_A0 as analog input 1 
   enable_interrupts ( int_ext ) ;
   enable_interrupts ( global ) ;
   
   setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM             
   set_pwm1_duty(duty);                   
   setup_timer_2(T2_DIV_BY_1, 255, 1);
   while(1);  // Prevent PIC from going to sleep  (Important !)
}
//==============================================================================


I want to use interrupt on RB0 pin to change duty cycle, duty cycle is read from ADC value at RA0, I simulated on proteus software. At first, the program run correctly, but when I press interrupt button to change duty cycle, the CCP1 pin always at 0 level, PWM is not working.

I don't know why, please help me ! Thanks !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 10, 2010 9:47 am     Reply with quote

You need to set the ADC channel. Do this in main(), after the other
two lines that setup the ADC.
pk178



Joined: 10 Mar 2010
Posts: 10

View user's profile Send private message

PostPosted: Thu Mar 11, 2010 9:01 pm     Reply with quote

I've already tested it in real circuit, It works well, no matter, the problem is simulator software !!!

Thanks for reply !
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