View previous topic :: View next topic |
Author |
Message |
killer_fighting
Joined: 22 Feb 2012 Posts: 9
|
PWM for PIC16F648A |
Posted: Fri Feb 24, 2012 5:42 am |
|
|
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
|
PWM |
Posted: Fri Feb 24, 2012 5:56 am |
|
|
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
|
Re: PWM |
Posted: Fri Feb 24, 2012 5:58 am |
|
|
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
|
PWM |
Posted: Fri Feb 24, 2012 6:46 am |
|
|
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
|
|
Posted: Fri Feb 24, 2012 7:06 am |
|
|
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
|
Duty ratio |
Posted: Fri Feb 24, 2012 7:37 am |
|
|
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
|
|
Posted: Sat Feb 25, 2012 6:59 am |
|
|
Hi guys,
I found out the problem already! My programming is okay, but there is problem for my PIC fuses. Thanks. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
#fuses |
Posted: Sat Feb 25, 2012 7:19 am |
|
|
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
|
|
Posted: Sat Feb 25, 2012 3:44 pm |
|
|
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. |
|
|
|