2015-04-02 11:01 PM
I am using SPC560D40L3 with SPC5Studio. I am trying to generate a PWM signal. Using Application configuration, I configured the HAL drivers to generate a PWM of 5Hz. Here, I configured the clock frequency and period for the PWM using application configuration which gets stored to structure 'PWMConfig pwm_config'.
Now I want to use a runtime value for PWM frequency i.e. my device will get input for PWM frequency (say via UART) and will use the same for 'PWMConfig pwm_config'. How to pass the clock frequency and period values (derived from PWM frequency) to this structure? Is there any other way to implement this?Thanks in advance.Mike. #pwm-configuration2015-04-02 11:29 PM
It looks that I can access the two elements - frequency and period from the structure 'pwm_config' but assigning the value to these results in following error,
error: assignment of member 'frequency' in read-only object error: assignment of member 'period' in read-only objectWhat is the solution for this?Thanks.Mike.2015-04-03 01:00 AM
Hello Mike ,
The structure is a const you can not update it. (Cf Code attached)/**
* @brief Structure defining the PWM configuration ''cfg0''.
*/
const PWMConfig pwm_config_cfg
0
= {
100000
,
20000
,
pwmpcb,
{
{
PWM_OUTPUT_ACTIVE_HIGH,
pwmc
1
cb
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
},
{
PWM_OUTPUT_DISABLED,
NULL
}
},
PWM_ALIGN_EDGE
};
Anyway , if you want to change on the fly , i recommend to use an other global configuration structure copy-paste of the first one (the const).
This structure will be updated by UART.
after each update , pwm should be stopped and restart.
pwmStop(&PWMD1);
...............
/* Update my GLOBAL CONFIGURATION */
pwmStart(&PWMD
1
, &pwm_my_config);
Best regards
Erwan
2015-04-03 04:06 AM
Yes Erwan. I used my own config structure similar to one used by HAL driver but a non constant one.
Thanks.Mike.2015-04-03 06:11 AM
2015-05-13 05:40 AM
Hello Erwan,
I am trying to pass a non-constant PWMConfig structre to function 'pwmstart'. It looks that the compiler accepts it but the configuration not getting applied to the MCU and hence no PWM on the respective pin.Earlier I only verified the compilation part but not the execution of it. From your reply, it looks that it is not so easy to pass a non constant struct to this function..:). Can you please explain the way to implement this? May be with a small example!
Thanks in advance.Mike.2015-05-14 02:55 AM
Hello Erwan,
It looks that I am able to pass a non-constant PWMConfig struct to the function. Can you please provide info on PWM frequency or duty cycle calculations? Any formula for this?Thanks.Mike.2015-05-18 08:13 AM
Posted on May 18, 2015 at 17:13
Hello Mike ,
In the reference manual RM0045,
http://www.st.com/web/en/resource/technical/document/reference_manual/CD00259pdf
You can find useful information on chapter 24 by charts and detailed description.
at the current time , PWM is usesModulus Counter Buffered (Up counter)
(Cf Figure 346)
pwmp->emiosp->CH[8U].CCR.B.UCPEN = 0;
pwmp->emiosp->CH[8U].CCNTR.R = 1U;
pwmp->emiosp->CH[8U].CADR.R = pwmp->config->period;
pwmp->emiosp->CH[8U].CCR.R |= EMIOSC_BSL(EMIOS_BSL_INTERNAL_COUNTER);
pwmp->emiosp->CH[8U].CCR.R |= EMIOS_CCR_MODE_MCB_UP;
pwmp->emiosp->CH[8U].CCR.B.UCPRE = psc - 1U;
pwmp->emiosp->CH[8U].CCR.R |= EMIOSC_UCPREN;
Anyway , you can find in this attachment an example of application.
if you want to change of PWM mode ,you can patch the driver using the patch mode
Best regards
Erwan
____________________
Attachments :
SPC560Dxx_OS-Less_PWM-ICU_Own_Configuration_Test_Application_for_Discovery.7z : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006qaV6&d=%2Fa%2F0X0000000bpC%2FvcpzsMbdg_hmDODD98BguHcKsbkkbl5nc3FnJwsUv9g&asPdf=false