Initialer Commit
This commit is contained in:
commit
945d2870aa
17 changed files with 3370 additions and 0 deletions
115
relay-tuyafw/Delay.c
Normal file
115
relay-tuyafw/Delay.c
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*---------------------------------------------------------------------------------------------------------*/
|
||||
/* */
|
||||
/* Copyright(c) 2016 Nuvoton Technology Corp. All rights reserved. */
|
||||
/* */
|
||||
/*---------------------------------------------------------------------------------------------------------*/
|
||||
|
||||
//***********************************************************************************************************
|
||||
// Nuvoton Technoledge Corp.
|
||||
// Website: http://www.nuvoton.com
|
||||
// E-Mail : MicroC-8bit@nuvoton.com
|
||||
// Date : Apr/21/2016
|
||||
//***********************************************************************************************************
|
||||
|
||||
#include "nuvoton/N76E003.h"
|
||||
#include "nuvoton/Common.h"
|
||||
#include "nuvoton/Delay.h"
|
||||
#include "nuvoton/SFR_Macro.h"
|
||||
#include "nuvoton/functions.h"
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
void Timer0_Delay100us(UINT32 u32CNT)
|
||||
{
|
||||
clr_T0M; //T0M=0, Timer0 Clock = Fsys/12
|
||||
TMOD |= 0x01; //Timer0 is 16-bit mode
|
||||
set_TR0; //Start Timer0
|
||||
while (u32CNT != 0)
|
||||
{
|
||||
TL0 = LOBYTE(TIMER_DIV12_VALUE_100us); //Find define in "Function_define.h" "TIMER VALUE"
|
||||
TH0 = HIBYTE(TIMER_DIV12_VALUE_100us);
|
||||
while (TF0 != 1); //Check Timer0 Time-Out Flag
|
||||
clr_TF0;
|
||||
u32CNT --;
|
||||
}
|
||||
clr_TR0; //Stop Timer0
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void Timer0_Delay1ms(UINT32 u32CNT)
|
||||
{
|
||||
clr_T0M; //T0M=0, Timer0 Clock = Fsys/12
|
||||
TMOD |= 0x01; //Timer0 is 16-bit mode
|
||||
set_TR0; //Start Timer0
|
||||
while (u32CNT != 0)
|
||||
{
|
||||
TL0 = LOBYTE(TIMER_DIV12_VALUE_1ms); //Find define in "Function_define.h" "TIMER VALUE"
|
||||
TH0 = HIBYTE(TIMER_DIV12_VALUE_1ms);
|
||||
while (TF0 != 1); //Check Timer0 Time-Out Flag
|
||||
clr_TF0;
|
||||
u32CNT --;
|
||||
}
|
||||
clr_TR0; //Stop Timer0
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void Timer1_Delay10ms(UINT32 u32CNT)
|
||||
{
|
||||
clr_T1M; //T1M=0, Timer1 Clock = Fsys/12
|
||||
TMOD |= 0x10; //Timer1 is 16-bit mode
|
||||
set_TR1; //Start Timer1
|
||||
while (u32CNT != 0)
|
||||
{
|
||||
TL1 = LOBYTE(TIMER_DIV12_VALUE_10ms); //Find define in "Function_define.h" "TIMER VALUE"
|
||||
TH1 = HIBYTE(TIMER_DIV12_VALUE_10ms);
|
||||
while (TF1 != 1); //Check Timer1 Time-Out Flag
|
||||
clr_TF1;
|
||||
u32CNT --;
|
||||
}
|
||||
clr_TR1; //Stop Timer1
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void Timer2_Delay500us(UINT32 u32CNT)
|
||||
{
|
||||
clr_T2DIV2; //Timer2 Clock = Fsys/4
|
||||
clr_T2DIV1;
|
||||
set_T2DIV0;
|
||||
set_TR2; //Start Timer2
|
||||
while (u32CNT != 0)
|
||||
{
|
||||
TL2 = LOBYTE(TIMER_DIV4_VALUE_500us); //Find define in "Function_define.h" "TIMER VALUE"
|
||||
TH2 = HIBYTE(TIMER_DIV4_VALUE_500us);
|
||||
while (TF2 != 1); //Check Timer2 Time-Out Flag
|
||||
clr_TF2;
|
||||
u32CNT --;
|
||||
}
|
||||
clr_TR2; //Stop Timer2
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void Timer3_Delay100ms(UINT32 u32CNT)
|
||||
{
|
||||
T3CON = 0x07; //Timer3 Clock = Fsys/128
|
||||
set_TR3; //Trigger Timer3
|
||||
while (u32CNT != 0)
|
||||
{
|
||||
RL3 = LOBYTE(TIMER_DIV128_VALUE_100ms); //Find define in "Function_define.h" "TIMER VALUE"
|
||||
RH3 = HIBYTE(TIMER_DIV128_VALUE_100ms);
|
||||
while ((T3CON&SET_BIT4) != SET_BIT4); //Check Timer3 Time-Out Flag
|
||||
clr_TF3;
|
||||
u32CNT --;
|
||||
}
|
||||
clr_TR3; //Stop Timer3
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
void Timer3_Delay10us(UINT32 u32CNT)
|
||||
{
|
||||
T3CON = 0x07; //Timer3 Clock = Fsys/128
|
||||
set_TR3; //Trigger Timer3
|
||||
while (u32CNT != 0)
|
||||
{
|
||||
RL3 = LOBYTE(TIMER_DIV4_VALUE_10us); //Find define in "Function_define.h" "TIMER VALUE"
|
||||
RH3 = HIBYTE(TIMER_DIV4_VALUE_10us);
|
||||
while ((T3CON&SET_BIT4) != SET_BIT4); //Check Timer3 Time-Out Flag
|
||||
clr_TF3;
|
||||
u32CNT --;
|
||||
}
|
||||
clr_TR3; //Stop Timer3
|
||||
}
|
3
relay-tuyafw/build.sh
Executable file
3
relay-tuyafw/build.sh
Executable file
|
@ -0,0 +1,3 @@
|
|||
sdcc -mmcs51 -c Delay.c -D FOSC_160000 -I../include
|
||||
#sdcc -mmcs51 -c Common.c -D FOSC_160000 -I../include
|
||||
sdcc -mmcs51 -o 2relays.ihx main.c Delay.rel -D FOSC_160000 -I../include
|
13
relay-tuyafw/config.json
Normal file
13
relay-tuyafw/config.json
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"boot_select": "aprom",
|
||||
"pwm_enabled_during_ocd": false,
|
||||
"ocd_enabled": true,
|
||||
"reset_pin_disabled": false,
|
||||
"locked": false,
|
||||
"ldrom_size": "0kb",
|
||||
"bod_disabled": false,
|
||||
"bod_voltage": "2v2",
|
||||
"iap_enabled_in_brownout": false,
|
||||
"bod_reset_disabled": false,
|
||||
"wdt": "disabled"
|
||||
}
|
196
relay-tuyafw/main.c
Normal file
196
relay-tuyafw/main.c
Normal file
|
@ -0,0 +1,196 @@
|
|||
#include <nuvoton/functions.h>
|
||||
#include <nuvoton/N76E003.h>
|
||||
#include <nuvoton/Common.h>
|
||||
#include <nuvoton/SFR_Macro.h>
|
||||
#include <string.h>
|
||||
|
||||
#define RELAY2 P01
|
||||
#define RELAY1 P13
|
||||
|
||||
#define IN1 P05
|
||||
#define IN2 P15
|
||||
|
||||
#define MAXCMD 64
|
||||
|
||||
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] ? 1 : 0;
|
||||
break;
|
||||
case 2:
|
||||
RELAY2 = buffer[4] ? 1 : 0;
|
||||
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() {
|
||||
recvpkt();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
P01_PushPull_Mode;
|
||||
P13_PushPull_Mode;
|
||||
|
||||
/* Inputs */
|
||||
P05_Input_Mode;
|
||||
P15_Input_Mode;
|
||||
|
||||
while (1)
|
||||
{
|
||||
uart_loop();
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue