Jump to content
Pradeepa

Polling a switch

Recommended Posts

I am a newbie to Stellaris. I have a LM4F120H5QR launchpad with me. I just want to poll a switch and at least turn on an LED.

 

all i want to do is:

 

if(button pressed){

        turn on the led;

}

 

 

For this do I need to use the ButtonPoll() API as per the Lab3 of the stellaris wiki.

 

I cannot really understand the code much, i mean i know what bouncing is but i did not see how they actually poll the switch in the buttons.c. For example what is that g_s_Appstate or something which is there in the code. 

 

Please help me.

Share this post


Link to post
Share on other sites

Lab3 code has nothing to do with this. They simply asks us to write a program using Buttons.h API to switch on an LED. 

 

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "buttons.h"

int main(void) {
	
	unsigned char button_val;
	unsigned char Delta;

	SysCtlClockSet(SYSCTL_SYSDIV_5| SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE , GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);

	ButtonsInit();

	while(1){
		SysCtlDelay(100000);

		button_val = ButtonsPoll(Δ, 0);
	}

}

 

This is my code so far. This will not compile. 

 

Errors:

 

 

Description Resource Path Location Type
unresolved symbol ButtonsPoll, first referenced in ./main.obj Lab3HW C/C++ Problem
unresolved symbol ButtonsInit, first referenced in ./main.obj Lab3HW C/C++ Problem
#10010 errors encountered during linking; "Lab3HW.out" not built Lab3HW C/C++ Problem
<a href="file:/C:/ti/ccsv5/tools/compiler/dmed/HTML/10234.html">#10234-D</a>  unresolved symbols remain Lab3HW C/C++ Problem
 
I just want to know how to use buttons.h in the correct way.

Share this post


Link to post
Share on other sites

It didn't work. 

 

Stellaris ware qs-rgb example uses this ButtonPoll() thing. I don't know how it is working. I included buttons.h and i included the path too. But somehow the linker cannot resolve symbols it seems. What can I do?

Share this post


Link to post
Share on other sites
I cleaned and rebuilt it. Now i get the following errors.

 

Description Resource Path Location Type

unresolved symbol ROM_GPIODirModeSet, first referenced in ./drivers/buttons.obj Lab3HW   C/C++ Problem

unresolved symbol ROM_GPIOPadConfigSet, first referenced in ./drivers/buttons.obj Lab3HW   C/C++ Problem

unresolved symbol ROM_GPIOPinRead, first referenced in ./drivers/buttons.obj Lab3HW   C/C++ Problem

unresolved symbol ROM_SysCtlPeripheralEnable, first referenced in ./drivers/buttons.obj Lab3HW   C/C++ Problem

<a href="file:/C:/ti/ccsv5/tools/compiler/dmed/HTML/10234.html">#10234-D</a>  unresolved symbols remain Lab3HW   C/C++ Problem

#10010 errors encountered during linking; "Lab3HW.out" not built Lab3HW   C/C++ Problem

Share this post


Link to post
Share on other sites

This is something i just discovered which might shed some light on the overall picture. Just now i navigated to the rom.h under includes from the project explorer. I found out that the functions inside the Rom.h file looks disabled. With faded blue color. 

 

https://dl.dropboxusercontent.com/u/81891666/error.JPG

 

But in qs-rgb.c file which actually is an example that uses this Button.h i found that those functions looks enabled. With dark blue color. 

Share this post


Link to post
Share on other sites

Hi,

It would be useful to know what tools do you use (CCS/other?) I will assume CCS. The ButtonsPoll function must also be part of your project - so just #include drivers/buttons.h will not suffices. Add to the project the path to this file (in Include paths). Add the file buttons.c to your project, as a symbolic link; this way it will be part of the project, compiled and the linker will find out the code of this function. Do not forget to call also ButtonsInit() before using ButtonsPoll() (yes, you did it..).

Now, still for CCS, but should be done for other tools: to the Compiler | preprocessor add the followings defined macros:

DEBUG

PART_LM4F120H5QR

TARGET_IS_BLIZZARD_RA1 (this will solve the ROM_xxx complaints..)

 

Lyon

Share this post


Link to post
Share on other sites

Hello Friend,

 

 

Try out this code. It will clarify a lot your questions:

 

 

 

#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"

#define LED_RED GPIO_PIN_1
#define LED_BLUE GPIO_PIN_2
#define LED_GREEN GPIO_PIN_3

#define BUTTON_1 GPIO_PIN_0
#define BUTTON_2 GPIO_PIN_4

void main(void) {
        int light;

        // configure system clock to run at 50 MHz
        // use external crystal (16 MHz) and PLL
        SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
        SYSCTL_OSC_MAIN);

        // Enable PORT F GPIO
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

        // Unlock PF0 so we can change it to a GPIO input
        // Once we have enabled (unlocked) the commit register then re-lock it
        // to prevent further changes.  PF0 is muxed with NMI thus a special case.
        HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY_DD;
        HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01;
        HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;

        // set LED pins as outputs
        GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_RED|LED_BLUE|LED_GREEN);

        // SEE COMMENTS IN THE POST

        // set user switches as inputs
        GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, BUTTON_1|BUTTON_2);

        // turn weak pull-ups on
        GPIOPadConfigSet(GPIO_PORTF_BASE, BUTTON_1|BUTTON_2, GPIO_STRENGTH_2MA,
        GPIO_PIN_TYPE_STD_WPU);

        // loop forever
        for (; {
                light = 0;

                if(!GPIOPinRead(GPIO_PORTF_BASE, BUTTON_1))
                        light |= LED_RED;

                if(!GPIOPinRead(GPIO_PORTF_BASE, BUTTON_2))
                        light |= LED_BLUE;

                GPIOPinWrite(GPIO_PORTF_BASE, LED_RED|LED_BLUE|LED_GREEN, light);

                SysCtlDelay(500000);
        }
}


 

 

It is derived from a post found on the internet and also a search in buttons.h and buttons.c under StellarisWare directory .

 

Costin,

Share this post


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...