1 | /* Includes *******************************************************************/
|
2 | #include <avr/io.h>
|
3 | #include <inttypes.h>
|
4 | #include <avr/interrupt.h>
|
5 | #include "adc.h"
|
6 | #include "motor.h"
|
7 | #include "defs.h"
|
8 |
|
9 | /* Constants ******************************************************************/
|
10 | #define MAXVAL 0.1108
|
11 | #define VREF 5
|
12 |
|
13 |
|
14 | /* Macros *********************************************************************/
|
15 |
|
16 | /* Type definitions ***********************************************************/
|
17 |
|
18 | /* Prototypes *****************************************************************/
|
19 |
|
20 | /* Variables ******************************************************************/
|
21 | static uint16_t value;
|
22 | static float voltage;
|
23 |
|
24 | /* Functions ******************************************************************/
|
25 | //------------------------------------------------------------------------------
|
26 | //--- Interrupt routine
|
27 | //------------------------------------------------------------------------------
|
28 | ISR (ADC_vect)
|
29 | {
|
30 | value = ADCL;
|
31 | value += (ADCH<<8);
|
32 | voltage = (float) value * (float)VREF / 1023.0;
|
33 | if (voltage > MAXVAL)
|
34 | {
|
35 | MOTOR_Move (STOP);
|
36 | SetMotorState (OVERCUR);
|
37 | }
|
38 | }
|
39 |
|
40 | //------------------------------------------------------------------------------
|
41 | //--- Initialize analog digital converter of controller
|
42 | //------------------------------------------------------------------------------
|
43 | void ADC_Init (void)
|
44 | {
|
45 |
|
46 | ADCSRA |= (1<<ADSC) | (1<<ADATE) | (1<<ADIE) | (1<<ADPS1) | (1<<ADPS2);
|
47 | ADMUX |= (1<<REFS0);
|
48 | ADCSRA |= (1<<ADSC);
|
49 |
|
50 |
|
51 | }
|
52 |
|
53 | //------------------------------------------------------------------------------
|
54 | //--- Enable / Disable ADC
|
55 | //------------------------------------------------------------------------------
|
56 | void ADC_Enable (uint8_t enable)
|
57 | {
|
58 | if (enable) ADCSRA |= (1<<ADEN);
|
59 | else ADCSRA &= ~(1<<ADEN);
|
60 | }
|