PJ3007 Project  1.1
Motor Controller Board. 4A dual H Bridge
 All Files Functions Variables Macros
init.h
Go to the documentation of this file.
1 /****************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.0
5 Date : 13/01/2013
6 
7 CopyRight 2013 all rights are reserved
8 
9 
10 ********************************************************
11 SOFTWARE LICENSE AGREEMENT
12 ********************************************************
13 
14 The usage of the supplied software imply the acceptance of the following license.
15 
16 The software supplied herewith by Mauro Laurenti (the Author)
17 is intended for use solely and exclusively on Microchip PIC Microcontroller (registered mark).
18 The software is owned by the Author, and is protected under applicable copyright laws.
19 All rights are reserved.
20 Any use in violation of the foregoing restrictions may subject the
21 user to criminal sanctions under applicable laws (Italian or International ones), as well as to
22 civil liability for the breach of the terms and conditions of this license.
23 Commercial use is forbidden without a written acknowledgment with the Author.
24 Personal or educational use is allowed if the application containing the following
25 software doesn't aim to commercial use or monetary earning of any kind.
26 
27 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
28 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
31 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 
34 */
35 
36 /****************************************************************************/
37 
38 
39 #include <xc.h>
40 #include <pwm.h>
41 #include <timers.h>
42 #include <usart.h>
43 #include <stdlib.h>
44 #include "CNC_functions.h"
45 
46 
47 #ifndef FLAG_INIT
48 
49 #define FLAG_INIT
50 
51 #define CLOCK_FREQUENCY 40;
52 
53 #define IN1_HB_1 LATBbits.LATB0
54 #define IN2_HB_1 LATBbits.LATB1
55 
56 #define IN1_HB_2 LATBbits.LATB2
57 #define IN2_HB_2 LATBbits.LATB3
58 
59 #define ENABLE_HB_1 LATCbits.LATC2
60 #define ENABLE_HB_2 LATCbits.LATC1
61 
62 #define LED_1 LATDbits.LATD0
63 #define LED_2 LATDbits.LATD1
64 #define HEART_LED LATDbits.LATD0
65 #define ERROR_LED LATDbits.LATD1
66 
67 #define RS485_OUT_ENABLE LATDbits.LATD2
68 #define RS485_MODE LATDbits.LATD2
69 #define READ_ONLY 0
70 #define READ_WRITE 1
71 
72 #define TEMP LATDbits.LATD3
73 
74 #define AUX_SW_1 PORTBbits.RB4
75 #define AUX_SW_2 PORTBbits.RB5
76 
77 #define DIP_SW_1 PORTDbits.RD5
78 #define DIP_SW_2 PORTDbits.RD6
79 #define DIP_SW_3 PORTDbits.RD7
80 #define DIP_SW_4 PORTBbits.RB7
81 
82 #define LED_ON 1
83 #define LED_OFF 0
84 
85 #define VCC 1
86 #define GND 0
87 
88 // General Purpose
89 #define ACTIVATED 1
90 #define DEACTIVATED 0
91 
92 #define FLAG_SET 1
93 #define FLAG_CLEAR 0
94 
95 #define LOW_LEVEL_PRIORITY 0
96 #define HIGH_LEVEL_PRIORITY 1
97 
98 #define ENABLE_SIGNAL 0
99 #define STEP_SIGNAL 1
100 #define DIRECTION_SIGNAL 2
101 #define SWITCH_SIGNAL 3
102 
103 #define RISING_EDGE 1
104 #define FALLING_EDGE 0
105 
106 #define CNC_ENABLE PORTCbits.RC5
107 #define CNC_STEP PORTCbits.RC3
108 #define CNC_DIRECTION PORTCbits.RC4
109 
110 #define CNC_STEP_FLAG INTCONbits.INT0IF
111 #define CNC_DIRECTION_FLAG INTCON3bits.INT1IF
112 #define CNC_ENABLE_FLAG INTCON3bits.INT2IF
113 #define CNC_SWITCH_FLAG INTCONbits.RBIF
114 
115 #define EMG_STOP_OUT LATCbits.LATC0
116 
117 #define EMG_STOP_FLTA PORTDbits.RD4
118 #define EMG_STOP_RB6 PORTBbits.RB6
119 
120 //System Error Status
121 #define ERROR_OVER_VOLTAGE 1
122 #define ERROR_UNDER_VOLTAGE 2
123 #define ERROR_OVER_CURRENT_M_1 3
124 #define ERROR_OVER_CURRENT_M_2 4
125 #define ERROR_OVER_TEMP 5
126 #define ERROR_BOUDARY_SW_1 6
127 #define ERROR_BOUDARY_SW_2 7
128 #define ERROR_DELTA_VOLTAGE_O 8
129 #define ERROR_DELTA_VOLTAGE_B 9
130 
131 volatile unsigned char error_status = 0;
132 
133 //System Error Status
134 #define WARNING_MISSING_STEPS 0b00000001 // 1
135 #define WARNING_DELTA_VOLTAGE_O 0b00000010 // 2
136 #define WARNING_DELTA_VOLTAGE_B 0b00000100 // 3
137 #define WARNING_TEMPERATURE 0b00001000 // 4
138 #define WARNING_COMMUNICATION 0b00010000 // 5
139 #define WARNING_NOT_DEFINED_2 0b00100000 // 6
140 #define WARNING_NOT_DEFINED_3 0b01000000 // 7
141 #define WARNING_NOT_DEFINED_4 0b10000000 // 8
142 
143 volatile unsigned char warning_status = 0;
144 
145 #define MOTOR_1 1
146 #define MOTOR_2 2
147 #define MOTOR_ALL 3
148 
149 // Stepper Motor madality
150 #define FULL_STEP 1
151 #define FULL_STEP_2_PHASES 2
152 #define HALF_STEP 3
153 
154 // Motor madality
155 #define LAP 1
156 #define SIGNED_MAGNITUDE 2
157 #define STEPPER 3
158 
159 
160 // 10 bit PWM resolution
161 #define MAX_SPEED 1023
162 #define MIN_SPEED 0
163 
164 //V input range
165 // Once VIN_MAX or VIN_MIN is reached, the system should be shouted down.
166 // At the system start Vin is measured. A delta afterword might mean a power supply
167 // Problem so a warnign is set.
168 #define VIN_MIN 11
169 #define VIN_MAX 28
170 #define VIN_DELTA_WARNING 2
171 #define VIN_DELTA_ERROR 3
172 
173 //Temperature ranges
174 // Once TEMP_MAX is reached, the system should be shouted down.
175 // Reaching TEMP_WARNING_1 the fan should be turned ON.
176 // Reaching TEMP_WARNING_2 a warning signal is released
177 #define TEMP_MAX 65
178 #define TEMP_WARNING_1 35
179 #define TEMP_WARNING_2 45
180 #define TEMP_HYSTERESIS 6
181 
182 //Current Max
183 //The actual current is MAX_CURRENT * 10 mA
184 #define MAX_PHASE_CURRENT_PEAK 500
185 
186 //Stepper motor full step states
187 #define FULL_STEP_STATE_1 2
188 #define FULL_STEP_STATE_2 8
189 #define FULL_STEP_STATE_3 1
190 #define FULL_STEP_STATE_4 4
191 #define FULL_STEP_NR_STATES 4
192 
193 //Stepper motor full step with two phases states
194 //Tourque is 1.4 higher than standard full step (current is two time higher)
195 #define FULL_STEP_2_PHASES_STATE_1 10
196 #define FULL_STEP_2_PHASES_STATE_2 9
197 #define FULL_STEP_2_PHASES_STATE_3 5
198 #define FULL_STEP_2_PHASES_STATE_4 6
199 #define FULL_STEP_2_PHASES_NR_STATES 4
200 
201 //Stepper motor half step states
202 #define HALF_STEP_STATE_1 2
203 #define HALF_STEP_STATE_2 10
204 #define HALF_STEP_STATE_3 8
205 #define HALF_STEP_STATE_4 9
206 #define HALF_STEP_STATE_5 1
207 #define HALF_STEP_STATE_6 5
208 #define HALF_STEP_STATE_7 4
209 #define HALF_STEP_STATE_8 6
210 #define HALF_STEP_NR_STATES 8
211 
212 //**************************************************
213 // ADC State Machine
214 //**************************************************
215 
216 #define ADC_MOTOR_1_TEST 0
217 #define ADC_MOTOR_2_TEST 1
218 #define ADC_PHASE_1_TEST 0
219 #define ADC_PHASE_2_TEST 1
220 #define ADC_VIN_TEST 2
221 #define ADC_TEMP_TEST 3
222 #define SYSTEM_MONITOR_TEST 4
223 
224 #define ADC_IS_BUSY 1
225 #define ADC_IS_FREE 0
226 
227 //**************************************************
228 // Power Control PWM Module
229 //**************************************************
230 
231 // e.g. at 40MHz clock and 40KHz PWM the resolution is
232 // 10 bits so 1023 is the max PWM. Half of it is 511
233 
234 #define POWER_CONTROL_HALF_RESOLUTION 511
235 
236 //**************************************************
237 // PWM available frequency
238 // this value points the index within the array
239 // on which LAP and Signed Magnitude PWM frequency are sored
240 //**************************************************
241 
242 #define DEFAULT_PWM 30
243 
244 #define PWM_FREQ_1KHZ 0
245 #define PWM_FREQ_2KHZ 1
246 #define PWM_FREQ_3KHZ 2
247 #define PWM_FREQ_4KHZ 3
248 #define PWM_FREQ_5KHZ 4
249 #define PWM_FREQ_6KHZ 5
250 #define PWM_FREQ_7KHZ 6
251 #define PWM_FREQ_8KHZ 7
252 #define PWM_FREQ_9KHZ 8
253 #define PWM_FREQ_10KHZ 9
254 #define PWM_FREQ_11KHZ 10
255 #define PWM_FREQ_12KHZ 11
256 #define PWM_FREQ_13KHZ 12
257 #define PWM_FREQ_14KHZ 13
258 #define PWM_FREQ_15KHZ 14
259 #define PWM_FREQ_16KHZ 15
260 #define PWM_FREQ_17KHZ 16
261 #define PWM_FREQ_18KHZ 17
262 #define PWM_FREQ_19KHZ 18
263 #define PWM_FREQ_20KHZ 19
264 #define PWM_FREQ_21KHZ 20
265 #define PWM_FREQ_22KHZ 21
266 #define PWM_FREQ_23KHZ 22
267 #define PWM_FREQ_24KHZ 23
268 #define PWM_FREQ_25KHZ 24
269 #define PWM_FREQ_26KHZ 25
270 #define PWM_FREQ_27KHZ 26
271 #define PWM_FREQ_28KHZ 27
272 #define PWM_FREQ_29KHZ 28
273 #define PWM_FREQ_30KHZ 29
274 #define PWM_FREQ_31KHZ 30
275 #define PWM_FREQ_32KHZ 31
276 #define PWM_FREQ_33KHZ 32
277 #define PWM_FREQ_34KHZ 33
278 #define PWM_FREQ_35KHZ 34
279 #define PWM_FREQ_36KHZ 35
280 #define PWM_FREQ_37KHZ 36
281 #define PWM_FREQ_38KHZ 37
282 #define PWM_FREQ_39KHZ 38
283 #define PWM_FREQ_40KHZ 39
284 #define PWM_FREQ_41KHZ 40
285 #define PWM_FREQ_42KHZ 41
286 #define PWM_FREQ_43KHZ 42
287 #define PWM_FREQ_44KHZ 43
288 #define PWM_FREQ_45KHZ 44
289 #define PWM_FREQ_46KHZ 45
290 #define PWM_FREQ_47KHZ 46
291 #define PWM_FREQ_48KHZ 47
292 #define PWM_FREQ_49KHZ 48
293 #define PWM_FREQ_50KHZ 49
294 
295 //**************************************************
296 // PWM Frequency for LAP (standard CCP module is used)
297 //**************************************************
298 
299 // The following values can be use if Prescaler is set to 16 T2_PS_1_16
300 #define PWM_SIGNED_MAGNITUDE_FREQ_1KHZ 25 // this value is an approssimation
301 #define PWM_SIGNED_MAGNITUDE_FREQ_2KHZ 255 // this value is an approssimation
302 #define PWM_SIGNED_MAGNITUDE_FREQ_3KHZ 207
303 #define PWM_SIGNED_MAGNITUDE_FREQ_4KHZ 155
304 #define PWM_SIGNED_MAGNITUDE_FREQ_5KHZ 124
305 #define PWM_SIGNED_MAGNITUDE_FREQ_6KHZ 103
306 #define PWM_SIGNED_MAGNITUDE_FREQ_7KHZ 88
307 #define PWM_SIGNED_MAGNITUDE_FREQ_8KHZ 77
308 #define PWM_SIGNED_MAGNITUDE_FREQ_9KHZ 68
309 #define PWM_SIGNED_MAGNITUDE_FREQ_10KHZ 62
310 #define PWM_SIGNED_MAGNITUDE_FREQ_11KHZ 56
311 #define PWM_SIGNED_MAGNITUDE_FREQ_12KHZ 51
312 #define PWM_SIGNED_MAGNITUDE_FREQ_13KHZ 47
313 #define PWM_SIGNED_MAGNITUDE_FREQ_14KHZ 44
314 #define PWM_SIGNED_MAGNITUDE_FREQ_15KHZ 41
315 #define PWM_SIGNED_MAGNITUDE_FREQ_16KHZ 38
316 #define PWM_SIGNED_MAGNITUDE_FREQ_17KHZ 36
317 #define PWM_SIGNED_MAGNITUDE_FREQ_18KHZ 34
318 #define PWM_SIGNED_MAGNITUDE_FREQ_19KHZ 32
319 #define PWM_SIGNED_MAGNITUDE_FREQ_20KHZ 30
320 #define PWM_SIGNED_MAGNITUDE_FREQ_21KHZ 29
321 #define PWM_SIGNED_MAGNITUDE_FREQ_22KHZ 27
322 #define PWM_SIGNED_MAGNITUDE_FREQ_23KHZ 26
323 #define PWM_SIGNED_MAGNITUDE_FREQ_24KHZ 25
324 #define PWM_SIGNED_MAGNITUDE_FREQ_25KHZ 24
325 #define PWM_SIGNED_MAGNITUDE_FREQ_26KHZ 23
326 #define PWM_SIGNED_MAGNITUDE_FREQ_27KHZ 22
327 #define PWM_SIGNED_MAGNITUDE_FREQ_28KHZ 21
328 #define PWM_SIGNED_MAGNITUDE_FREQ_29KHZ 21
329 #define PWM_SIGNED_MAGNITUDE_FREQ_30KHZ 20
330 #define PWM_SIGNED_MAGNITUDE_FREQ_31KHZ 19
331 #define PWM_SIGNED_MAGNITUDE_FREQ_32KHZ 19
332 #define PWM_SIGNED_MAGNITUDE_FREQ_33KHZ 18
333 #define PWM_SIGNED_MAGNITUDE_FREQ_34KHZ 17
334 #define PWM_SIGNED_MAGNITUDE_FREQ_35KHZ 17
335 #define PWM_SIGNED_MAGNITUDE_FREQ_36KHZ 16
336 #define PWM_SIGNED_MAGNITUDE_FREQ_37KHZ 16
337 #define PWM_SIGNED_MAGNITUDE_FREQ_38KHZ 15
338 #define PWM_SIGNED_MAGNITUDE_FREQ_39KHZ 15
339 #define PWM_SIGNED_MAGNITUDE_FREQ_40KHZ 15
340 #define PWM_SIGNED_MAGNITUDE_FREQ_41KHZ 14
341 #define PWM_SIGNED_MAGNITUDE_FREQ_42KHZ 14
342 #define PWM_SIGNED_MAGNITUDE_FREQ_43KHZ 14
343 #define PWM_SIGNED_MAGNITUDE_FREQ_44KHZ 13
344 #define PWM_SIGNED_MAGNITUDE_FREQ_45KHZ 13
345 #define PWM_SIGNED_MAGNITUDE_FREQ_46KHZ 13
346 #define PWM_SIGNED_MAGNITUDE_FREQ_47KHZ 12
347 #define PWM_SIGNED_MAGNITUDE_FREQ_48KHZ 12
348 #define PWM_SIGNED_MAGNITUDE_FREQ_49KHZ 12
349 #define PWM_SIGNED_MAGNITUDE_FREQ_50KHZ 12
350 
351 
352 // The following values can be use if Prescaler is set to 4 T2_PS_1_4
353 // Minimum Frequency ios set to 20KHz
354 
355 //#define PWM_SIGNED_MAGNITUDE_FREQ_1KHZ 0
356 //#define PWM_SIGNED_MAGNITUDE_FREQ_2KHZ 0
357 //#define PWM_SIGNED_MAGNITUDE_FREQ_3KHZ 0
358 //#define PWM_SIGNED_MAGNITUDE_FREQ_4KHZ 0
359 //#define PWM_SIGNED_MAGNITUDE_FREQ_5KHZ 0
360 //#define PWM_SIGNED_MAGNITUDE_FREQ_6KHZ 0
361 //#define PWM_SIGNED_MAGNITUDE_FREQ_7KHZ 0
362 //#define PWM_SIGNED_MAGNITUDE_FREQ_8KHZ 0
363 //#define PWM_SIGNED_MAGNITUDE_FREQ_9KHZ 0
364 //#define PWM_SIGNED_MAGNITUDE_FREQ_10KHZ 0
365 //#define PWM_SIGNED_MAGNITUDE_FREQ_11KHZ 0
366 //#define PWM_SIGNED_MAGNITUDE_FREQ_12KHZ 0
367 //#define PWM_SIGNED_MAGNITUDE_FREQ_13KHZ 0
368 //#define PWM_SIGNED_MAGNITUDE_FREQ_14KHZ 0
369 //#define PWM_SIGNED_MAGNITUDE_FREQ_15KHZ 0
370 //#define PWM_SIGNED_MAGNITUDE_FREQ_16KHZ 0
371 //#define PWM_SIGNED_MAGNITUDE_FREQ_17KHZ 0
372 //#define PWM_SIGNED_MAGNITUDE_FREQ_18KHZ 0
373 //#define PWM_SIGNED_MAGNITUDE_FREQ_19KHZ 0
374 //#define PWM_SIGNED_MAGNITUDE_FREQ_20KHZ 120
375 //#define PWM_SIGNED_MAGNITUDE_FREQ_21KHZ 116
376 //#define PWM_SIGNED_MAGNITUDE_FREQ_22KHZ 108
377 //#define PWM_SIGNED_MAGNITUDE_FREQ_23KHZ 104
378 //#define PWM_SIGNED_MAGNITUDE_FREQ_24KHZ 100
379 //#define PWM_SIGNED_MAGNITUDE_FREQ_25KHZ 96
380 //#define PWM_SIGNED_MAGNITUDE_FREQ_26KHZ 92
381 //#define PWM_SIGNED_MAGNITUDE_FREQ_27KHZ 88
382 //#define PWM_SIGNED_MAGNITUDE_FREQ_28KHZ 84
383 //#define PWM_SIGNED_MAGNITUDE_FREQ_29KHZ 84
384 //#define PWM_SIGNED_MAGNITUDE_FREQ_30KHZ 80
385 //#define PWM_SIGNED_MAGNITUDE_FREQ_31KHZ 76
386 //#define PWM_SIGNED_MAGNITUDE_FREQ_32KHZ 76
387 //#define PWM_SIGNED_MAGNITUDE_FREQ_33KHZ 72
388 //#define PWM_SIGNED_MAGNITUDE_FREQ_34KHZ 68
389 //#define PWM_SIGNED_MAGNITUDE_FREQ_35KHZ 68
390 //#define PWM_SIGNED_MAGNITUDE_FREQ_36KHZ 64
391 //#define PWM_SIGNED_MAGNITUDE_FREQ_37KHZ 64
392 //#define PWM_SIGNED_MAGNITUDE_FREQ_38KHZ 60
393 //#define PWM_SIGNED_MAGNITUDE_FREQ_39KHZ 60
394 //#define PWM_SIGNED_MAGNITUDE_FREQ_40KHZ 60
395 //#define PWM_SIGNED_MAGNITUDE_FREQ_41KHZ 56
396 //#define PWM_SIGNED_MAGNITUDE_FREQ_42KHZ 56
397 //#define PWM_SIGNED_MAGNITUDE_FREQ_43KHZ 56
398 //#define PWM_SIGNED_MAGNITUDE_FREQ_44KHZ 52
399 //#define PWM_SIGNED_MAGNITUDE_FREQ_45KHZ 52
400 //#define PWM_SIGNED_MAGNITUDE_FREQ_46KHZ 52
401 //#define PWM_SIGNED_MAGNITUDE_FREQ_47KHZ 48
402 //#define PWM_SIGNED_MAGNITUDE_FREQ_48KHZ 48
403 //#define PWM_SIGNED_MAGNITUDE_FREQ_49KHZ 48
404 //#define PWM_SIGNED_MAGNITUDE_FREQ_50KHZ 48
405 
406 
407 
429  };
430 
431 //**************************************************
432 // PWM Frequency for LAP (standard CCP module is used)
433 //**************************************************
434 
435 #ifdef DC_MOTOR
436 
437  #define PWM_LAP_FREQ_1KHZ 2499
438  #define PWM_LAP_FREQ_2KHZ 1249
439  #define PWM_LAP_FREQ_3KHZ 832
440  #define PWM_LAP_FREQ_4KHZ 624
441  #define PWM_LAP_FREQ_5KHZ 499
442  #define PWM_LAP_FREQ_6KHZ 416
443  #define PWM_LAP_FREQ_7KHZ 356
444  #define PWM_LAP_FREQ_8KHZ 312
445  #define PWM_LAP_FREQ_9KHZ 277
446  #define PWM_LAP_FREQ_10KHZ 249
447  #define PWM_LAP_FREQ_11KHZ 226
448  #define PWM_LAP_FREQ_12KHZ 207
449  #define PWM_LAP_FREQ_13KHZ 191
450  #define PWM_LAP_FREQ_14KHZ 178
451  #define PWM_LAP_FREQ_15KHZ 166
452  #define PWM_LAP_FREQ_16KHZ 155
453  #define PWM_LAP_FREQ_17KHZ 146
454  #define PWM_LAP_FREQ_18KHZ 138
455  #define PWM_LAP_FREQ_19KHZ 131
456  #define PWM_LAP_FREQ_20KHZ 124
457  #define PWM_LAP_FREQ_21KHZ 118
458  #define PWM_LAP_FREQ_22KHZ 113
459  #define PWM_LAP_FREQ_23KHZ 108
460  #define PWM_LAP_FREQ_24KHZ 103
461  #define PWM_LAP_FREQ_25KHZ 99
462  #define PWM_LAP_FREQ_26KHZ 95
463  #define PWM_LAP_FREQ_27KHZ 92
464  #define PWM_LAP_FREQ_28KHZ 88
465  #define PWM_LAP_FREQ_29KHZ 85
466  #define PWM_LAP_FREQ_30KHZ 82
467  #define PWM_LAP_FREQ_31KHZ 80
468  #define PWM_LAP_FREQ_32KHZ 77
469  #define PWM_LAP_FREQ_33KHZ 75
470  #define PWM_LAP_FREQ_34KHZ 73
471  #define PWM_LAP_FREQ_35KHZ 70
472  #define PWM_LAP_FREQ_36KHZ 68
473  #define PWM_LAP_FREQ_37KHZ 67
474  #define PWM_LAP_FREQ_38KHZ 65
475  #define PWM_LAP_FREQ_39KHZ 63
476  #define PWM_LAP_FREQ_40KHZ 62
477  #define PWM_LAP_FREQ_41KHZ 60
478  #define PWM_LAP_FREQ_42KHZ 59
479  #define PWM_LAP_FREQ_43KHZ 57
480  #define PWM_LAP_FREQ_44KHZ 56
481  #define PWM_LAP_FREQ_45KHZ 55
482  #define PWM_LAP_FREQ_46KHZ 53
483  #define PWM_LAP_FREQ_47KHZ 52
484  #define PWM_LAP_FREQ_48KHZ 51
485  #define PWM_LAP_FREQ_49KHZ 50
486  #define PWM_LAP_FREQ_50KHZ 49
487 
488 #endif
489 
495 #ifdef DC_MOTOR
496 const int pwm_LAP_frequency_table [] = {PWM_LAP_FREQ_1KHZ,PWM_LAP_FREQ_2KHZ,PWM_LAP_FREQ_3KHZ,
497  PWM_LAP_FREQ_4KHZ, PWM_LAP_FREQ_5KHZ, PWM_LAP_FREQ_6KHZ,
498  PWM_LAP_FREQ_7KHZ,PWM_LAP_FREQ_8KHZ,PWM_LAP_FREQ_9KHZ,
499  PWM_LAP_FREQ_10KHZ,PWM_LAP_FREQ_11KHZ,PWM_LAP_FREQ_12KHZ,
500  PWM_LAP_FREQ_13KHZ,PWM_LAP_FREQ_14KHZ,PWM_LAP_FREQ_15KHZ,
501  PWM_LAP_FREQ_16KHZ,PWM_LAP_FREQ_17KHZ,PWM_LAP_FREQ_18KHZ,
502  PWM_LAP_FREQ_19KHZ,PWM_LAP_FREQ_20KHZ,PWM_LAP_FREQ_21KHZ,
503  PWM_LAP_FREQ_22KHZ,PWM_LAP_FREQ_23KHZ,PWM_LAP_FREQ_24KHZ,
504  PWM_LAP_FREQ_25KHZ,PWM_LAP_FREQ_26KHZ,PWM_LAP_FREQ_27KHZ,
505  PWM_LAP_FREQ_28KHZ,PWM_LAP_FREQ_29KHZ,PWM_LAP_FREQ_30KHZ,
506  PWM_LAP_FREQ_31KHZ,PWM_LAP_FREQ_32KHZ,PWM_LAP_FREQ_33KHZ,
507  PWM_LAP_FREQ_34KHZ,PWM_LAP_FREQ_35KHZ,PWM_LAP_FREQ_36KHZ,
508  PWM_LAP_FREQ_37KHZ,PWM_LAP_FREQ_38KHZ,PWM_LAP_FREQ_39KHZ,
509  PWM_LAP_FREQ_40KHZ,PWM_LAP_FREQ_41KHZ,PWM_LAP_FREQ_42KHZ,
510  PWM_LAP_FREQ_43KHZ,PWM_LAP_FREQ_44KHZ,PWM_LAP_FREQ_45KHZ,
511  PWM_LAP_FREQ_46KHZ,PWM_LAP_FREQ_47KHZ,PWM_LAP_FREQ_48KHZ,
512  PWM_LAP_FREQ_49KHZ,PWM_LAP_FREQ_50KHZ
513  };
514 #endif
515 
516 //**************************************************
517 // Irms Motor Current Table
518 //**************************************************
519 
520 // The actual current is time 10. So 25 is equal to 250mA.
521 // System current is returning the current as the table below
522 
523 #define CURRENT_0 0
524 #define CURRENT_1 25
525 #define CURRENT_2 50
526 #define CURRENT_3 75
527 #define CURRENT_4 100
528 #define CURRENT_5 125
529 #define CURRENT_6 150
530 #define CURRENT_7 175
531 #define CURRENT_8 200
532 #define CURRENT_9 225
533 #define CURRENT_10 250
534 #define CURRENT_11 275
535 #define CURRENT_12 300
536 #define CURRENT_13 325
537 #define CURRENT_14 350
538 #define CURRENT_15 375
539 
542 
543 //**************************************************
544 // RS485 Baud Rate and interrupt
545 //**************************************************
546 // The vlaues defined here are related to the SPBRG register.
547 // Refer to the datasheet for more information and additional values.
548 
549 #define BAUD_RATE_9600 255
550 #define BAUD_RATE_19200 129
551 #define BAUD_RATE_57600 42
552 #define BAUD_RATE_115200 21
553 
554 #define TX_INTERRUPT_ON USART_TX_INT_ON
555 #define TX_INTERRUPT_OFF USART_TX_INT_OFF
556 #define RX_INTERRUPT_ON USART_RX_INT_ON
557 #define RX_INTERRUPT_OFF USART_RX_INT_OFF
558 
559 
560 //**************************************************
561 // Returned Values List
562 //**************************************************
563 
564 #define EMG_STOP_NOT_ACTIVATED 2
565 #define OPERATION_PROPERLY_EXECUTED 1
566 
567 #define ERROR_MODALITY_NOT_DEFINED -1
568 #define ERROR_RX_DATA_NOT_READY -2
569 #define ERROR_VIN_MAX -3
570 #define ERROR_VIN_MIN -4
571 #define ERROR_EMG_STOP_ACTIVE -5
572 
573 
574 //**************************************************
575 // Function Prototypes
576 //**************************************************
577 
588 signed char board_initialization (void);
589 
590 
591 
607 signed char initialize_RS485 (unsigned char baud_rate, unsigned char rx_interrupt_enable, unsigned char tx_interrupt_enable);
608 
609 
610 
621 signed char read_data_RS485 (unsigned char * data);
622 
623 
624 
635 signed char write_const_string_RS485 (const char *data);
636 
637 
638 
651 signed char write_int_ASCII_RS485 (int value, char number_of_digits);
652 
661 void send_return_RS485 (void);
662 
663 
671 void send_delete_RS485 (void);
672 
673 
683 unsigned char get_dip_switch_value (void);
684 
685 
704 #ifdef DC_MOTOR
705 signed char initialize_DC_motor (unsigned char modality, int pwm_frequency, int max_current );
706 #endif
707 
722 #ifdef DC_MOTOR
723 signed char rotate_motor_clockwise (unsigned char motor, int speed);
724 #endif
725 
726 
741 #ifdef DC_MOTOR
742 signed char rotate_motor_anticlockwise (unsigned char motor, int speed);
743 #endif
744 
745 
760 signed char stop_motor_rotation (unsigned char motor);
761 
762 
763 
782 signed char initialize_stepper_motor (unsigned char modality, unsigned char pwm_frequency, int max_current );
783 
784 
785 
799 signed char step_motor_clockwise (int current);
800 
801 
814 signed char step_motor_anticlockwise (int current);
815 
816 
828 int ADC_read_POT_1 (void );
829 
830 
842 int ADC_read_POT_2 (void );
843 
844 
845 
859 int ADC_read_TEMP (void );
860 
861 
862 
875 int ADC_read_VIN_TEST (void );
876 
877 
890 int ADC_read_motor_current (unsigned char motor );
891 
892 
893 
903 void turn_on_FAN (void);
904 
905 
906 
915 void turn_off_FAN (void);
916 
917 
918 
936 unsigned char system_check (void);
937 
938 
939 
953 unsigned char is_emergency_stop_out_active (void);
954 
955 
956 
969 unsigned char is_emergency_stop_in_active (void);
970 
971 
972 
985 unsigned char set_emergency_stop_out (void);
986 
987 
988 
1001 unsigned char set_emergency_stop_in (void);
1002 
1003 
1015 unsigned char reset_emergency_stop_out (void);
1016 
1017 
1018 
1030 unsigned char reset_emergency_stop_in (void);
1031 
1032 
1044 unsigned char emergency_stops_check (void);
1045 
1046 
1047 /******************************************************************************************************
1048 // PRIVATE FUNCTIONS
1049 //
1050 // The following functions should be considered PRIVATE, the are used by internal functions.
1051 // Do not use or change it!
1052 /******************************************************************************************************
1053 
1054 
1065 int ADC_read_buffer (void);
1066 
1067 
1068 
1083 void power_control_PWM_set_period (int period);
1084 
1085 
1086 
1100 void set_duty_cycle_DC0 (int duty_cycle);
1101 
1102 
1115 void set_duty_cycle_DC1 (int duty_cycle);
1116 
1117 
1118 
1128 void initialize_interrupt (unsigned char interrupt_status);
1129 
1139 void initialize_TIMER0 (unsigned char interrupt_priority);
1140 
1141 
1152 void activate_signal_interupt (unsigned char signal, unsigned char interrupt_priority );
1153 
1154 
1163 void set_error_status (unsigned char error);
1164 
1165 
1174 unsigned char get_error_status (void);
1175 
1176 
1185 void set_warning_status (unsigned char warning);
1186 
1195 unsigned char get_warning_status (void);
1196 
1197 
1198 
1199 #endif