"); //-->
/*This is a programme for the demo of RTX51*/
#include <at89x51.h> /* special function registers 8052 */
#include <intrins.h>
#include <rtx51tny.h> /* RTX-51 tiny functions & defines */
/*Constants*/
#define INIT 0 /* task number of task: init */
#define SCAN_KEY 1 /* task number of task: scanKey */
#define DEBOUNCE 2 /* task number of task: debounce */
#define KEY_PROCESS 3 /* task number of task: keyProcess */
#define DISPLAY 4 /* task number of task: display */
/* Variables of Bytes */
unsigned char keyNumber;
unsigned char keyValue;
unsigned char keyTemp[5];
unsigned char keyInput[5];
unsigned char keyStore[5];
unsigned char keyReady[5];
unsigned char keyOutput[5];
/*Constant Data*/
code unsigned char keyCode[]=
{0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,
0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,
0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,
0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8};
/*Bit Addressable of Ports and Register*/
unsigned char bdata led_buffer;
sbit led1 = led_buffer^0;
sbit led2 = led_buffer^1;
sbit led3 = led_buffer^2;
sbit led4 = led_buffer^3;
sbit acc_7 = ACC^7;
/*Function Prototypes*/
void serialInit (void);
unsigned char *getKeyValue (void);
/*Task 0 'init': Initialize*/
void init (void) _task_ INIT /* program execution starts here */
{
serialInit (); /* initialize the serial interface */
os_create_task (SCAN_KEY); /* start scanKey task */
os_create_task (DISPLAY); /* start display task */
os_delete_task (INIT); /* stop init task (no longer needed)*/
led_buffer = 0xff; /* turn off all leds at the start */
}
/*Task 1 'scanKey'*/
void scanKey (void) _task_ SCAN_KEY
{
unsigned char i;
unsigned char temp;
while (1) /* scanKey is an endless loop */
{
P2 = temp = 0xfe; /* set the first scan bit */
for (i = 0; i < 4; i++)
{
keyInput[i] = ~P0; /* have reversed data from P1 */
P2 = P2 | ~temp; /* reset the scan bit */
if (i == 3) break; /* end last loop early */
temp = (temp << 1) + 1; /* scan bit shift left */
P2 = P2 & temp; /* set new scan bit */
}
os_create_task (DEBOUNCE); /* start debounce task */
os_wait (K_IVL, 2, 0); /* wait interval: 20mS */
}
}
/*Task 2 'debounce'*/
void debounce (void) _task_ DEBOUNCE
{
unsigned char i;
for (i = 0; i < 4; i++)
{
keyReady[i] = keyTemp[i] & keyInput[i] | keyReady[i]
& (keyTemp[i] ^ keyInput[i]);
keyTemp[i] = keyInput[i];
keyOutput[i] = keyReady[i] & ~keyStore[i];
keyStore[i] = keyReady[i];
}
if (keyOutput[0] | keyOutput[1] | keyOutput[2]
| keyOutput[3] | keyOutput[4])
os_create_task (KEY_PROCESS); /* start keyProcess task */
os_delete_task (DEBOUNCE); /* stop debounce task */
}
/*Task 3 'keyProcess'*/
void keyProcess (void) _task_ KEY_PROCESS
{
unsigned char *pointer;
pointer = getKeyValue();
keyValue = *pointer;
SBUF = keyValue; /* send the key Value to host */
os_delete_task (KEY_PROCESS); /* stop debounce task */
}
/*Task 4 'display'*/
void display (void) _task_ DISPLAY
{
while (1) /* dispaly is an endless loop */
{
os_wait (K_SIG, 0, 0); /* wait a signal */
if (led1) P1_0 = 0;else P1_0 = 1;/*turn on/off led 1 */
if (led2) P1_1 = 0;else P1_1 = 1;/*turn on/off led 2 */
if (led3) P1_2 = 0;else P1_2 = 1;/*turn on/off led 3 */
if (led4) P1_3 = 0;else P1_3 = 1;/*turn on/off led 4 */
}
}
/*Function 'getKeyValue'*/
unsigned char *getKeyValue (void)
{
unsigned char temp;
unsigned char i;
unsigned char j;
unsigned char *pointer = keyCode;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 8; j++)
{
temp = _cror_(keyOutput[i], 1);/* rotate right for pressed key */
if (_testbit_(acc_7)) /* find pressed key if acc_7=1 */
{
keyNumber = 4 * j + i; /* calculate the key order number */
return(pointer + keyNumber);
}
else keyOutput[i] = temp; /* prepare for rotate next */
}
}
}
/*serialInit: Initialize Ierial Interface*/
void serialInit (void)
{
SCON = 0x50; /* mode 1:8bit UART,};enable receiver*/
TMOD |= 0x20; /* timer 1 mode 2: 8-Bit reload */
TH1 = 0xe8; /* reload value 1200 baud */
TR1 = 1; /* timer 1 run */
ES = 1; /* enable serial port interrupt */
}
/*Serial Port Interrupt Handler*/
void serial_i_o_interrupt_handler(void) interrupt 4 using 2
{
if (RI)
{
led_buffer = SBUF; /* keep received led on/off data */
RI = 0; /* clear receive interrupt flag */
isr_send_signal (DISPLAY); /* signal task display */
}
TI = 0; /* clear transmit interrupt flag */
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。