root / branches / autonomous_recharging / code / projects / autonomous_recharging / archs / homing stop / stop.c @ 703
History | View | Annotate | Download (3.22 KB)
1 |
#include<avr/io.h> |
---|---|
2 |
#include <avr/interrupt.h> |
3 |
#include "i2c.h" |
4 |
|
5 |
#define LEDG PB4 //Green |
6 |
#define LEDR PB5 //Red |
7 |
|
8 |
// for i2c_byte coming from charge board
|
9 |
//I2C Message Codes
|
10 |
#define I2C_MSG_ACKNOWLEDGE 'A' |
11 |
#define I2C_MSG_BATTERY_CHARGING 'C' |
12 |
#define I2C_MSG_DATA 'D' |
13 |
#define I2C_MSG_CONTACT_ERROR 'E' |
14 |
#define I2C_MSG_BATTERY_FULL 'F' |
15 |
#define I2C_MSG_NO_CONTACT 'N' |
16 |
#define I2C_MSG_REQUEST_DATA 'R' |
17 |
#define I2C_MSG_GO_TO_SLEEP 'Y' |
18 |
#define I2C_MSG_ENTERING_SLEEP 'Z' |
19 |
#define I2C_MSG_HOMING 'H' |
20 |
|
21 |
#define DETECT_12V PB6
|
22 |
|
23 |
|
24 |
#define SW0 PA6
|
25 |
#define HOMING_PIN PA7
|
26 |
|
27 |
uint8_t read_homing(void)
|
28 |
{ |
29 |
uint8_t ret = PINA & _BV(HOMING_PIN); |
30 |
if(ret)
|
31 |
PORTA |= _BV(PA3); |
32 |
else
|
33 |
PORTA &= ~_BV(PA3); |
34 |
return ret;
|
35 |
} |
36 |
|
37 |
uint8_t get_delay(void)
|
38 |
{ |
39 |
uint8_t count = 0;
|
40 |
|
41 |
PORTB|=_BV(LEDR); |
42 |
while(read_homing())
|
43 |
{ |
44 |
delay_ms(1);
|
45 |
count++; |
46 |
|
47 |
if (count >= 100) |
48 |
return 1; |
49 |
} //wait a beacon cycle to make sure we aren't starting the count in the middle of one
|
50 |
PORTB &= ~_BV(LEDR); |
51 |
count = 0;
|
52 |
PORTB |= _BV(LEDG); |
53 |
while(!read_homing())
|
54 |
{ |
55 |
delay_ms(1);
|
56 |
count++; |
57 |
if(count==255) |
58 |
return 2; |
59 |
} |
60 |
PORTB &= ~_BV(LEDG); |
61 |
|
62 |
/*RECH_PUTS("\n\rCount: ");
|
63 |
RECH_PUTI(count);
|
64 |
RECH_PUTC('.');*/
|
65 |
|
66 |
return count;
|
67 |
} |
68 |
|
69 |
int main( void ) |
70 |
{ |
71 |
uint8_t count; |
72 |
|
73 |
|
74 |
DDRB |= /*_BV(PB0) |*/ _BV(LEDR) | _BV(LEDG); //what is PB0 for? |
75 |
DDRB &= ~_BV(DETECT_12V); |
76 |
|
77 |
i2c_init(); |
78 |
|
79 |
|
80 |
/*tempData[0] = 'C';
|
81 |
tempData[1] = abs_time>>8;
|
82 |
tempData[2] = abs_time&0xFF;
|
83 |
i2c_putpacket(0x01, tempData, 3);*/
|
84 |
|
85 |
char data[4]; |
86 |
data[0]=I2C_MSG_DATA;
|
87 |
|
88 |
|
89 |
|
90 |
while(1) |
91 |
{ |
92 |
|
93 |
PORTB |=_BV(LEDG); |
94 |
PORTB |=_BV(LEDR); |
95 |
delay_ms(500);
|
96 |
PORTB &= ~_BV(LEDG); |
97 |
PORTB &= ~_BV(LEDR); |
98 |
|
99 |
|
100 |
while(1) |
101 |
{ |
102 |
|
103 |
if((PINB & _BV(DETECT_12V)))
|
104 |
{ |
105 |
PORTB |= _BV(LEDG); |
106 |
|
107 |
//This is a junk byte. For some reason the first packet is always ignored????
|
108 |
data[1] = 'a'; |
109 |
i2c_putpacket(0x01, data, 2); |
110 |
data[1]=I2C_MSG_BATTERY_CHARGING;
|
111 |
i2c_putpacket(0x01, data, 2); |
112 |
break;
|
113 |
} |
114 |
else
|
115 |
{ |
116 |
count = 0;
|
117 |
data[1]=I2C_MSG_NO_CONTACT;
|
118 |
|
119 |
get_delay(); //reject the first reading //homing import stuff, uncomment this!!!!!!
|
120 |
count = get_delay(); |
121 |
if(count>2){ |
122 |
data[2]=I2C_MSG_HOMING;
|
123 |
data[3]= count;
|
124 |
|
125 |
i2c_putpacket(0x01, data, 4); |
126 |
|
127 |
} |
128 |
else{
|
129 |
i2c_putpacket(0x01, data, 2); |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
count = 0;
|
135 |
|
136 |
PORTB &= ~_BV(LEDG); |
137 |
PORTB |= _BV(LEDR); |
138 |
|
139 |
|
140 |
for(unsigned int i=0;i<60000;i++) |
141 |
{ |
142 |
delay_ms(1);
|
143 |
if(! (PINB & _BV(DETECT_12V)))
|
144 |
{ |
145 |
PORTB |= _BV(LEDR); |
146 |
data[1]=I2C_MSG_CONTACT_ERROR;
|
147 |
i2c_putpacket(0x01, data, 2); |
148 |
|
149 |
if((++count)>=200) |
150 |
break;
|
151 |
} |
152 |
else
|
153 |
count = 0;
|
154 |
|
155 |
} |
156 |
|
157 |
data[1]=I2C_MSG_BATTERY_FULL;
|
158 |
i2c_putpacket(0x01, data, 2); |
159 |
} |
160 |
|
161 |
} |