|
|
View previous topic :: View next topic |
Author |
Message |
ALPHA4390
Joined: 16 Feb 2016 Posts: 9
|
PWM output on a PIC |
Posted: Tue Feb 16, 2016 11:00 am |
|
|
I am trying to get a working example of PWM on a 33EP256MU806 pic.
I am not having much success as the help in the compiler does not give a very good description of the PWM functions.
setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER2);
I am getting this error and i haven't a clue how to find out whats wrong
*** Error 108 "PWM_Edge_AlignedT.c" Line 28(39,54): Invalid parameters to built in function :: Invalid Pin
Any help is welcomed |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1342
|
|
Posted: Tue Feb 16, 2016 11:38 am |
|
|
99% of the time "invalid pin" errors are from lack of using #pin_select. If your chip needs pin select for the PWM, then you need to use that before calling setup_compare. That said, at first glance yours shouldn't need that, so this may be a case where CCS missed it and needs to make corrections. Study the datasheet and if they indeed implemented this wrong, let them know via email.
For workarounds you can access the registers directly. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Tue Feb 16, 2016 1:07 pm |
|
|
Deep breath.
It is the #PIN_SELECT.
Key is that this chip has two different ways of actually generating a PWM. You can generate a PWM, on any of the PPS pins using output compare (which is what is being tried). This comes out on the OCx pins, and these are PPS pins (so must be selected to the required pin before use).
So:
#PIN_SELECT OC1=PIN_yy
The command will then be accepted.
This is the standard DsPIC PWM.
However this chip also has a separate high speed PWM module. This allows configuration designed for things like motor control etc. This outputs on the PWMxH & PWMxL pins, which are predefined. This module is controlled by the hspwm functions.
setup_hspwm_unit(), set_hspwm_phase(), set_hspwm_duty(), &
set_hspwm_event()
So for a simple PWM, the current function is the way to go, but this requires a #PIN_SELECT. Alternatively use the hspwm functions. |
|
|
ALPHA4390
Joined: 16 Feb 2016 Posts: 9
|
|
Posted: Tue Feb 16, 2016 4:07 pm |
|
|
Thanks for the quick response,
I searched all of the examples and could not find reference to #PIN_SELECT OC1=PIN_yy, I even have the embedded C book with PWM examples that did not refer to #PIN_SELECT OC1=PIN_yy.
I assume the defined pin must be a CCP PWM pin? As i have defined a general io pin for a quick test on the dev board and it looks like i can only get 50% duty cycle. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9215 Location: Greensville,Ontario
|
|
Posted: Tue Feb 16, 2016 9:26 pm |
|
|
I don't use that PIC or have the compiler for it but you should be able to search the CCS manual for syntax and use of it.
Mr T is trying to lead you to what must be done.
BTW this is a CCS specific item and will NOT be covered in a general C text.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Wed Feb 17, 2016 2:16 am |
|
|
With CCS, you need to be using several layers of data:
1) General C book (for the syntax of C operations).
You will not find chip specific or CCS specific functions here.
2) CCS's manual (for the list of their functions and syntax).
3) The chip's data sheet (what features the chip has).
4) The include file for the processor. This will tell you which 'extra' functions this chip has, and the values they can use.
5) The examples
First key one here is the chip's data sheet.
PPS, is peripheral pin select. Section 11/4. This is a specific feature of the later PIC's, that allows certain peripherals to be connected to any PPS pin.
If you look at the data sheet, there are tables for the output peripherals and input peripherals, listing for each whether it is a fixed peripheral, or a PPS peripheral.
Tables 11-1, 11-2 & 11-3
These show what peripherals/pins can be mapped.
Then if you open the header file for the PIC, after it's initial section (fuses etc.), there is a section listing all the peripheral names that CCS uses for the devices that can (must) be mapped, and all the pin names they can be mapped to.
This section starts with the line:
// #pin_select function=pin
So, telling you how it is used to map a function to a pin.
No. This PWM output, has nothing to do with the PWMx pins.
As already said, the PWMx pins are the fixed pins for the HSPWM peripheral.
How the selection works, is shown in the CCS examples.
If you look at
EX_Pinselect.c
EX_pinselect2.c
These show the generic way the pin select function works.
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_C12,PIN_C13,PIN_C14,
// PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4,PIN_D5,PIN_D6,PIN_D7,PIN_D8,PIN_D9,
// PIN_D10,PIN_D11,PIN_E0,PIN_E1,PIN_E2,PIN_E3,PIN_E4,PIN_E5,PIN_E6,PIN_E7,
// PIN_F0,PIN_F1,PIN_F3,PIN_F4,PIN_F5,PIN_G6,PIN_G7,PIN_G8,PIN_G9
// Input Functions:
// INT1,INT2,INT3,INT4,T2CK,T3CK,T4CK,T5CK,T6CK,T7CK,T8CK,T9CK,IC1,IC2,IC3,
// IC4,IC5,IC6,IC7,IC8,OCFA,OCFB,FLT1,FLT2,FLT3,FLT4,QEA1,QEB1,INDX1,HOME1,
// QEA2,QEB2,INDX2,HOME2,U1RX,U1CTS,U2RX,U2CTS,SDI1,SCK1IN,SS1IN,SS2IN,DCIDI,
// DCISCKIN,DCIFSIN,C1RX,C2RX,U3RX,U3CTS,U4RX,U4CTS,SDI3,SCK3IN,SS3IN,SDI4,
// SCK4IN,SS4IN,IC9,IC10,IC11,IC12,IC13,IC14,IC15,IC16,OCFC,SYNCI1,SYNCI2,
// DTCMP1,DTCMP2,DTCMP3,DTCMP4,DTCMP5,DTCMP6,DTCMP7,FLT5,FLT6,FLT7
// Output Functions:
// NULL,U1TX,U1RTS,U2TX,U2RTS,SDO1,SCK1OUT,SS1OUT,SS2OUT,DCIDO,DCISCKOUT,
// DCIFSOUT,C1TX,C2TX,OC1,OC2,OC3,OC4,OC5,OC6,OC7,OC8,C1OUT,C2OUT,C3OUT,U3TX,
// U3RTS,U4TX,U4RTS,SDO3,SCK3OUT,SS3OUT,SDO4,SCK4OUT,SS4OUT,OC9,OC10,OC11,
// OC12,OC13,OC14,OC15,OC16,SYNCO1,SYNCO2,QEI1CCMP,QEI2CCMP,REFCLK
//
|
So the 'output compare' peripherals are OC1 to OC16, and can be mapped to just about any pin on the PIC.
For the hspwm, this is a complex peripheral, since it allows you to specify current limit controls, and the phase relationship between it's outputs. It has up to seven PWM outputs, each offerimg complementary pins. It is a very powerful feature, but not the way to generate a simple PWM....
The compiler 'knows' if you try to use a peripheral before having mapped it's I/O, and will give the 'invalid pin' error when this is the case.
Once you have mapped the pin, the ex_pcd_pwm example shows how to use the PWM, and control it's duty cycle/ |
|
|
|
|
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
|