00001 /* @file main.c 00002 ** 00003 **************************************************************************** 00004 ** 00005 ** 00006 ** Copyright (c) 2007 - Atmel Corporation 00007 ** Proprietaty Information 00008 ** 00009 ** Project : ATMEGA88 + ATA6824 High Temperature H-bridge System 00010 ** Module : main.c 00011 ** Description : High temperature DC motor control 00012 ** To be used with "High temperature H-bridge System board" 00013 ** 00014 ** Version : Date: Author: Comment: 00015 ** 1.0 12.02.2007 F.G. Creation 00016 ** 00017 ** LICENSE - 00018 ** 00019 ** ATMEL - 2007 00020 ** All software programs are provided 'as is' without warranty of any kind: 00021 ** Atmel does not state the suitability of the provided materials for any 00022 ** purpose. Atmel hereby disclaim all warranties and conditions with regard 00023 ** to the provided software, including all implied warranties, fitness for 00024 ** a particular purpose, title and non-infringement.In no event will Atmel 00025 ** be liable for any indirect or consequential damages or any damages 00026 ** whatsoever resulting from the usage of the software program. 00027 **************************************************************************** 00028 ** 00029 */ 00030 00031 00032 /*_____ F U S E S C O N F I G U R A T I O N ______________________________*/ 00033 /* - 8Mhz internal RC Oscillator in being used 00034 * 00035 * Caution about MCU start-up time in Fuse configuration : 00036 * - Care should be taken in Fuse configuration to set up a MCU start-up time 00037 * of 0ms or 4.1ms. (By default, it is set to 65ms) 00038 * - ATA6824 waits a watchdog trigger before typically td = 68ms with 10% tolerance 00039 * - When no trigger happens during this period of time, MCU is reseted. 00040 * - Default MCU startup time is set to 65ms, which might be too long regarding 00041 * td (68ms ~10%) before a reset occur. MCU won't be able to generate the 00042 * WD trig if td < 65ms. As a result, MCU won't start before being reseted! 00043 ***************************************************************************/ 00044 00045 00046 00047 /*_____ I N C L U D E S ____________________________________________________*/ 00048 #include "config.h" 00049 #include "ADC_task.h" // ADC used to acquire Motor current, desired speed and Vbat 00050 #include "Timer0_PWM.h" // Timer0 used to generate PWM and Software time base 00051 #include "ATA6824_defs.h" // ATA6824 specific routines (WD, Diagnostic) 00052 #include "Hall_sensors.h" // Manages hall sensors pin change interrupts (PCINT0) 00053 #include "an_compare.h" // Detects over current condition 00054 00055 00056 /*_____ M A C R O S ________________________________________________________*/ 00057 /*_____ D E F I N I T I O N S ______________________________________________*/ 00061 typedef enum 00062 { 00063 MOTOR_STATUS_STOPPED, 00064 MOTOR_STATUS_CW, 00065 MOTOR_STATUS_CCW 00066 } Motor_ctrl_t; 00067 00068 00069 /*_____ P R O T O T Y P E S - D E C L A R A T I O N ________________________*/ 00070 void display_defaults(void); // Fault output on LEDs 00071 00072 00073 /*_____ G L O B A L S ______________________________________________________*/ 00074 00079 void main(void) 00080 { 00081 Motor_ctrl_t motor_status = MOTOR_STATUS_STOPPED; 00082 00083 CLKPR = 0x80; 00084 CLKPR = 0x00; // No system clock prescaler 00085 CONFIG_IO_PORTS(); // Configure I/O ports 00086 00087 ADC_Init(); // initialize ADC 00088 Timer0_start(); // Starts Timer 0 for a 31250Hz PWM and 00089 // time management. 00090 Hall_sensors_ISR_init(); // Configures Pin change interrupts (PCINT0) 00091 Diag_inputs_ISR_init(); // Configures DG1/2/3 pin change interrupts (PCINT1) 00092 AN_compare_init(); // Configures over-current detection through 00093 // analog comparator 00094 00095 SET_WD_TRIG(); // 1st watchdog refresh 00096 00097 __enable_interrupt(); // enable interrupts 00098 00099 while(1) // End-less loop 00100 { 00101 ADC_task(); // Manage ADC acquisitions 00102 manage_time_base(); // Manage time base 00103 refresh_ATA6824_watchdog(); // Refresh ATA6824 Watchdog 00104 00105 /* Motor management */ 00106 switch(motor_status) 00107 { 00108 //-------------------------------------------------------------------- 00109 case MOTOR_STATUS_STOPPED: // Motor is stopped 00110 if (GET_SWITCH_CW()) 00111 { 00112 SET_MOTOR_DIR(); // Set motor direction 00113 TIMER0_SET_OC0B_PWM((U8)((adc_get_speed()>>2))); // Update PWM ratio 00114 motor_status = MOTOR_STATUS_CW; 00115 } 00116 else if (GET_SWITCH_CCW()) 00117 { 00118 RESET_MOTOR_DIR(); // Reset motor direction 00119 TIMER0_SET_OC0B_PWM((U8)((adc_get_speed()>>2))); // Update PWM ratio 00120 motor_status = MOTOR_STATUS_CCW; 00121 } 00122 else 00123 TIMER0_SET_OC0B_PWM(0x00); // Set PWM ratio to zero 00124 break; 00125 00126 //-------------------------------------------------------------------- 00127 case MOTOR_STATUS_CW: // Motor runs Clockwise 00128 case MOTOR_STATUS_CCW: // Motor runs counter clockwise 00129 00130 if (!GET_SWITCH_CW() && !GET_SWITCH_CCW()) // Stop command from switch 00131 { 00132 TIMER0_SET_OC0B_PWM(0x00); // Set PWM ratio to zero 00133 motor_status = MOTOR_STATUS_STOPPED; // Update motor status 00134 } 00135 else 00136 TIMER0_SET_OC0B_PWM((U8)((adc_get_speed()>>2))); // Update PWM ratio 00137 break; 00138 00139 //-------------------------------------------------------------------- 00140 default: 00141 motor_status = MOTOR_STATUS_STOPPED; 00142 TIMER0_SET_OC0B_PWM(0x00); // Set PWM ratio to zero 00143 break; 00144 } 00145 00146 // Default display on LEDs 00147 display_defaults(); 00148 00149 // Clear faults when Release push-button is pushed and faults no more remain 00150 if (GET_RELEASE_BP()) 00151 clear_faults(); // Clear not remaining faults (latched by software) 00152 }; 00153 } 00154 00155 00156 00164 void display_defaults(void) 00165 { 00166 // Display short-cicuit detected (latched DG1 signal) 00167 if (ATA6824_diag_mgt.dg1 != NO_FAILURE) SET_DG1_LED(); 00168 else RESET_DG1_LED(); 00169 00170 // Display pump charge failure, undervoltage or overvoltage detected (latched DG2 signal) 00171 if (ATA6824_diag_mgt.dg2 != NO_FAILURE) SET_DG2_LED(); 00172 else RESET_DG2_LED(); 00173 00174 // Toggle DG3_LED in over-temperature conditions 00175 if (time_count_1s.ovf == true) 00176 { 00177 if (ATA6824_diag_mgt.dg3 != NO_FAILURE) 00178 TOGGLE_DG3_LED(); 00179 else 00180 RESET_DG3_LED(); 00181 time_count_1s.ovf = false; // Clear 1s overflow flag 00182 } 00183 } 00184
1.4.7