Now lets talk about the GPIOs. GPIO are General Purpose Input Output registers which available to the user to configure. These are connected to
Step1 – Set up System Clock
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
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);
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
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
Step 3 – Setting The Data direction
If this step is not performed properly, the GPIO may not work as we wish. The pins are configured by bit banding, which means that each pin to be configured must be mentioned separately when we configure the port. The function that we use to set a pin as input is “GPIOPinTypeGPIOInput()”, whereas, the one used to set a pin as output is “GPIOPinTypeGPIOOutput()”. Both the functions are having two parameters. The first one refers to the PORT that we want to configure. The second one refers to the pins that we want to configure. In case we want to configure more than one pin in the same port at a time, we will have to
logically OR each of the pin macros.
Eg. Consider that we want to
configure the PIN 6 of GPIO PORT F as output. This can be done as shown below :
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_6);
GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_5|GPIO_PIN_7);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_3);
Step 4 – Writing data into the GPIO
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 2); //Lights the RED LED
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4); //Lights the BLUE LED
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 8); //Lights the GREEN LED
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 14); //Lights the ALL LEDs
Step 5 – Delay loop
From the Stellaris API it is evident that the microcontrolleer takes 3 clock cycles to run a single delay loop. Therefore we create the delay getting the system clock frequency using the function “SysCtlClockGet()”, dividing the value by 3 and then multiplying the result by the delay required in seconds.
The function provided below will do the job for us.
void delay_ms(int del) //generates delay in milliseconds
{
del = (SysCtlClockGet()/3.0)*del/1000.0;
SysCtlDelay(del);
}
Winding Up
main.c | |
File Size: | 1 kb |
File Type: | c |
/*
* Stellaris LAUNCHPAD LM4F120H5QR
* C++ code to blink the tri colour led
*/
#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <driverlib/gpio.h>
#include <driverlib/sysctl.h>
void delay_ms(int del)
{
del = (SysCtlClockGet()/3.0)*del/1000.0;
SysCtlDelay(del);
}
int main(void)
{
/*
* The System clock is run using a 16 Mhz crystal connected to the main oscillator pins of the microcontroller
* This generates a internal clock signal of 400 Mhz using the PLL
* The signal is prescaled by the system by 2
* Now we are defining a prescale of 5 in addition to make the clock frequency 40MHz
* The system clock frequency must be less than or equal to 80MHz
*/
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
/*
* Enabling the system clock for GPIO PORT F
* We must do this for every peripheral that we use
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
while(1)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 2);
delay_ms(1000); // 1 second delay
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0); //explicitly turning off the led
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
delay_ms(1000);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 8);
delay_ms(1000);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0);
}
}
Tweet
(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);
ga(‘create’, ‘UA-56939176-1’, ‘auto’);
ga(‘send’, ‘pageview’);
I get pleasure from, result in I discovered just what I used to be looking for.
You have ended my four day lengthy hunt! God Bless you man. Have a great day.
Bye