Sync work

This commit is contained in:
Jochen Friedrich 2025-11-08 19:26:58 +01:00
parent 43d1811d2e
commit d4e346c5a0
7 changed files with 241 additions and 28 deletions

155
boards/rs485relay4.c Normal file
View file

@ -0,0 +1,155 @@
#include <nuvoton/functions.h>
#include <nuvoton/N76E003.h>
#include <nuvoton/Common.h>
#include <nuvoton/SFR_Macro.h>
#include <string.h>
#include "MySensors.h"
#define RS485_DIR P15
#define IN1 P30
#define IN2 P17
#define IN3 P03
#define IN4 P04
#define RELAY1 P00
#define RELAY2 P10
#define RELAY3 P12
#define RELAY4 P11
#define MAXCMD 64
unsigned char relay;
unsigned char crc;
void rs485_in(void) {
RS485_DIR = 0;
}
void rs485_out(void) {
RS485_DIR = 1;
}
void send_state(void) {
MyMessage state;
state.data[0] = 0;
state.version_length = (1 << 3) + V2_MYS_HEADER_PROTOCOL_VERSION;
state.sensor = 0;
state.type = V_TRIPPED;
send(&state, P_BYTE);
state.sensor = 1;
send(&state, P_BYTE);
state.sensor = 2;
send(&state, P_BYTE);
state.sensor = 3;
send(&state, P_BYTE);
state.data[0] = RELAY1;
state.sensor = 4;
state.type = V_STATUS;
send(&state, P_BYTE);
state.data[0] = RELAY2;
state.sensor = 5;
send(&state, P_BYTE);
state.data[0] = RELAY3;
state.sensor = 6;
send(&state, P_BYTE);
state.data[0] = RELAY4;
state.sensor = 7;
send(&state, P_BYTE);
sendBattery(100);
}
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 == 4)
RELAY1 = (mymsg->data[0]=='1')?1:0;
if (sensor == 5)
RELAY2 = (mymsg->data[0]=='1')?1:0;
if (sensor == 6)
RELAY3 = (mymsg->data[0]=='1')?1:0;
if (sensor == 7)
RELAY4 = (mymsg->data[0]=='1')?1:0;
}
}
void present_node(void) {
present(NODE_SENSOR_ID, S_ARDUINO_NODE,"2.3.2");
sendSketchInfo("Relay_Nuvoton", "1.2");
present(0, S_DOOR, "IN1");
present(1, S_DOOR, "IN2");
present(2, S_DOOR, "IN3");
present(3, S_DOOR, "IN4");
present(4, S_BINARY, "Relay 1");
present(5, S_BINARY, "Relay 2");
present(6, S_BINARY, "Relay 3");
present(7, S_BINARY, "Relay 4");
registerNode();
delay(20);
send_state();
}
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;
P15_PushPull_Mode;
/* Relays */
RELAY1 = 0;
RELAY2 = 0;
RELAY3 = 0;
RELAY4 = 0;
P00_PushPull_Mode;
P12_PushPull_Mode;
P11_PushPull_Mode;
P10_PushPull_Mode;
/* Inputs */
P30_Input_Mode;
P17_Input_Mode;
P03_Input_Mode;
P04_Input_Mode;
transportInitialise();
while (1)
{
uart_loop();
}
}