One way for a microcontroller to retrieve input is known as polling.  Polling means repeatedly measuring  a sensors value.  I will describe how to poll a pushbutton switch  and react to its measurement when using a Psoc3 microcontroller.

Capture

 

/*
 * Filename: switchpolling.c
 *
 * Title: Polling a switch with a Psoc microcontroller
 *
 * Description: This source code file is used with a Psoc 3 microcontroller to poll a switch.
 *  Upon detecting a change in the switches state, An LCD is updated to display the number of
 *  times the switch has been closed in Hex and decimal format.
 *
 * Copyright 2011 BK Turley & Robert Sanson. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *      conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright notice, this list
 *      of conditions and the following disclaimer in the documentation and/or other materials
 *      provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY BK Turley & Robert Sanson ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BK Turley & Robert Sanson OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of BK Turley & Robert Sanson.
 *
 */

#include         // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include 	// needed to use itoa()

void main(void)
{
	WORD count = 0; // holds the number of switch presses
	unsigned char c2[4]; // holds count's value after conversion to a C string.
	unsigned char cnt[12] = "Counting   "; //a label for LCD
	unsigned char lbl[12] = "Hex:   Dec:"; //another label for LCD

	LCD_Start();

	while (1)//poll, but only increment count once per switch press.
	{
		BOOL pressed = SWITCH_Data_ADDR & SWITCH_MASK; //take a measurement of the switch state
		if (pressed)
		{
			count++;
			while (pressed)
			{
				LCD_Position(0,0);
				LCD_PrString(cnt);
				pressed = SWITCH_Data_ADDR & SWITCH_MASK;
			}
		}

		// Display count to LCD in Hex and Decimal format.
		LCD_Position(0,0);
		LCD_PrString(lbl);
		LCD_Position(1,0);
		LCD_PrHexByte(count);
		LCD_Position(1,7);
		itoa(c2,count,10);// store count's value as a decimal string for display purposes.
		LCD_PrString(c2);
	}

}

Leave a Reply

Your email address will not be published.