-
Notifications
You must be signed in to change notification settings - Fork 10
/
relay.c
198 lines (162 loc) · 3.17 KB
/
relay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Code for STC15F104W MCU which was installed in cheap ESP-01 relay module marked "LCTECH".
I bought few such modules from ebay but they had broken MCU firmware and not works.
UART interaction code I got from STC15F100 docs
http://www.stcmicro.com/datasheet/STC15F100-en.pdf
To compile this code under linux run:
sdcc -mmcs51 --iram-size 128 --xram-size 0 --code-size 4096 --nooverlay --noinduction --verbose --debug -V --std-sdcc89 --model-small "relay.c"
To upload code into
stcgal -p /dev/ttyUSB1 -b 1200 -D -t 11059 relay.ihx
*/
#include <8051.h>
#define relay_pin P3_2
__sfr __at(0x8E) AUXR;
__sfr __at(0xB1) P3M1;
__sfr __at(0xB2) P3M0;
#define RXB P3_0
#define TXB P3_1
#define BAUD 0xFE80
typedef __bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF, RBUF;
BYTE TDAT, RDAT;
BYTE TCNT, RCNT;
BYTE TBIT, RBIT;
BOOL TING, RING;
BOOL TEND, REND;
BYTE t, r;
BYTE buf[16];
BYTE step;
BYTE chr;
BYTE mode;
BOOL OK;
BOOL ANSWER;
BYTE answer_data;
int i;
void uart_send(char chr);
void uart_init();
void main()
{
char a[] = { 'U','P' };
// set push-pull mode to P3.2
P3M1=0;
P3M0=4;
relay_pin = 0;
step = 0;
mode = 0;
OK = 0;
uart_init();
for (i = 0; a[i] != 0; i++)
uart_send(a[i]);
while (1) {
if (REND) {
REND = 0;
chr = RBUF;
switch (step) {
case 0:
if (chr == 0xA0)
step++;
break;
case 1:
if (chr == 0x01)
step++;
else
step = 0;
break;
case 2:
if (chr == 0x01 || chr == 0x00) {
step++;
mode = chr;
} else {
step = 0;
}
break;
case 3:
if (chr == 0xA2 && mode == 0x01) {
relay_pin = 1;
OK = 1;
} else {
if (chr == 0xA1 && mode == 0) {
relay_pin = 0;
OK = 1;
}
}
step = 0;
break;
}
}
if (OK) {
OK = 0;
uart_send(relay_pin);
}
}
}
void tm0(void) __interrupt 1 __using 1
{
if (RING) {
if (--RCNT == 0) {
RCNT = 3;
if (--RBIT == 0) {
RBUF = RDAT;
RING = 0;
REND = 1;
} else {
RDAT >>= 1;
if (RXB) RDAT |= 0x80;
}
}
} else if (!RXB) {
RING = 1;
RCNT = 4;
RBIT = 9;
}
if (--TCNT == 0) {
TCNT = 3;
if (TING) {
if (TBIT == 0) {
TXB = 0;
TDAT = TBUF;
TBIT = 9;
} else {
TDAT >>= 1;
if (--TBIT == 0) {
TXB = 1;
TING = 0;
TEND = 1;
} else {
TXB = CY;
}
}
}
}
}
void uart_send(char chr)
{
while (!TEND) ;
TEND = 0;
TBUF = chr;
TING = 1;
}
void uart_init()
{
SCON = 0x50; // Asynchronous mode, 8-bit data and 1-stop bit
EA = 0;
PT0 = 0;
ET0 = 0; //enable timer0 interrupt
TR0 = 0; //tiemr0 start running
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD >> 8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}