📄 switch1.c
字号:
/* File: Switch1.C */
/****************************************************************************
*
* STK16XSW.SWITCH4.MAIN
* =====================
*
* main function for STK16xU DIP switch demo,
* reads DIP switch and transfers the state of the switches to the Terminal;
* 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 10.12.01 K. Z鰌f taken over from STK16XU.200
*****************************************************************************/
/****************************************************************************
*
* availability summary
*
* available for Starterkit: STK16xU
* conformed for Starterkit: STK16xU
* available for Module : TQM161US
*
* conformed for Module : TQM161US
*
*****************************************************************************/
//#pragma MEDIUM
// Setting the Programm Area into the fast-RAM Area at 0x380000 - 0x3FFFFF
#pragma RENAMECLASS (FCODE=FCD_F)
/*==========================================================================*
* include files (#INCLUDE):
*===========================================================================*/
/*--------------------------------------------------------------------------*
* Standard-Include-Files:
*---------------------------------------------------------------------------*/
#include <stdio.h>
#include <ctype.h> /* Typecast functions */
#include <string.h> /* String functions */
#include <setjmp.h> /* Global jump functions */
/*--------------------------------------------------------------------------*
* Hardware- and Compiler-spezifische Include-Files:
*---------------------------------------------------------------------------*/
#include <absacc.h> /* absolute accesss */
#include <intrins.h> /* Intrinsic functions */
#include <reg167.h> /* Special function register */
/*--------------------------------------------------------------------------*
* project specific include files:
*---------------------------------------------------------------------------*/
#include "timer.h"
#include "led.h"
#include "dpswtch1.h" /* switch function */
/*==========================================================================*
* module internal definitions (#DEFINE):
*===========================================================================*/
/* control character: */
#define BK 0x03
#define BS 0x08
#define CR 0x0D
#define LF 0x0A
#define XON 0x11
#define XOFF 0x13
/* baudrate definition */
#define BR9600 0
#define BR19200 1
#define BR38400 2
#define BR57600 3
/* appropriated Baudrate */
#define USED_BAUDRATE BR57600
/* control-bits serial interface: */
sbit P3_8 = P3^8;
sbit P3_10 = P3^10;
sbit DP3_8 = DP3^8;
sbit DP3_9 = DP3^9;
sbit DP3_10 = DP3^10;
sbit DP3_11 = DP3^11;
/* to initialize Asynchrone serial Interface ASC0 : */
void initasc()
{
/* Reload-Values for 9600, 19200, 38400 und 57600 Baud: */
const int br[4] = { 0x74, 0x3A, 0x1C, 0x13 };
int n; /* Number of Baudrate */
n = USED_BAUDRATE;
/* to initialize data reception over interface: */
P3_10 =1; /* P3.10-Output-Latch (TxD0) enable */
DP3_10 =1; /* P3.10 (TxD0) switch to output */
DP3_11 =0; /* switch RxD0 to input */
S0TIC =0x80; /* Transmit-Interrupt disable */
S0RIC =0x00; /* Receive-Interrupt disable */
if (!S0CON) /* init Bautrate in case the */
/* Baudrate is not initialized */
{ /* Wegen Unterschiedlichen Baudraten */
S0BG =br[n]; /* initialize Baudrate */
S0CON =0x8011; /* start up Baudrate-Generator , */
} /* activate receiving Data , */
/* 8-Datenbits, no Parit鋞sbit */
IEN = 1; /* to enable global Interrupt */
}
/*==========================================================================*
* 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) */
if ((inputs & 0x01) != 0) /* SWITCH 1: turn LED 2 on/off */
led_set(1, LED_BLINK);
else
led_set(1, LED_OFF);
}
/*==========================================================================*
* 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, sw_inputs_old;/* state of DIP-SWITCH */
TIMER_COUNTER t_base;
BOOL blink_state;
timer_init(NULL); /* init timer modul */
led_init(); /* init LED control */
initasc();
printf("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: */
printf("\n\nSet the Dip-switch 1!");
sw_inputs = dpswtch4_read(); /* read DIP switch */
blink_state = TRUE;
t_base = timer_get_10ms(TIMER_MIN);
while(sw_inputs == dpswtch4_read())
sw_inputs_old = 0xFF; /* init variable */
/* endless main loop: */
while(1)
{
sw_inputs = dpswtch4_read(); /* read DIP switch */
if (sw_inputs_old != sw_inputs) /* changes from Dip switch */
{
printf("\nState of switch: %d", (sw_inputs & 0x01)); /* SWITCH 1 */
}
sw_inputs_old = sw_inputs;
set_leds(sw_inputs); /* show state of SWITCH with LEDs */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -