This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LEDS & Switches Simple program not working

Hi all,

Iam new to embedded systems and I have started to learn with TIVA c TM4C123 board.

I have created the below program but it doesn't  work proberly. Hope you can show me where the error is.

This program shall take input from sw1 and sw2 on the board and show specific colour on RGB led connected to PF 1,2,3

What happens when I run the code is when no switches pressed led light is blue and when sw1 is pressed it shows sky blue and when sw1 is pressed nothing happens.

what it should do is show green light if no switches pressed and red light if sw1 is pressed and blue light if switch 2 is pressed.

Here is the code

============

/*************************
PORT F Addresses
*************************/

#define RCGCGPIO (*((volatile unsigned long*)0x400FE608)) //CLOCK
#define PORTFDATA (*((volatile unsigned long*)0x400253FC)) //DATA
#define PORTFDIR (*((volatile unsigned long*)0x40025400)) //DIRECTION
#define PORTFDEN (*((volatile unsigned long*)0x4002551C)) //ENABLE
#define PORTFLOCK (*((volatile unsigned long*)0x40025520)) //LOCK (lock or unlocks PF0)
#define PORTFCR (*((volatile unsigned long*)0x40025524)) //COMMIT (uncommit PF0)
#define PORTFPUR (*((volatile unsigned long*)0x40025510)) // PULL UP resistor
#define PORTFPDR (*((volatile unsigned long*)0x40025514)) // PULL Down resistor
/*************************/
int sw1;
int sw2;
int main (void)

{
RCGCGPIO |= 0x20; //Enable clock for PORT F
PORTFLOCK = 0x4C4F434B; // unlock commit reg
PORTFCR |= 0x01; // unlock PF0
PORTFDEN |= 0x1F; //Enable pins 0 to 4
PORTFDIR |= 0x0E; // pins 0 and 4 input - pins 1,2,3 output
PORTFPUR |= 0x01;

while (1)
{
sw2 = PORTFDATA & 0x00000001;
sw1 = PORTFDATA & 0x00000010;

if (sw1 == 1)
{PORTFDATA = PORTFDATA | 0x00000002;}

else if (sw2 == 1)
{PORTFDATA = PORTFDATA | 0x00000004;}

else
{PORTFDATA = PORTFDATA | 0x00000008;}
}
}

================

Thank you

  • Hello Sherif

    I would first of all request to move away from the DRM method of coding to a proven API based coding. I can point mistakes in the code but rather than helping you, it may prove counter constructive. ARM micro controllers and their peripherals have been advancing significantly beyond DRM.

    Regards
    Amit
  • Hello Amit,

    Thanks for your response.

    Iam sorry I don't understand what you mean by DRM or API.

    Iam googling it now but appreciate if you can provide a link that clarifies more about these methods.

    Thanks again.
    Sherif
  • Bravo Amit!

    Only if the, "Do not use DRM or ASM method AND request help" message is delivered regularly - by ALL vendor staff - might this "extra complicated & overly demanding - and inefficient method" be ramped down.

    Seeking the "hard way" rather than, "Tried, True, Tested" way - hardly makes sense - and surely should NOT be encouraged.

    The claim is (so often) made that the "significantly extra effort" imposed by DRM/ASM "aids learning!" Yet - for that to prove true - why do SO MANY land here - with failed results? (that's never/ever answered - isn't that so?)

    Should not students and/or others employ "known good and efficient methods" - rather than the archaic "DRM/ASM - which (most always) delays, frustrates posters and compounds that ill by placing (undue) demands upon vendor's (short) staff?

    Really - where is the "higher learning?" (surely not provided by crash/burn DRM/ASM!)
  • Hello Sherif,

    Thanks for being flexible in asking the questions.

    sw2 = PORTFDATA & 0x00000001;

    is called DRM (Direct Register Macro). While it is efficient it is complicated enough when beginning on a uC. Some vendors use it some don't (we are the latter for TM4C)

    The same can be achieved by sw2 = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0); which is more readable and usable from a beginners point of view.

    Regards
    Amit
  • Hello Amit,

    Thanks for your explanation. Now I understand what you mean by API, but I have a question.

    I tried to write code by this method but I couldn't find out where these variables and functions are defined. Do I need to defined and build them from zero by myself or what?

    Thanks
    Sherif
  • Hello Sherif

    These API are already a part of TivaWare, so you do not need to define or build them. Take the blinky project as a start up project.

    Regards
    Amit
  • Hello Amit,

    I have built my first project with Tivaware based on the blinky project but still doesn't work. Hope if you can check the code and point out my errors.

    The code is very simple it shall take input from PF4 and show it on RGB LED by setting PF1.

    Here is the code

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "driverlib/gpio.h"
    #include "driverlib/sysctl.h"
    
    int
    main(void)
    {
        volatile uint32_t ui32Loop;
    
        //
        // Enable the GPIO port that is used for the on-board LED.
        //
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    
        //
        // Check if the peripheral access is enabled.
        //
        while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF))
        {
        }
    
        GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
    		GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4 );
        
        while(1)
        {
     
    				GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4 );
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 , GPIO_PIN_4 );
           
        }
    }
    

  • Hello Sherif,

    This is not correct method

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

    It should be more like

    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 );
    }

    Regards
    Amit
  • Thanks amit for your clarification it worked but why the first method is wrong?

    Doesn't it take input from pin4 and copy it to pin1? what is wrong with that?
  • Hello Sherif

    No. The GPIO data register is bitbanded. I would suggest data sheet section read for Bit Banded address and data register.

    Regards
    Amit