July 9, 2015

Blink….

Step 3 : Setting The Data direction

This is an important step in using the GPIO. Setting the Data direction means nothing but to say the controller whether the GPIO PIN is an Input/output pin. 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);

Let us take another example. Consider that we want to configure the PIN5 and 7 of PORTA and PIN3 of PORTB also as input. This can be done as shown below :

GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_5|GPIO_PIN_7);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_3);

Step 4 : Writing data into the GPIO

This is the last step. We use the function called “GPIOPinWrite()” to write data to the bitbanded pins. The function has three 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. The third parameter is the value to be transferred to the pins. Eg. consider we want to set PIN3 of any port as high. So the data must be 0b00001000 in binary representation which in decimals can be written as 16. Now if the second parameter only contain GPIO_PIN_3, then even if we send 0b11111111 to the PORT, the value of PIN 3 will only be altered. This is due to bit banding. This means that in this case a value of 255 and 16 have the same effect.

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

Delay can be created by using the “SysCtlDelay()” function. The parameter to be provided inside the function specifies the number of loops it has to wait. 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);
}

Leave a Reply

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