It’s Relax Time: LED PWM

In the previous posting, we haven’t use DAVE CE application. Now, let’s start use it!!
We will make an application, still base on LED, but we will control the LED with PWM (Pulse Width Modulation) signal. Then we can control the brightness of the LED.

Just create new project and choose DAVE CE Project.

ADDING PWMSP001 APPLICATION

DAVE CE application, allows us to use component based program. The component actually is the library provided by DAVE IDE. When we want to create PWM application, we can use PWMSP001,  the component to generate PWM signal base on CCU4 (Capture/Compare Unit 4). CCU4 consists of 4 16-bit timer. And can be concatenated to generate 32, 48 or 64 bit timer. To use this component, also others component, can be select from App Selection View. Double click PWMSP001 to add to our project. We will use 2 PWM, so do it twice..:).

When we add the second PWMSP001, DAVE will show App Sharability dialog, because PWMSP001 use CCU4, so DAVE will ask to create new instance for CCU4 or use the same CCU4 (share/Existing Apps). Just select use Existing Apps, by click  app/ccuglobal/0.

We will have 2 PWMSP001 (PWMSP001/0 and PWMSP001/1) application on App Dependency Treeview with the same CCU4 (CCU4GLOBAL/0). If we select new instance, PWMSP001/1 will use new CCU4, CCU4GLOBAL/1.

UI EDITOR

Let start to initialize 2 PWM applications. Right click on the application and click UIEditor.

UIEditor also can be display by double click the application from S/W App Connectivity View.

The configure the both PWM application as below:

  • Counting Mode: Edge-Aligned Mode
  • Check Start during initialization
  • CCU4 resolution : 10uSec (10000 nsec)
  • PWM freq: 200 Hz
  • Duty Cycle: 0 % (First time LED off)
  • Check Period Match (check Enable at initialization)

The CCU4GLOBAL use default setting (with PLL, frequency  120 MHz).

MANUAL PIN ASSIGNMENT

PWM application need to output the signal to one of MCU pin. Then we need to assign a pin for our PWM applicaion, 2 pin for 2 PWM application. As we know that Relax Kit has 2 LEDs, then we can assign these LEDs for our PWM applicaion. Right click from our application then select Manual Pin Assignment.

Select pin_directouput from Resource, and from Port-Pin/Pin Number select P1.0 and P1.1 for each application. Then click Solve And Save. OK, we’ve done the setting for our application. If we want use, example, interrupt we must add interrupt controller application  (NVIC), and we must connect it to the PWM application using Signal Connection. For now we will not use interrupt.

Next step is to generate the code from application setting, click SOLVER and Generate Code. DAVE will generate source code base on application setting. Every time we change the setting, Solver and Generate Code must be done.

Off course that the generated code by DAVE still need to edit. Just like when we make a PC program by visual programming language, like Delphi. We just do to set windows appearance by set the visual properties from the component we used. When we run the program, there is nothing happen if we click a button without a code in the click event handler.

PWMPS001 FUNCTIONS

DAVE has a good documentation from its help. Just click Help->Help Content. When help come out select Dave Apps then click PWMSP001.

 

The API Documentation, will show the functions for PWMSP001. The functions will control the PWMSP001 application: start or stop PWM. set PWM frequency, change duty cycle, etc. What we’ve done before was to configure the PWMSP001 application in design time. And the code that we will make is to configure/control it in run time.

PWM PROGRAM

We will make a program to adjust the LED brightness by PWM signal. After power ON, both LED will be off, duty cycle 0%, then PWMSP001/0 duty cycle will be increased to 100% with 25 mS interval. Interval time come from delay function using System Tick Timer. After 100%, PWMSP001/1 will be configure in the same way. Next is to decrease the duty cycle back to 0%.

And here is the program:

/*
* Main.c
*
* Created on: Dec 16, 2012
* Author: Kang Usman
*/

#include <DAVE3.h> //Declarations from DAVE3 Code Generation (includes SFR declaration)

uint32_t ticks = 0UL;

void SysTick_Handler(void);
void delayms(uint32_t thedelay);

int main(void)
{
  status_t status; //declaration of return variable for DAVE3 APIs
  uint32_t PWMerrorcount = 0; // accumulation of number of not successful executed API for the PWMSP001 App
  uint32_t PWMerrorcount2 = 0; // accumulation of number of not successful executed API for the PWMSP001 App
  int i;

  DAVE_Init(); // Initialization of DAVE Apps
  SysTick_Config(SystemCoreClock / 1000UL);

  status = PWMSP001_Start(&PWMSP001_Handle0); // start of the PWMSP001 App, instance 0
  if (status != DAVEApp_SUCCESS) PWMerrorcount++; // if start was not successful PWM error counter incremented
  status = PWMSP001_Start(&PWMSP001_Handle1); // start of the PWMSP001 App, instance 0
  if (status != DAVEApp_SUCCESS) PWMerrorcount2++; // if start was not successful PWM error counter incremented

  while(1)
  {
    for(i=0;i<101;i+=5)
     {
       status = PWMSP001_SetDutyCycle(&PWMSP001_Handle0, i); //change brightness of LED
       if (status != DAVEApp_SUCCESS) PWMerrorcount++; //if set of new duty cycle was not successful
       delayms(25);
     }

   for(i=0;i<101;i+=5)     {       status = PWMSP001_SetDutyCycle(&PWMSP001_Handle1, i); //change brightness of LED       if (status != DAVEApp_SUCCESS) PWMerrorcount2++; //if set of new duty cycle was not successful       delayms(25);     }    for(i=100;i>=0;i-=5)
    {
      status = PWMSP001_SetDutyCycle(&PWMSP001_Handle0, i); //change brightness of LED
      if (status != DAVEApp_SUCCESS) PWMerrorcount++; //if set of new duty cycle was not successful
      delayms(25);
    }

   for(i=100;i>=0;i-=10)
    {
      status = PWMSP001_SetDutyCycle(&PWMSP001_Handle1, i); //change brightness of LED
      if (status != DAVEApp_SUCCESS) PWMerrorcount2++; //if set of new duty cycle was not successful
      delayms(25);
    }

  }
 return 0;
}

void SysTick_Handler(void)
{
  ticks++;
}

void delayms(uint32_t thedelay)
{
  uint32_t currenttick;

  currenttick= ticks;
  while(ticks-currenttick<thedelay);
}

And here is the video…

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: