root / branches / autonomous_recharging / code / projects / autonomous_recharging / archs / homing stop / stop.c @ 663
History | View | Annotate | Download (2.9 KB)
1 |
#include<avr/io.h> |
---|---|
2 |
#include <avr/interrupt.h> |
3 |
#include "i2c.h" |
4 |
|
5 |
#define LED1 PB4 //Green |
6 |
#define LED2 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(LED2); |
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(LED2); |
51 |
count = 0;
|
52 |
PORTB|=_BV(LED1); |
53 |
while(!read_homing())
|
54 |
{ |
55 |
delay_ms(1);
|
56 |
count++; |
57 |
if(count==255) |
58 |
return 2; |
59 |
} |
60 |
PORTB&=~_BV(LED1); |
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 |
int count;
|
72 |
|
73 |
DDRB = _BV(PB0) | _BV(PB5) | _BV(PB4); |
74 |
i2c_init(); |
75 |
|
76 |
/*tempData[0] = 'C';
|
77 |
tempData[1] = abs_time>>8;
|
78 |
tempData[2] = abs_time&0xFF;
|
79 |
i2c_putpacket(0x01, tempData, 3);*/
|
80 |
|
81 |
char data[2]; |
82 |
data[0]=I2C_MSG_DATA;
|
83 |
|
84 |
|
85 |
while(1) |
86 |
{ |
87 |
PORTB=_BV(LED2); |
88 |
while(1) |
89 |
{ |
90 |
if(PINB & _BV(DETECT_12V))
|
91 |
{ |
92 |
PORTB = _BV(LED1); |
93 |
//This is a junk byte. For some reason the first packet is always ignored????
|
94 |
data[1] = 'a'; |
95 |
i2c_putpacket(0x01, data, 2); |
96 |
data[1]=I2C_MSG_BATTERY_CHARGING;
|
97 |
i2c_putpacket(0x01, data, 2); |
98 |
break;
|
99 |
} |
100 |
else
|
101 |
{ |
102 |
data[1]=I2C_MSG_NO_CONTACT;
|
103 |
|
104 |
get_delay(); //reject the first reading //homing import stuff, uncomment this!!!!!!
|
105 |
count = get_delay(); |
106 |
if(count>2){ |
107 |
data[2]=I2C_MSG_HOMING;
|
108 |
data[3]=count;
|
109 |
i2c_putpacket(0x01, data, 4); |
110 |
} |
111 |
else{
|
112 |
i2c_putpacket(0x01, data, 2); |
113 |
} |
114 |
} |
115 |
} |
116 |
|
117 |
count = 0;
|
118 |
|
119 |
for(int i=0;i<60000;i++) |
120 |
{ |
121 |
delay_ms(1);
|
122 |
if(! (PINB & _BV(DETECT_12V)))
|
123 |
{ |
124 |
PORTB = _BV(LED2); |
125 |
data[1]=I2C_MSG_CONTACT_ERROR;
|
126 |
i2c_putpacket(0x01, data, 2); |
127 |
|
128 |
if((++count)>=500) |
129 |
break;
|
130 |
} |
131 |
else
|
132 |
count = 0;
|
133 |
|
134 |
} |
135 |
|
136 |
data[1]=I2C_MSG_BATTERY_FULL;
|
137 |
i2c_putpacket(0x01, data, 2); |
138 |
} |
139 |
|
140 |
} |