提交 | 用户 | age
|
5dd1b7
|
1 |
#include "STC15F2K60S2.H" |
Q |
2 |
#include "intrins.h" |
|
3 |
#include "SLP.h" |
|
4 |
#include "modbus.h" |
|
5 |
|
|
6 |
//#include "main.h" |
|
7 |
//#include "STC15Wxx.h" |
|
8 |
//#include "delay.h" |
|
9 |
//#include "uart.h" |
|
10 |
//#include "tim.h" |
|
11 |
//#include "gpio.h" |
|
12 |
//#include "pwm.h" |
|
13 |
//#include "modbus.h" |
|
14 |
//#include "unibus.h" |
|
15 |
|
|
16 |
|
|
17 |
#define S3RI 0x01 //S3CON.0 |
|
18 |
#define S3TI 0x02 //S3CON.1 |
|
19 |
#define S3RB8 0x04 //S3CON.2 |
|
20 |
#define S3TB8 0x08 //S3CON.3 |
|
21 |
|
|
22 |
|
|
23 |
//#define uchar unsigned char |
|
24 |
//#define uint unsigned int |
|
25 |
|
|
26 |
|
|
27 |
//typedef unsigned char uchar; |
|
28 |
typedef unsigned int uint; |
|
29 |
|
|
30 |
sbit IN0=P0^0; |
|
31 |
sbit IN1=P0^1; |
|
32 |
sbit IN2=P0^2; |
|
33 |
sbit IN3=P0^3; |
|
34 |
sbit IN4=P0^4; |
|
35 |
sbit IN5=P0^5; |
|
36 |
sbit IN6=P0^6; |
|
37 |
sbit IN7=P0^7; |
|
38 |
|
|
39 |
sbit TXD3_2=P5^1; |
|
40 |
sbit RXD3_2=P5^0; |
|
41 |
|
|
42 |
sbit OUT0=P2^0; |
|
43 |
sbit OUT1=P2^1; |
|
44 |
sbit OUT2=P2^2; |
|
45 |
sbit OUT3=P2^3; |
|
46 |
sbit OUT4=P2^4; |
|
47 |
sbit OUT5=P2^5; |
|
48 |
sbit OUT6=P2^6; |
|
49 |
sbit OUT7=P2^7; |
|
50 |
|
|
51 |
sbit ERR=P4^5; |
|
52 |
sbit RUN=P4^6; |
|
53 |
|
|
54 |
sbit KEY1=P3^6; |
|
55 |
sbit KEY2=P3^7; |
|
56 |
sbit KEY3=P4^1; |
|
57 |
sbit KEY4=P4^2; |
|
58 |
sbit KEY5=P4^3; |
|
59 |
sbit KEY6=P4^4; |
|
60 |
|
|
61 |
bit uart1busy; |
|
62 |
bit uart3busy; |
|
63 |
|
|
64 |
|
|
65 |
//#pragma anon_unions |
|
66 |
char uart1recvbuf[8]; |
|
67 |
uchar uart1recvlen=0; |
|
68 |
uchar uart1recvtimeout =0; |
|
69 |
|
|
70 |
|
|
71 |
char uart3recvbuf[8]; |
|
72 |
uchar uart3recvlen=0; |
|
73 |
uchar uart3recvtimeout =0; |
|
74 |
|
|
75 |
int nCount=0; |
|
76 |
|
842bb6
|
77 |
stSLPDef SLP1; |
Q |
78 |
|
|
79 |
unsigned char bSLPMaster; |
|
80 |
unsigned char nSLPStation; |
5dd1b7
|
81 |
|
Q |
82 |
|
|
83 |
void PutOutput(uchar a); |
|
84 |
|
|
85 |
typedef union tagcoils |
|
86 |
{ |
|
87 |
uchar Byte; |
|
88 |
struct stByte{ |
|
89 |
uchar b0:1; |
|
90 |
uchar b1:1; |
|
91 |
uchar b2:1; |
|
92 |
uchar b3:1; |
|
93 |
uchar b4:1; |
|
94 |
uchar b5:1; |
|
95 |
uchar b6:1; |
|
96 |
uchar b7:1; |
|
97 |
}bits; |
|
98 |
}Coils; |
|
99 |
|
|
100 |
Coils inputdata; |
842bb6
|
101 |
|
Q |
102 |
/* |
5dd1b7
|
103 |
void Delay1ms() //@22.1184MHz |
Q |
104 |
{ |
|
105 |
uchar data i, j; |
|
106 |
|
|
107 |
_nop_(); |
|
108 |
_nop_(); |
|
109 |
i = 22; |
|
110 |
j = 128; |
|
111 |
do |
|
112 |
{ |
|
113 |
while (--j); |
|
114 |
} while (--i); |
|
115 |
} |
|
116 |
|
|
117 |
void Delay_ms(uint n) |
|
118 |
{ |
|
119 |
while(n--) |
|
120 |
{ |
|
121 |
Delay1ms(); |
|
122 |
} |
|
123 |
|
|
124 |
} |
842bb6
|
125 |
*/ |
5dd1b7
|
126 |
void Delay_us(uint n) |
Q |
127 |
{ |
|
128 |
uchar data j; |
|
129 |
while(n--){ |
|
130 |
j=5; |
|
131 |
while (--j); |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
void Timer0_Isr(void) interrupt 1 |
|
136 |
{} |
|
137 |
/* |
|
138 |
void Timer0_Init(void) //125微秒@22.1184MHz |
|
139 |
{ |
|
140 |
AUXR |= 0x80; //定时器时钟1T模式 |
|
141 |
TMOD &= 0xF0; //设置定时器模式 |
|
142 |
TL0 = 0x33; //设置定时初始值 |
|
143 |
TH0 = 0xF5; //设置定时初始值 |
|
144 |
TF0 = 0; //清除TF0标志 |
|
145 |
TR0 = 1; //定时器0开始计时 |
|
146 |
ET0 = 1; //使能定时器0中断 |
|
147 |
} |
|
148 |
|
|
149 |
*/ |
|
150 |
void Timer1_Isr(void) interrupt 3 |
|
151 |
{} |
|
152 |
/* |
|
153 |
void Timer1_Init(void) //1毫秒@22.1184MHz |
|
154 |
{ |
|
155 |
AUXR |= 0x40; //定时器时钟1T模式 |
|
156 |
TMOD &= 0x0F; //设置定时器模式 |
|
157 |
TL1 = 0x9A; //设置定时初始值 |
|
158 |
TH1 = 0xA9; //设置定时初始值 |
|
159 |
TF1 = 0; //清除TF1标志 |
|
160 |
TR1 = 1; //定时器1开始计时 |
|
161 |
ET1 = 1; //使能定时器1中断 |
|
162 |
} |
|
163 |
*/ |
|
164 |
//void UartInit(void) //115200bps@22.1184MHz |
|
165 |
//{ |
|
166 |
// SCON = 0x50; //8位数据,可变波特率 |
|
167 |
// AUXR |= 0x40; //定时器时钟1T模式 |
|
168 |
// AUXR &= 0xFE; //串口1选择定时器1为波特率发生器 |
|
169 |
// TMOD &= 0x0F; //设置定时器模式 |
|
170 |
// TL1 = 0xD0; //设置定时初始值 |
|
171 |
// TH1 = 0xFF; //设置定时初始值 |
|
172 |
// ET1 = 0; //禁止定时器中断 |
|
173 |
// TR1 = 1; //定时器1开始计时 |
|
174 |
//} |
|
175 |
|
|
176 |
|
|
177 |
//void UART1_Isr() interrupt 4 |
|
178 |
//{} |
|
179 |
|
|
180 |
|
|
181 |
//void Uart3Init(void) //115200bps@22.1184MHz |
|
182 |
//{ |
|
183 |
// S3CON = 0x10; //8位数据,可变波特率 |
|
184 |
// S3CON |= 0x40; //串口3选择定时器3为波特率发生器 |
|
185 |
// T4T3M |= 0x02; //定时器时钟1T模式 |
|
186 |
// T3L = 0xD0; //设置定时初始值 |
|
187 |
// T3H = 0xFF; //设置定时初始值 |
|
188 |
// T4T3M |= 0x08; //定时器3开始计时 |
|
189 |
//} |
|
190 |
|
|
191 |
|
|
192 |
void Uart3Init(void) //57600bps@22.1184MHz |
|
193 |
{ |
|
194 |
S3CON = 0x10; //8位数据,可变波特率 |
|
195 |
S3CON |= 0x40; //串口3选择定时器3为波特率发生器 |
|
196 |
T4T3M |= 0x02; //定时器时钟1T模式 |
|
197 |
T3L = 0xA0; //设置定时初始值 |
|
198 |
T3H = 0xFF; //设置定时初始值 |
|
199 |
T4T3M |= 0x08; //定时器3开始计时 |
|
200 |
P_SW2 |= 0x02; |
|
201 |
IE2 |= 0x08; |
|
202 |
|
|
203 |
} |
|
204 |
|
|
205 |
|
|
206 |
void UART3_Isr() interrupt 17 |
|
207 |
{ |
|
208 |
if (S3CON & S3RI) |
|
209 |
{ |
|
210 |
S3CON &= ~S3RI; //清除S3RI位 |
|
211 |
|
|
212 |
uart3recvbuf[uart3recvlen] = S3BUF; |
|
213 |
if (uart3recvlen < sizeof(uart3recvbuf)-1 ) {uart3recvlen++;} |
|
214 |
uart3recvtimeout=0; |
|
215 |
// PutOutput(S3BUF); |
|
216 |
// P0 = S3BUF; //P0显示串口数据 |
|
217 |
// P2 = (S3CON & S3RB8); //P2.2显示校验位 |
|
218 |
} |
|
219 |
if (S3CON & S3TI) |
|
220 |
{ |
|
221 |
S3CON &= ~S3TI; //清除S3TI位 |
|
222 |
uart3busy = 0; //清忙标志 |
|
223 |
} |
|
224 |
} |
|
225 |
|
|
226 |
|
|
227 |
void IO_Init(void) |
|
228 |
{ |
|
229 |
P0M0 = 0x00; P0M1 = 0x00; |
|
230 |
P1M0 = 0x00; P1M1 = 0x00; |
|
231 |
P2M0 = 0xff; P2M1 = 0x00; |
|
232 |
P3M0 = 0x00; P3M1 = 0x00; |
|
233 |
P4M0 = 0x00; P4M1 = 0x00; |
|
234 |
P5M0 = 0x00; P5M1 = 0x00; |
|
235 |
P6M0 = 0x00; P6M1 = 0x00; |
|
236 |
P7M0 = 0x00; P7M1 = 0x00; |
|
237 |
|
|
238 |
|
|
239 |
} |
|
240 |
|
|
241 |
//void senddata(uint n) |
|
242 |
//{ |
|
243 |
// uchar i ,j; |
|
244 |
// if(i=0,i<n;i++) |
|
245 |
// { |
|
246 |
// if(j=0;j<i;j++) |
|
247 |
// { |
|
248 |
// S3BUF = rece[0]; |
|
249 |
// |
|
250 |
// } |
|
251 |
// } |
|
252 |
//} |
|
253 |
|
|
254 |
uchar GetInput() |
|
255 |
{ |
|
256 |
|
|
257 |
return ~P0; |
|
258 |
/* |
|
259 |
Coils input; |
|
260 |
Coils input1; |
|
261 |
input.bits.b0=~IN0; |
|
262 |
input.bits.b1=~IN1; |
|
263 |
input.bits.b2=~IN2; |
|
264 |
input.bits.b3=~IN3; |
|
265 |
input.bits.b4=~IN4; |
|
266 |
input.bits.b5=~IN5; |
|
267 |
input.bits.b6=~IN6; |
|
268 |
input.bits.b7=~IN7; |
|
269 |
|
|
270 |
intput.Byte = P0; |
|
271 |
|
|
272 |
return input.Byte; |
|
273 |
*/ |
|
274 |
} |
|
275 |
|
|
276 |
void PutOutput(uchar out) |
|
277 |
{ |
|
278 |
P2 = out; |
|
279 |
return ; |
|
280 |
/* |
|
281 |
Coils output; |
|
282 |
output.Byte=out; |
|
283 |
|
|
284 |
|
|
285 |
OUT0 = output.bits.b0; |
|
286 |
OUT1 = output.bits.b1; |
|
287 |
OUT2 = output.bits.b2; |
|
288 |
OUT3 = output.bits.b3; |
|
289 |
OUT4 = output.bits.b4; |
|
290 |
OUT5 = output.bits.b5; |
|
291 |
OUT6 = output.bits.b6; |
|
292 |
OUT7 = output.bits.b7; |
|
293 |
*/ |
|
294 |
} |
|
295 |
|
|
296 |
void Uart3SendPacket(char * str, uchar len) |
|
297 |
{ |
|
298 |
uchar i; |
|
299 |
for (i=0;i<len;i++) |
|
300 |
{ |
|
301 |
while (uart3busy); |
|
302 |
S3BUF = str[i]; |
|
303 |
uart3busy=1; |
|
304 |
} |
|
305 |
} |
842bb6
|
306 |
/* |
5dd1b7
|
307 |
void SLPSendPacket(char * str, uchar len) |
Q |
308 |
{ |
|
309 |
Uart3SendPacket(str,len); |
|
310 |
} |
842bb6
|
311 |
*/ |
5dd1b7
|
312 |
void Uart3RecvPacket() |
Q |
313 |
{ |
|
314 |
if (uart3recvtimeout <2){ |
|
315 |
uart3recvtimeout++; |
|
316 |
}else |
|
317 |
{ /// recieved packet; |
|
318 |
RUN=~RUN; //闪灯 |
842bb6
|
319 |
SLPparsePacket(&SLP1,uart3recvbuf,uart3recvlen); |
5dd1b7
|
320 |
uart3recvlen =0; |
Q |
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
void Uart1RecvPacket() |
|
325 |
{ |
|
326 |
if (uart1recvtimeout <2){ |
|
327 |
uart1recvtimeout++; |
|
328 |
}else |
|
329 |
{ /// recieved packet; |
|
330 |
RUN=~RUN; //闪灯 |
|
331 |
ModbusparsePacket(uart1recvbuf,uart1recvlen); |
|
332 |
uart1recvlen =0; |
|
333 |
} |
|
334 |
} |
|
335 |
|
|
336 |
void OtherProcess() |
|
337 |
{ |
|
338 |
/* |
|
339 |
if(inputdata.bits.b4==1) |
|
340 |
{ |
|
341 |
OUT0 = ~ OUT0; |
|
342 |
OUT1 = ~ OUT1; |
|
343 |
OUT2 = ~ OUT2; |
|
344 |
OUT3 = ~ OUT3; |
|
345 |
OUT4 = ~ OUT4; |
|
346 |
OUT5 = ~ OUT5; |
|
347 |
OUT6 = ~ OUT6; |
|
348 |
OUT7 = ~ OUT7; |
|
349 |
//Delay_ms(500); |
|
350 |
} |
|
351 |
*/ |
|
352 |
} |
|
353 |
|
|
354 |
uchar GetJumper() |
|
355 |
{ |
|
356 |
Coils keys; |
|
357 |
keys.bits.b0=~KEY1; |
|
358 |
keys.bits.b1=~KEY2; |
|
359 |
keys.bits.b2=~KEY3; |
|
360 |
keys.bits.b3=~KEY4; |
|
361 |
keys.bits.b4=~KEY5; |
|
362 |
keys.bits.b5=~KEY6; |
|
363 |
keys.bits.b6=0; |
|
364 |
keys.bits.b7=0; |
|
365 |
|
|
366 |
return keys.Byte; |
|
367 |
} |
|
368 |
int main(void) |
|
369 |
{ |
|
370 |
unsigned char daa; |
|
371 |
daa = DPH; |
|
372 |
|
|
373 |
// Init |
|
374 |
IO_Init(); |
|
375 |
// Timer0_Isr(); |
|
376 |
Uart3Init(); |
|
377 |
// Timer0_Init(); |
|
378 |
// Timer1_Init(); |
|
379 |
|
|
380 |
ES = 1; |
|
381 |
EA = 1; |
|
382 |
|
|
383 |
// Get config jumpper |
|
384 |
|
|
385 |
inputdata.Byte = GetJumper(); |
|
386 |
bSLPMaster = inputdata.bits.b5 ; //master? |
842bb6
|
387 |
nSLPStation = inputdata.Byte & 0x0f; |
Q |
388 |
SLPInit(&SLP1,Uart3SendPacket); |
|
389 |
SLP1.bSLPMaster = bSLPMaster; |
|
390 |
SLP1.nStation = nSLPStation; |
|
391 |
|
5dd1b7
|
392 |
// bSLPMaster=1; |
Q |
393 |
|
|
394 |
while(1) |
|
395 |
{ |
|
396 |
// toggle LED |
|
397 |
// RUN =~ RUN; |
|
398 |
// Delay_ms(1000); |
|
399 |
|
|
400 |
// Get Input Port; |
|
401 |
inputdata.Byte = GetInput(); |
842bb6
|
402 |
SLP1.SLPinputB = inputdata.Byte; |
5dd1b7
|
403 |
RUN =~ RUN; |
Q |
404 |
// Delay_ms(1); |
|
405 |
// receive packet |
|
406 |
Uart3RecvPacket(); |
|
407 |
// Process Packet |
842bb6
|
408 |
SLPProcess(&SLP1); |
5dd1b7
|
409 |
|
842bb6
|
410 |
if (SLP1.SLPErrSign) { |
5dd1b7
|
411 |
ERR = 0; |
Q |
412 |
}else { |
|
413 |
ERR =1; |
|
414 |
|
|
415 |
} |
|
416 |
|
|
417 |
// Set Ouput Port |
842bb6
|
418 |
PutOutput(SLP1.SLPoutputB); |
5dd1b7
|
419 |
|
Q |
420 |
|
|
421 |
// receive packet |
|
422 |
Uart1RecvPacket(); |
|
423 |
// Process Packet |
|
424 |
ModbusProcess(); |
|
425 |
|
|
426 |
|
|
427 |
// Other process |
|
428 |
OtherProcess(); |
|
429 |
|
|
430 |
|
|
431 |
Delay_us(100); |
|
432 |
|
|
433 |
nCount++; |
|
434 |
} |
|
435 |
|
|
436 |
//return ; |
|
437 |
} |
|
438 |
|
|
439 |
/* |
|
440 |
中断系统,函数名与前面命名无关,只与interrupt后面接的序号有关 |
|
441 |
void INT0_Isr() interrupt 0 // 外部中断0 |
|
442 |
void TM0_Isr() interrupt 1 // 定时器0 |
|
443 |
void INT1_Isr() interrupt 2 // 外部中断1 |
|
444 |
void TM1_Isr() interrupt 3 // 定时器1 |
|
445 |
void UART1_Isr() interrupt 4 // 串口1 |
|
446 |
void ADC_Isr() interrupt 5 // 模数转换 |
|
447 |
void LVD_Isr() interrupt 6 // 低压检测 |
|
448 |
void PCA_Isr() interrupt 7 // 捕获 |
|
449 |
void UART2_Isr() interrupt 8 // 串口2 |
|
450 |
void SPI_Isr() interrupt 9 // 串行外设接口 |
|
451 |
void INT2_Isr() interrupt 10 // 外部中断2 |
|
452 |
void INT3_Isr() interrupt 11 // 外部中断3 |
|
453 |
void TM2_Isr() interrupt 12 // 定时器2 |
|
454 |
void INT4_Isr() interrupt 16 // 外部中断4 |
|
455 |
void UART3_Isr() interrupt 17 // 串口3 |
|
456 |
void UART4_Isr() interrupt 18 // 串口4 |
|
457 |
void TM3_Isr() interrupt 19 // 定时器3 |
|
458 |
void TM4_Isr() interrupt 20 // 定时器4 |
|
459 |
void CMP_Isr() interrupt 21 // 比较器 |
|
460 |
void PWM0_Isr() interrupt 22 // PWM0 |
|
461 |
void PWM0FD_Isr() interrupt 23 // PWM0异常检测 |
|
462 |
void I2C_Isr() interrupt 24 // I2C总线 |
|
463 |
|
|
464 |
|
|
465 |
void PWM1_Isr() interrupt 28 // PWM1 |
|
466 |
void PWM2_Isr() interrupt 29 // PWM2 |
|
467 |
void PWM3_Isr() interrupt 30 // PWM3 |
|
468 |
void PWM4_Isr() interrupt 31 // PWM4 |
|
469 |
|
|
470 |
// 32以上的中断号,即中断向量大于0100H的中断,keil编译会出错,需要特殊处理才可使用 |
|
471 |
void PWM5_Isr() interrupt 32 // PWM5 |
|
472 |
void PWM2FD_Isr() interrupt 33 // PWM2异常检测 |
|
473 |
void PWM4FD_Isr() interrupt 34 // PWM4异常检测 |
|
474 |
void PTKSU_Isr() interrupt 35 // 触摸按键中断 |
|
475 |
*/ |