Other Parts Discussed in Post: TMS570LS0432, HALCOGEN

When I was shopping around for a microcontroller development kit, I came across the Texas Instruments Hercules™ microcontroller (MCU) LaunchPad™ development kit. It offers CPUs with clock speeds upwards of 80 MHZ, which was just right for someone like me, who didn't want to spend much, but wanted ample leg room for more CPU-intensive projects.

1.Prepare Your HALCoGen Project.

2.Prepare Your CCS Project.

3.The Final Step: Write Your Code.

A friend told me that the Hercules MCU family isn't for beginners, so I heeded that warning, but the alternatives I was interested in exceeded my budget. Considering the fact that the Hercules™ MCU family was well-suited for inverters, PWM applications, and is automotive-grade, I eventually went ahead and purchased the Hercules TMS570LS0432 (TMS57004) MCU for $20.

I was a bit lost at first, but I eventually connected the dots with the help of Project 0, and the datasheet. The LaunchPad development kit turned out to have a refreshingly straightforward interface. I was able to generate a PWM signal with just a handful of clicks using HALCoGen, and three lines of code in Code Composer Studio™ (CCS) IDE. I eventually went on to write a beginners' guide for the LS04x to help new users get started quickly.

Before we continue, let's take a brief look at the tools we will be using. HALCoGen is a HAL code generator for Windows, provided by Texas Instruments, and it enables you to configure your LaunchPad development kit’s pins, ports, timers, and much more using an intuitive GUI. You can use HALCoGen to switch pins on and off, generate a PWM signal and adjust its duty cycle, but you'll often need to do this programmatically instead (for example: varying the duty cycle of PWM signals to control the speed of fan motors), and that's the focus of this tutorial. HALCoGen can be downloaded for free from the Texas Instruments website.

Code Composer Studio is the IDE that we'll use to edit our code, debug it, and load it to the Hercules microcontroller via USB. In this tutorial, the TMS570LS1224x MCU (TMS57012) is used to blink an LED and vary its brightness by generating a PWM signal with an N2HET timer. HET stands for High End Timer. We will use the hetInit(), pwmStart(), and pwmSetDuty() functions in this article. Hercules LaunchPad development kit users can download CCS IDE for free from the Texas Instruments website (if you log into your T.I. account first).

hetInit() initializes the N2HET High-End Timer (HET), pwmStart will turn on the PWM signal, and pwmSetDuty() is used to adjust the duty cycle of the PWM signal to control the brightness of the LED. You can find these functions in the het.c file (found under /source in your project directory) after generating your code with HALCoGen.

Prepare Your HALCoGen Project

First, create your HALCoGen project, and name it 'Blink'.

Next, enable the HET1 driver. We will use HET port 1 for this exercise.

Configure PWM Settings

The pin highlighted in the PWM 0 section is labelled HET 20 at the bottom of the LaunchPad development kit. Conveniently, this is right beside a ground pin. Ensure that you select pin 20 (otherwise known as bit 20), as that is what our PWM signal will be supplied to. Check the 'Enable' checkboxes for PWM 0. There are sockets under the LaunchPad that you can plug an LED into. Please use a discrete LED for this project to avoid damaging the MCU. 'Discrete' LEDs are low-wattage LEDs that are typically used to indicate when an appliance is on, when someone is calling, to indicate that your laptop is plugged in, etc.

Set the Period[us] field to 1000000 microseconds as shown below (this causes the LED to blink every second). Leave the Duty[%] field for now, as that is the duty cycle value, which we will set via our own code near the end of this tutorial.

The next step is to configure the pin HET 20. Set it to the output direction as shown below. Setting it to the output direction enables you to write to it/power it on.

Leave the DOUT drop down box as it is. We will turn this pin on programmatically. DOUT is the output value. 0 means off, and 1 means on. Now you can finally generate the necessary code using HALCoGen by pressing the F5 key. You can now launch Code Composer Studio IDE and create a new project, as shown below.

Prepare The CCS IDE Project

In the workspace field, enter the exact same directory that your HALCoGen project folder is in. If your HALCoGen Location field was set to ‘C:\LaunchPad’ and your project name was Blink, then your HALCoGen project folder was placed inside ‘C:\LaunchPad’, which will be your workspace. Your CCS IDE project directory should also have the same name as your HALCoGen project directory.

Your CCS DIE and HALCoGen files should be in the same directory. So your HALCoGen project directory should be C:\LaunchPad\Blink, and your CCS IDE project directory should be C:\LaunchPad\Blink. This enables you to update your HALCoGen code easily without having to copy it all over again. HALCogen won't delete the code you write between the user code comments mentioned below, in the 'Start Coding' section.

Select 'Empty Project', and name your project 'Blink'. For this exercise, your project name in CCS IDE should match your HALCoGen project name, and it should be in the same directory. Select your MCU from the Target drop down box on the right.

Now you can add the include directory that HALCoGen generated to your project's dependencies. Right click your project in the Project Explorer (Blink [Active - Debug]), click Properties, and them Include Options. In the 'Add dir to #include search path' section, click the white icon with the green plus sign on it, select the include directory under /Blink, and click ok as follows. You can now start coding!

Start Coding

Open the sys_main.c file in CCS (it's under /source). Between the /* USER CODE BEGIN (1) */ and /* USER CODE END */ comments, include the HET driver as shown below:

/* USER CODE BEGIN (1) */

#include "het.h"

/* USER CODE END */

Between the third set of user code tags, inside the void main function, type the following:

void main(void) {

/* USER CODE BEGIN (3) */

    hetInit();

    pwmStart(hetRAM1, pwm0);

    pwmSetDuty(hetRAM1, pwm0, 50U); 

/* USER CODE END */

}

pwm0 refers to the timer PWM 0, which is the one we just configured in HALCoGen. From there, you can identify the other PWM timers as follows.

  • PWM 1: pwm1.
  • PWM 2: pwm2.
  • PWM 3: pwm3.
  • PWM 4: pwm4.
  • PWM 5: pwm5.
  • PWM 6: pwm6.
  • PWM 7: pwm7.

On execution, pwmStart starts the PWM signal and the LED connected to HET 20 starts blinking every second. pwmSetDuty sets the duty cycle of the PWM signal supplied by pwm0. The duty cycle is the percentage of the time that the pin is on. The pwmSetDuty statement used in this exercise sets the duty cycle to 50%. If you wanted to increase it to 80%, for example, change the 50U to 80U. This is especially helpful if you want to vary the brightness of an LED, or control the speed of a motor. Increasing the duty cycle parameter will increase a motor's speed or in this case, an LED's brightness, but only if you reduce the value of the Period[us].

Why? Reducing that value causes the LED to blink faster/increases the frequency of the PWM signal. If you reduce it enough, the pin will switch on and off so many times per second, that it will appear as if the LED is perfectly steady. Change Period[us] to 1000 and the LED will stop blinking. Now you'll have the opportunity to adjust its brightness. Set the duty cycle to various values and observe the brightness of the LED. If you want to dim it to 50%, change the duty cycle parameter to 50U. You can learn more about the PWM and GPIO functions in the het.c and gio.c files (located in your /source directory).

If you'd like to experiment with PWM motor control, it's important to note that only certain types of motors support PWM. A three-wire variable speed laptop fan is a good start for experimenting with PWM, as they are among the cheapest. There are also variable speed air conditioner condenser fans on the market.

Learn more about Hercules MCUs:

 

Anonymous