HardwareDriver/c51/relay_controller/main.c

201 lines
4.2 KiB
C

/*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC10/11xx Series MCU UART (8-bit/9-bit)Demo ----------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "STC11.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT NONE_PARITY //Testing even parity
#define UART_P1 0x80 //(AUXR1.7) switch RXD/TXD from P3.0/P3.1 to P1.6/P1.7
sbit bit9 = P2^2; //P2.2 show UART data bit9
bit busy;
bit recv_flag = 0;
bit overflow_flag = 0;
char recv_buf[30] = {0};
char index = 0;
void SendData(BYTE dat);
void SendString(char *s);
code char SetUp[30] = "ATE0\r\n";
code char Connect[] = "AT+CIPSTART=\"TCP\",\"192.168.2.108\",8080\r\n";
void P1_Uart(){
AUXR1 &= 0x7f;
AUXR1 |= UART_P1;
}
void P3_Uart(){
AUXR1 &= 0x7f;
}
void Delay3020ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 127;
j = 233;
k = 148;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay1020us() //@11.0592MHz
{
unsigned char i, j;
i = 11;
j = 246;
do
{
while (--j);
} while (--i);
}
void Uart_Init(){
PCON &= 0x7F; //??????
SCON = 0x50; //8???,?????
AUXR |= 0x40; //???1???Fosc,?1T
AUXR &= 0xFE; //??1?????1???????
TMOD &= 0x0F; //?????1???
TMOD |= 0x20; //?????1?8???????
TL1 = 0xFD; //??????
TH1 = 0xFD; //????????
ET1 = 0; //?????1??
TR1 = 1; //?????1
}
void callbackUart(){
if (recv_flag == 1 || overflow_flag == 1){
P3_Uart();
SendString(recv_buf);
recv_flag = 0;
overflow_flag = 0;
P1_Uart();
}
}
void main()
{
bit flag1 = 0;
bit flag2 = 0;
Delay3020ms();
Delay3020ms();
Delay3020ms();
Uart_Init();
P1M1 = 0x00;
P1M0 = 0x3f; // realy gpio init
// open uart interrupt
ES = 1;
EA = 1;
REN = 1;
P1_Uart();
Delay1020us();
// close P1 input
//P1 = 0X00;
Delay1020us();
while(1){
if (flag1 == 0){
flag1 = 1;
SendString(SetUp);
Delay3020ms();
}
callbackUart();
if ((flag1 == 1) && (flag2 == 0)) {
flag2 = 1;
SendString(Connect);
Delay3020ms();
}
callbackUart();
};
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4
{
static char last_byte;
if (RI)
{
RI = 0; //Clear receive interrupt flag
if((last_byte == '\r') &&(SBUF == '\n' )&& (index != 1)) {
recv_flag = 1;
index = 0;
}
RI = 0;
recv_buf[index] = SBUF;
last_byte = SBUF;
index ++;
if(index > 29){
index = 0;
overflow_flag = 1;
}
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while(busy == 1) return;
ACC = dat; //Calculate the even parity bit P (PSW.0)
busy = 1;
SBUF = ACC; //Send data to UART buffer
while(busy == 1) ;
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s != '\0') //Check the end of the string
{
Delay1020us();
SendData(*s++); //Send current char and increment string ptr
}
}