You are here

More PDL Examples - Pulse-Width Modulators | Cypress Semiconductor

More PDL Examples - Pulse-Width Modulators

Last week I showed an example of using the PDL UART driver in a PSoC 6 application. Today I am going to show you a PWM and do more compare-and-contrast with the PDL-based code versus the generated API you might be used to with PSoC 4.

My project is going to use a PWM to blink an LED - because that has never been done before!!! If you have ever used PSoC Creator to make a design, you'll recognize what is going on here.

PSoC Creator PWM

The schematic looks just like it would for PSoC 4. I have a fairly slow (1kHz) clock feeding the PWM and a pair of pins connected to the inverting ("pwm_n") and non-inverting ("pwm") outputs. I chose recognizable names for the components because it will help me describe the API usage better. I also routed the two pins to P1.5 and P13.7 on the Pioneer kit because, step back in amazement folks, those are the physical pins that connect to LED8 and LED9.

PWM pin selections

Inside the PWM customizer I chose a period of 1000 for a 1Hz blink rate (OK, the PWM counter is actually zero-based so it is a 0.999Hz rate, but who wants to do the math?). I set the compare value to 100 so the pins will blink either mostly-on or mostly-off.

PWM customizer - setting period to 100, compare to 100

When I build this design PSoC Creator pulls in the PDL driver and generates some configuration code (BlinkyPWM.*) to make it easy to use.

PSoC Creator workspace with generated source

So, to write the firmware I have a couple of options. For the PWM component the authors include a thin "shim" that makes the PWM look identical to the PSoC 4 API. So the following code turns on the PWM and the pin blink away like troopers.

BlinkyPWM_Start();

Now that is great, the whole application is almost indistinguishable from a PSoC 4 version. It is easy to write and it works perfectly. But this is a very simple application and that shim layer does not provide "instance-based API" for every PWM function. Eventually we are going to need to call the PDL directly. Let's start by replacing the _Start().

To do this you can look in two places - the PDL documentation or the generated code we are replacing! The PDL tells you how to write firmware that refers directly to the hardware. Here is the snippet of code it suggests you use to turn on a PWM.

/* Scenario: there is need to initialize
 * the first (index = 0) PWM of the TCPWM0 block
 * with the previously defined configuration settings
 */
#define MY_TCPWM_PWM_NUM   (0UL)
#define MY_TCPWM_PWM_MASK  (1UL << MY_TCPWM_PWM_NUM)
    
if (CY_TCPWM_SUCCESS != Cy_TCPWM_PWM_Init(TCPWM0, MY_TCPWM_PWM_NUM, &config))
{
    /* Handle possible errors */
}
    
/* Enable the initialized PWM */
Cy_TCPWM_PWM_Enable(TCPWM0, MY_TCPWM_PWM_NUM);
   
/* Then start the PWM */
Cy_TCPWM_TriggerStart(TCPWM0, MY_TCPWM_PWM_MASK);

This example sets up counter 0 in block TCPWM0. Note that the documentation also tells you how to define the "config" struct in firmware. From here I can figure out that I need to call the following functions.

  • Cy_TCPWM_PWM_Init
  • Cy_TCPWM_PWM_Enable
  • Cy_TCPWM_TriggerStart

​That all seems pretty easy but with PSoC Creator it is even easier because it generates macros for all the arguments - so I do not need to figure out which physical TCPWM block is used. Instead of "TCPWM0" I can just use "BlinkyPWM_HW". For "MY_TCPWM_PWM_NUM" I use "BlinkyPWM_CNT_NUM". I do not have to write the configuration struct because "BlinkyPWM_config" is created for me from the choices I made in the customizer dialog. And, finally, the trigger mask is "BlinkyPWM_CNT_MASK". Here is the code, which turns on the PWM just like the _Start() function.

Cy_TCPWM_PWM_Init( BlinkyPWM_HW, BlinkyPWM_CNT_NUM, &BlinkyPWM_config );
Cy_TCPWM_PWM_Enable( BlinkyPWM_HW, BlinkyPWM_CNT_NUM );
Cy_TCPWM_TriggerStart( BlinkyPWM_HW, BlinkyPWM_CNT_MASK );

OK, that's simple enough but there is a way to cheat! I like that plan! Take a look in the generated BlinkyPWM.c file and find the _Start() function itself. It looks like this.

void BlinkyPWM_Start(void)
{
    if (0U == BlinkyPWM_initVar)
    {
        (void) Cy_TCPWM_PWM_Init(BlinkyPWM_HW, BlinkyPWM_CNT_NUM, &BlinkyPWM_config);
        BlinkyPWM_initVar = 1U;
    }
    Cy_TCPWM_Enable_Multiple(BlinkyPWM_HW, BlinkyPWM_CNT_MASK);
    
    #if (BlinkyPWM_INPUT_DISABLED == 7UL)
        Cy_TCPWM_TriggerStart(BlinkyPWM_HW, BlinkyPWM_CNT_MASK);
    #endif /* (BlinkyPWM_INPUT_DISABLED == 7UL) */   
 
}

Surprise, surprise! It's basically identical code. This is really useful because, even if your development flow is to use PSoC Creator only to draw the schematic and do all your firmware in another IDE, you still get a lot of generated reference code which will work perfectly in your application.

With PDL (and the generated shim layer) we are just adding to your available choices for firmware development. Using PDL is not really any harder than before, plus it saves a lot of code bloat and is easier to use with revision control systems (the drivers do not generated with the design). Next week I'll add some more examples... closing out the PWM application with an interrupt component and an ISR handler.

 

Blog: 

Comments

David Phillips's picture

Sharing such sort of article are truly helpful for individuals exceptionally undergrads to think about the most recent innovation. The photo gives a general thought regarding the procedure.

0234rihankhan_3298166's picture

Robux is very funny game and easy to get free robux this ,maximum played game many person can play it same on time enjoy it free robux its give many platform to play its and its simply to get on my website.

Chloe Norris's picture

The principle preferred standpoint of PWM is that power misfortune in the exchanging gadgets is low. At the point when a turn is off there is for all intents and purposes no present, and when it is on, there is no voltage drop over the switch. Power misfortune, being the result of voltage and current, is in this manner in the two cases near zero. PWM additionally functions admirably with computerized controls, which, on account of their on/off nature, can without much of a stretch set the required obligation cycle.

 

Regards: assignment help UK

본 사이트의 모든 컨텐츠와 자료는 "있는 그대로" 제공됩니다. CYPRESS SEMICONDUCTOR와 해당 공급자는 그 목적에 관계 없이 이러한 자료의 적합성에 대한 표현을 하지 않으며 상업성, 특정 목적에의 적합성, 권리 및 제3자 지적 재산권의 비침해에 대한 모든 묵시적 보증과 조건을 포함하여(이에 제한되지 않음) 이러한 자료와 관련한 모든 보증과 조건을 부인합니다. CYPRESS SEMICONDUCTOR에서는 명시적 또는 묵시적으로 금반언이나 여타의 다른 방법으로 라이센스를 부여하지 않습니다. 이 사이트의 정보를 사용하려면 제3자의 라이센스 또는 CYPRESS SEMICONDUCTOR에서 제공하는 라이센스가 필요할 수 있습니다.

이 사이트의 컨텐츠에는 특정 사용 지침이나 제한이 포함되거나 그러한 제한이 적용될 수 있습니다. 모든 게시물과 이 사이트 컨텐츠 사용에는 사이트 약관이 적용됩니다. 이 컨텐츠를 사용하는 제3자는 제한 또는 지침을 따르고 이 사이트의 약관을 준수할 것이라는 데 동의합니다. Cypress Semiconductor와 그 공급자는 컨텐츠와 자료, 그 제품, 프로그램 및 서비스를 언제든 수정, 삭제, 개조, 개선, 향상 및 기타 변경하거나 예고 없이 컨텐츠, 제품, 프로그램 또는 서비스를 이동 또는 중단할 수 있는 권리를 보유합니다.