Example: Reading switch inputs (basic code)


This switch-reading code is adequate if we want to perform
operations such as:
• Drive a motor while a switch is pressed.
• Switch on a light while a switch is pressed.


• Activate a pump while a switch is pressed.
These operations could be implemented using an electrical switch,
without using a microcontroller; however, use of a microcontroller
may well be appropriate if we require more complex behaviour. For
example:
• Drive a motor while a switch is pressed
Condition: If the safety guard is not in place, don’t turn the
motor. Instead sound a buzzer for 2 seconds.
• Switch on a light while a switch is pressed
Condition: To save power, ignore requests to turn on the
light during daylight hours.
• Activate a pump while a switch is pressed
Condition: If the main water reservoir is below 300 litres, do
not start the main pump: instead, start the reserve pump and
draw the water from the emergency tank.

/*-------------------------------------------------------------*-
Switch_read.C (v1.00)
--------------------------------------------------------
A simple 'switch input' program for the 8051.
- Reads (and debounces) switch input on Pin 1^0
- If switch is pressed, changes Port 3 output
-*-------------------------------------------------------------*/
#include <Reg52.h>
/* Connect switch to this pin */
sbit Switch_pin = P1^0;
/* Display switch status on this port */
#define Output_port P3
/* Return values from Switch_Get_Input() */
#define SWITCH_NOT_PRESSED (bit) 0
#define SWITCH_PRESSED (bit) 1
/* Function prototypes */
void SWITCH_Init(void);
bit SWITCH_Get_Input(const unsigned char DEBOUNCE_PERIOD);
void DISPLAY_SWITCH_STATUS_Init(void);
void DISPLAY_SWITCH_STATUS_Update(const bit);
void DELAY_LOOP_Wait(const unsigned int DELAY_MS);

/* ---------------------------------------------------------------- */
void main(void)
{
bit Sw_state;
/* Init functions */
SWITCH_Init();
DISPLAY_SWITCH_STATUS_Init();
while(1)
{
Sw_state = SWITCH_Get_Input(30);
DISPLAY_SWITCH_STATUS_Update(Sw_state);
}
}
/*-------------------------------------------------------------*-
SWITCH_Init()
Initialisation function for the switch library.
-*-------------------------------------------------------------*/
void SWITCH_Init(void)
{
Switch_pin = 1; /* Use this pin for input */
}
/*-------------------------------------------------------------*-
SWITCH_Get_Input()
Reads and debounces a mechanical switch as follows:
1. If switch is not pressed, return SWITCH_NOT_PRESSED.
2. If switch is pressed, wait for the DEBOUNCE_PERIOD (in ms).
Then:
a. If switch is no longer pressed, return SWITCH_NOT_PRESSED.
b. If switch is still pressed, return SWITCH_PRESSED
See Switch_Wait.H for details of return values.
-*-------------------------------------------------------------*/
bit SWITCH_Get_Input(const unsigned char DEBOUNCE_PERIOD)
{
bit Return_value = SWITCH_NOT_PRESSED;
if (Switch_pin == 0)
{
/* Switch is pressed */
/* Debounce - just wait... */
DELAY_LOOP_Wait(DEBOUNCE_PERIOD);
/* Check switch again */
if (Switch_pin == 0)
{
Return_value = SWITCH_PRESSED;
}
}
/* Now return switch value */
return Return_value;
}
/*-------------------------------------------------------------*-
DISPLAY_SWITCH_STATUS_Init()
Initialization function for the DISPLAY_SWITCH_STATUS library.
-*-------------------------------------------------------------*/
void DISPLAY_SWITCH_STATUS_Init(void)
{
Output_port = 0xF0;
}
/*-------------------------------------------------------------*-
DISPLAY_SWITCH_STATUS_Update()
Simple function to display data (SWITCH_STATUS)
on LEDs connected to port (Output_Port)
-*-------------------------------------------------------------*/
void DISPLAY_SWITCH_STATUS_Update(const bit SWITCH_STATUS)
{
if (SWITCH_STATUS == SWITCH_PRESSED)
{
Output_port = 0x0F;
}
else
{
Output_port = 0xF0;
}
}
/*-------------------------------------------------------------*-
DELAY_LOOP_Wait()
Delay duration varies with parameter.
Parameter is, *ROUGHLY*, the delay, in milliseconds,
on 12MHz 8051 (12 osc cycles).
You need to adjust the timing for your application!
-*-------------------------------------------------------------*/
void DELAY_LOOP_Wait(const unsigned int DELAY_MS)
{
unsigned int x, y;
for (x = 0; x <= DELAY_MS; x++)
{
for (y = 0; y <= 120; y++);
}
}