|
|
View previous topic :: View next topic |
Author |
Message |
pk178
Joined: 10 Mar 2010 Posts: 10
|
Help me with PWM |
Posted: Wed Mar 10, 2010 7:56 am |
|
|
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
|
|
Posted: Wed Mar 10, 2010 9:47 am |
|
|
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
|
|
Posted: Thu Mar 11, 2010 9:01 pm |
|
|
I've already tested it in real circuit, It works well, no matter, the problem is simulator software !!!
Thanks for reply ! |
|
|
|
|
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
|