View previous topic :: View next topic |
Author |
Message |
castek
Joined: 12 Jul 2012 Posts: 14
|
PWM problem with pic16F1503 |
Posted: Fri Jul 13, 2012 6:38 am |
|
|
Hello everyone!
I'm trying to setup pwm1 on pic16F1503 but I don't have success.
I try to setup_ccp1(CCP_PWM) as I saw in forums, but compiler does not recognize that function. It seems way to do it has changed:
I saw inside pic16f1503.h file functions for pwm are
Code: |
////////////////////////////////////////////////////////////////// PWM
// PWM Functions: SETUP_PWMx, SET_PWMx_DUTY
// Constants used for SETUP_PWMx() are:
#define PWM_ENABLED 0x80
#define PWM_DISABLED 0
#define PWM_OUTPUT 0x40
#define PWM_ACTIVE_LOW 0x10
#define PWM_ACTIVE_HIGH 0
#bit PWM1OUT=getenv("BIT:PWM1OUT")
#bit PWM2OUT=getenv("BIT:PWM2OUT")
#bit PWM3OUT=getenv("BIT:PWM3OUT")
#bit PWM4OUT=getenv("BIT:PWM4OUT")
|
But I dont know how to use this....
Can anyone help me?? |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Fri Jul 13, 2012 6:55 am |
|
|
for your PIC
SETUP_PWMx() is the right function as you saw on the .h File.
and the constants you can use are:
PWM_ENABLED
PWM_DISABLED
PWM_OUTPUT
PWM_ACTIVE_LOW
PWM_ACTIVE_HIGH
.
.
.
soooo... you can do things like: SETUP_PWMx(PWM_ENABLED);
Same as "setup_ccp1(CCP_PWM)" but with the appropiate names for your PIC.
What is your compiler version? _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Jul 13, 2012 10:51 am |
|
|
solid support - even on 4.134 for 16f15xx still has a way to go.
unless you need some other unique feature of that chip
an older pic may be a better choice. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 13, 2012 10:56 am |
|
|
Quote: | but compiler does not recognize that function.
|
Always post your compiler version. The amount of support for a new PIC
is dependent upon the compiler version. |
|
![](templates/subSilver/images/spacer.gif) |
castek
Joined: 12 Jul 2012 Posts: 14
|
|
Posted: Fri Jul 13, 2012 11:39 am |
|
|
Thanks for you help!!!
My compiler version is 4.133.
I like new pics because they are cheap and more powerful than older ones.
Are there anyway to run pwm on my new pic16F1503??
Which code have I got to use???
Best Rgds. |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Jul 13, 2012 11:56 am |
|
|
I would become familiar with these registers in section 22 of the data sheet:
Quote: |
• PR2
• T2CON
• PWMxDCH
• PWMxDCL
• PWMxCON |
as in, #DEFINE them, and a also a few significant bits ( #BIT )
and then direct manipulate those registers to get PWM functions.
That hinges on any FUSES you need being set correctly by the compiler too.
That is another whole can of wiggly worms if you are not used to doing such. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 13, 2012 1:37 pm |
|
|
I don't have a 16F1503, but I have a 16F1507 and it's in the same PIC
family. I installed compiler vs. 4.133 and ran the following test program.
It works. The PIC outputs a pwm signal on pin C5, with a frequency of
4.0 KHz and a 50% duty cycle. Try this program and see if it works with
your PIC:
Code: |
#include <16F1507.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
//=========================================
void main()
{
setup_pwm1(PWM_ENABLED | PWM_OUTPUT | PWM_ACTIVE_HIGH);
setup_timer_2(T2_DIV_BY_1, 249, 1);
set_pwm1_duty(500L);
while(1);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
castek
Joined: 12 Jul 2012 Posts: 14
|
|
Posted: Mon Jul 16, 2012 5:53 am |
|
|
It works!!!
Thanks a lot
CCS is GREAT |
|
![](templates/subSilver/images/spacer.gif) |
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Jul 16, 2012 8:01 am |
|
|
Hi,
Hmmm, the compiler was already capable of doing what you wanted, so
while CCS may indeed be great, in this case, I think the real praise should
go to the forum members that helped you, and in particular, to PCM
programmer!
John |
|
![](templates/subSilver/images/spacer.gif) |
twidget
Joined: 21 Feb 2013 Posts: 32 Location: Orange County, California
|
|
Posted: Wed Mar 06, 2013 12:53 am |
|
|
Well I will say Thank you, I am working on a project with PIC16F1509 and the PWM was killing me! This thread helps a lot! Thank you PCM programmer. |
|
![](templates/subSilver/images/spacer.gif) |
mrcdcscc
Joined: 02 Dec 2013 Posts: 1
|
|
Posted: Mon Dec 02, 2013 2:06 pm |
|
|
Dear PCM programmer,
God bless you! I was suffered by PWM on PIC16F1503 and your code helped me. Thank you very much for your code snippet.
I have one question: what does the letter L means beside the number 500 in the command set_pwm1_duty(500L) ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Mon Dec 02, 2013 3:38 pm |
|
|
Standard C....
'Long'. Not actually needed here (since '500' is automatically a 'long'), but the point is that if you changed the PWM value, and started to use values that could be a short (like 200), adding the 'L' makes sure the compiler will keep treating them as 'long'. CCS will by default 'switch operation' and treat a 'short' value fed to the PWM, as if it is 4* the size.....
A search here will find about this.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
16F1938 PWM |
Posted: Thu Dec 26, 2013 3:08 pm |
|
|
I tried this code:
Quote: |
setup_ccp2(CCP_PWM | CCP_P2B_C0 | CCP_PWM_L_H);
setup_timer_2(T2_DIV_BY_16, 249, 1);
set_pwm2_duty(500L);
|
and expect to get the PWM on pin RC0
actually it goes out on pin RC1
Any idea? is it a compiler bug as I used "CCP_P2B_C0" ?
Compiler ver 4.140 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Dec 26, 2013 3:20 pm |
|
|
Read the data sheet.....
Look at table 1.
What pins can CCP2 be routed to?. The name in the setup is confusing (with the '0'), but the signal can only route to B3, or C1...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Thu Dec 26, 2013 4:05 pm |
|
|
Thanks.
I looked in table 12-7 and it misleading...why it says CCP2/P2B RC0? |
|
![](templates/subSilver/images/spacer.gif) |
|