8051/boards/lc.c

302 lines
5.2 KiB
C
Raw Permalink Normal View History

2021-03-07 18:24:42 +01:00
#include <nuvoton/functions.h>
#include <nuvoton/N76E003.h>
#include <nuvoton/Common.h>
#include <nuvoton/SFR_Macro.h>
#include <string.h>
#define RELAY1 P12
#define RELAY2 P15
#define RED P01
#define GREEN P04
#define BLUE P03
#define PWM_GREEN PWM3L
#define PWM_BLUE PWM5L
#define PWM_RED PWM4L
#define BUTTON1 P00
#define BUTTON2 P10
#define MAXCMD 64
__sbit BIT_TMP;
__sbit S1;
__sbit S2;
unsigned char crc;
unsigned char getchar1(void)
{
UINT8 c;
while (!RI);
c = SBUF;
RI = 0;
return (c);
}
int putchar1 (unsigned char c)
{
crc += c;
TI = 0;
SBUF = c;
while(TI==0);
return 0;
}
void sendpkt(unsigned char cmd, unsigned int len, unsigned char *buffer) {
unsigned int i;
crc = 0;
putchar1(0x55);
putchar1(0xAA);
putchar1(0x03);
putchar1(cmd);
putchar1(len >> 8);
putchar1(len & 0xFF);
for (i=0; i<len; i++) {
putchar1(buffer[i]);
}
putchar1(crc);
}
void tuya_receive (unsigned int len, unsigned char *buffer) {
unsigned char dpid;
unsigned char dtype;
unsigned int dlen;
if (len < 4) return;
dpid = buffer[0];
dtype = buffer[1];
dlen = buffer[2] << 8 + buffer[3];
switch(dpid) {
case 1:
RELAY1 = buffer[4] ? 0 : 1;
break;
case 2:
RELAY2 = buffer[4] ? 0 : 1;
break;
case 3:
BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;
PWM_RED = buffer[4];
TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP;
LOAD = 1;
break;
case 4:
PWM_GREEN = buffer[4];
LOAD = 1;
break;
case 5:
BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;
PWM_BLUE = buffer[4];
TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP;
LOAD = 1;
break;
}
sendpkt(7, len, buffer);
}
void process(unsigned char cmd, unsigned int len, unsigned char *buffer) {
static unsigned char restart = 0;
switch (cmd) {
case 0: /* Heartbeat */
buffer[0] = restart;
restart = 1;
sendpkt(0, 1, buffer);
break;
case 1: /* Identify */
strcpy(buffer,"Nuovo");
len = strlen(buffer);
sendpkt(1, len, buffer);
break;
case 3: /* WIFI State */
sendpkt(3, 0, NULL);
break;
case 6: /* Set Command */
tuya_receive(len, buffer);
break;
case 8: /* Query Command */
break;
}
}
void recvpkt(void)
{
static unsigned char state = 0;
static unsigned char cmd;
static unsigned int len;
static unsigned char rcrc;
static unsigned char i;
static unsigned char command[MAXCMD];
if (RI) {
int inByte = getchar1();
switch (state) {
case 0:
if (inByte == 0x55) state++;
rcrc = 0;
break;
case 1:
if (inByte == 0xaa) state++; else state = 0;
break;
case 2:
if (inByte == 0) state++; else state = 0;
break;
case 3:
cmd = inByte;
state++;
break;
case 4:
len = inByte << 8;
state++;
break;
case 5:
len += inByte;
if (len < MAXCMD) state++; else state = 0;
if (len == 0) state++;
i = 0;
break;
case 6:
command[i] = inByte;
i++;
if (len == i) state++;
break;
case 7:
if (inByte == rcrc)
process(cmd, len, command);
state = 0;
break;
}
rcrc = rcrc + inByte;
}
}
void uart_loop() {
static unsigned int S1_debounce = 0;
static unsigned int S2_debounce = 0;
unsigned char buf[5];
recvpkt();
if (BUTTON1 != S1) {
if (S1_debounce == 4096) {
S1 = BUTTON1;
S1_debounce = 0;
if (!S1) {
RELAY1 = !RELAY1;
buf[0] = 1;
buf[1] = 1;
buf[2] = 0;
buf[3] = 1;
buf[4] = (1 - RELAY1);
sendpkt(7, 5, buf);
}
}
S1_debounce++;
} else {
S1_debounce = 0;
}
if (BUTTON2 != S2) {
if (S2_debounce == 4096) {
S2 = BUTTON2;
S2_debounce = 0;
if (!S2) {
RELAY2 = !RELAY2;
buf[0] = 2;
buf[1] = 1;
buf[2] = 0;
buf[3] = 1;
buf[4] = (1 - RELAY2);
sendpkt(7, 5, buf);
}
}
S2_debounce++;
} else {
S2_debounce = 0;
}
}
void uart_init(UINT32 u32Baudrate) //T1M = 1, SMOD = 1
{
P06_PushPull_Mode;
P07_Input_Mode;
SCON = 0x50; //UART0 Mode1,REN=1,TI=1
TMOD |= 0x20; //Timer1 Mode1
set_SMOD; //UART0 Double Rate Enable
set_T1M;
clr_BRCK; //Serial port 0 baud rate clock source = Timer1
#ifdef FOSC_160000
TH1 = 256 - (1000000 / u32Baudrate + 1); /*16 MHz */
#endif
#ifdef FOSC_166000
TH1 = 256 - (1037500 / u32Baudrate); /*16.6 MHz */
#endif
set_TR1;
set_TI; //For printf function must setting TI = 1
}
int main()
{
uart_init(9600);
/* Relays */
RELAY1 = 0;
RELAY2 = 0;
P12_PushPull_Mode;
P15_PushPull_Mode;
/* LEDS */
P01_PushPull_Mode;
P04_PushPull_Mode;
P03_PushPull_Mode;
/* Buttons */
P00_Quasi_Mode;
P10_Quasi_Mode;
/* buttonz */
PWMPH = 0;
PWMPL = 255;
PWM_CLOCK_FSYS;
PWM_CLOCK_DIV_128;
PWM_IMDEPENDENT_MODE;
PWM_EDGE_TYPE;
PWM5_P03_OUTPUT_ENABLE;
PWM3_P04_OUTPUT_ENABLE;
PWM4_P01_OUTPUT_ENABLE;
RED = 0;
GREEN = 0;
BLUE = 0;
BUTTON1 = 1;
BUTTON2 = 1;
S1 = 0;
S2 = 0;
BIT_TMP=EA;EA=0;TA=0xAA;TA=0x55;SFRS|=0x01;
PWM4H = 0;
PWM_RED = 0;
PWM5H = 0;
PWM_BLUE = 0;
TA=0xAA;TA=0x55;SFRS&=0xFE;EA=BIT_TMP;
PWM3H = 0;
PWM_GREEN = 0;
LOAD = 1;
PWMRUN = 1;
while (1)
{
uart_loop();
}
}