-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLC_MITSU.c
417 lines (390 loc) · 17.3 KB
/
PLC_MITSU.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* PLCMITSU.c
*
* Created on: Nov 2, 2018
* Author: Itachi
*/
#include "include.h"
#include "PLC_MITSU.h"
/* --------------Read_D(uint16_t Start_addr)--------------------
* Operating: Read D memory of PLC FX series
* Read from D address (Start_addr)
* up to D1023 (maximum)
* Data frame structure collect from Autobase
* Input: Start address (Address want to read)
* Output: Data value at Start_addr (uint16)
---------------------------------------------------------------*/
uint16_t Read_D(uint16_t Start_addr){
/*-----Variables for PLC Read------- */
uint16_t data_d=0;
uint32_t csum=0;
uint8_t csum_low,csum_high;
uint8_t Addr,Amount; //Change if want to configure different protocol
uint8_t addr_low,addr_high;
uint8_t amount_low,amount_high;
uint8_t start_addr_b0,start_addr_b1,start_addr_b2;
uint8_t send_count=0;
//Data processing
Addr= D_MEM_READ; //Change if want to connect to different memory address
addr_low = Convert_2Char(Addr&0x0F);
addr_high = Convert_2Char((Addr>>4)&0x000F);
Start_addr= Start_addr*2; //*2 because 2 byte (autobase data showing that)
start_addr_b0= Convert_2Char(Start_addr&0x0F); Start_addr>>=4; //2 address per unit
start_addr_b1= Convert_2Char(Start_addr&0x0F); Start_addr>>=4;
start_addr_b2= Convert_2Char(Start_addr&0x0F); Start_addr>>=4;
Amount=0x01*2; //Read 1 word by default, (2 byte-autobase data showing that)
amount_low = Convert_2Char(Amount&0x000F);
amount_high = Convert_2Char((Amount>>4)&0x000F);
csum = addr_high+addr_low+start_addr_b2+start_addr_b1+start_addr_b0+amount_high+amount_low+ETX;
csum = csum&0x00FF; // Collect 2 last nibbles for sum check
csum_low= Convert_2Char(csum&0x000F); // Last digit have 4 bits
csum_high= Convert_2Char((csum>>4)&0x000F);
//Package the data
data_send_2plc[0]=STX;
data_send_2plc[1]=addr_high;
data_send_2plc[2]=addr_low;
data_send_2plc[3]=start_addr_b2;
data_send_2plc[4]=start_addr_b1;
data_send_2plc[5]=start_addr_b0;
data_send_2plc[6]=amount_high;
data_send_2plc[7]=amount_low;
data_send_2plc[8]=ETX;
data_send_2plc[9]=csum_high;
data_send_2plc[10]=csum_low;
// Send data_send_2plc to UART transmitter and send to PLC
for(send_count=0;send_count<11;send_count++){
UARTCharPut(UART1_BASE, data_send_2plc[send_count]);
delay_us(50);
}
delay_us(10000);
data_d=Correct_Process(D_MEM_READ);
return data_d;
}
/* ------------------Read_M(uint16_t Start_addr)----------------
* Operating: Read M memory of PLC FX series
* Read from address (Start_addr) (bit)
* Data frame structure collect from Autobase
* Input: Start address (Address want to read)
* Output: Data value at Start_addr (bool)
---------------------------------------------------------------*/
bool Read_M(uint16_t Start_addr){
//Calculate variables (SEND)
uint8_t Addr;
uint8_t addr_low,addr_high;
uint32_t base_addr=0;
uint8_t start_addr_b0,start_addr_b1,start_addr_b2;
uint8_t amount,amount_low,amount_high;
uint32_t csum=0;
uint8_t csum_low,csum_high;
uint8_t send_count=0;
//Calculate variables (RECEIVE)
uint8_t offset=0;
uint8_t return_type=0;
uint8_t Bit_shift=0; //Bit shift for Read_M_MEM
bool Bit_return=0; //Bit return for Read_M_MEM
uint16_t ui16Data_Read_M=0;
//****************** SEND DATA TO PLC *********************//
Addr= M_MEM_READ;
addr_low = Convert_2Char(Addr&0x0F);
addr_high = Convert_2Char((Addr>>4)&0x000F);
//Convert Start_add to 3 bytes of character to send to PLC
//Offset definition
if (( Start_addr/10)<4) offset=0;
else if (((Start_addr/10)>=4) && ((Start_addr/10)<8)) offset=1;
else if (((Start_addr/10)>=8) && ((Start_addr/10)<12)) offset=2;
else if (((Start_addr/10)>=12) && ((Start_addr/10)<16)) offset=3;
else if (((Start_addr/10)>=16) && ((Start_addr/10)<20)) offset=4;
else if (((Start_addr/10)>=20) && ((Start_addr/10)<22)) offset=5;
base_addr= (( Start_addr/10)|0x100)+offset;
start_addr_b0= Convert_2Char(base_addr&0x0F); base_addr>>=4;
start_addr_b1= Convert_2Char(base_addr&0x0F); base_addr>>=4;
start_addr_b2= Convert_2Char(base_addr&0x0F); base_addr>>=4;
//Size of data
if ( ( (Start_addr/10)==12 ) || ((Start_addr/10)==19)) amount=0x03;
else amount=0x02;
amount_low = Convert_2Char(amount&0x000F);
amount_high = Convert_2Char((amount>>4)&0x000F);
//Check sum
csum = addr_high+addr_low+start_addr_b2+start_addr_b1+start_addr_b0+amount_high+amount_low+ETX;
csum = csum&0x00FF; // Collect 2 last nibbles for sum check
csum_low= Convert_2Char(csum&0x000F); // Last digit have 4 bits
csum_high= Convert_2Char((csum>>4)&0x000F);
//Package the data
data_send_2plc[0]=STX;
data_send_2plc[1]=addr_high;
data_send_2plc[2]=addr_low;
data_send_2plc[3]=start_addr_b2;
data_send_2plc[4]=start_addr_b1;
data_send_2plc[5]=start_addr_b0;
data_send_2plc[6]=amount_high;
data_send_2plc[7]=amount_low;
data_send_2plc[8]=ETX;
data_send_2plc[9]=csum_high;
data_send_2plc[10]=csum_low;
// Send data_send_2plc to UART transmitter and send to PLC
for(send_count=0;send_count<11;send_count++)
{
UARTCharPut(UART1_BASE, data_send_2plc[send_count]);
delay_us(10);
}
delay_us(10000);
//****************** RECEIVE DATA FROM PLC *********************//
// When finish collect data from Process there will be a
// right shift to collect corresponding bit
return_type= (Start_addr/10)%4; //There are 4 type of return
Bit_shift= Start_addr%10; //Bit shift will be 0-9
ui16Data_Read_M=Correct_Process(M_MEM_READ);
// type0 - real data in bit 0-9 of 16bits
if (return_type==0)
{
ui16Data_Read_M=ui16Data_Read_M&0x3FF;
}
else if (return_type==1)
{
ui16Data_Read_M=(ui16Data_Read_M>>2)&0x3FF;
}
else if (return_type==2)
{
ui16Data_Read_M=(ui16Data_Read_M>>4)&0x3FF;
}
else if (return_type==3)
{
ui16Data_Read_M=(ui16Data_Read_M>>6)&0x3FF;
}
else;
Bit_return=(ui16Data_Read_M>>Bit_shift)&0x01;
return Bit_return;
}
/* ---------Write_D(uint16_t Start_addr, uint16_t Value)---------
* Operating: Write D memory of PLC FX series
* write to D address (word-2 bytes)
* Data frame structure collect from Autobase
* Input: uint16_t Start_add(Address want to write), uint16_t Value(value to write)
* Output: None
* Write D address is 0x11
----------------------------------------------------------------*/
void Write_D(uint16_t Start_addr, uint16_t Value){
/*-----Local Variables ------ */
uint32_t csum=0;
uint8_t csum_low,csum_high;
uint8_t Addr,Amount; //Change if want to configure different protocol
uint8_t addr_low,addr_high;
uint8_t amount_low,amount_high;
uint8_t start_addr_b0,start_addr_b1,start_addr_b2;
uint8_t send_count=0;
uint8_t value_b0,value_b1,value_b2,value_b3;
// Data processing
Addr = D_MEM_WRITE; //Change if want to connect to different memory address
addr_low = Convert_2Char(Addr&0x0F);
addr_high = Convert_2Char((Addr>>4)&0x000F);
Start_addr= Start_addr*2; //*2 because 2 byte (autobase data showing that)
start_addr_b0= Convert_2Char(Start_addr&0x0F); Start_addr>>=4; //2 address per unit
start_addr_b1= Convert_2Char(Start_addr&0x0F); Start_addr>>=4;
start_addr_b2= Convert_2Char(Start_addr&0x0F); Start_addr>>=4;
Amount=0x01*2; //Read 1 word by default, (2 byte-autobase data showing that)
amount_low = Convert_2Char(Amount&0x000F);
amount_high = Convert_2Char((Amount>>4)&0x000F);
value_b0= Convert_2Char(Value&0x0F); Value>>=4;
value_b1= Convert_2Char(Value&0x0F); Value>>=4;
value_b2= Convert_2Char(Value&0x0F); Value>>=4;
value_b3= Convert_2Char(Value&0x0F); Value>>=4;
csum = addr_high+addr_low+start_addr_b2+start_addr_b1+start_addr_b0+amount_high+amount_low+value_b0+value_b1+value_b2+value_b3+ETX;
csum = csum&0x00FF; // Collect 2 last nibbles for sum check
csum_low= Convert_2Char(csum&0x000F); // Last digit have 4 bits
csum_high= Convert_2Char((csum>>4)&0x000F);
//Pack data and send
data_send_2plc[0]=STX;
data_send_2plc[1]=addr_high;
data_send_2plc[2]=addr_low;
data_send_2plc[3]=start_addr_b2;
data_send_2plc[4]=start_addr_b1;
data_send_2plc[5]=start_addr_b0;
data_send_2plc[6]=amount_high;
data_send_2plc[7]=amount_low;
data_send_2plc[8]=value_b1;
data_send_2plc[9]=value_b0;
data_send_2plc[10]=value_b3;
data_send_2plc[11]=value_b2;
data_send_2plc[12]=ETX;
data_send_2plc[13]=csum_high;
data_send_2plc[14]=csum_low;
for(send_count=0;send_count<15;send_count++)
{
UARTCharPut(UART1_BASE, data_send_2plc[send_count]);
delay_us(10);
}
delay_us(10000);
}
/* ------------Write_M(uint16_t Start_addr, uint8_t Type)------
* Operating: Write M memory of PLC FX series
* write to M address (Start_addr) (bit)
* Data frame structure collect from Autobase
* Input: uint16_t Start_add(Address want to write), uint8_t can be either SET or RESET
* Output: None
* Write M address is 0x0800
---------------------------------------------------------------*/
void Write_M(uint16_t Start_addr, uint8_t Type){
uint32_t csum=0;
uint8_t csum_low,csum_high;
uint8_t start_addr_b0,start_addr_b1,start_addr_b2,start_addr_b3;
uint8_t send_count=0;
// Data processing
// Type can be SET or RESET
Type=Convert_2Char(Type);
Start_addr = Start_addr+0x0800; // Write M address is 0x0800
start_addr_b0= Convert_2Char(Start_addr&0x0F); Start_addr>>=4; //2 address per unit
start_addr_b1= Convert_2Char(Start_addr&0x0F); Start_addr>>=4;
start_addr_b2= Convert_2Char(Start_addr&0x0F); Start_addr>>=4;
start_addr_b3= Convert_2Char(Start_addr&0x0F);
csum = Type+start_addr_b3+start_addr_b2+start_addr_b1+start_addr_b0+ETX;
csum = csum&0x00FF; // Collect 2 last nibbles for sum check
csum_low= Convert_2Char(csum&0x000F); // Last digit have 4 bits
csum_high= Convert_2Char((csum>>4)&0x000F);
//Package the data
data_send_2plc[0]=STX;
data_send_2plc[1]=Type;
data_send_2plc[2]=start_addr_b1;
data_send_2plc[3]=start_addr_b0;
data_send_2plc[4]=start_addr_b3;
data_send_2plc[5]=start_addr_b2;
data_send_2plc[6]=ETX;
data_send_2plc[7]=csum_high;
data_send_2plc[8]=csum_low;
// Send data_send_2plc to UART transmitter and send to PLC
for(send_count=0;send_count<9;send_count++)
{
UARTCharPut(UART1_BASE, data_send_2plc[send_count]);
delay_us(50);
}
delay_us(10000);
}
/* ----Calculate_Data(char byte0,char byte1,char byte2,char byte3)---
* Operating: Convert data receive from PLC to real value
* Input: Byte 0,1,2,3 of real data
* Output: 16-bit Real data
--------------------------------------------------------------------*/
uint16_t Calculate_Data(char byte0,char byte1,char byte2,char byte3){
uint16_t cal_data=0;
byte0=Convert_2Numb(byte0);
byte1=Convert_2Numb(byte1);
byte2=Convert_2Numb(byte2);
byte3=Convert_2Numb(byte3);
cal_data=(byte3<<12)+(byte2<<8)+(byte1<<4)+byte0;
return cal_data;
}
/* ----------Convert_2Numb(char char_in)-----------------
* Operating: Convert character to number
* Input: character want to convert to number
* Output: corresponding number
* '0' - '9' -> 0 - 9
* 'A' - 'F' -> 0x0A - 0x0F
-------------------------------------------------------*/
uint8_t Convert_2Numb(char char_in){
uint8_t numb_out;
if(char_in<'A') numb_out=char_in-0x30; //'0-9' return 0-9
else numb_out=char_in-55; //'A'-'F' return 0x0A-0x0F
return numb_out;
}
/* ---------Convert_2Char(uint8_t numb_in)--------------
* Operating: Convert number to character
* Input: Number want to convert to char
* Output: corresponding char character
* 0 - 9 -> '0' -> '9'
* 0x0A ->0x0F return 'A'->'F'
-------------------------------------------------------*/
char Convert_2Char(uint8_t numb_in){
char char_out;
if(numb_in<0x0A) char_out=numb_in+0x30; //0-9 return '0'-'9'
else char_out=numb_in+55; //0x0A-0x0F return 'A'-'F'
return char_out;
}
uint16_t ProcessData(uint8_t Mem_Type)
{
uint16_t data_value=0;
char recv_temp=0; //variable to collect data from UART
uint8_t Start_process=0; //processing running flag
uint8_t byte_count=0 ; //increase after collect 1 byte data
uint8_t byte_end=0; //assign the last value of data frame "ETX"
uint8_t csum_count_recv=0; //increase after collect 1 byte checksum data
uint8_t csum_count_cal=0; //use to calculate check sum
uint8_t sum_cal=0;
uint8_t sum_low,sum_high;
uint8_t data_correct=0; //data receive flag
//Stay here as long as there are data in receive FIFO
while(UARTCharsAvail(UART1_BASE))
{
recv_temp=UARTCharGet(UART1_BASE);
//Detect START signal
if(recv_temp==STX)
{
byte_count=0; //Reset
Start_process=1; //Start the processing
data_recv[byte_count]=recv_temp; //Store the STX signal (1st element)
byte_count++; //Next byte
delay_us(100);
}
//If it is not the start or stop bit but start bit already appear
//Store data value
else if( (Start_process==1) && (recv_temp!=ETX) )
{
data_recv[byte_count]=recv_temp;
byte_count++;
delay_us(5000);
}
//Detect STOP signal, start ending process, check sum and store value
else if( (Start_process==1) && (recv_temp==ETX))
{
data_recv[byte_count]=recv_temp; //Store ETX signal (0x03) in DATA[byte_count]
byte_end=byte_count; //Detect how many bytes received
byte_count++;
delay_us(5000);
// Collect check sum data from PLC
for(csum_count_recv=0;csum_count_recv<2;csum_count_recv++)
{
//Careful with infinite loop below
while(UARTCharsAvail(UART1_BASE)==0); //If no data stay in loop, do next instruction when have data
data_recv[byte_count]=UARTCharGet(UART1_BASE);
byte_count++;
}
// Calculate sum by receiving data
for(csum_count_cal=1;csum_count_cal<=byte_end;csum_count_cal++) sum_cal+=data_recv[csum_count_cal]; //Sum=data_recv[1->byte_end]
sum_low =Convert_2Char(sum_cal&0x0F); //sum_low (char)
sum_high=Convert_2Char((sum_cal>>4)&0x0F); //sum_high (char)
// Compare data from PLC VS receiving data
if( (data_recv[byte_end+1]==sum_high) && (data_recv[byte_end+2]==sum_low)) data_correct=1;
else data_correct=0;
}
// Ending process
if(data_correct==1)
{
// Store received data (correct data)
data_value=Calculate_Data(data_recv[2], data_recv[1], data_recv[4], data_recv[3]);
return data_value;
}
}
return 0;
}
/* ---------uint16_t Correct_Process(uint16_t Mem_type)-------------
* Operating: Find the true value of Process function
* Because Process usually return 0, try (n=200) time if none zero return
* After 200 time but there no data except 0-> real data =0 return 0
* Input: uint16_t Data memory type
* Output: True value
* Change: Change the time n to test, n bigger test more time
* -> Increase accuracy but take more time to execute
-------------------------------------------------------*/
uint16_t Correct_Process(uint16_t Mem_type)
{
unsigned long temp=0;
unsigned long temp1=0;
unsigned int ui16Data=0;
unsigned long n=250;
//Try 250 time to see if the data != 0, if not -> true data =0, if there is data =0
//escape the loop and return true value
for(temp=0;((temp<n)&&(temp1==0));temp++)
{
temp1=ProcessData(Mem_type);
if (temp1!=0) ui16Data=temp1;
}
return ui16Data;
}