/*******************************************************************************
*******************************************************************************/
#include "main.h"
unsigned char i ;
unsigned char flag;
#define OSC_FREQ 20000000
#define BAUD_RATE 115200
void Clock_Init()
{
CLKSEL = 0x04;
OSCICN = 0x00; // enable Internal oscillator;running clock at programmed frequency;
RSTSRC= 0x04 ;//if clock miss ,rst MCU
}
/*****************************************************************************
*PROT initialize
*****************************************************************************/
void Port_Init()
{
P0MDOUT|=0X50;// 使能 UTX 为推挽输出
XBR0=0x01 ;// 使能 UART0 占用 P0.4(TXD) 和 P0.5(RXD)
XBR2=0x40 ; //使能交叉开关
P0SKIP = 0XCF;// skip port 0 ,every port bit as GPIO
// P0DRV = 0X50;
}
/*****************************************************************************
* Uart Hardware initial
*****************************************************************************/
void Uart_HwInit()
{
PCON |= 0x80;
TMOD &= 0x0f;
TMOD |= 0x20;
SCON0 = 0x10;
CKCON = 0x08;
TH1 = 0xAA;
TL1 = TH1;
//g_cSavedTL0 = ((0x10000L - (OSC_FREQ / BAUD_RATE * 2)) % 256);
//g_cSavedTH0 = ((0x10000L - (OSC_FREQ / BAUD_RATE * 2)) / 256);
//TH0 = g_cSavedTH0;
//TL0 = g_cSavedTL0;
TR1 = 1;
//TR0 = 1;
RI0 = 0;
TI0 = 0;
}
/*****************************************************************************
* interrupt initial
*****************************************************************************/
void interrupt_init(){
PCA0CPM0 |= 0x01; // Enable module 0 (receive) interrupts.
PCA0CPM1 |= 0x01; // Enable module 1 (transmit) interrupts.
CR = 1; // Start PCA counter.
//EIE1 |= 0x08; // Enable PCA interrupts
EA =1 ;
ES0 =1;
}
void SoftReset(){
RSTSRC |= 0x10; ///software reset
}
void Password_Init(){
info_pre6[0] = 0x82;
info_pre6[1] = 0x6B;
info_pre6[2] = 0x6D;
info_pre6[3] = 0x8D;
info_pre6[4] = 0xC1;
info_pre6[5] = 0x66;
info_pre6[6] = 0xBC;
info_pre6[7] = 0x63;
info_pre6[8] = 0xCA;
info_pre6[9] = 0x78;
info_pre6[10] =0x0A;
info_pre6[11] = 0x62;
info_pre6[12] = 0x36;
info_pre6[13] = 0x33;
info_pre6[14] = 0x50;
info_pre6[15] = 0x38;
info_pre6[16] = 0x34;
info_pre6[17] = 0x1E;
info_pre6[18] = 0xCE;
info_pre6[19] = 0xC2;
info_pre6[20] = 0xE8;
info_pre6[21] = 0x71;
info_pre6[22] = 0x67;
info_pre6[23] = 0xDF;
info_pre6[24] = 0x22;
info_pre6[25] = 0xD1;
info_pre6[26] = 0x33;
info_pre6[27] = 0x9B;
info_pre6[28] = 0xAD;
info_pre6[29] = 0xFC;
info_pre6[30] = 0x5C;
info_pre6[31] = 0x83;
info_pre6[32] = 0xC6;
info_pre6[33] = 0x3F;
info_pre6[34] = 0x55;
info_pre6[35] = 0xEC;
info_pre6[36] = 0xC7;
info_pre6[37] = 0x5B;
info_pre6[38] = 0x4C;
info_pre6[39] = 0xA2;
info_pre6[40] = 0x37;
info_pre6[41] = 0x1B;
info_pre6[42] = 0XCA;
info_pre6[43] = 0XAC;
info_pre6[44] = 0X9D;
info_pre6[45] = 0X1E;
info_pre6[46] = 0x37;
info_pre6[47] = 0xC2;
info_pre6[48] = 0xF8;
info_pre6[49] = 0x11;
info_pre6[50] = 0x1C;
info_pre6[51] = 0x61;
info_pre6[52] = 0x17;
info_pre6[53] = 0xBD;
info_pre6[54] = 0x6A;
info_pre6[55] = 0x9B;
info_pre6[56] = 0x35;
info_pre6[57] = 0xAF;
info_pre6[58] = 0xF8;
info_pre6[59] = 0xB7;
info_pre6[60] = 0xC9;
info_pre6[61] = 0xF9;
info_pre6[62] = 0x15;
info_pre6[63] = 0x4A;
info_pre6[64] = 0xCC;
info_pre6[65] = 0x9C;
info_pre6[66] = 0xEC;
info_pre6[67] = 0x28;
info_pre6[68] = 0x81;
info_pre6[69] = 0x34;
info_pre6[70] = 0x73;
info_pre6[71] = 0x0F;
info_pre6[72] = 0x03;
info_pre6[73] = 0xEF;
info_pre6[74] = 0x51;
info_pre6[75] = 0x1A;
info_pre6[76] = 0xD4;
info_pre6[77] = 0x4F;
info_pre6[78] = 0xF7;
info_pre6[79] = 0xB3;
info_pre6[80] = 0x6A;
info_pre6[81] = 0x3E;
info_pre6[82] = 0x09;
info_pre6[83] = 0x05;
info_pre6[84] = 0x87;
info_pre6[85] = 0xE1;
info_pre6[86] = 0x78;
info_pre6[87] = 0x47;
info_pre6[88] = 0xBE;
info_pre6[89] = 0x0B;
info_pre6[90] = 0x7B;
info_pre6[91] = 0x82;
info_pre6[92] = 0x92;
info_pre6[93] = 0x92;
info_pre6[94] = 0x3D;
info_pre6[95] = 0x2E;
info_pre6[96] = 0x66;
info_pre6[97] = 0x78;
info_pre6[98] = 0xA9;
info_pre6[99] = 0xE2;
info_pre6[100] =0xEB;
info_pre6[101] =0xD8;
info_pre6[102] = 0x5D;
info_pre6[103] = 0x76;
info_pre6[104] = 0x40;
info_pre6[105] = 0x34;
info_pre6[106] = 0x82;
info_pre6[107] = 0x0C;
info_pre6[108] = 0x4B;
info_pre6[109] = 0xD4;
info_pre6[110] = 0x32;
info_pre6[111] = 0xFB;
info_pre6[112] = 0xD7;
info_pre6[113] = 0x07;
info_pre6[114] = 0xB4;
info_pre6[115] = 0x7D;
info_pre6[116] = 0x8D;
info_pre6[117] = 0x8C;
info_pre6[118] = 0x11;
info_pre6[119] = 0xE5;
info_pre6[120] = 0x96;
info_pre6[121] = 0x2B;
info_pre6[122] = 0x12;
info_pre6[123] = 0xD3;
info_pre6[124] = 0x36;
info_pre6[125] = 0x6D;
info_pre6[126] = 0x66;
info_pre6[127] = 0x27;
info_pre6[128] = 0x7C;
info_pre6[129] = 0x47;
}
/*****************************************************************************
* UART test
*****************************************************************************/
/*void serial_test(void) interrupt 4 using 3
{
for (g_cReceiveCounter = 0;g_cReceiveCounter < sizeof(g_cReceiveBuffer);g_cReceiveCounter ++){
g_cReceiveBuffer[g_cReceiveCounter] = SBUF0;
}
RI0 = 0;
flag = 1 ;
} */
/*****************************************************************************
* main process
*****************************************************************************/
void main()
{
// unsigned int iCnt;
//unsigned char cStatus;
unsigned char j ;
PCA0MD&=~0x40 ;
Clock_Init();
Port_Init();
Uart_HwInit();
interrupt_init();
Password_Init();
// LEDRED = 0;
//Delay100uS(5000); //delay 0.5 second for wait RF module initialize, reset of RF module need less than 500ms normally
for(j = 0; j<sizeof(g_cReceiveBuffer);j++){
g_cReceiveBuffer[j] = 0;
}
g_cReceiveCounter = 0;
g_bReceiveOK = 0;
while (1)
{
if(g_bReceiveOK == 1){
if(g_cReceiveBuffer[1] == 0x10)
{
g_bReceiveOK = 0;
UartSendModBus(pro_info);
}
if(0x02 == g_cReceiveBuffer[1])
{
g_bReceiveOK = 0;
Delay100uS(1);
UartSendModBus(info_pre1);
}
if(0x11 == g_cReceiveBuffer[1]){
g_bReceiveOK = 0;
Delay100uS(1);
UartSendModBus(info_pre2);
}
if (0x63 == g_cReceiveBuffer[1]){
g_bReceiveOK = 0;
Delay100uS(25);
UartSendModBus(info_pre3);
}
if (0x65 == g_cReceiveBuffer[1]){
g_bReceiveOK = 0;
Delay100uS(25);
UartSendModBus(g_cReceiveBuffer);
}
if (0x6e == g_cReceiveBuffer[1]){
g_bReceiveOK = 0;
Delay100uS(30);
UartSendModBus(info_pre4);
}
if ((0x6b == g_cReceiveBuffer[1]) & (0x05 == g_cReceiveBuffer[2])){
g_bReceiveOK = 0;
Delay100uS(350);
UartSendModBus(info_pre5);
}
if ((0x6b == g_cReceiveBuffer[1]) & (0x60 == g_cReceiveBuffer[2])){
g_bReceiveOK = 0;
Delay100uS(800);
UartSendModBus(info_pre6);
}
if (0x67 == g_cReceiveBuffer[1]){
g_bRec