📄 switch4.c
字号:
/* File: Switch4.C */
/****************************************************************************
*
* STK16XSW.SWITCH4.MAIN
* =====================
*
* main function for STK16X DIP switch demo,
* reads DIP switch and shows the state of the switches on the LCD;
* each switch also turns a LED on or off.
*
* TQ-Systems GmbH
* ----------------
* Customer: TQ-Components
* Project : STK16XSW
* Tools : uVision 2.05
*
* Rev: Date: Name: Modification:
* ----+---------+----------------+------------------------------------------
* 100 19.01.01 A. Lichte taken over from STK16X.506
*****************************************************************************/
/****************************************************************************
*
* availability summary
*
* available for Starterkit: STK16X STK16XU
* conformed for Starterkit: STK16X STK16XU
* available for Modul : TQM164 TQM165 TQM165U TQM166 TQM167
* TQM167UL TQM167U TQM167LC
*
* conformed for Modul : TQM164 TQM165 TQM165U TQM166 TQM167
* TQM167UL TQM167U TQM167LC
*****************************************************************************/
/*==========================================================================*
* include files (#INCLUDE):
*===========================================================================*/
/*--------------------------------------------------------------------------*
* standard include files:
*---------------------------------------------------------------------------*/
#include <stdio.h> /* standard I/O functions */
#include <reg167.h> /* special function register */
/*--------------------------------------------------------------------------*
* project specific include files:
*---------------------------------------------------------------------------*/
#include "timer.h"
#include "lcd.h"
#include "led.h"
#include "dpswtch4.h" /* switch function */
/*==========================================================================*
* module internal definitions (#DEFINE):
*===========================================================================*/
/*==========================================================================*
* module internal type declarations (TYPEDEF):
*===========================================================================*/
/*==========================================================================*
* module internal constants (CONST):
*===========================================================================*/
/*==========================================================================*
* extern available constants (CONST):
*===========================================================================*/
/*==========================================================================*
* modul internal variables:
*===========================================================================*/
/*==========================================================================*
* globale external available variables (EXTERN):
*===========================================================================*/
/*==========================================================================*
* modul internal functions:
*===========================================================================*/
/*--------------------------------------------------------------------------*
* void set_leds(void)
*---------------------------------------------------------------------------*
* FT: show state of SWITCH with LEDs
* EP: -
* RV: -
* GP: -
*---------------------------------------------------------------------------*/
void set_leds(BYTE inputs)
{
led_idle(); /* make LED to be blinking (to be) */
/* called as often as possible) */
led_set(0, LED_BLINK); /* set LED 0 blinking */
if ((inputs & 0x01) != 0) /* SWITCH 1: turn LED 1 on/off */
led_set(1, LED_ON);
else
led_set(1, LED_OFF);
if ((inputs & 0x02) != 0) /* SWITCH 2: turn LED 2 on/off */
led_set(2, LED_ON);
else
led_set(2, LED_OFF);
if ((inputs & 0x04) != 0) /* SWITCH 3: turn LED 3 on/off */
led_set(3, LED_ON);
else
led_set(3, LED_OFF);
if ((inputs&0x08)!=0) /* SWITCH 4: turn LED 4 on/off */
led_set(4, LED_ON);
else
led_set(4, LED_OFF);
led_set(5, LED_BLINK); /* set LED 5 blinking*/
}
/*==========================================================================*
* extern available functions:
*===========================================================================*/
/*--------------------------------------------------------------------------*
* void main(void)
*---------------------------------------------------------------------------*
* FT: read analog input-channel and write value to LCD
* show value as LED-bar
* EP: -
* RV: -
* GP: -
*---------------------------------------------------------------------------*/
void main(void)
{ int i;
BYTE sw_inputs; /* state of DIP-SWITCH */
char txt[17]; /* textstring for output on LCD */
TIMER_COUNTER t_base;
BOOL blink_state;
timer_init(NULL); /* init timer modul */
lcd_init(); /* init LCD */
led_init(); /* init LED control */
lcd_center(0, "DIP-switch demo");
/* start-up LED demo: */
for (i=0; i<=LED_MAX; i++)
{
led_set(i, LED_ON);
timer_delay_10ms(5);
}
for (i=0; i<=LED_MAX; i++)
{
led_set(i, LED_OFF);
timer_delay_10ms(5);
}
dpswtch4_init(); /* init DIP-SWITCH input */
/* overwrites LED control lines of */
/* LED 12 .. LED 15 (=> now inputs) */
/* wait for switch to be changed: */
lcd_center(1, "Set the switch!");
sw_inputs = dpswtch4_read(); /* read DIP switch */
blink_state = TRUE;
t_base = timer_get_10ms(TIMER_MIN);
while(sw_inputs == dpswtch4_read())
{
if (timer_get_10ms(t_base) > 50)
{
t_base = timer_get_10ms(TIMER_MIN);
if (blink_state == TRUE)
{
lcd_center(1, "");
blink_state = FALSE;
}
else
{
lcd_center(1, "Set the switch!");
blink_state = TRUE;
}
}
}
lcd_clr();
lcd_center(0, "State of switch:");
/* endless main loop: */
while(1)
{
sw_inputs = dpswtch4_read(); /* read DIP switch */
sprintf(txt, "%d%d%d%d", /* generate tetxstring for output */
(sw_inputs&0x01)!=0, /* SWITCH 1 */
(sw_inputs&0x02)!=0, /* SWITCH 2 */
(sw_inputs&0x04)!=0, /* SWITCH 3 */
(sw_inputs&0x08)!=0); /* SWITCH 4 */
lcd_center(1, txt); /* show state of SWITCH on display */
set_leds(sw_inputs); /* show state of SWITCH with LEDs */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -