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 using 16F1824 and optional pins

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



Joined: 14 Nov 2012
Posts: 2
Location: UK

View user's profile Send private message

PWM using 16F1824 and optional pins
PostPosted: Wed Nov 14, 2012 6:16 am     Reply with quote

I am having problems with re-allocating the PWM pin to CCP2_A5. Also I am having to use timer 2, if I set Timer 6 as commented out I get no output. I presently get the PWM on C3.

What have I missed?

My code is as follows:
Code:

#include "16F1824.h"
#device PIC16F1824 ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=32000000)

void main(void)
{
   set_tris_a(0x00);
   set_tris_c(0x00);
//   setup_ccp1(CCP_PWM | CCP_TIMER6);   
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM | CCP2_A5);
//   setup_timer_6(T6_DIV_BY_1,200,2);   
   setup_timer_2(T2_DIV_BY_1,200,2);   
   set_pwm1_duty(100);
   set_pwm2_duty(50);
   while(1)
   {   
      delay_ms(1000);
   }
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Nov 14, 2012 10:14 am     Reply with quote

#FUSES ??
check the header file and data sheet

this is an optional pin assign controlled by #fuses i believe
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Nov 15, 2012 2:32 am     Reply with quote

OK, deep breath.

There are four different 'setups' of PIC's in this regard:

1) Most old PIC's, where all pins are fixed.
2) Some later chips, where the pins are re-mappable using fuses.
3) Newer chips where the some pins are configured using the device configuration.
4) Generally more complex chips with lots of peripherals where the pins are setup using #PIN_SELECT.

The 1824, is one of the third.
The critical question is 'what compiler version'?. Unfortunately, CCS has the habit of adding support for things like this, then not getting it working for a few compiler versions.

So real answer I'm afraid is to upgrade the compiler. Otherwise you need to set these bits yourself.

I can confirm that with the 4.137, the syntax as posted, _does_ correctly set the APFCON1.0 bit to select the PWM to A5, and does set bit 1, and clear bit 0 of the CCPTMRS1 register to select timer6.

There are a couple of other comments though, one which _will_ cause problems.

First, on line 2, get rid of the processor name. This is already set when you include the processor file, and having it in two places can lead to problems later if (for instance) you move the code to a different chip.

Then the clock settings. The chip _does not allow a external 32MHz crystal_ which is what you are configured for. 32MHz, can only be done by having an external 32MHz oscillator (EC fuse), or by using an 8Mhz crystal, and enabling the PLL. The maximum crystal frequency supported is 20MHz - Table 30-1 in the data sheet. If it is working, it is 'luck'.....

Best Wishes
stephen haggas



Joined: 14 Nov 2012
Posts: 2
Location: UK

View user's profile Send private message

PostPosted: Thu Nov 15, 2012 3:46 am     Reply with quote

Thanks Ttelmah for your reply,

I now realise my mistake, I had set the clock as external, should of been INTRC_IO, which frees up RA5, so that APFCON1.0 now sets A5 to PWM output.

The problem with Timer 6 is on MPLAB SIM, when I reprogram the header AC2444043 Timer 6 now works.

Thanks for your help.

Regards,
Stephen
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Thu Nov 15, 2012 5:28 am     Reply with quote

Brilliant.
So the extra bit about the clock, pointed into the right direction, and "Robert's your parent's brother"... Smile

Best Wishes
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Wed Dec 11, 2019 2:44 am     Reply with quote

Hello, in the above code, I have
Code:
setup_ccp1 (CCP_PWM | CCP2_A5);

I can't get results when I do.

Also, can Timer4 be used instead of timer2?
Code:
setup_ccp1 (CCP_PWM | CCP_TIMER4);

I think no.

Finally, can I get different frequencies from both pwm outputs?
Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Wed Dec 11, 2019 5:04 am     Reply with quote

ressas wrote:
Hello, in the above code, I have
Code:
setup_ccp1 (CCP_PWM | CCP2_A5);

I can't get results when I do.

Also, can Timer4 be used instead of timer2?
Code:
setup_ccp1 (CCP_PWM | CCP_TIMER4);

I think no.

Finally, can I get different frequencies from both pwm outputs?
Thank you.


Yes, yes, & yes.

The original poster had the CCP2_A5 working successfully, once he stopped
using A5 for the master clock. Problem you are having is
it is CCP2 that can be directed to A5, not CCP1.
Code:

setup_ccp2(CCP_PWM | CCP2_A5);

Note the change to '2' here.
CCP1 goes to C5, nowhere else.
CCP2 is the only CCP with alternate pins. All the other CCP devices have
fixed pins.

Then yes, you can use different timers for two different PWM's. Remember
you need to set the timers up for them to work.

Then, yes, if you use two different timers, you can have different
frequencies from different PWM outputs.
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Sat Dec 14, 2019 5:53 am     Reply with quote

Thank you. 16F1824 built-in oscillator 32MHz. Can I get 32MHZ frequency from pwm? What are the 'timer2' settings needed to get the highest frequency from the pwm pin?
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Dec 14, 2019 6:42 am     Reply with quote

ressas

learn by reading, learn by doing !

Download the PIC dataheet, and read the PWM section. There are 3 simple equations that deal with period and duty cycle . Cut code to use them in your PIC and send the results to a PC or LCD module. Use the samples given by uChip in the chart to confirm your code is correct as 'braces' or () are important to get the numbers right. Once the code is correct, enter 00 or ff and see what frequency your PIC can do PWM.
This way YOU learn and be a better programmer

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Dec 14, 2019 8:52 am     Reply with quote

and 'no'. The PWM always involves division. The fastest possible is 8MHz from
a 32Mhz clock, and with this you have just four different pulse widths.
To run a PWM at 32MHz, would be a very unusual requirement. Few drivers
would support the switching speeds needed for this. Typically even 500KHz
is 'unusually fast' for a PWM.
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Mon Dec 16, 2019 1:25 am     Reply with quote

Thank you Temtronic and Ttelmah
You're right. I always ask. But I have to try it myself.
My English is weak. away from concepts.

As a Turkish proverb says;
To do someone a favor, teach him how to fish. Dont give a fish
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