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

PWM for PIC16F648A

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



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PWM for PIC16F648A
PostPosted: Fri Feb 24, 2012 5:42 am     Reply with quote

I am currently using PIC16F648A. I have succeeded to build my programming code. However, I can't detect any signal from pin B3 (ccp1) directly from my probe. Maybe I know what is the reason that this will happen?
Mike Walne



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

View user's profile Send private message

PWM
PostPosted: Fri Feb 24, 2012 5:56 am     Reply with quote

Post a SHORT section of compilable code which shows the problem. I.e. less that a screen-full with your version No.

You are making us second guess at the cause. From your code it should be obvious.

Mike
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

Re: PWM
PostPosted: Fri Feb 24, 2012 5:58 am     Reply with quote

Mike Walne wrote:
Post a SHORT section of compilable code which shows the problem. I.e. less that a screen-full with your version No.

You are making us second guess at the cause. From your code it should be obvious.

Mike


Sorry, below is my code:
Code:

#include <16F648A.h>
#fuses NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP,INTRC_IO
#use delay(clock = 4000000)

void main()
{
output_low(PIN_B3);   // Set CCP1 output low
setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 39, 1);  // 25 kHz     
set_pwm1_duty(15);                  // 25% duty cycle on pin B3 Must always less than '39'
delay_ms(3000);
while(1);  // Prevent PIC from going to sleep  (Important !)
}
Mike Walne



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

View user's profile Send private message

PWM
PostPosted: Fri Feb 24, 2012 6:46 am     Reply with quote

You have not posted a version No. It's possible, but unlikely, there is problem with your version.

Your code looks OK. When compiled it ran in MPLAB SIM.

I don't have your PIC but I do have a PIC16F876A.

The code runs OK on the PIC16F876A, albeit with PWM now on pin C2.

Try a simple 2Hz LED flasher on your B3 pin. You may have a hardware problem.

Incidentally setting duty to 15 does NOT give 25% ratio, it's simple arithmetic. When you post code use the code box or you lose indenting.

Mike
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PostPosted: Fri Feb 24, 2012 7:06 am     Reply with quote

I changed the PIC already. And I have checked there's 5v in the PIC. However, there is zero voltage at CCP1 pin. How can I get the correct PWM duty cycle?
Mike Walne



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

View user's profile Send private message

Duty ratio
PostPosted: Fri Feb 24, 2012 7:37 am     Reply with quote

25% of 40 is 10, NOT 15.

You need to step back from the problem.

(1) Have you got decoupling caps close to the PIC power pins?
(2) Have you checked that your clock is running correctly?
(3) Can you get your PIC to do ANYTHING?
(4) Have you tried a simple LED flasher to prove that the CCP1 pin is NOT being held low by something else external to the PIC?

Your PIC has an internal oscillator option, you could try that. You get the syntax from the CCS manual AND the 16F648A.h file. You need BOTH to get ALL the information.

Be certain that your hardware is OK FIRST, then worry about duty ratio.

As I said before, someone else may be aware of a known bug on the CCP1 for your PIC. For an old device you would expect all such issues to have been resolved long since. However CCS does sometimes introduce silly mistakes with compiler upgrades.

Mike
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PostPosted: Sat Feb 25, 2012 6:59 am     Reply with quote

Hi guys,

I found out the problem already! My programming is okay, but there is problem for my PIC fuses. Thanks. Very Happy
Mike Walne



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

View user's profile Send private message

#fuses
PostPosted: Sat Feb 25, 2012 7:19 am     Reply with quote

Which #fuse was wrong?

Did you have the correct fuse for your clock?

It helps if you tell us so that we can pass it on!

Mike
killer_fighting



Joined: 22 Feb 2012
Posts: 9

View user's profile Send private message

PostPosted: Sat Feb 25, 2012 3:44 pm     Reply with quote

Code:

#include <16F648A.h>
#fuses NOWDT, NOPROTECT, BROWNOUT, NOPUT, NOLVP,INTRC,NOCPD,NOMCLR
#use delay(clock = 4000000)

void main()
{
output_low(PIN_B3);   // Set CCP1 output low
setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 39, 1);  // 25 kHz     
set_pwm1_duty(20);                  // 25% duty cycle on pin B3 Must always less than '39'
delay_ms(3000);
while(1); 
}

Initially, I didn't put "NOCPD and NOMCLR" in fuses. Finally my programming code is works!

Above is my final programming code for PWM.
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