root / branches / autonomous_recharging / code / projects / autonomous_recharging / archs / I2c test / main.c @ 355
History | View | Annotate | Download (3.69 KB)
1 |
#include <avr/io.h> |
---|---|
2 |
#include <avr/io.h> |
3 |
#include <avr/interrupt.h> |
4 |
#include <avr/sleep.h> |
5 |
#include "ring_buffer.h" |
6 |
#include "i2c.h" |
7 |
|
8 |
|
9 |
// for i2c_byte coming from charge board
|
10 |
//I2C Message Codes
|
11 |
#define I2C_MSG_ACKNOWLEDGE 'A' |
12 |
#define I2C_MSG_BATTERY_CHARGING 'C' |
13 |
#define I2C_MSG_DATA 'D' |
14 |
#define I2C_MSG_CONTACT_ERROR 'E' |
15 |
#define I2C_MSG_BATTERY_FULL 'F' |
16 |
#define I2C_MSG_NO_CONTACT 'N' |
17 |
#define I2C_MSG_REQUEST_DATA 'R' |
18 |
#define I2C_MSG_GO_TO_SLEEP 'Y' |
19 |
#define I2C_MSG_ENTERING_SLEEP 'Z' |
20 |
#define I2C_MSG_HOMING 'H' |
21 |
|
22 |
|
23 |
#define SW0 PA6
|
24 |
#define HOMING_PIN PA7
|
25 |
|
26 |
#define DEBUG 0 |
27 |
#define USE_I2C 1 |
28 |
|
29 |
|
30 |
#define MAX_T 300 |
31 |
#define MIN_T 730 |
32 |
//range is 0 to 45 C
|
33 |
//cal tests:
|
34 |
//room temp - 25
|
35 |
//value ~500, varies from battery to battery, but is consistent on one battery
|
36 |
//freezer 737
|
37 |
//heat gun at a distance 461
|
38 |
|
39 |
#define MAX_DT -4 //this is the LOWEST ACCEPTABLE ADC value |
40 |
#define MAX_DT_ABS 400 |
41 |
#define VOLT_PLATEAU 50 |
42 |
|
43 |
|
44 |
//The following times are in seconds
|
45 |
#define MAX_FAST_TIME 5400 |
46 |
#define MAX_TRICKLE_TIME 600 |
47 |
|
48 |
//debug pins
|
49 |
#define debug_time PA3
|
50 |
#define debug_curr PA4
|
51 |
#define debug_volt PA5
|
52 |
#define debug_temp PA6
|
53 |
#define debug_12in PA7
|
54 |
|
55 |
//be sure admux also sets the MUX5 bit which is in ADCSRB
|
56 |
#define ADMUX_I
|
57 |
#define ADMUX_V
|
58 |
#define ADMUX_T
|
59 |
|
60 |
#define ROBOT_TX PB1
|
61 |
#define ROBOT_RX PB2
|
62 |
#define PWM PB3
|
63 |
#define DETECT_12V PB6
|
64 |
|
65 |
#define LED1 PB4 //Green |
66 |
#define LED2 PB5 //Red |
67 |
|
68 |
|
69 |
//LED States:
|
70 |
//Red - Fast Charging
|
71 |
//Green - Trickle Charging
|
72 |
//Both steady - done charging
|
73 |
//Both Blinking - Error
|
74 |
|
75 |
#define INT_COUNT 2 //interrupts per second |
76 |
#define AVG_COUNT 64 //number of times to count current |
77 |
|
78 |
//To enable the PWM write : TCCR1B = (_Bv(CS10));//enable PWM
|
79 |
|
80 |
uint8_t interrupt_count = INT_COUNT; |
81 |
|
82 |
volatile uint32_t abs_time=1; // start at one second so it doesnt do the minute checks right away |
83 |
volatile uint8_t new_second=0; //only used as a boolean |
84 |
|
85 |
volatile uint8_t error=0; |
86 |
volatile uint8_t status;
|
87 |
|
88 |
volatile uint8_t steady_current = 0; |
89 |
|
90 |
//DT must be triggered twice in a row
|
91 |
volatile uint8_t last_DT = 0; |
92 |
//same for DV
|
93 |
volatile uint8_t last_DV = 0; |
94 |
|
95 |
#define FAST_CHARGE 1 |
96 |
#define TRICKLE_CHARGE 2 |
97 |
|
98 |
|
99 |
void setup(void) |
100 |
{ |
101 |
DDRA = _BV(PA3); |
102 |
PORTA = 0x00;
|
103 |
DDRB = (_BV(ROBOT_TX)|_BV(PWM)|_BV(LED1)|_BV(LED2)); //confiure output pins
|
104 |
PORTB = 0x00;
|
105 |
/*
|
106 |
ADCSRA = (_BV(ADEN)|_BV(ADPS2)|_BV(ADPS1)); //start ADC with a division factor of 64
|
107 |
|
108 |
TCCR0B = (_BV(CS01)); //set timer 0 for realtime mode
|
109 |
TCCR0A = (_BV(TCW0));
|
110 |
TIMSK = (_BV(TOIE0)); //enable overflow interrupts
|
111 |
|
112 |
TCCR1A = (_BV(COM1B1)|_BV(PWM1B)|_BV(COM1A1)|_BV(PWM1A)); //clear timer 1 on compare, set at 0x00. Fast PWM mode
|
113 |
TCCR1B |= _BV(CS10); //leave timer on and set compare to 0 to make output off
|
114 |
|
115 |
//hack stuff so it will run in continuous mode
|
116 |
TIMSK |= _BV(TOIE1); //enable overflow interrupt for timer 1
|
117 |
|
118 |
OCR1B = 0;
|
119 |
OCR1A = 0;
|
120 |
|
121 |
|
122 |
sei();*/
|
123 |
} |
124 |
|
125 |
|
126 |
int main(void) |
127 |
{ |
128 |
setup(); |
129 |
|
130 |
i2c_init(); |
131 |
setup(); |
132 |
|
133 |
OCR1B=0;
|
134 |
|
135 |
sei(); |
136 |
|
137 |
|
138 |
PORTB|=_BV(LED2); |
139 |
PORTB|=_BV(LED1); |
140 |
delay_ms(500);
|
141 |
PORTB&=~_BV(LED2); |
142 |
PORTB&=~_BV(LED1); |
143 |
|
144 |
i2c_putpacket(0x01,"a ", 2); |
145 |
|
146 |
delay_ms(500);
|
147 |
PORTB|=_BV(LED2); |
148 |
PORTB|=_BV(LED1); |
149 |
|
150 |
while(1); |
151 |
|
152 |
return 1; |
153 |
} |
154 |
|
155 |
ISR(TIMER0_OVF_vect) |
156 |
{ |
157 |
if(error)
|
158 |
PORTB ^= (_BV(LED1)|_BV(LED2)); |
159 |
|
160 |
interrupt_count--; |
161 |
if(interrupt_count==0) |
162 |
{ |
163 |
abs_time++; |
164 |
new_second=1;
|
165 |
|
166 |
interrupt_count=INT_COUNT; |
167 |
} |
168 |
} |
169 |
|
170 |
ISR(TIMER1_OVF_vect) |
171 |
{ |
172 |
//wait(2); //wait a bit so we know the ouput gets set (which happens at timer = 0)
|
173 |
|
174 |
TCNT1 = 0;//156; // start out at 156. Now OCR1B - 156 = duty cycle |
175 |
} |
176 |
|
177 |
ISR(PCINT_vect){;} //so the interrupt doesnt go to the reset vector
|