In the previous lessons, we were introduced to the TiTAN family development board based on MSP430 and two development environments from Texas Instruments. We hope you have chosen the most convenient one for you.
So, we have learned how to configure the microcontroller pins to control the led, and how to turn it on and off.
We suggest you to learn how to work with a button on our example “Simple button” by clicking the link http://titan–project.com/wp–content/uploads/2020/01/TiTAN_example_02_simply_button.zip
You should understand that this method is only for informational purposes and is practically not used in real programming.
Detailed study of the microcontroller with the MSP430
Now we will dive into a more detailed study of the microcontroller with the MSP430 core and learn how to use two very important mechanisms for this. These are timers and interrupts.
`In this lesson, we will look at two examples at once, in which we will learn how to simply control the state of the led by interrupting the timer and how to control the brightness of the LEDs using a timer in PWM mode.
From now you should always have at hand following books:
- Reference data sheet: http://www.ti.com/lit/ds/symlink/cc430f5137.pdf
- User manual: http://www.ti.com/lit/ug/slau259e/slau259e.pdf
From theCC430F5137 microcontroller reference documentation, we can find that we have two 16-bit timers at our disposal.
We choose TA1 as an example.
In the user guide on page 398, you can familiarize with the structure of the timer and its configuration registers.
Do not be afraid of such a large number of registers! In fact, there are not many of them. On page 411 of the user guide, they are all listed.
Don’t be lazy to read this section carefully. Gradually, you will understand their purpose and can easily configure the desired mode of operation.
Page 409 describes the mechanism for interrupting timer events.
Let’s get started.
First, establish the possibility of interruption by the timer event
Next set the count limit to 500 milliseconds for the timer
Configure the timer so that it works from the 32768 Hz clock source (ACLK = TASSEL_1), count to the value written to the TA1CCR0 register (MC_1), and clear the current value of the count register (TACLR) of the timer itself (TA0R).
Almost everything is ready.
It remains to create the interrupt handler itself, in which we will turn the led on and off.
You can do it easily. This function is created with a specific type of headline, which states that this function must be executed if an event occurs that interrupts the timer we need. And in the body of this handler, we just change the state of the led to the opposite.
Everything is ready!
We collect our project and launch it.
It is not difficult, is it? Then let’s continue.
PWM operation mode
In the second example (in which we will study the PWM operation mode), we need to connect four LEDs. This is where we will learn how to control brightness.
If you still don’t know what PWM is, then you can get acquainted with the principle that is well demonstrated by Andrey Kulagin at this link: https://www.youtube.com/watch?v=qvKFTU7SNj8
We will need to refer to the reference documentation of Texas Instruments again. Now we are interested in TA0 timer output signals, which have the CCRxA common name . Here they are.
As should be clear from the written, P3. 2 … P3.5 ports have a second function as timer outputs. But there is one small point – it is not convenient for us to use these ports. There can be many reasons for this (they are already used for other purposes, the board topology interferes, it’s just boring, etc.).
What should you do? Remember that when we were introduced to the Titan family boards, we talked about the remapping mechanism. Let’s apply it.
To do this, we select the ports P2.0 … P2.3, and with just five lines of code, we will reassign the ports as we have chosen.
With the first command, we enter a “password” to change the port assignment, and assign each port a new function that it didn’t have before. And the last line we save the settings. Easy, right?
Now let’s set the timer.
In the TA0CCR0 register, we will set the total count period of the timer (this will determine the PWM frequency). In the TA0CCTLx registers, we indicate that we are interested in the PWM mode with switching
And in the TACCRx registers, set the width of the generated pulses for each channel.
And at the end, configure the timer clock source and configure the ports to exit mode.
That’s it! We collect our project and launch it.
Here’s what we’ve got
We will demonstrate a similar result using the TiTAN-I board and the TiTAN board as a debugger programmer
Congratulations! You have mastered the basic functions of the timer and learned how to use interrupts.
We suggest you to combine these two examples into one and analyze the result.