245 lines
5.5 KiB
C
245 lines
5.5 KiB
C
#include "STC15F2K60S2.H"
|
|
#include "adc.h"
|
|
#include "const.h"
|
|
#include "displayer.h"
|
|
#include "key.h"
|
|
#include "sys.h"
|
|
#include "uart1.h"
|
|
#include "uart2.h"
|
|
#include "universal_decode_table.h"
|
|
|
|
code unsigned long SysClock = 11059200; //必须。定义系统工作时钟频率(Hz),用户必须修改成与实际工作频率(下载时选择的)一致
|
|
|
|
char onlineStatus = 0x01;
|
|
|
|
char buf1[8], buf2[8], buf3[8] = {0}, match[1] = {PROTOCOL_HEADER}, status;
|
|
|
|
void uart1Process()
|
|
{
|
|
if (buf1[1] == DEVICE_SPECIAL_PROTOCOL)
|
|
{
|
|
switch (buf1[2])
|
|
{
|
|
case KEY_TEST_CONNECTION: // connection test
|
|
buf3[0] = PROTOCOL_HEADER;
|
|
buf3[1] = DEVICE_SPECIAL_PROTOCOL;
|
|
buf3[2] = KEY_TEST_CONNECTION;
|
|
buf3[3] = KEY_ACTION_CONNECTION_OK;
|
|
buf3[4] = onlineStatus;
|
|
buf3[5] = 0x00;
|
|
buf3[6] = 0x00;
|
|
buf3[7] = 0x00;
|
|
break;
|
|
}
|
|
Uart1Print(buf3, 8);
|
|
return;
|
|
}
|
|
return;
|
|
}
|
|
|
|
void uart2Process()
|
|
{
|
|
#ifdef DEBUG
|
|
Uart1Print(buf2, 8);
|
|
#endif
|
|
if (buf2[1] == DEVICE_SPECIAL_PROTOCOL)
|
|
{
|
|
switch (buf2[2])
|
|
{
|
|
case KEY_REGISTER_SLAVE: // register slave
|
|
if (buf2[3] != KEY_ACTION_SLAVE_REQUEST || buf2[4] > 0x08 || buf2[4] < 0x02)
|
|
{
|
|
return;
|
|
}
|
|
buf3[0] = PROTOCOL_HEADER;
|
|
buf3[1] = DEVICE_SPECIAL_PROTOCOL;
|
|
buf3[2] = KEY_REGISTER_SLAVE;
|
|
buf3[3] = KEY_ACTION_SLAVE_OK;
|
|
buf3[4] = buf2[4];
|
|
buf3[5] = 0x00;
|
|
buf3[6] = 0x00;
|
|
buf3[7] = 0x00;
|
|
// set the buf2[4]-th bit of onlineStatus to 1
|
|
onlineStatus |= (0x01 << (buf2[4] - 1));
|
|
Uart2Print(buf3, 8);
|
|
#ifdef DEBUG
|
|
Uart1Print(&onlineStatus, 1);
|
|
#endif
|
|
break;
|
|
}
|
|
}
|
|
else // slave device key action; pass it along to uart1
|
|
{
|
|
Uart1Print(buf3, 8);
|
|
}
|
|
return;
|
|
}
|
|
|
|
void navEvent()
|
|
{
|
|
buf3[0] = PROTOCOL_HEADER;
|
|
buf3[1] = DEVICE_NUM;
|
|
// up
|
|
status = GetAdcNavAct(enumAdcNavKeyUp);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY_UP;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto navSend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY_UP;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto navSend;
|
|
default:
|
|
break;
|
|
}
|
|
// down
|
|
status = GetAdcNavAct(enumAdcNavKeyDown);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY_DOWN;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto navSend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY_DOWN;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto navSend;
|
|
default:
|
|
break;
|
|
}
|
|
// left
|
|
status = GetAdcNavAct(enumAdcNavKeyLeft);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY_LEFT;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto navSend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY_LEFT;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto navSend;
|
|
default:
|
|
break;
|
|
}
|
|
// right
|
|
status = GetAdcNavAct(enumAdcNavKeyRight);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY_RIGHT;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto navSend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY_RIGHT;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto navSend;
|
|
default:
|
|
break;
|
|
}
|
|
// ok
|
|
status = GetAdcNavAct(enumAdcNavKeyCenter);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY_OK;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto navSend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY_OK;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto navSend;
|
|
default:
|
|
break;
|
|
}
|
|
// Key 3
|
|
status = GetAdcNavAct(enumAdcNavKey3);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY3;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto navSend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY3;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto navSend;
|
|
default:
|
|
break;
|
|
}
|
|
navSend:
|
|
Uart1Print(buf3, 8);
|
|
return;
|
|
}
|
|
|
|
void keyEvent()
|
|
{
|
|
buf3[0] = PROTOCOL_HEADER;
|
|
buf3[1] = DEVICE_NUM;
|
|
// key 1
|
|
status = GetKeyAct(enumKey1);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY1;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto keySend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY1;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto keySend;
|
|
default:
|
|
break;
|
|
}
|
|
// key 2
|
|
status = GetKeyAct(enumKey2);
|
|
switch (status)
|
|
{
|
|
case enumKeyPress:
|
|
buf3[2] = KEY2;
|
|
buf3[3] = KEY_ACTION_PRESS;
|
|
goto keySend;
|
|
case enumKeyRelease:
|
|
buf3[2] = KEY2;
|
|
buf3[3] = KEY_ACTION_RELEASE;
|
|
goto keySend;
|
|
default:
|
|
break;
|
|
}
|
|
keySend:
|
|
Uart1Print(buf3, 8);
|
|
return;
|
|
}
|
|
|
|
void onlineProcess()
|
|
{
|
|
LedPrint(onlineStatus & 0x0f);
|
|
return;
|
|
}
|
|
|
|
void main()
|
|
{
|
|
MySTC_Init();
|
|
Uart1Init(4800);
|
|
Uart2Init(4800, Uart2Usedfor485ModBus);
|
|
DisplayerInit();
|
|
AdcInit(ADCexpEXT);
|
|
LedPrint(0);
|
|
SetDisplayerArea(0, 7);
|
|
Seg7Print(0, 0, 0, 0, 0, 0, 0, 0);
|
|
KeyInit();
|
|
SetUart1Rxd(buf1, 8, match, 1);
|
|
SetUart2Rxd(buf2, 8, match, 1);
|
|
SetEventCallBack(enumEventUart1Rxd, uart1Process);
|
|
SetEventCallBack(enumEventUart2Rxd, uart2Process);
|
|
SetEventCallBack(enumEventKey, keyEvent);
|
|
SetEventCallBack(enumEventNav, navEvent);
|
|
SetEventCallBack(enumEventSys100mS, onlineProcess);
|
|
while (1)
|
|
{
|
|
MySTC_OS();
|
|
}
|
|
}
|