167 lines
3 KiB
C
167 lines
3 KiB
C
#include <nuvoton/functions.h>
|
|
#include <nuvoton/N76E003.h>
|
|
#include <nuvoton/Common.h>
|
|
#include <nuvoton/SFR_Macro.h>
|
|
#include <string.h>
|
|
#include "MySensors.h"
|
|
|
|
#define M0 P03
|
|
#define M1 P04
|
|
#define M2 P01
|
|
|
|
#define RS485_DIR P05
|
|
|
|
#define OE P13
|
|
#define DS P14
|
|
#define SHCP P11
|
|
#define STCP P12
|
|
|
|
#define MAXCMD 64
|
|
|
|
unsigned char relay;
|
|
|
|
unsigned char crc;
|
|
|
|
void rs485_in(void) {
|
|
RS485_DIR = 0;
|
|
}
|
|
|
|
void rs485_out(void) {
|
|
RS485_DIR = 1;
|
|
}
|
|
|
|
void load(void) {
|
|
OE = 0;
|
|
STCP = 0;
|
|
SHCP = 0;
|
|
|
|
DS = (relay & 0x01)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x02)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x04)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x08)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x10)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x20)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x40)? 1 : 0;
|
|
SHCP = 1;
|
|
SHCP = 0;
|
|
DS = (relay & 0x80)? 1 : 0;
|
|
|
|
SHCP = 1;
|
|
STCP = 1;
|
|
}
|
|
|
|
void receive(const MyMessage* mymsg)
|
|
{
|
|
uint8_t sensor;
|
|
|
|
// We only expect one type of message from controller. But we better check anyway.
|
|
if (mymsg->type == V_STATUS) {
|
|
sensor = mymsg->sensor;
|
|
if (sensor == 1)
|
|
relay = (relay & 0xFE) + ((mymsg->data[0]=='1')?1:0);
|
|
if (sensor == 2)
|
|
relay = (relay & 0xFD) + ((mymsg->data[0]=='1')?2:0);
|
|
if (sensor == 3)
|
|
relay = (relay & 0xFB) + ((mymsg->data[0]=='1')?4:0);
|
|
if (sensor == 4)
|
|
relay = (relay & 0xF7) + ((mymsg->data[0]=='1')?8:0);
|
|
if (sensor == 5)
|
|
relay = (relay & 0xEF) + ((mymsg->data[0]=='1')?0x10:0);
|
|
if (sensor == 6)
|
|
relay = (relay & 0xDF) + ((mymsg->data[0]=='1')?0x20:0);
|
|
if (sensor == 7)
|
|
relay = (relay & 0xBF) + ((mymsg->data[0]=='1')?0x40:0);
|
|
if (sensor == 8)
|
|
relay = (relay & 0x7F) + ((mymsg->data[0]=='1')?0x80:0);
|
|
load();
|
|
}
|
|
}
|
|
|
|
void present_node(void) {
|
|
present(NODE_SENSOR_ID, S_ARDUINO_NODE,"Nuvoton Relay Board");
|
|
sendSketchInfo("Relay_Nuvoton", "1.1");
|
|
present(1, S_BINARY, "Relay 1");
|
|
present(2, S_BINARY, "Relay 2");
|
|
present(3, S_BINARY, "Relay 3");
|
|
present(4, S_BINARY, "Relay 4");
|
|
present(5, S_BINARY, "Relay 5");
|
|
present(6, S_BINARY, "Relay 6");
|
|
present(7, S_BINARY, "Relay 7");
|
|
present(8, S_BINARY, "Relay 8");
|
|
registerNode();
|
|
delay(20);
|
|
}
|
|
|
|
void uart_loop() {
|
|
transportProcess();
|
|
}
|
|
|
|
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);
|
|
|
|
RS485_DIR = 0;
|
|
P05_PushPull_Mode;
|
|
|
|
/* Relays */
|
|
relay = 0;
|
|
|
|
M0 = 1;
|
|
M1 = 1;
|
|
M2 = 1;
|
|
|
|
P01_Quasi_Mode;
|
|
P03_Quasi_Mode;
|
|
P04_Quasi_Mode;
|
|
|
|
P11_PushPull_Mode;
|
|
P12_PushPull_Mode;
|
|
P13_PushPull_Mode;
|
|
P14_PushPull_Mode;
|
|
|
|
load();
|
|
|
|
OE = 0;
|
|
|
|
transportInitialise();
|
|
|
|
while (1)
|
|
{
|
|
uart_loop();
|
|
}
|
|
}
|
|
|