PJ3007 Project  1.1
Motor Controller Board. 4A dual H Bridge
 All Files Functions Variables Macros
init.c
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 #include "init.h"
38 
39 
40 //**************************************************
41 // Global variables
42 //**************************************************
43 
44 unsigned char motor_modality = 0;
45 
46 
47 //it points to the stepper motor state maximum number depends on the stepper modality)
48 signed char current_state = 0;
49 
50 // Depending on the stepper modality the number of states are different
51 unsigned char number_of_states = 0;
52 
53 // It indicates the level of the emergency stop out. It should be changed with the functions set and reset
54 // 0: Deactivated 1: Activated
55 unsigned char emergency_stop_out = 0;
56 
57 // It indicates the level of the emergency stop out. It should be changed with the functions set and reset
58 // 0: Deactivated 1: Activated
59 unsigned char emergency_stop_in = 0;
60 
61 
62 //************************************
63 // Stepper Motor Settings
64 //************************************
65 
66 // Motor torque refers the Stepper motor (is proportional to the current)
67 //int motor_torque = 0;
68 
69 // Stepper current can not be set beyond that value but it could be changed at lower values.
71 
72 
73 // Array that contains the value for the H bridge INx inputs
74 // bit0 = IN1_HB_1
75 // bit1 = IN2_HB_1
76 // bit2 = IN1_HB_2
77 // bit3 = IN2_HB_2
78 unsigned char stepper_state[8];
79 
80 //************************************
81 //DC Motor Settings
82 //************************************
83 
84 
85 //int DC_motor_max_speed = 0;
86 //int DC_motor_wanted_speed = 0;
87 //int DC_motor_actual_speed = 0;
88 //int DC_motor_feedback_speed = 0;
89 
90 
91 //**************************************************
92 // board_initialization
93 //**************************************************
94 signed char board_initialization (void) {
95 
96 
97 //**************************************************
98 // PORT Initialization
99 //**************************************************
100  // Analog Inputs
101  LATA = 0x00;
102  TRISA = 0xFF;
103 
104  // Switches and H bridge signals
105  LATB = 0x00;
106  TRISB = 0xF0;
107 
108  // CNC signals
109  LATC = 0x00;
110  TRISC = 0b10111000;
111 
112  // LED and dip switches
113  LATD = 0x00;
114  TRISD = 0b11110000;
115 
116  // Analog Inputs
117  LATE = 0x00;
118  TRISE = 0xFF;
119 
120 //**************************************************
121 // H bridges Initialization
122 //**************************************************
125 
128 
131 
132 //**************************************************
133 //ADC Initialization
134 //**************************************************
135 
136  ADCON0bits.ACONV = 0;
137  ADCON0bits.ACSCH = 0;
138 
139  //Disable buffer
140  ADCON1bits.FIFOEN = 0;
141 
142  //set the analog input
143  ANSEL0 = 0b11110011;
144  ANSEL1 = 0x01;
145 
146  // ADC timing settings
147  ADCON2 = 0b01111110;
148 
149 //**************************************************
150 // Turn On peripherals test
151 //**************************************************
152  LED_1 = LED_ON;
153  delay_ms (1000);
154  LED_1 = LED_OFF;
155 
156  LED_2 = LED_ON;
157  delay_ms (1000);
158  LED_2 = LED_OFF;
159 
160  TEMP = ACTIVATED;
161  delay_ms (2000);
162  TEMP = DEACTIVATED;
163 
164  if (ADC_read_VIN_TEST () < VIN_MIN){
165  ERROR_LED = LED_ON;
167  return (ERROR_VIN_MIN);
168  }
169 
170  if (ADC_read_VIN_TEST () > VIN_MAX){
171  ERROR_LED = LED_ON;
173  return (ERROR_VIN_MAX);
174  }
175 
177 }
178 
179 
180 
181 //**************************************************************************************************
182 // OTHER FUNCTIONS
183 //**************************************************************************************************
184 
185 //**************************************************
186 // system_check
187 //**************************************************
188 #ifdef DC_MOTOR
189 signed char system_check (void) {
190 
191  unsigned char temperature;
192 
193  if (ADC_read_VIN_TEST () < VIN_MIN){
195  ERROR_LED = LED_ON;
197  return (ERROR_VIN_MIN);
198  }
199 
200  if (ADC_read_VIN_TEST () > VIN_MAX){
202  ERROR_LED = LED_ON;
204  return (ERROR_VIN_MAX);
205  }
206 
207  temperature = ADC_read_TEMP ();
208 
209  if (temperature > TEMP_MAX){
210  turn_on_FAN ();
212  ERROR_LED = LED_ON;
214  while (1);
215  }
216 
217  if (temperature > TEMP_WARNING_1){
218  turn_on_FAN ();
219  } else
220  if (temperature < TEMP_WARNING_1 - TEMP_HYSTERESIS)
221  turn_off_FAN ();
222 
223 
226  ERROR_LED = LED_ON;
228  while (1);
229  }
230 
233  ERROR_LED = LED_ON;
235  while (1);
236  }
237 
238  return (0);
239 }
240 #endif
241 
242 //**************************************************
243 // initialize_DC_motor
244 //**************************************************
245 #ifdef DC_MOTOR
246 signed char initialize_DC_motor (unsigned char modality, int pwm_frequency, int max_current ) {
247 
248  motor_max_current = max_current;
249 
251  return (ERROR_EMG_STOP_ACTIVE);
252 
253  //*************************************************
254  // SIGNED_MAGNITUDE Modality
255  //*************************************************
256 
257  if (modality == SIGNED_MAGNITUDE) {
258 
259  // Open Timer 2 as PWM time base
260  OpenTimer2 (TIMER_INT_OFF & T2_PS_1_16 & T2_POST_1_1);
261 
262  OpenPWM2(pwm_SIGNED_MAGNITUDE_frequency_table[pwm_frequency]);
263  OpenPWM1(pwm_SIGNED_MAGNITUDE_frequency_table[pwm_frequency]);
264 
266  }
267 
268 
269  //*************************************************
270  // LAP Modality
271  //*************************************************
272  if (modality == LAP) {
273 
274  // Complemtary Mode for PWM0-PWM1 and PWM2-PWM3
275  PWMCON0 = 0b00110000;
276 
277  // Set prescaler to 4
278  PTCON0 = 0b00000100;
279 
280  // Set PWM Frequency according to the PWM frequency table
281  power_control_PWM_set_period (pwm_LAP_frequency_table[pwm_frequency]);
282 
283  // Set Duty Cycle at 50% keeping the motors active but a speed 0 rad/s
286 
287  // Start PWM
288  PTCON1bits.PTEN = 0x01;
289 
290  //Enables the motors
293 
295  }
297 }
298 
299 #endif
300 
301 //**************************************************
302 // power_control_PWM_set_period
303 //**************************************************
304 void power_control_PWM_set_period (int period) {
305 
306  PTPERL = (unsigned char) (period & 0x00FF);
307  PTPERH = (unsigned char) ((period & 0xFF00) >> 8);
308 }
309 
310 
311 //**************************************************
312 // set_duty_cycle_DC0
313 //**************************************************
314 void set_duty_cycle_DC0 (int duty_cycle) {
315 
317  PDC0L = (unsigned char) (duty_cycle & 0x00FF);
318  PDC0H = (unsigned char) ((duty_cycle & 0xFF00) >> 8);
319 }
320 
321 
322 //**************************************************
323 // set_duty_cycle_DC1
324 //**************************************************
325 void set_duty_cycle_DC1 (int duty_cycle) {
326 
328  PDC1L = (unsigned char) (duty_cycle & 0x00FF);
329  PDC1H = (unsigned char) ((duty_cycle & 0xFF00) >> 8);
330 }
331 
332 //**************************************************
333 // rotate_motor_clockwise
334 //**************************************************
335 #ifdef DC_MOTOR
336 signed char rotate_motor_clockwise (unsigned char motor, int speed){
337 
339  return (ERROR_EMG_STOP_ACTIVE);
340 
341  // If the motor madality has been not set, the function returns an error
342  if (motor_modality == 0)
344 
345  // Speed check
346  if (speed > MAX_SPEED)
347  speed = MAX_SPEED;
348 
349  if (speed < MIN_SPEED)
350  speed = MIN_SPEED;
351 
352  DC_motor_wanted_speed = speed;
353 
354 
356 
357  if (motor == MOTOR_1) {
360  SetDCPWM1 (DC_motor_wanted_speed);
362  }
363 
364  if (motor == MOTOR_2) {
367  SetDCPWM2(DC_motor_wanted_speed);
369  }
370 
371  if (motor == MOTOR_ALL) {
374  SetDCPWM1 (DC_motor_wanted_speed);
376 
379  SetDCPWM2(DC_motor_wanted_speed);
381  }
382  }
383 
384  if (motor_modality == LAP ) {
385 
386  if (speed > POWER_CONTROL_HALF_RESOLUTION)
388 
389  if ( motor == MOTOR_1) {
391  }
392 
393  if ( motor == MOTOR_2) {
395  }
396 
397  if ( motor == MOTOR_ALL) {
400  }
401  }
403 }
404 #endif
405 
406 
407 //**************************************************
408 // rotate_motor_anticlockwise
409 //**************************************************
410 #ifdef DC_MOTOR
411 signed char rotate_motor_anticlockwise (unsigned char motor, int speed){
412 
414  return (ERROR_EMG_STOP_ACTIVE);
415 
416  // If the motor madality has been not set, the function returns an error
417  if (motor_modality == 0)
419 
420  // Speed check
421  if (speed > MAX_SPEED)
422  speed = MAX_SPEED;
423 
424  if (speed < MIN_SPEED)
425  speed = MIN_SPEED;
426 
427  DC_motor_wanted_speed = speed;
428 
430 
431  if (motor == MOTOR_1) {
434  SetDCPWM1 (DC_motor_wanted_speed);
436  }
437 
438  if (motor == MOTOR_2) {
441  SetDCPWM2(DC_motor_wanted_speed);
443  }
444 
445  if (motor == MOTOR_ALL) {
448  SetDCPWM1 (DC_motor_wanted_speed);
450 
453  SetDCPWM2(DC_motor_wanted_speed);
455  }
456  }
457 
458  if (motor_modality == LAP ) {
459 
460  if (speed > POWER_CONTROL_HALF_RESOLUTION)
462 
463  if ( motor == MOTOR_1) {
465  }
466 
467  if ( motor == MOTOR_2) {
469  }
470 
471  if ( motor == MOTOR_ALL) {
474  }
475  }
476 
478 }
479 
480 #endif
481 
482 //**************************************************
483 // stop_motor_rotation
484 //**************************************************
485 signed char stop_motor_rotation (unsigned char motor){
486 
487  #ifdef DC_MOTOR
489 
490  // Close PWM Modules
491  ClosePWM1 ();
492  ClosePWM2 ();
493 
494  if (motor == MOTOR_1) {
497  }
498 
499  if (motor == MOTOR_2) {
502  }
503 
504  if (motor == MOTOR_ALL) {
509  }
510  }
511 
512  if (motor_modality == LAP) {
513 
514  // Close PWM Modules
515  ClosePWM1 ();
516  ClosePWM2 ();
517 
518  if (motor == MOTOR_1) {
522  }
523 
524  if (motor == MOTOR_2) {
528  }
529 
530  if (motor == MOTOR_ALL) {
537  }
538  }
539 
540  #endif
541 
542  if (motor_modality == STEPPER){
543 
544  // Close PWM Modules
545  SetDCPWM2 (0);
546  SetDCPWM1 (0);
547 
551 
555  }
556 
558 }
559 
560 
561 //**************************************************
562 // initialize_RS485
563 //**************************************************
564 signed char initialize_RS485 (unsigned char baud_rate, unsigned char rx_interrupt_enable, unsigned char tx_interrupt_enable) {
565 
566 //**************************************************
567 // RS485 Initialization
568 //**************************************************
569 
570  // Crystal 10MHz and 4x PLL (40MHz Clock)
571  // Data Format 8 bit
572  // 1 stop bit
573  // Baud rate 19200bit/s
574  // Interrupt off
575  OpenUSART( tx_interrupt_enable &
576  rx_interrupt_enable &
577  USART_ASYNCH_MODE &
578  USART_EIGHT_BIT &
579  USART_CONT_RX &
580  USART_BRGH_HIGH,
581  baud_rate);
582 
583  // Turn On receiver
584  RCSTAbits.CREN = 0x01;
585 
586  // Enable Interrupt
587  PIE1bits.RCIE = 0x01;
588 
589  IPR1bits.RCIP = LOW_LEVEL_PRIORITY;
590 
591  // RS485 is in listening mode
593 
594  return (0);
595 }
596 
597 
598 //**************************************************
599 // read_data_RS485
600 //**************************************************
601 signed char read_data_RS485 (unsigned char * data) {
602 
603  if (PIR1bits.RCIF == ACTIVATED)
604  *data = RCREG;
605  else
606  return (ERROR_RX_DATA_NOT_READY);
607 
609 }
610 
611 //**************************************************
612 // write_const_string_RS485
613 //**************************************************
614 signed char write_const_string_RS485 (const char *data) {
615 
617 
618  putrsUSART (data);
619  // wait until data is sent out
620  while (BusyUSART());
622 
624 }
625 
626 //**************************************************
627 // send_return_RS485
628 //**************************************************
629 void send_return_RS485 (void) {
630 
632  // carriage return
633  WriteUSART(13);
634  // wait until data is sent out
635  while (BusyUSART());
637 }
638 
639 //**************************************************
640 // send_delete_RS485
641 //**************************************************
642 void send_delete_RS485 (void) {
643 
645  // carriage return
646  WriteUSART(24);
647  // wait until data is sent out
648  while (BusyUSART());
650 }
651 
652 
653 //**************************************************
654 // write_const_string_RS485
655 //**************************************************
656 signed char write_int_ASCII_RS485 (int value, unsigned char number_of_digits) {
657 
658  // The array size is 5 plus end of string \0
659  unsigned char convertedInt [6];
660 
661  // Index used to shift to the right the digit
662  unsigned char index = 0;
663 
664  // Integer is converted to string
665  itoa ((unsigned char*) convertedInt, value,10);
666 
667  if (number_of_digits >0 ) {
668 
669  convertedInt[number_of_digits] = '\0';
670 
671  // Shift the digit to the right removing the empty one
672  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')){
673 
674  for (index = number_of_digits-1; index > 0; index--){
675  convertedInt[index] = convertedInt[index-1];
676  convertedInt[index-1] = ' ';
677  }
678  }
679  }
680 
682 
683  putsUSART (convertedInt);
684 
685  // wait until data is sent out
686  while (BusyUSART());
687 // // carriage return
688 // WriteUSART(13);
689 // // wait until data is sent out
690 // while (BusyUSART());
691 
693 
695 }
696 
697 //**************************************************
698 // get_dip_switch_value
699 //**************************************************
700 
701 unsigned char get_dip_switch_value (void) {
702 
703  unsigned char dip_switch = 0;
704 
705 
706  if (DIP_SW_1 == GND)
707  dip_switch |= 0x01;
708 
709  if (DIP_SW_2 == GND)
710  dip_switch |= 0x02;
711 
712  if (DIP_SW_3 == GND)
713  dip_switch |= 0x04;
714 
715  if (DIP_SW_4 == GND)
716  dip_switch |= 0x08;
717 
718  return (dip_switch);
719 }
720 
721 //**************************************************
722 // ADC_read_buffer
723 //**************************************************
724 int ADC_read_buffer (void) {
725 
726  static int ADC_buffer_reading;
727 
728  // Activate ADC
729  ADCON0bits.ADON = 1;
730  ADCON0bits.GO_DONE = 1;
731 
732  while (ADCON0bits.GO_DONE);
733 
734  ADC_buffer_reading = (int) ADRESH;
735  ADC_buffer_reading = ADC_buffer_reading << 2;
736  ADC_buffer_reading = ADC_buffer_reading + (int) (ADRESL >> 6);
737 
738  return (ADC_buffer_reading);
739 
740 }
741 
742 //**************************************************
743 // ADC_read_POT_1
744 //**************************************************
745 int ADC_read_POT_1 (void ){
746 
747  // Select Group C Select
748  ADCON0bits.ACMOD0 = 0;
749  ADCON0bits.ACMOD1 = 1;
750 
751  //Select AN6 Channel within the group C
752  ADCHS = 0b00000100;
753 
754  return (ADC_read_buffer ());
755 }
756 
757 //**************************************************
758 // ADC_read_POT_2
759 //**************************************************
760 int ADC_read_POT_2 (void ){
761 
762  // Select Group D
763  ADCON0bits.ACMOD0 = 1;
764  ADCON0bits.ACMOD1 = 1;
765 
766  //Select AN7 Channel within the group D
767  ADCHS = 0b01000000;
768 
769  return (ADC_read_buffer ());
770 }
771 
772 //**************************************************
773 // ADC_read_TEMP
774 //**************************************************
775 int ADC_read_TEMP (void ){
776 
777  // Select Group A
778  ADCON0bits.ACMOD0 = 0;
779  ADCON0bits.ACMOD1 = 0;
780 
781  //Select AN8 Channel within the group A
782  ADCHS = 0b00000010;
783 
784  return (ADC_read_buffer () >> 1);
785 }
786 
787 //**************************************************
788 // ADC_read_VIN_TEST
789 //**************************************************
790 int ADC_read_VIN_TEST (void ){
791 
792  // Select Group B
793  ADCON0bits.ACMOD0 = 1;
794  ADCON0bits.ACMOD1 = 0;
795 
796  //Select AN5 Channel within the group B
797  ADCHS = 0b00010000;
798 
799  //return the voltage in volts
800  return (ADC_read_buffer ()/ 23);
801 }
802 
803 
804 //**************************************************
805 // ADC_read_motor_current
806 //**************************************************
807 int ADC_read_motor_current (unsigned char motor ) {
808 
809 
810  switch (motor) {
811 
812  case MOTOR_1:
813  // Select Group A
814  ADCON0bits.ACMOD0 = 0;
815  ADCON0bits.ACMOD1 = 0;
816 
817  //Select AN0 Channel within the group A
818  ADCHS = 0b00000000;
819  break;
820  case MOTOR_2:
821  // Select Group B
822  ADCON0bits.ACMOD0 = 1;
823  ADCON0bits.ACMOD1 = 0;
824 
825  //Select AN1 Channel within the group B
826  ADCHS = 0b00000000;
827  break;
828  default:
829  break;
830  }
831 
832  //return the current in mA
833  return (ADC_read_buffer () * 10);
834 }
835 
836 
837 //**************************************************
838 // initialize_stepper_motor
839 //**************************************************
840 signed char initialize_stepper_motor (unsigned char modality, unsigned char pwm_frequency, int max_current ) {
841 
843  return (ERROR_EMG_STOP_ACTIVE);
844 
847 
850 
853 
854 
855  // Open Timer 2 as PWM time base
856  OpenTimer2 (TIMER_INT_OFF & T2_PS_1_16 & T2_POST_1_1);
857 
858  // Set the PWM frequency
859  OpenPWM2(pwm_SIGNED_MAGNITUDE_frequency_table[pwm_frequency]);
860  OpenPWM1(pwm_SIGNED_MAGNITUDE_frequency_table[pwm_frequency]);
861 
862  motor_max_current = max_current;
863 
864  SetDCPWM2 (0);
865  SetDCPWM1 (0);
866 
867  switch (modality) {
868 
869  case FULL_STEP:
875  break;
876  case FULL_STEP_2_PHASES:
882  break;
883  case HALF_STEP:
893  break;
894  default:
895  break;
896  }
897 
899 
901 }
902 
903 
904 //**************************************************
905 // step_motor_clockwise
906 //**************************************************
907 signed char step_motor_clockwise (int current) {
908 
910  return (ERROR_EMG_STOP_ACTIVE);
911 
912  #ifdef DC_MOTOR
913  SetDCPWM2 (current);
914  SetDCPWM1 (current);
915  #endif
916 
918  IN2_HB_1 = (stepper_state[current_state] & 0x02) >> 1;
919  IN1_HB_2 = (stepper_state[current_state] & 0x04) >> 2;
920  IN2_HB_2 = (stepper_state[current_state] & 0x08) >> 3;
921 
922  current_state++;
923 
925  current_state = 0;
926  }
927 
928  return (OPERATION_PROPERLY_EXECUTED);
929 }
930 
931 
932 //**************************************************
933 // step_motor_anticlockwise
934 //**************************************************
935 signed char step_motor_anticlockwise (int current) {
936 
938  return (ERROR_EMG_STOP_ACTIVE);
939 
940  #ifdef DC_MOTOR
941  SetDCPWM2 (current);
942  SetDCPWM1 (current);
943  #endif
944 
945  current_state--;
946 
947  if (current_state < 0) {
949  }
950 
952  IN2_HB_1 = (stepper_state[current_state] & 0x02) >> 1;
953  IN1_HB_2 = (stepper_state[current_state] & 0x04) >> 2;
954  IN2_HB_2 = (stepper_state[current_state] & 0x08) >> 3;
955 
957 }
958 
959 
960 //************************************************************
961 // turn_on_FAN implementation
962 //************************************************************
963 void turn_on_FAN (void) {
964  TEMP = ACTIVATED;
965 }
966 
967 //************************************************************
968 // turn_off_FAN implementation
969 //************************************************************
970 void turn_off_FAN (void) {
971  TEMP = DEACTIVATED;
972 }
973 
974 //************************************************************
975 // is_emergency_stop_out_active
976 //************************************************************
977 unsigned char is_emergency_stop_out_active (void){
978 
980  return (ACTIVATED);
981  else
982  return (DEACTIVATED);
983 }
984 
985 
986 //************************************************************
987 // is_emergency_stop_in_active
988 //************************************************************
989 unsigned char is_emergency_stop_in_active (void){
990 
992  return (ACTIVATED);
993  else
994  return (DEACTIVATED);
995 }
996 
997 //************************************************************
998 // set_emergency_stop_out
999 //************************************************************
1000 unsigned char set_emergency_stop_out (void){
1001 
1004  return (OPERATION_PROPERLY_EXECUTED);
1005 }
1006 
1007 
1008 //************************************************************
1009 // set_emergency_stop_in
1010 //************************************************************
1011 unsigned char set_emergency_stop_in (void){
1012 
1016  return (OPERATION_PROPERLY_EXECUTED);
1017 }
1018 
1019 //************************************************************
1020 // reset_emergency_stop_out
1021 //************************************************************
1022 unsigned char reset_emergency_stop_out (void){
1023 
1026  return (OPERATION_PROPERLY_EXECUTED);
1027 }
1028 
1029 
1030 //************************************************************
1031 // reset_emergency_stop_out
1032 //************************************************************
1033 unsigned char reset_emergency_stop_in (void){
1034 
1038  return (OPERATION_PROPERLY_EXECUTED);
1039 }
1040 
1041 
1042 //************************************************************
1043 // emergency_check
1044 //************************************************************
1045 unsigned char emergency_stops_check (void){
1046 
1049  return (ERROR_EMG_STOP_ACTIVE);
1050  }
1051 
1054  return (ERROR_EMG_STOP_ACTIVE);
1055  }
1056 
1057  return (EMG_STOP_NOT_ACTIVATED);
1058 }
1059 
1060 
1061 //************************************************************
1062 // initialize_TIMER0
1063 //************************************************************
1064 void initialize_TIMER0 (unsigned char interrupt_priority) {
1065 
1066  // 16 bit Modality
1067  T0CONbits.T016BIT = 0;
1068  // Intenal Clock
1069  T0CONbits.T0CS = 0;
1070 
1071  // Enable Prescaler
1072  T0CONbits.PSA = 0;
1073 
1074  // Prescaler 4
1075  T0CONbits.T0PS0 = 1;
1076  T0CONbits.T0PS1 = 0;
1077  T0CONbits.T0PS2 = 0;
1078 
1079  // Enable Interrupt Timer0
1080  INTCONbits.TMR0IE = 1;
1081 
1082  if (interrupt_priority == LOW_LEVEL_PRIORITY) {
1083  // Enable Low Priority Interrupt
1084  INTCON2bits.TMR0IP = 0;
1085  } else {
1086  // Enable High Priority Interrupt
1087  INTCON2bits.TMR0IP = 1;
1088  }
1089 
1090  // Start Timer0
1091  T0CONbits.TMR0ON = 1;
1092 }
1093 
1094 
1095 //************************************************************
1096 // activate_signal_interupt
1097 //************************************************************
1098 void activate_signal_interupt (unsigned char signal, unsigned char interrupt_priority ) {
1099 
1100  if (signal == STEP_SIGNAL) {
1101  // Enable Interrupt for INT0 = STEP
1102  // INT0 is only High only High Priority
1103  INTCONbits.INT0IE = 0x01;
1104  INTCON2bits.INTEDG0 = RISING_EDGE;
1105 
1106  }
1107 
1108  if (signal == DIRECTION_SIGNAL) {
1109  // Enable Interrupt for INT1 = DIRECTION
1110 
1111  if (interrupt_priority == HIGH_LEVEL_PRIORITY) {
1112  INTCON3bits.INT1IE = ACTIVATED;
1113  INTCON3bits.INT1IP = HIGH_LEVEL_PRIORITY;
1114  INTCON2bits.INTEDG1 = RISING_EDGE;
1115  } else {
1116  INTCON3bits.INT1IE = ACTIVATED;
1117  INTCON3bits.INT1IP = LOW_LEVEL_PRIORITY;
1118  INTCON2bits.INTEDG1 = RISING_EDGE;
1119  }
1120 
1121  }
1122 
1123  if (signal == ENABLE_SIGNAL) {
1124  // Enable Interrupt for INT1 = ENABLE
1125 
1126  if (interrupt_priority == HIGH_LEVEL_PRIORITY) {
1127  INTCON3bits.INT2IE = ACTIVATED;
1128  INTCON3bits.INT2IP = HIGH_LEVEL_PRIORITY;
1129  INTCON2bits.INTEDG2 = RISING_EDGE;
1130  } else {
1131  INTCON3bits.INT2IE = ACTIVATED;
1132  INTCON3bits.INT2IP = LOW_LEVEL_PRIORITY;
1133  INTCON2bits.INTEDG2 = RISING_EDGE;
1134  }
1135 
1136  }
1137 
1138  if (signal == SWITCH_SIGNAL) {
1139  // Abilito le interruzioni su PORTB
1140  INTCONbits.RBIE = 1;
1141  // Abilito le interruzioni su PORTB come alta priorità
1142  INTCON2bits.RBIP = 1;
1143  }
1144 
1145 }
1146 
1147 //************************************************************
1148 // initialize_interrupt
1149 //************************************************************
1150 void initialize_interrupt (unsigned char interrupt_status) {
1151 
1152  if (interrupt_status == ACTIVATED) {
1153  // Enable two level priorities
1154  RCONbits.IPEN = 1;
1155  // Enable HIGH level Priprity
1156  INTCONbits.GIEH = 1;
1157  // Enable LOW level Priprity
1158  INTCONbits.GIEL = 1 ;
1159 
1160  } else {
1161 
1162  // Disable two level priorities
1163  RCONbits.IPEN = 0;
1164  // Disable HIGH level Priprity
1165  INTCONbits.GIEH = 0;
1166  // Disable LOW level Priprity
1167  INTCONbits.GIEL = 0 ;
1168  }
1169 
1170 }
1171 
1172 
1173 //************************************************************
1174 // set_error_status
1175 //************************************************************
1176 void set_error_status (unsigned char error) {
1177  error_status |= error;
1178 }
1179 
1180 //************************************************************
1181 // get_error_status
1182 //************************************************************
1183 unsigned char get_error_status (void){
1184  return (error_status) ;
1185 }
1186 
1187 
1188 //************************************************************
1189 // set_warning_status
1190 //************************************************************
1191 void set_warning_status (unsigned char warning){
1192  warning_status |= warning;
1193 }
1194 
1195 //************************************************************
1196 // get_warning_status
1197 //************************************************************
1198 unsigned char get_warning_status (void){
1199  return (warning_status);
1200 }
1201 
1202 
1203 
1204