|
|
View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
24FJ128GA704 PWM w/o using a timer? |
Posted: Tue Mar 10, 2020 2:43 pm |
|
|
EDIT: Updated title to reflect current question.
I have inherited a 24FJ256GA704 project that currently uses all three timers, and I am going to have to rework it to use only one, since we'll be needing the combined timer 2/3 for new functionality.
One of our timers is set to 4khz and it just being used to pulse a buzzer to make sound and also dim a backlight.
I started looking into PWM today. The buzzer pin is on a PWM compatible pin, so I was able to port my Arduino music player over and have it play music. My setup is:
Code: | #use pwm(output=PIN_A10, TIMER=1, FREQUENCY=4kHz, DUTY=50, PWM_OFF) |
...and I was able to make an Arudino-style tone() command doing this:
Code: | void tone(unsigned int frequency, unsigned int duration)
{
pwm_set_frequency(frequency);
pwm_on();
delay_ms(duration);
pwm_off();
} |
Does this limit my timer1 to only being used for the PWM? Is the pwm_set_frequency() reprogramming it each time, so if I also had a Timer 1 ISR it would be messed with?
My thought is I should be able to use Timer 1 as a general ticker, then hijack it when needed to beep (basically switching from timer functions to beeper). My application is not real time and if it's not doing anything while it beeps, that's okay.
BUT, my LCD backlight pin is not on a PWM capable pin. It also needs to have constant service to make it dim.
Even if it was on a PWM pin, I expect I'd still need a dedicated timer for PWM to work with it, separate of the buzzer PWM?
Suggestions appreciated. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Last edited by allenhuffman on Thu Mar 12, 2020 7:46 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed Mar 11, 2020 1:51 am |
|
|
Yes, you would have to ensure that you disabled the timer ISR, before
using for the PWM, and reprogrammed the timer how you wanted it for
this after the PWM_off.
As a comment, you do realise that the CCP module on this can be set to
PWM mode and has it's own timer. There are also extra timers in the
input capture and output compare modules. Though there are only 3
general purpose timers, there are other timers available for almost all
the other peripherals. Don't use #use PWM, and instead just setup
the peripheral, and your timer won't be affected. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Wed Mar 11, 2020 7:07 am |
|
|
This part has many timers available and a lot of flexability.
The 24FJ256GA704 has 3 16 bit timers along with:
4 MCCP, which each have a 32 bit timer capability
3 IOC, which each have a 16 bit timer. 2 adjacent can be cascaded to 32 bit
3 OC/PWM, which each have a 16 bit timer. 2 adjacent can be cascaded to 32 bit. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed Mar 11, 2020 7:55 am |
|
|
Exactly.
Use the general purpose timers for the general purpose timing functions
and use the specific peripheral ones to give the peripheral functions.... |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Wed Mar 11, 2020 2:22 pm |
|
|
Ttelmah wrote: | As a comment, you do realise that the CCP module on this can be set to
PWM mode and has it's own timer. There are also extra timers in the
input capture and output compare modules. Though there are only 3
general purpose timers, there are other timers available for almost all
the other peripherals. Don't use #use PWM, and instead just setup
the peripheral, and your timer won't be affected. |
I did not realize that! I was looking at the data sheet, and then decided to check the CCS help file and found #pwm and started playing with that. (Note to self -- looks like pwm_set_frequency() is a quick way to set a time frequency without using the setup timer call and prescalers and such... Ha!)
I will read up on CCP. I have two things that need PWM (backlight dimming, and tone generation), but we need to do a board spin to move the backlight on a PWM-capable pin. So for now, I'll have to use an ISR for the backlight, and PWM for the buzzer if I can figure out CCP.
Thanks for the pointer! _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Wed Mar 11, 2020 3:11 pm |
|
|
Unless I am misunderstanding the data sheet, it doesn't look like PIN_A10 can be used with the CCP.
But, if that is how it works, I could get them to spin the board so I could get those pins switched. I just need to make sure I understand how it works before I have them do that ($$$, USA made boards right now). _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Wed Mar 11, 2020 3:27 pm |
|
|
Ttelmah wrote: | Don't use #use PWM, and instead just setup
the peripheral, and your timer won't be affected. |
Are we talking setup_ccpX(CCP_PWM)?
If so, are these the OCM1x in the data sheet? If so, it doesn't look like I have any pins available, but they will redo the board for me -- we will be needing an input counter for the next revision, and I was going to be doing that manually with an ISR. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Wed Mar 11, 2020 4:37 pm |
|
|
allenhuffman wrote: | ($$$, USA made boards right now) |
My regular Chinese supplier has been back to normal for almost 2 weeks now: JLCPCB. Have your hardware people shopped around? |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Thu Mar 12, 2020 7:46 am |
|
|
PWM can be done from the MCCP/SCCP modules or from the OC (output compare) modules. Don't forget to check them as well.
Depending on frequency and response time needed it also possible to set a compare timer interrupt and have the ISR toggle a generic I/O pin. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Mar 12, 2020 7:50 am |
|
|
I am stuck trying to figure out what pins are PWM capable. I found a tutorial online and it shows an excerpt from the data sheet for a different pic with a block diagram clearly going to "RC2/CCP1". For the 24FJ128GA704 datasheet, I cannot find this. I see things like it routing to "Compare/PWM Output(s)".
The CCS compiler knows -- if I specify an invalid pin, it tells me. But I don't know what pins are valid. If I can hijack one of those pins on our board and verify I can make this work, they will rework the board and reassign pins, but I can't tell them what pins I need yet because I can't figure out this data sheet _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Thu Mar 12, 2020 8:13 am |
|
|
There are several places to look.
PPS pins, ones labeled RPxx, can take a PPS function from table 11-7: SELECTABLE OUTPUT SOURCES.
These include: OC1, OC2, OC3, OCM2A, OCM2B, OCM3A, OCM3B, OCM4A, OCM4B
There are also dedicated hardware pins:
OCM1A, OCM1B, OCM1C, OCM1D, OCM1E, OCM1F
Pin RA10 you asked about earlier also has RP28 |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Thu Mar 12, 2020 8:20 am |
|
|
That processor has PPS; you need to a) see if the pin you want to use is an RP or RPI pin, b) set up PPS properly first (if the pin you want to use supports being connected to a PWM/OC output).
RP = reprogrammable pin, can be either input or output.
RPI = reprogrammable input pin, can only function as a peripheral input.
The table showing what pins are RP/RPI is on page 6 of the data sheet. One other thing: if you haven't already, poke around the Microchip site to download the relevant portions of the FRM (family reference manual). You'll find much better/more complete documentation regarding how to set up the PWM, how to use PPS, etc. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Mar 12, 2020 8:33 am |
|
|
gaugeguy wrote: | There are several places to look.
PPS pins, ones labeled RPxx, can take a PPS function from table 11-7: SELECTABLE OUTPUT SOURCES.
These include: OC1, OC2, OC3, OCM2A, OCM2B, OCM3A, OCM3B, OCM4A, OCM4B
There are also dedicated hardware pins:
OCM1A, OCM1B, OCM1C, OCM1D, OCM1E, OCM1F
Pin RA10 you asked about earlier also has RP28 |
Thanks!
In that table, I see "CCP2A/CCP2B Output Compare" but what is that to the CCS API? I even tried to use the PIC24 Wizard PWM example to set this up but it complains that "PIN_A10 is not pin for CCP2".
Do I need to use #PIN_SELECT to remap that pin? Looks like #use does it:
Quote: | [PCD] OUTPUT=PIN_xx - Selects the PWM pin to use, pin must be one of the OC pins. If device has remappable pins compiler will assign specified pin to specified OC module. If OC module not specified it will assign remappable pin to first available module. |
Code: | ////////////////////////////////////////////////////////////////// PIN_SELECT
// #pin_select function=pin
// Valid Pins:
// PIN_B0,PIN_B1,PIN_B2,PIN_B3,PIN_B4,PIN_B5,PIN_B6,PIN_B7,PIN_B8,PIN_B9,
// PIN_B10,PIN_B11,PIN_B12,PIN_B13,PIN_B14,PIN_B15,PIN_C0,PIN_C1,PIN_C2,
// PIN_C3,PIN_C4,PIN_C5,PIN_C6,PIN_C7,PIN_C8,PIN_C9,PIN_A0,PIN_A1,PIN_A10
// Input Functions:
// OCTRIG1,INT1,INT2,INT3,INT4,OCTRIG2,T2CK,T3CK,CCP1,CCP2,CCP3,CCP4,IC1,IC2,
// IC3,OCFA,OCFB,CCPCKIA,CCPCKIB,U1RX,U1CTS,U2RX,U2CTS,SDI1,SCK1IN,SS1IN,SDI2,
// SCK2IN,SS2IN,TMRCLK,CLCINA,CLCINB,SDI3,SCK3IN,SS3IN
// Output Functions:
// NULL,C1OUT,C2OUT,U1TX,U1RTS,U2TX,U2RTS,SDO1,SCK1OUT,SS1OUT,SDO2,SCK2OUT,
// SS2OUT,OC1,OC2,OC3,CCP2OUTA,CCP2OUTB,CCP3OUTA,CCP3OUTB,CCP4OUTA,CCP4OUTB,
// SDO3,SCK3OUT,SS3OUT,C3OUT,PWRGT,REFCLK,CLC1OUT,CLC2OUT,RTCCOUT
// |
I've tried various combinations:
Code: | //#PIN_SELECT C2OUT=PIN_A10
#PIN_SELECT CCP2OUTA=PIN_A10
#use pwm(CCP2,OUTPUT=PIN_A10,FREQUENCY=4000,DUTY=50) |
_________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Mar 12, 2020 9:49 am |
|
|
I got it working by removing OUTPUT= from the #use PWM, and then manually setting the pin. Does this look okay?
Code: |
#use fixed_io(a_outputs=PIN_A10)
#PIN_SELECT CCP2OUTA=PIN_A10
//#use pwm(CCP2,OUTPUT=PIN_A10,FREQUENCY=4000,DUTY=50)
#use pwm(CCP2,FREQUENCY=4000,DUTY=50)
|
_________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Thu Mar 12, 2020 9:54 am |
|
|
When you have several different 'lettered' outputs, look at the CCP_PULSE_STEERING_x to determine which output it gets directed to.
Refer to the processor .h file |
|
|
|
|
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
|