July 9, 2015

Blink

Blink

In this tutorial we will be dealing with the basic idea of GPIOs and how to configure them so as to light the on Board tri-color LED on the Stellaris LaunchPad. There are 6 General Purpose Input Output ports namely PA,PB,PC,PD,PE and PF. Each of the ports are capable of bit banding which can be used to configure each pin of any port in whatever way we wish to. A Newbie can think of bit banding as masking the corresponding pins that we wish to configure, so that the value that we send into the port will only affect the masked pins.For example if i am masking 6th pin of PORT – A and then I send a value into the PORT – A, the value will affect only the value of PIN6. This is what effectively bit banding does. Now lets talk about the GPIOs. GPIO are General Purpose Input Output registers which available to the user to configure. These are connected to the external output pins of the Stellaris LaunchPad. These have an output voltage typically of 3.3V but it depends largely on the VBat. The maximum Current rating of each pin is around 40mA. The Tri-color LED on the Stellaris LaunchPad is made up of three LEDs namely RED GREEN and BLUE. When we look into the schematic of the LaunchPad, we see that the red LED is connected to the PF1, Blue LED is connected to the PF2 and Green LED is connected to PF3.

Step1 – Set up System Clock

In the Stellaris LaunchPad, there is an external main oscillator of 16MHz frequency. This is the Oscillator of our interest. If we check in the datasheet, we can see that this External Main Oscillator is fed into the Phase Loop Lock(PLL) to generate a clock of 400 MHz. This clock is divided by 2 in the hardware itself to generate a 200 MHz clock. This is then fed into the SYSDIV which is user configurable to generate the required system clock frequency.Now, if we check in the datasheet, it is evident that the maximum system clock frequency that can be delivered to the LM4F120H5QR CPU is 80 MHz. This is the reason why SYSDIV is important to us. In any code for this uC the firs line is supposed to set the clock frequency. Now let us see how to configure the clock frequency using the StellarisWare DriverLib functions. The “SysCtlClockSet()” function can be used to set the clock frequency. The code snippet given below shows how to configure the system clock to 40MHz.

SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

We need to generate a system clock of 40 MHz. We know that the input clock frequency after system prescale is 200 MHz. Now we need to prescale this by 5 which is done my the macro :

 SYSCTL_SYSDIV_5.

There are many more prescales available which you are free to use. But make sure that the generated system clock frequency is less than 80 MHz.

SYSCTL_USE_PLL –

this macro is used to specify that we are using the PLL to generate the 400 MHz clock frequency.

SYSCTL_XTAL_16MHz

This says to the uC that we give the input to the PLL from an external crystal(XTAL) of frequency 16 MHz.

SYSCTL_OSC_MAIN –

This denotes that we are using an external main oscillator ( Again represents the 16 MHz crystal ).

Now we know that all these are to be done together, and that is the reason why we logically OR all the macros.
##NOTE – Generally inside a function whose name starts with SysCtl, the macro names will start with “SYSCTL_”. This will help you in selecting the correct macro most of the time.

Step 2 – Enable the GPIO

When an LM4F120h5QR uC is powered up, all the peripherals are disabled by default. Therefore we have to mention in our code, the GPIO ports that we need to enable. In our case, we know that all the 3 LEDs are in the PORTF. Therefore it is only required to enable the GPIO PORTF. By enabling a GPIO PORT, we mean that we enable the system clock to that peripheral so that the peripheral will work properly. This is done using the “SysCtlPeripheralEnable()” function as shown below.

 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); 

The above given statement is self explainatory. Now let us consider that we want to enable PORTA. this can be done as ginen below :

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); 

Leave a Reply

Your email address will not be published. Required fields are marked *