Maker.io main logo

Getting Started with the Tiva Launchpad Board Part 2.

2023-08-18 | By Sarah Aman

License: Attribution Microcontrollers Launchpad

To continue with our series on the Tiva Launchpad board, we will talk about getting started with C programming for the Tiva Launchpad Board. In this tutorial, we will write a program that will blink the LED on the board in different colors when the buttons on the panel are pressed. To get started, make sure you have downloaded the software discussed in part 1 here and have the datasheet for the TM4C123GH6PM microcontroller downloaded. This can be found on DigiKey’s website here: https://www.digikey.com/en/products/detail/texas-instruments/EK-TM4C123GXL/3996736

Photo of where the datasheet can be found

To begin, make sure your board is in debug mode. You can set it in debug mode by sliding the switch next to the port where the microcontroller is plugged into the computer. The switch says debug on it so you can see where it needs to be. Make sure you plug the power cable into your computer and the board at the port directly opposite the push buttons. It is also important to locate the reset button on your board. We will use that later when programming code to the board. To begin, we will need to create a brand-new Kiel project. The process for this can be seen in part 1 of this series. To begin writing a C program we will need to add a C file to the project, then name and save it. Once again, this process can be seen in part 1 of this series.

The first line of the C file includes the device header.

first line of the program

Next, we will need to define the device register locations. To do this, we will use the datasheet for the microcontroller. We will need to define the following based on the buttons and LEDs we will be using. We will need to define the Data Register, the Direction Register, the Alternate Function Select Register, the Pull Up Resistor Register, the Digital Enable Register, the Lock Register, the Commit Register, the Analog Mode Select Register, the Port Control Register all on Port F, and the Run Mode Clock Gate Control 2 Register for the System Clock Control. All these registers will be defined using volatile unsigned long values in hexadecimal. The process for declaring a number is hexadecimal is to put “0x” in front of the actual number value. The table with all these registers can be found on page 660. On each page of this data sheet section, we will also be able to find the base address of the port. We will use the APB address for right now, so the base address for Port F is 0x40025000. These different register addresses work by using the base address plus an offset to determine which register address is being accessed. The data register does not have an offset, but to activate the port you need to add 0x3FC to the address. To turn on each pin, we need to write a 1 to the binary location offset by 2. 0x3FC in binary is 0011 1111 1100, and binary is read right to left. This means the zeros in the first and second digits are simply an offset. There are 1’s in all eight digits left of the two 0’s, therefore, all eight pins on Port F are turned on. If you would like more information on how the addressing and registers work on Port F, I recommend looking into the datasheet.

To set the register address for the System Clock register, you will need to go to page 236 of the data sheet to find the table for the System Control Register. From there, click on RCGC2, and you should be able to find the correct base address and the offset for the default clock.

section of code

Next, I am going to create a function to initialize Port F called “init_portf”. To begin, I am going to OR the value 0x20 with SYSCTL_RCGC2_R. This will turn on the Clock for Port F by writing a one to the eight binary digits, which equates to F. Next, we will need to run a delay function to give the clock time to settle. To create a delay loop, we will use a while loop that subtracts one each time. This function delays for about 0.1 seconds. I would recommend just copying this delay function as it is a useful tool when working with this board. After the delay, we are going to write the hex value 0x4C4F434B to the lock register and write the value 0x1F to the Commit Register. These values essentially function as a password to unlock PF4, which is normally locked. We also need to write 0x0E to the direction register to set pin 0 and pin 4 as inputs and pins 1, 2, and 3 as outputs. The hex value 0x11 is written to the Pull-Up Resistor Register to turn on the Pull-Up Resistors for Pins 0 and 4. These pins are the push buttons located on the board, and in order to use a button, they require a pull-up resistor. Finally, we need to write the value 0x1F to the Digital Enable Register to digitally enable pins 0-4. The rest of the pins are written to 0 because they do not need to be enabled for this program.

section of code

section of code

Finally, we just need to write the main portion of the program. To begin, we need to define three global variables, as seen below. Then, we will create an area for function prototypes; a function prototype is essentially the first line of the function. In this function, we will have two prototypes, as seen below. Then, in the main section of the function, we will call the init_portf function. Next, we will call a while loop that will loop infinitely by calling while(1). Inside the while loop, we will set the global variable for switch one equal to GPIO_PORTF_DATA_R AND 0x10. This will set Switch one equal to pin 4, which is switch one on the board. We will set the global variable for switch two equal to GPIO_PORTF_DATA_R AND 0x01, which is Pin 0. Both switches are active low, so when they are equal to 0x00 they have been pressed. This means that we need to tell the light to turn on when the switch has been pressed. As you can see below, the green LED flashes while switch one is pressed. The red LED flashes when switch two is pressed, and the blue LED flashes when nothing is pressed. The delay values underneath the if statement ensure that it does not look like all the LEDs are on at the same time. The microcontroller operates much faster than the human eye can see changes, so it is important to use delay loops like these.

 

main program

Congratulations, now you have created a program in C. To compile the project, press the build button or F7 to compile the project. This must be done before loading it onto the board. To load it to your board, press the download button at the top of the screen or F8 to see it in action. Once it is loaded to your board press the reset button and test your program. If at any time you want to start your program over, press the reset button again. This program is based on a program I found on GitHub here: http://github.com/sphanlung/

build button

load button

制造商零件编号 EK-TM4C123GXL
LAUNCHPAD TM4C123G EVAL BRD
Texas Instruments
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum