Jump to content
43oh

Simple Button to LED to task given problems.


Recommended Posts

I've written a program, which switches the  states of the LED when i Press it. 
It has been programmed so, that it works flawless on the select button on my µC LM3s6965. 

But when i change the buttons to be one of the other buttons, does the code not work properly. 

The code i've programmed only works for the select button, but when i choose to use the down,up,right,left button on my board as the input, it does do the same thing as when select was my input.. 

How come?? I don't understand how changing the input to another button can cause so many issues...

Link to post
Share on other sites

Hi,

The first obvious thing seen from your code is the PF0 needs a special treatment, since this pin is NMI enabled by default, so you must revert it to true GPIO behavior by unlocking it with the following code:

 

HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;

HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= 0x01;

GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_0);

 

You can simplify your code - no need for separate timers for each button - just play with all at once and if a button is pressed, just send a message.

L

Link to post
Share on other sites

Hi,

OK, my misunderstanding, due to this line:

// Enable the GPIO pins for digital function (PF0).

 

About your code - I have doubts it works correctly - once the "state" variable is set up, the other cases do not check other buttons - maybe removing the break statement will restore the whole scanning (or better to think different and re-write this task). 

 

L

Link to post
Share on other sites

Hi,

If you use the development board, the buttons are placed on PE0…PE3, but you changed from PE1… maybe is a custom board...

Try this in your initialization:

 

    //
    // Configure the GPIOs used to read the state of the on-board push buttons.
    //
    GPIOPinTypeGPIOInput(GPIO_PORTE_BASE,
                         GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
    GPIOPadConfigSet(GPIO_PORTE_BASE,
                     GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

 

L

Link to post
Share on other sites

Hi,

To initialize PF1:

 

GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_1);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA,
                     GPIO_PIN_TYPE_STD_WPU);
 
And yes, you may have problem(s) scanning...
L
Link to post
Share on other sites

Hi,

I understand - first step is to use a uniform timing for all switches - and only one timing, not separate times for each switch. I presume you got into this by the fact you have to scan PF1 which is on separate port. But I think there is a possibility to manage that - allow me some time to think.

 

Also you can reduce the scanning interval - 100 ms is too much - 10 ms would be better, since actual switches need only 1 ms debuncing time (data sheet).

 

L

Link to post
Share on other sites

Hi,

 Read this - is an excerpt from the application qs_ek-lm3s6965.c file:

    //
    // Read the state of the push buttons.
    //
    ulData = (GPIOPinRead(GPIO_PORTE_BASE, (GPIO_PIN_0 | GPIO_PIN_1 |
                                            GPIO_PIN_2 | GPIO_PIN_3)) |
              (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1) << 3));

    //
    // Determine the switches that are at a different state than the debounced
    // state.
    //
    ulDelta = ulData ^ g_ucSwitches;

    //
    // Increment the clocks by one.
    //
    g_ucSwitchClockA ^= g_ucSwitchClockB;
    g_ucSwitchClockB = ~g_ucSwitchClockB;

    //
    // Reset the clocks corresponding to switches that have not changed state.
    //
    g_ucSwitchClockA &= ulDelta;
    g_ucSwitchClockB &= ulDelta;

    //
    // Get the new debounced switch state.
    //
    g_ucSwitches &= g_ucSwitchClockA | g_ucSwitchClockB;
    g_ucSwitches |= (~(g_ucSwitchClockA | g_ucSwitchClockB)) & ulData;

    //
    // Determine the switches that just changed debounced state.
    //
    ulDelta ^= (g_ucSwitchClockA | g_ucSwitchClockB);

    //
    // See if any switches just changed debounced state.
    //
    if(ulDelta)
    {

I think this is best to do - and when changes are detected just send messages as you wish.

 

L

Link to post
Share on other sites

Hi,

This code is part of application qs_ek-lm3s6965 - it uses a sys timer interrupt at 10 ms to make several things, including scanning all switches. You may read the .c file for all variables declared there and their meaning. Mainly it uses a static variable and a temporary one; XORing both determines if a switch was pressed or not.

ulData is the state of switches - please note how different ports (E and F) are read and mixed to have all five switches info in a single variable. 

The last ulDelta has a 1 for a pressed switch ( inverted info in previous stages, since the real info is a 0 ) - and from there it is easy to manage them - hope this will help you.

 

L

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...