Jump to content
sherifsorour

Led & Switch Program Failure - Tiva C

Recommended Posts

Hello Guys,

 

Iam starting to learn embedded systems with Tiva C board.

 

I have issue in understanding simple code below

 

this is part of the code

input = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4);
 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , input);

PIN4 connected to switch and pin connected to led. It shall take the switch value and show it on the led but it doesn't work.

 

Someone suggested to write the same code with the below if condition and it worked but I don't understand why the above code doesn't work.

if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4 ) == GPIO_PIN_4)
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , GPIO_PIN_1 );
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , 0 );
}

Appreciate if you can clarify this to me.

 

Thanks

Share this post


Link to post
Share on other sites

Assuming you are using TivaWare, make sure to enable the GPIO peripheral and configure the pins as input and output respectively.

 

From a quick Google, something like this:

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlDelay(3);

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

You might want to check out these tutorials:

https://sites.google.com/site/luiselectronicprojects/tutorials/tiva-tutorials/tiva-gpio

Share this post


Link to post
Share on other sites
input = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4);
 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , input);

GPIOPinRead reads the GPIO pin status and applies the mask given as the second parameter before returning a value, in this case 0x00 or 0x10.

GPIOPinWrite similarly uses a mask (telling which GPIO pins to change) as the second parameter, the third is the actual pin values to change.

For the code above 0x02 is required to output a high on pin 1, 0x10 as returned from GPIOPinRead is simply ignored.

 

I addition to use a if...then...else statement to change the led state you may using a shift:

 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , input >> 3);

to change 0x10 to 0x02 (00010000 shifted right 3 times becomes 000000010 which is what is needed), or the ternary operator:

 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , input ? GPIO_PIN_1 : 0);

A safer version of the "ternary" version above, must be used if input represents the state of several pins:

 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , (input & GPIO_PIN_4) ? GPIO_PIN_1 : 0);

Using a shift may be a bit dangerous if you change the pins used, the code will likely fail if you forget to change the shift accordingly.

 

The GPIOPinWrite implementation (or rather the on-chip physical implementation) is fact nice because it performs atomic changes to the pins (will set/reset pins simultaneously in an uninterruptable way).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×