Revision 486
removed more stuff
trunk/code/projects/colonet/robot/robot_dongle_test/robot_dongle_test.cpp | ||
---|---|---|
1 |
/** |
|
2 |
* Eugene Marinelli |
|
3 |
* 1/31/07 |
|
4 |
* |
|
5 |
* robot_dongle_test - trying to figure out how to get bytes from the robot. |
|
6 |
*/ |
|
7 |
|
|
8 |
#include <unistd.h> |
|
9 |
#include <stdio.h> |
|
10 |
#include <fcntl.h> |
|
11 |
|
|
12 |
int main() |
|
13 |
{ |
|
14 |
int fd = open("/dev/ttyS0", O_RDWR); |
|
15 |
char buf[80]; |
|
16 |
|
|
17 |
while(1){ |
|
18 |
//write(fd, "0", 1); |
|
19 |
if(read(fd, buf, 1)){ |
|
20 |
printf("%c\n", buf[0]); |
|
21 |
} |
|
22 |
} |
|
23 |
|
|
24 |
close(fd); |
|
25 |
|
|
26 |
return 0; |
|
27 |
} |
trunk/code/projects/colonet/robot/dongle/robot_receiver/wireless.c | ||
---|---|---|
1 |
/* |
|
2 |
Wireless |
|
3 |
Eugene Marinelli |
|
4 |
|
|
5 |
(See .h for details) |
|
6 |
*/ |
|
7 |
|
|
8 |
/* Includes */ |
|
9 |
#include <firefly+_lib.h> |
|
10 |
#include <string.h> |
|
11 |
#include <avr/interrupt.h> |
|
12 |
|
|
13 |
#include "wireless.h" |
|
14 |
|
|
15 |
/* Constants */ |
|
16 |
#define PREFIX0 'R' |
|
17 |
#define PREFIX1 'C' |
|
18 |
|
|
19 |
//wl_buf positions |
|
20 |
#define WL_SRC_LOC 2 |
|
21 |
#define WL_DEST_LOC 3 |
|
22 |
#define WL_DATA_START 4 |
|
23 |
|
|
24 |
#define WL_TIMEOUT 4 |
|
25 |
#define WL_TIMEOUT_INITIAL 4 |
|
26 |
|
|
27 |
/* Globals */ |
|
28 |
static char wl_buf[WL_PACKET_MAX_LEN]; |
|
29 |
static WL_Packet latest_packet; |
|
30 |
static WL_Packet tmp_packet; |
|
31 |
static char new_message; |
|
32 |
static char listener_addr; |
|
33 |
|
|
34 |
static int message_length; |
|
35 |
|
|
36 |
static unsigned int wl_index; |
|
37 |
static unsigned char wl_chksum; |
|
38 |
static unsigned char wl_to_flag; |
|
39 |
static unsigned int wl_to_count; |
|
40 |
|
|
41 |
static unsigned int wl_to_max; |
|
42 |
|
|
43 |
static char first_time; |
|
44 |
|
|
45 |
/* Internal function headers */ |
|
46 |
static void wl_timeout_handler(void); |
|
47 |
|
|
48 |
/* External function definitions */ |
|
49 |
int wl_init(int msg_len, char listener_address){ |
|
50 |
if(msg_len > WL_MSG_MAX_LEN){ |
|
51 |
printf("Error: %s: message length must be less than %d\n", __FUNCTION__, |
|
52 |
WL_MSG_MAX_LEN); |
|
53 |
return -1; |
|
54 |
} |
|
55 |
|
|
56 |
message_length = msg_len; |
|
57 |
|
|
58 |
listener_addr = listener_address; |
|
59 |
|
|
60 |
wl_index = 0; |
|
61 |
wl_chksum = 1; |
|
62 |
|
|
63 |
rtc_init(PRESCALE_DIV_128, 64, &wl_timeout_handler); |
|
64 |
/* executes the function about every 1/4 sec */ |
|
65 |
|
|
66 |
wl_to_flag = 0; |
|
67 |
wl_to_count = 0; |
|
68 |
|
|
69 |
first_time = 1; |
|
70 |
|
|
71 |
UCSR0B |= _BV(RXCIE) | _BV(RXEN); |
|
72 |
|
|
73 |
sei(); |
|
74 |
|
|
75 |
new_message = 0; |
|
76 |
memset(&latest_packet, 0, sizeof(WL_Packet)); |
|
77 |
|
|
78 |
return 0; |
|
79 |
} |
|
80 |
|
|
81 |
int wl_send(char* msg, char dest){ |
|
82 |
int i = 0; |
|
83 |
WL_Packet packet; |
|
84 |
|
|
85 |
if(strlen(msg) > WL_MSG_MAX_LEN){ |
|
86 |
printf("Error: %s: message is too long\n", __FUNCTION__); |
|
87 |
return -1; |
|
88 |
} |
|
89 |
|
|
90 |
wl_create_packet(msg, listener_addr, dest, &packet); |
|
91 |
|
|
92 |
serial_putchar(packet.prefix[0]); |
|
93 |
serial_putchar(packet.prefix[1]); |
|
94 |
serial_putchar(packet.src); |
|
95 |
serial_putchar(packet.dest); |
|
96 |
|
|
97 |
for(i = 0; i < message_length; i++){ |
|
98 |
serial_putchar(packet.msg[i]); |
|
99 |
} |
|
100 |
|
|
101 |
serial_putchar(packet.checksum); |
|
102 |
|
|
103 |
//printf("%x %x %x %x", packet.prefix[0], packet.prefix[1], packet.src, |
|
104 |
// packet.dest); |
|
105 |
|
|
106 |
return 0; |
|
107 |
} |
|
108 |
|
|
109 |
int wl_recv(char* msgbuf, char* src, char* dest){ |
|
110 |
if(!new_message){ |
|
111 |
return 0; //no new message |
|
112 |
} |
|
113 |
|
|
114 |
strcpy(msgbuf, latest_packet.msg); |
|
115 |
|
|
116 |
*src = latest_packet.src; |
|
117 |
*dest = latest_packet.dest; |
|
118 |
|
|
119 |
new_message = 0; |
|
120 |
|
|
121 |
return strlen(msgbuf); |
|
122 |
} |
|
123 |
|
|
124 |
char wl_get_checksum(WL_Packet* packet){ |
|
125 |
char checksum = 0; |
|
126 |
int i; |
|
127 |
|
|
128 |
checksum += packet->src; |
|
129 |
checksum += packet->dest; |
|
130 |
for(i = 0; i < message_length; i++) { |
|
131 |
checksum += packet->msg[i]; |
|
132 |
} |
|
133 |
|
|
134 |
return checksum; |
|
135 |
} |
|
136 |
|
|
137 |
int wl_create_packet(char* msg, char src, char dest, WL_Packet* packet){ |
|
138 |
packet->prefix[0] = 'R'; |
|
139 |
packet->prefix[1] = 'C'; |
|
140 |
|
|
141 |
packet->src = src; |
|
142 |
packet->dest = dest; |
|
143 |
|
|
144 |
memset(packet->msg, 0, message_length); |
|
145 |
strcpy(packet->msg, msg); |
|
146 |
|
|
147 |
packet->checksum = wl_get_checksum(packet); |
|
148 |
|
|
149 |
return 0; |
|
150 |
} |
|
151 |
|
|
152 |
/* Internal function definitions */ |
|
153 |
|
|
154 |
//use on new avr-libc |
|
155 |
//ISR(USART0_RX_vect) |
|
156 |
SIGNAL(SIG_USART0_RECV){ |
|
157 |
char wl_input = UDR0; |
|
158 |
|
|
159 |
/* LOCAL HACK!!! */ |
|
160 |
//orb_set_color(ORANGE); |
|
161 |
//printf("%x ", wl_input); |
|
162 |
lcd_putint(wl_input); |
|
163 |
|
|
164 |
if(wl_index == 0){ |
|
165 |
//first packet? - check chksum to make sure we're not beginning |
|
166 |
if(wl_input == PREFIX0) { |
|
167 |
wl_to_count = 0; //at this point we're not timing out |
|
168 |
wl_index = 1; |
|
169 |
} |
|
170 |
}else if(wl_index == 1){ |
|
171 |
if(wl_input != PREFIX1){ |
|
172 |
wl_index = 0; |
|
173 |
}else{ |
|
174 |
wl_index = WL_SRC_LOC; |
|
175 |
} |
|
176 |
}else if(wl_index == WL_SRC_LOC){ |
|
177 |
tmp_packet.src = wl_input; |
|
178 |
wl_index = WL_DEST_LOC; |
|
179 |
}else if(wl_index == WL_DEST_LOC){ |
|
180 |
tmp_packet.dest = wl_input; |
|
181 |
wl_index = WL_DATA_START; |
|
182 |
}else if(wl_index < WL_DATA_START + message_length){ |
|
183 |
//getting the rest of the packet |
|
184 |
tmp_packet.msg[wl_index-WL_DATA_START] = wl_input; |
|
185 |
wl_index++; |
|
186 |
|
|
187 |
//printf("%d: %d; %d\n", wl_index, wl_input, wl_chksum); |
|
188 |
}else if(wl_index == WL_DATA_START + message_length){ |
|
189 |
//we are at checksum location |
|
190 |
tmp_packet.checksum = wl_input; |
|
191 |
wl_chksum = wl_get_checksum(&tmp_packet); |
|
192 |
|
|
193 |
if(tmp_packet.checksum == wl_chksum) { |
|
194 |
//checksum is correct |
|
195 |
memcpy(&latest_packet, &tmp_packet, sizeof(WL_Packet)); |
|
196 |
new_message = 1; |
|
197 |
} else { //chksum failed |
|
198 |
serial1_putchar('F'); |
|
199 |
lcd_putchar('F'); |
|
200 |
|
|
201 |
printf("checksum failed - was %d, should have been %d\n", wl_input, |
|
202 |
wl_chksum); |
|
203 |
} |
|
204 |
|
|
205 |
wl_index = 0; |
|
206 |
} |
|
207 |
} |
|
208 |
|
|
209 |
|
|
210 |
/* |
|
211 |
\fn wl_timeout_handler |
|
212 |
Handles the timeout case. |
|
213 |
|
|
214 |
A time out occurs about every 1/4 second. This function is called each time one occurs. |
|
215 |
In the case where a robot first turns on, it will wait for a long time before declaring itself the leader. |
|
216 |
Otherwise, if enough time-outs occur in a row, wl_to_flag will be set to 1. This will cause a timeout packet |
|
217 |
to be sent next time around |
|
218 |
|
|
219 |
*/ |
|
220 |
static void wl_timeout_handler( void ) { |
|
221 |
//increment the total number of wireless time-outs |
|
222 |
wl_to_count++; |
|
223 |
|
|
224 |
static int wl_first_to_count = 0; |
|
225 |
|
|
226 |
//first time time out routine - listen for a long time |
|
227 |
if(first_time) { |
|
228 |
if(wl_to_count > WL_TIMEOUT_INITIAL) { |
|
229 |
//increment the counter for first timeouts |
|
230 |
wl_first_to_count++; |
|
231 |
|
|
232 |
//you don't actually want to send out a packet |
|
233 |
//-- you would confuse other robots |
|
234 |
wl_to_flag = 0; |
|
235 |
wl_to_count = 0; |
|
236 |
} |
|
237 |
} else { |
|
238 |
//not the first time - regular time out routine |
|
239 |
//if you have enough timeouts |
|
240 |
if(wl_to_count > WL_TIMEOUT) { |
|
241 |
//lcd_putchar('z'); |
|
242 |
|
|
243 |
//change the time-out flag to 1 |
|
244 |
wl_to_flag = 1; |
|
245 |
wl_to_count = 0; |
|
246 |
} |
|
247 |
} |
|
248 |
} |
trunk/code/projects/colonet/robot/dongle/robot_receiver/help.h | ||
---|---|---|
1 |
#include "serial.h" |
|
2 |
|
|
3 |
char helper(char c){ |
|
4 |
switch(c){ |
|
5 |
case 0x00: return '0'; |
|
6 |
case 0x01: return '1'; |
|
7 |
case 0x02: return '2'; |
|
8 |
case 0x03: return '3'; |
|
9 |
case 0x04: return '4'; |
|
10 |
case 0x05: return '5'; |
|
11 |
case 0x06: return '6'; |
|
12 |
case 0x07: return '7'; |
|
13 |
case 0x08: return '8'; |
|
14 |
case 0x09: return '9'; |
|
15 |
case 0x0A: return 'A'; |
|
16 |
case 0x0B: return 'B'; |
|
17 |
case 0x0C: return 'C'; |
|
18 |
case 0x0D: return 'D'; |
|
19 |
case 0x0E: return 'E'; |
|
20 |
case 0x0F: return 'F'; |
|
21 |
default: return '?'; |
|
22 |
} |
|
23 |
} |
|
24 |
|
|
25 |
void print_hex(char val){ |
|
26 |
char hgh = (val & 0xF0) >> 4; |
|
27 |
char low = (val & 0x0F); |
|
28 |
|
|
29 |
serial1_putchar(helper(hgh)); |
|
30 |
serial1_putchar(helper(low)); |
|
31 |
} |
trunk/code/projects/colonet/robot/dongle/robot_receiver/servo.c | ||
---|---|---|
1 |
/* |
|
2 |
servo.c - Contains functions and interrupts necessary to activate servos |
|
3 |
author: Tom Lauwers |
|
4 |
|
|
5 |
3/1/06 Iain |
|
6 |
Man, I don't know what a servo is. |
|
7 |
*/ |
|
8 |
#include <avr/io.h> |
|
9 |
#include <avr/interrupt.h> |
|
10 |
#include <servo.h> |
|
11 |
#include <dio.h> |
|
12 |
#include <lights.h> |
|
13 |
#include <lcd.h> |
|
14 |
//1677.7216 c/ms |
|
15 |
//i dunno must be 2^k |
|
16 |
//250 |
|
17 |
//9C40 ~ 20ms |
|
18 |
//#A000 ~ 20.48 but prolly wont work |
|
19 |
//div by 256: 6.55078125 c/ms 80 ~ 19.5ms |
|
20 |
#define SERVO_PERIOD 0x80 |
|
21 |
//needs be 1ms |
|
22 |
//is 1.22ms |
|
23 |
#define SERVO_CONSTANT 0x8 |
|
24 |
|
|
25 |
/* dirty dirty |
|
26 |
unsigned int servo_vals[4] = {0,0,0,0}; // Stores the set servo values |
|
27 |
int current_servo = 0; // Stores which servo is current in the interrupt routine |
|
28 |
*/ |
|
29 |
unsigned int servo_vals[8] = {0,0,0,0,0,0,0,0}; // Stores the set servo values |
|
30 |
int phase_time=0; |
|
31 |
int phase_base=0; |
|
32 |
int servos_on = 0; // Stores which servos are enabled |
|
33 |
int servos_enabled=0; //which are high |
|
34 |
int servo_flag = 0; // Stores whether or not servos are enabled |
|
35 |
|
|
36 |
/* Timer 1 output compare B interrupt. Does the following: |
|
37 |
Checks to see if the current servo is 4, in which case just need |
|
38 |
to set low servo 3's signal and return. |
|
39 |
Else, check to see if the current servo is enabled, and set it's signal high |
|
40 |
Then, set the previous servo's signal low |
|
41 |
Now, set the next output compare to occur for some time from now |
|
42 |
specified by the current servo's value |
|
43 |
*/ |
|
44 |
SIGNAL (SIG_OUTPUT_COMPARE3B) |
|
45 |
{ |
|
46 |
lcd_putchar('i'); |
|
47 |
if(servo_flag == 0) |
|
48 |
return; |
|
49 |
|
|
50 |
int i; |
|
51 |
if(phase_time == 0){ |
|
52 |
lcd_putchar('p'); |
|
53 |
for(i=0;i<8;i++){ |
|
54 |
if(servos_on & _BV(i)) |
|
55 |
digital_output((_PORT_E << 3) +i,1); |
|
56 |
} |
|
57 |
servos_enabled = servos_on; |
|
58 |
phase_base = OCR3B; |
|
59 |
} |
|
60 |
|
|
61 |
unsigned int min=~0; |
|
62 |
for(i=0;i<8;i++){ |
|
63 |
if(_BV(i) & servos_enabled){ |
|
64 |
if(SERVO_CONSTANT*servo_vals[i] <= phase_time){ |
|
65 |
digital_output((_PORT_E << 3) + i,0); |
|
66 |
servos_enabled &= ~_BV(i); |
|
67 |
} |
|
68 |
else if(SERVO_CONSTANT*servo_vals[i] < min) { |
|
69 |
min = servo_vals[i]; |
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
if(!servos_enabled){ |
|
74 |
OCR3B = phase_base + SERVO_PERIOD; |
|
75 |
phase_time = 0; |
|
76 |
} |
|
77 |
else { |
|
78 |
OCR3B = phase_base + min; |
|
79 |
phase_time = min; |
|
80 |
} |
|
81 |
|
|
82 |
} |
|
83 |
|
|
84 |
void init_servo() |
|
85 |
{ |
|
86 |
//1010 1001 |
|
87 |
TCCR3A = 0; |
|
88 |
//0000 0100 |
|
89 |
TCCR3B = 0x4; //prescaler to 256 |
|
90 |
TCCR3C=0; |
|
91 |
lcd_init(); |
|
92 |
ETIMSK |= 0x08; |
|
93 |
servo_flag = 1; |
|
94 |
OCR3B = 0x0; |
|
95 |
lcd_putchar('i'); |
|
96 |
} |
|
97 |
|
|
98 |
// Enable a servo specified by config |
|
99 |
void enable_servo(int config) |
|
100 |
{ |
|
101 |
servos_on |= _BV(config); |
|
102 |
} |
|
103 |
|
|
104 |
// Disable a servo specified by config |
|
105 |
void disable_servo(int config) |
|
106 |
{ |
|
107 |
servos_on &= (0xFF-_BV(config)); |
|
108 |
} |
|
109 |
|
|
110 |
// Disables the timer1 interrupt, disabling the servos |
|
111 |
void disable_servos() |
|
112 |
{ |
|
113 |
// Disables interrupts |
|
114 |
ETIMSK &= ~0x08; |
|
115 |
servo_flag=0; |
|
116 |
|
|
117 |
} |
|
118 |
|
|
119 |
// Enables the timer1 interrupt, enabling the servos |
|
120 |
void enable_servos() |
|
121 |
{ |
|
122 |
// Enable interrupts on output compare B |
|
123 |
ETIMSK |= 0x08; |
|
124 |
servo_flag=1; |
|
125 |
} |
|
126 |
// Set a servo to a value. Automatically enables the servo. |
|
127 |
void set_servo(int servo, unsigned int value) |
|
128 |
{ |
|
129 |
enable_servo(servo); |
|
130 |
servo_vals[servo] = value; |
|
131 |
} |
|
132 |
|
trunk/code/projects/colonet/robot/dongle/robot_receiver/lights.h | ||
---|---|---|
1 |
/* |
|
2 |
lights.h |
|
3 |
|
|
4 |
most of this is shamelessly copied from FWR's orb.h (Tom Lauwers and Steven Shamlian) |
|
5 |
|
|
6 |
author: CMU Robotics Club, Colony Project |
|
7 |
|
|
8 |
*/ |
|
9 |
|
|
10 |
#ifndef _LIGHTS_H_ |
|
11 |
#define _LIGHTS_H_ |
|
12 |
|
|
13 |
//user LED |
|
14 |
#ifdef FFPP |
|
15 |
|
|
16 |
#define USERLED 15 |
|
17 |
#define USERLED1 15 |
|
18 |
#define USERLED2 31 |
|
19 |
|
|
20 |
#else |
|
21 |
|
|
22 |
#define USERLED PING2 |
|
23 |
#define USERLED1 PING2 |
|
24 |
#define USERLED2 PING2 //only have 1 |
|
25 |
|
|
26 |
//LEDs are on bank E |
|
27 |
#define REDLED PE3 |
|
28 |
#define GREENLED PE4 |
|
29 |
#define BLUELED PE5 |
|
30 |
|
|
31 |
#endif |
|
32 |
|
|
33 |
//ORB |
|
34 |
#define RED 0xE0 |
|
35 |
#define ORANGE 0xE8 |
|
36 |
#define YELLOW 0xFC |
|
37 |
#define LIME 0x7C |
|
38 |
#define GREEN 0x1C |
|
39 |
#define CYAN 0x1F |
|
40 |
#define BLUE 0x03 |
|
41 |
#define PINK 0x63 |
|
42 |
#define PURPLE 0x23 |
|
43 |
#define MAGENTA 0xE3 |
|
44 |
#define WHITE 0xFF |
|
45 |
#define ORB_OFF 0x00 |
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
//? |
|
50 |
#define COUNT_START 0x8000 |
|
51 |
|
|
52 |
|
|
53 |
//user LED |
|
54 |
void led_init( void ); |
|
55 |
void led_user(int value); |
|
56 |
|
|
57 |
|
|
58 |
// For function descriptions see orb.c |
|
59 |
void orb_init(void); |
|
60 |
void orb_set(unsigned int red_led, unsigned int green_led, unsigned int blue_led); |
|
61 |
void orb_set_color(int col); |
|
62 |
void orb_disable(void); |
|
63 |
void orb_enable(void); |
|
64 |
|
|
65 |
void orb_set_dio(int red, int green, int blue); |
|
66 |
|
|
67 |
#ifdef FFPP |
|
68 |
int orbs[32]; |
|
69 |
|
|
70 |
//send now |
|
71 |
void orb_set_num(unsigned char num, unsigned int red_led, unsigned int green_led, unsigned int blue_led); |
|
72 |
|
|
73 |
//no send |
|
74 |
void orb_set_num_ns(unsigned char num, unsigned int red_led, unsigned int green_led, unsigned int blue_led); |
|
75 |
|
|
76 |
//force send |
|
77 |
void orb_send(void); |
|
78 |
|
|
79 |
void tlc5940test(void); |
|
80 |
|
|
81 |
void tlc_clock1(int data);//may not need these 2 |
|
82 |
void tlc_clock2(int data); |
|
83 |
|
|
84 |
void tlc_send(void); |
|
85 |
void tlc_latch(void); |
|
86 |
|
|
87 |
|
|
88 |
void tlc5940init(void); |
|
89 |
#endif |
|
90 |
|
|
91 |
#endif |
trunk/code/projects/colonet/robot/dongle/robot_receiver/wireless.h | ||
---|---|---|
1 |
#ifndef WIRELESS_H |
|
2 |
#define WIRELESS_H |
|
3 |
|
|
4 |
/* |
|
5 |
Wireless - wireless functions for Colony |
|
6 |
|
|
7 |
Eugene Marinelli |
|
8 |
7/22/06 |
|
9 |
*/ |
|
10 |
|
|
11 |
#define WL_MSG_MAX_LEN 16 |
|
12 |
#define WL_PACKET_MAX_LEN (WL_MSG_MAX_LEN+5) |
|
13 |
|
|
14 |
#define GLOBAL_DEST 200 |
|
15 |
|
|
16 |
typedef struct { |
|
17 |
char prefix[2]; |
|
18 |
char src; |
|
19 |
char dest; |
|
20 |
char msg[WL_MSG_MAX_LEN]; |
|
21 |
char checksum; |
|
22 |
} WL_Packet; |
|
23 |
|
|
24 |
int wl_init(int msg_len, char listener_address); |
|
25 |
int wl_send(char* msg, char dest); |
|
26 |
int wl_recv(char* msgbuf, char* src, char* dest); |
|
27 |
// get most recent valid message - implement message queue later |
|
28 |
|
|
29 |
int wl_create_packet(char* msg, char src, char dest, WL_Packet* packet); |
|
30 |
char wl_get_checksum(WL_Packet* packet); |
|
31 |
|
|
32 |
#endif |
trunk/code/projects/colonet/robot/dongle/robot_receiver/lcd.c | ||
---|---|---|
1 |
/* |
|
2 |
|
|
3 |
lcd-ar2.c - implementation for lcd functions |
|
4 |
|
|
5 |
Author: CMU Robotics Club, Colony Project |
|
6 |
|
|
7 |
This uses SPI. |
|
8 |
|
|
9 |
lcd_init MUST be called before anything can be used. |
|
10 |
|
|
11 |
*/ |
|
12 |
|
|
13 |
#include <avr/io.h> |
|
14 |
#include <lcd.h> |
|
15 |
#include <time.h> |
|
16 |
|
|
17 |
#include "oled.h" |
|
18 |
|
|
19 |
/* |
|
20 |
FontLookup - a lookup table for all characters |
|
21 |
*/ |
|
22 |
static const unsigned char FontLookup [][5] = |
|
23 |
{ |
|
24 |
{ 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp |
|
25 |
{ 0x00, 0x00, 0x5f, 0x00, 0x00 }, // ! |
|
26 |
{ 0x00, 0x07, 0x00, 0x07, 0x00 }, // " |
|
27 |
{ 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // # |
|
28 |
{ 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $ |
|
29 |
{ 0x23, 0x13, 0x08, 0x64, 0x62 }, // % |
|
30 |
{ 0x36, 0x49, 0x55, 0x22, 0x50 }, // & |
|
31 |
{ 0x00, 0x05, 0x03, 0x00, 0x00 }, // ' |
|
32 |
{ 0x00, 0x1c, 0x22, 0x41, 0x00 }, // ( |
|
33 |
{ 0x00, 0x41, 0x22, 0x1c, 0x00 }, // ) |
|
34 |
{ 0x14, 0x08, 0x3E, 0x08, 0x14 }, // * |
|
35 |
{ 0x08, 0x08, 0x3E, 0x08, 0x08 }, // + |
|
36 |
{ 0x00, 0x00, 0x50, 0x30, 0x00 }, // , |
|
37 |
{ 0x10, 0x10, 0x10, 0x10, 0x10 }, // - |
|
38 |
{ 0x00, 0x60, 0x60, 0x00, 0x00 }, // . |
|
39 |
{ 0x20, 0x10, 0x08, 0x04, 0x02 }, // / |
|
40 |
{ 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0 |
|
41 |
{ 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1 |
|
42 |
{ 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2 |
|
43 |
{ 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3 |
|
44 |
{ 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4 |
|
45 |
{ 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5 |
|
46 |
{ 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6 |
|
47 |
{ 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7 |
|
48 |
{ 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8 |
|
49 |
{ 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9 |
|
50 |
{ 0x00, 0x36, 0x36, 0x00, 0x00 }, // : |
|
51 |
{ 0x00, 0x56, 0x36, 0x00, 0x00 }, // ; |
|
52 |
{ 0x08, 0x14, 0x22, 0x41, 0x00 }, // < |
|
53 |
{ 0x14, 0x14, 0x14, 0x14, 0x14 }, // = |
|
54 |
{ 0x00, 0x41, 0x22, 0x14, 0x08 }, // > |
|
55 |
{ 0x02, 0x01, 0x51, 0x09, 0x06 }, // ? |
|
56 |
{ 0x32, 0x49, 0x59, 0x51, 0x3E }, // @ |
|
57 |
{ 0x7E, 0x11, 0x11, 0x11, 0x7E }, // A |
|
58 |
{ 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B |
|
59 |
{ 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C |
|
60 |
{ 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D |
|
61 |
{ 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E |
|
62 |
{ 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F |
|
63 |
{ 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G |
|
64 |
{ 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H |
|
65 |
{ 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I |
|
66 |
{ 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J |
|
67 |
{ 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K |
|
68 |
{ 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L |
|
69 |
{ 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M |
|
70 |
{ 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N |
|
71 |
{ 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O |
|
72 |
{ 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P |
|
73 |
{ 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q |
|
74 |
{ 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R |
|
75 |
{ 0x46, 0x49, 0x49, 0x49, 0x31 }, // S |
|
76 |
{ 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T |
|
77 |
{ 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U |
|
78 |
{ 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V |
|
79 |
{ 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W |
|
80 |
{ 0x63, 0x14, 0x08, 0x14, 0x63 }, // X |
|
81 |
{ 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y |
|
82 |
{ 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z |
|
83 |
{ 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [ |
|
84 |
{ 0x02, 0x04, 0x08, 0x10, 0x20 }, // backslash |
|
85 |
{ 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ] |
|
86 |
{ 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^ |
|
87 |
{ 0x40, 0x40, 0x40, 0x40, 0x40 }, // _ |
|
88 |
{ 0x00, 0x01, 0x02, 0x04, 0x00 }, // ' |
|
89 |
{ 0x20, 0x54, 0x54, 0x54, 0x78 }, // a |
|
90 |
{ 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b |
|
91 |
{ 0x38, 0x44, 0x44, 0x44, 0x20 }, // c |
|
92 |
{ 0x38, 0x44, 0x44, 0x48, 0x7F }, // d |
|
93 |
{ 0x38, 0x54, 0x54, 0x54, 0x18 }, // e |
|
94 |
{ 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f |
|
95 |
{ 0x0C, 0x52, 0x52, 0x52, 0x3E }, // g |
|
96 |
{ 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h |
|
97 |
{ 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i |
|
98 |
{ 0x20, 0x40, 0x44, 0x3D, 0x00 }, // j |
|
99 |
{ 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k |
|
100 |
{ 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l |
|
101 |
{ 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m |
|
102 |
{ 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n |
|
103 |
{ 0x38, 0x44, 0x44, 0x44, 0x38 }, // o |
|
104 |
{ 0x7C, 0x14, 0x14, 0x14, 0x08 }, // p |
|
105 |
{ 0x08, 0x14, 0x14, 0x18, 0x7C }, // q |
|
106 |
{ 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r |
|
107 |
{ 0x48, 0x54, 0x54, 0x54, 0x20 }, // s |
|
108 |
{ 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t |
|
109 |
{ 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u |
|
110 |
{ 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v |
|
111 |
{ 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w |
|
112 |
{ 0x44, 0x28, 0x10, 0x28, 0x44 }, // x |
|
113 |
{ 0x0C, 0x50, 0x50, 0x50, 0x3C }, // y |
|
114 |
{ 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z |
|
115 |
{ 0x00, 0x08, 0x36, 0x41, 0x41 }, // { |
|
116 |
{ 0x00, 0x00, 0x7F, 0x00, 0x00 }, // | |
|
117 |
{ 0x41, 0x41, 0x36, 0x08, 0x00 }, // } |
|
118 |
{ 0x02, 0x01, 0x01, 0x02, 0x01 }, // ~ |
|
119 |
{ 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // del |
|
120 |
}; |
|
121 |
|
|
122 |
|
|
123 |
/* |
|
124 |
initializes the LCD |
|
125 |
*/ |
|
126 |
void lcd_init(void) |
|
127 |
{ |
|
128 |
|
|
129 |
#ifdef FFPP |
|
130 |
|
|
131 |
OLED_init(); |
|
132 |
|
|
133 |
#else |
|
134 |
|
|
135 |
LCDDDR |= (D_C | SCE | SDI | SCK); |
|
136 |
LCDRESETDDR |= RST; |
|
137 |
|
|
138 |
LCDPORT &= ~(D_C | SCE | SDI | SCK); |
|
139 |
|
|
140 |
SPCR |= 0b01010000; // no SPI int, SPI en, Master, sample on rising edge, fosc/2 |
|
141 |
SPSR |= 0x01; // a continuation of the above |
|
142 |
|
|
143 |
LCDRESETPORT |= RST; |
|
144 |
delay_ms(10); |
|
145 |
LCDRESETPORT &= (~RST); |
|
146 |
delay_ms(100); |
|
147 |
LCDRESETPORT |= RST; |
|
148 |
|
|
149 |
lcd_putbyte( 0x21 ); // LCD Extended Commands. |
|
150 |
lcd_putbyte( 0xC8 ); // Set LCD Vop (Contrast). |
|
151 |
lcd_putbyte( 0x06 ); // Set Temp coefficent. |
|
152 |
lcd_putbyte( 0x13 ); // LCD bias mode 1:48. |
|
153 |
lcd_putbyte( 0x20 ); // LCD Standard Commands, Horizontal addressing mode. |
|
154 |
lcd_putbyte( 0x0C ); // LCD in normal mode. |
|
155 |
|
|
156 |
LCDPORT |= D_C; //put it in init instead of main |
|
157 |
|
|
158 |
lcd_clear_screen(); |
|
159 |
#endif |
|
160 |
} |
|
161 |
|
|
162 |
/* |
|
163 |
clear the lcd screen |
|
164 |
*/ |
|
165 |
void lcd_clear_screen( void ) { |
|
166 |
|
|
167 |
#ifdef FFPP |
|
168 |
drawclear(); |
|
169 |
#else |
|
170 |
int i; |
|
171 |
for (i = 0; i < 504; i++) |
|
172 |
lcd_putbyte(0x0); |
|
173 |
|
|
174 |
lcd_gotoxy(0,0); |
|
175 |
#endif |
|
176 |
|
|
177 |
} |
|
178 |
|
|
179 |
|
|
180 |
/* |
|
181 |
print a byte on the lcd screen |
|
182 |
*/ |
|
183 |
void lcd_putbyte(unsigned char b) |
|
184 |
{ |
|
185 |
|
|
186 |
#ifndef FFPP |
|
187 |
SPDR = b; |
|
188 |
while (!(SPSR & 0x80)); /* Wait until SPI transaction is complete */ |
|
189 |
#endif |
|
190 |
|
|
191 |
} |
|
192 |
|
|
193 |
/* |
|
194 |
print a character on the lcd |
|
195 |
*/ |
|
196 |
void lcd_putchar(char c) |
|
197 |
{ |
|
198 |
|
|
199 |
#ifndef FFPP |
|
200 |
int i; |
|
201 |
|
|
202 |
for (i = 0; i < 5; i++) |
|
203 |
lcd_putbyte(FontLookup[c-32][i]); |
|
204 |
lcd_putbyte(0); |
|
205 |
#endif |
|
206 |
|
|
207 |
} |
|
208 |
|
|
209 |
/* |
|
210 |
print an entire string to the lcd |
|
211 |
*/ |
|
212 |
void lcd_putstr(char *s) |
|
213 |
{ |
|
214 |
char *t = s; |
|
215 |
while (*t != 0) |
|
216 |
{ |
|
217 |
lcd_putchar(*t); |
|
218 |
t++; |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
/* |
|
223 |
go to coordinate x, y |
|
224 |
y: vertically - 1 char |
|
225 |
x: horizontally - 1 pixel |
|
226 |
|
|
227 |
multiply x by 6 if want to move 1 entire character |
|
228 |
|
|
229 |
origin (0,0) is at top left corner of lcd screen |
|
230 |
*/ |
|
231 |
void lcd_gotoxy(int x, int y) |
|
232 |
{ |
|
233 |
|
|
234 |
#ifndef FFPP |
|
235 |
LCDPORT &= ~(D_C); |
|
236 |
lcd_putbyte(0x40 | (y & 0x07)); |
|
237 |
lcd_putbyte(0x80 | (x & 0x7f)); |
|
238 |
LCDPORT |= D_C; |
|
239 |
#endif |
|
240 |
|
|
241 |
} |
|
242 |
|
|
243 |
/* |
|
244 |
prints an int to the lcd |
|
245 |
|
|
246 |
code adapted from Chris Efstathiou's code (hendrix@otenet.gr) |
|
247 |
*/ |
|
248 |
void lcd_putint(int value ) { |
|
249 |
unsigned char lcd_data[6]={'0','0','0','0','0','0' }, position=sizeof(lcd_data), radix=10; |
|
250 |
|
|
251 |
/* convert int to ascii */ |
|
252 |
if(value<0) { lcd_putchar('-'); value=-value; } |
|
253 |
do { position--; *(lcd_data+position)=(value%radix)+'0'; value/=radix; } while(value); |
|
254 |
|
|
255 |
|
|
256 |
/* start displaying the number */ |
|
257 |
for(;position<=(sizeof(lcd_data)-1);position++) |
|
258 |
{ |
|
259 |
|
|
260 |
lcd_putchar(lcd_data[position]); |
|
261 |
} |
|
262 |
|
|
263 |
return; |
|
264 |
} |
trunk/code/projects/colonet/robot/dongle/robot_receiver/follow.c | ||
---|---|---|
1 |
|
|
2 |
#include "firefly+_lib.h" |
|
3 |
#include "follow.h" |
|
4 |
|
|
5 |
#define FWD 0 |
|
6 |
#define BCK 1 |
|
7 |
|
|
8 |
#define TOTAL 16 |
|
9 |
|
|
10 |
|
|
11 |
/* |
|
12 |
int find_max |
|
13 |
returns the index of the BOM with the highest reading |
|
14 |
* note: index, not value |
|
15 |
*/ |
|
16 |
|
|
17 |
void seek(int relative_position) |
|
18 |
{ |
|
19 |
int speed = 255; |
|
20 |
|
|
21 |
//MOTOR1 - left |
|
22 |
//MOTOR2 - right |
|
23 |
|
|
24 |
if(relative_position < 5 || relative_position > 13) |
|
25 |
{ //turn right |
|
26 |
motor2_set(BCK, 0); |
|
27 |
motor1_set(FWD, speed); |
|
28 |
} |
|
29 |
else if(relative_position > 5 && relative_position <= 13) |
|
30 |
{ //turn left |
|
31 |
motor2_set(FWD, speed); |
|
32 |
motor1_set(BCK, 0); |
|
33 |
} |
|
34 |
else |
|
35 |
{ //go forward |
|
36 |
//turn_off_motors(); |
|
37 |
motor1_set(FWD, speed); |
|
38 |
motor2_set(FWD, speed); |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
void drive_forward (void) |
|
43 |
{ |
|
44 |
int speed = 255; |
|
45 |
motor1_set(FWD, speed); |
|
46 |
motor2_set(FWD, speed); |
|
47 |
} |
trunk/code/projects/colonet/robot/dongle/robot_receiver/servo.h | ||
---|---|---|
1 |
/* |
|
2 |
servo.h - Contains function prototypes for servo activation |
|
3 |
author: Tom Lauwers |
|
4 |
*/ |
|
5 |
#ifndef _SERVO_H_ |
|
6 |
#define _SERVO_H_ |
|
7 |
|
|
8 |
// Information on functions available in servo.c |
|
9 |
void init_servo(void); |
|
10 |
void enable_servos(void); |
|
11 |
void enable_servo(int config); |
|
12 |
void set_servo(int servo, unsigned int value); |
|
13 |
void disable_servos(void); |
|
14 |
void disable_servo(int ); |
|
15 |
|
|
16 |
#endif |
trunk/code/projects/colonet/robot/dongle/robot_receiver/follow.h | ||
---|---|---|
1 |
|
|
2 |
#ifndef _FOLLOW_H |
|
3 |
#define _FOLLOW_H |
|
4 |
|
|
5 |
|
|
6 |
void seek(int velocity); |
|
7 |
void drive_forward (void); |
|
8 |
|
|
9 |
#endif |
trunk/code/projects/colonet/robot/dongle/robot_receiver/lcd.h | ||
---|---|---|
1 |
#ifndef _LCD_H_ |
|
2 |
#define _LCD_H_ |
|
3 |
|
|
4 |
#ifndef FFPP |
|
5 |
|
|
6 |
//////lcd defines |
|
7 |
#define RST 0x04 // pe2 (GPIO) |
|
8 |
#define SCE 0x01 // pb0 (~SS) |
|
9 |
#define D_C 0x10 // pb4 (GPIO?) |
|
10 |
#define SDI 0x04 // pb2 (MOSI) |
|
11 |
#define SCK 0x02 // pb1 (SCK) |
|
12 |
|
|
13 |
#define LCDPORT PORTB |
|
14 |
#define LCDDDR DDRB |
|
15 |
|
|
16 |
#define LCDRESETPORT PORTE |
|
17 |
#define LCDRESETDDR DDRE |
|
18 |
|
|
19 |
#else |
|
20 |
|
|
21 |
|
|
22 |
// other OLED pins |
|
23 |
//#define LCD_RW 0x00000080 |
|
24 |
#define LCD_RS PB4 // Command/Data |
|
25 |
//#define LCD_RD 0x00008000 |
|
26 |
#define LCD_RSTB PE2 // reset line |
|
27 |
#define LCD_CS PB0 |
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
// inialize OLED and SPI |
|
32 |
void OLED_init(void); |
|
33 |
|
|
34 |
// reset Controller |
|
35 |
void Reset_SSD1339(void); |
|
36 |
|
|
37 |
// write command or data |
|
38 |
void write_c(unsigned char out_command); |
|
39 |
void write_d(unsigned char out_data); |
|
40 |
|
|
41 |
// these write data to the OLED on 8 bit data bus, depends on MCU |
|
42 |
void LCD_out(unsigned char cmd); |
|
43 |
|
|
44 |
// these functions set / clear pins for OLED control lines. they accecpt a 0 or 1 |
|
45 |
//void RD(char stat); |
|
46 |
//void RW(char stat); |
|
47 |
void DC(char stat); |
|
48 |
void RES(char stat); |
|
49 |
void CS(char stat); |
|
50 |
|
|
51 |
void drawcircle(char col, char row, char radius, int lcolor, int fcolor); |
|
52 |
void drawrect(char scol, char srow, char ecol, char erow, int lcolor, int fcolor); |
|
53 |
void drawline(char scol, char srow, char ecol, char erow, int lcolor); |
|
54 |
void drawcopy(char scol, char srow, char ecol, char erow, char ncol, char nrow); |
|
55 |
void drawfill(char fill); |
|
56 |
|
|
57 |
void drawclearwindow(char scol, char srow, char ecol, char erow); |
|
58 |
inline void drawclear(void); |
|
59 |
|
|
60 |
void drawscroll(char hoff, char srow, char rows, char speed); |
|
61 |
inline void drawscrollstart(void); |
|
62 |
inline void drawscrollstop(void); |
|
63 |
|
|
64 |
void drawIR(void); |
|
65 |
|
|
66 |
void crazycircle (void); |
|
67 |
void crazyrect (void) ; |
|
68 |
|
|
69 |
void OLEDtest (void) ; |
|
70 |
|
|
71 |
|
|
72 |
#endif |
|
73 |
|
|
74 |
void lcd_init(void); |
|
75 |
void lcd_clear_screen( void ); |
|
76 |
void lcd_putbyte(unsigned char c); |
|
77 |
void lcd_putchar(char c); |
|
78 |
void lcd_putstr(char *s); |
|
79 |
void lcd_gotoxy(int x, int y); |
|
80 |
void lcd_putint(int value); |
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
#endif |
trunk/code/projects/colonet/robot/dongle/robot_receiver/robot_receiver.c | ||
---|---|---|
1 |
/* |
|
2 |
Eugene Marinelli |
|
3 |
7/22/06 |
|
4 |
*/ |
|
5 |
|
|
6 |
/* Includes */ |
|
7 |
#include <stdio.h> |
|
8 |
#include <string.h> |
|
9 |
|
|
10 |
#include <firefly+_lib.h> |
|
11 |
#include "pindefs_ff.h" |
|
12 |
|
|
13 |
#include <wireless.h> |
|
14 |
|
|
15 |
void init_hardware(void); |
|
16 |
|
|
17 |
/* Main */ |
|
18 |
int main(void){ |
|
19 |
char buf[80]; |
|
20 |
char src, dest; |
|
21 |
|
|
22 |
init_hardware(); |
|
23 |
wl_init(15,1); |
|
24 |
|
|
25 |
orb_set_color(BLUE); |
|
26 |
|
|
27 |
while(1){ |
|
28 |
if(wl_recv(buf, &src, &dest)){ |
|
29 |
printf("%s", buf); |
|
30 |
orb_set_color(RED); |
|
31 |
}else{ |
|
32 |
printf("."); |
|
33 |
orb_set_color(BLUE); |
|
34 |
} |
|
35 |
|
|
36 |
wl_send("ABC", 200); |
|
37 |
|
|
38 |
delay_ms(500); |
|
39 |
} |
|
40 |
|
|
41 |
return 0; |
|
42 |
} |
|
43 |
|
|
44 |
void init_hardware(){ |
|
45 |
motors_init(); |
|
46 |
orb_init(); |
|
47 |
led_init(); |
|
48 |
analog_init(); |
|
49 |
|
|
50 |
serial_init(BAUD9600); |
|
51 |
serial1_init(BAUD115200); |
|
52 |
lcd_init(); |
|
53 |
|
|
54 |
fdevopen(&serial1_putchar, &serial1_getchar); |
|
55 |
} |
trunk/code/projects/colonet/robot/dongle/robot_receiver/dio.c | ||
---|---|---|
1 |
/* |
|
2 |
|
|
3 |
dio.c |
|
4 |
Controls digital input and output |
|
5 |
|
|
6 |
A general note on how this code works: |
|
7 |
portpin is used to select both the bank and which pin is selected |
|
8 |
6 bits are used (lower 6, ex: 0b00abcdef) |
|
9 |
the first 3 (abc in this example) are used to select the bank |
|
10 |
A = 001 |
|
11 |
B = 010 |
|
12 |
C = 011 |
|
13 |
D = 100 |
|
14 |
E = 101 |
|
15 |
F = 110 |
|
16 |
G = 111 |
|
17 |
|
|
18 |
the bank can be found by doing portpin >> 3 |
|
19 |
|
|
20 |
the next 3 (def in this example) are used to select the pin number |
|
21 |
the 3 bits are just the binary representation of the pin number |
|
22 |
0 = 000 |
|
23 |
1 = 001 |
|
24 |
2 = 010 |
|
25 |
3 = 011 |
|
26 |
4 = 100 |
|
27 |
5 = 101 |
|
28 |
6 = 110 |
|
29 |
7 = 111 |
|
30 |
|
|
31 |
the pin number can be found by doing portping & 0b111 |
|
32 |
|
|
33 |
|
|
34 |
*/ |
|
35 |
|
|
36 |
#include <avr/interrupt.h> |
|
37 |
|
|
38 |
#include <dio.h> |
|
39 |
#include <time.h> |
|
40 |
#include <lights.h> |
|
41 |
/* |
|
42 |
digital_input |
|
43 |
|
|
44 |
reads the value on the selected portpin, returns it as 1 or 0 |
|
45 |
see general description (above) for definition of portpin |
|
46 |
|
|
47 |
*/ |
|
48 |
int digital_input(int portpin){ |
|
49 |
int pin = portpin & 0x7; |
|
50 |
int pin_val = 0; |
|
51 |
|
|
52 |
switch(portpin >> 3){ |
|
53 |
case _PORT_A: |
|
54 |
DDRA &= ~_BV(pin); |
|
55 |
pin_val = PINA; |
|
56 |
return (pin_val >> pin) & 1; |
|
57 |
case _PORT_B: |
|
58 |
DDRB &= ~_BV(pin); |
|
59 |
pin_val = PINB; |
|
60 |
return (pin_val >> pin) & 1; |
|
61 |
case _PORT_C: |
|
62 |
DDRC &= ~_BV(pin); |
|
63 |
pin_val = PINC; |
|
64 |
return (pin_val >> pin) & 1; |
|
65 |
case _PORT_D: |
|
66 |
DDRD &= ~_BV(pin); |
|
67 |
pin_val = PIND; |
|
68 |
return (pin_val >> pin) & 1; |
|
69 |
case _PORT_E: |
|
70 |
DDRE &= ~_BV(pin); |
|
71 |
pin_val = PINE; |
|
72 |
return (pin_val >> pin) & 1; |
|
73 |
case _PORT_F: |
|
74 |
if(pin>=4){ |
|
75 |
MCUSR|=1<<7; |
|
76 |
MCUSR|=1<<7; |
|
77 |
} |
|
78 |
DDRF &= ~_BV(pin); |
|
79 |
pin_val = PINF; |
|
80 |
return (pin_val >> pin) & 1; |
|
81 |
case _PORT_G: |
|
82 |
DDRG &= ~_BV(pin); |
|
83 |
pin_val = PING; |
|
84 |
return (pin_val >> pin) & 1; |
|
85 |
default: break; |
|
86 |
} |
|
87 |
|
|
88 |
return -1; |
|
89 |
} |
|
90 |
|
|
91 |
/* |
|
92 |
digital_pull_up |
|
93 |
Enables pullup on a pin. if pin is output, it will make it output 1. |
|
94 |
|
|
95 |
*/ |
|
96 |
void digital_pull_up(int portpin) { |
|
97 |
int pins = portpin & 0x07; |
|
98 |
|
|
99 |
|
|
100 |
switch(portpin >> 3) { |
|
101 |
case _PORT_A: |
|
102 |
PORTA |= _BV(pins); |
|
103 |
break; |
|
104 |
case _PORT_B: |
|
105 |
PORTB |= _BV(pins); |
|
106 |
break; |
|
107 |
case _PORT_C: |
|
108 |
PORTC |= _BV(pins); |
|
109 |
break; |
|
110 |
case _PORT_D: |
|
111 |
PORTD |= _BV(pins); |
|
112 |
break; |
|
113 |
case _PORT_E: |
|
114 |
PORTE |= _BV(pins); |
|
115 |
break; |
|
116 |
case _PORT_F: |
|
117 |
PORTF |= _BV(pins); |
|
118 |
break; |
|
119 |
case _PORT_G: |
|
120 |
PORTG |= _BV(pins); |
|
121 |
break; |
|
122 |
} |
|
123 |
|
|
124 |
} |
|
125 |
|
|
126 |
/* |
|
127 |
digital_output |
|
128 |
|
|
129 |
sets portpin to the value |
|
130 |
|
|
131 |
see general description above for explanation of portpin |
|
132 |
|
|
133 |
val can only be 0 for off, nonzero for on |
|
134 |
*/ |
|
135 |
void digital_output(int portpin, int val) { |
|
136 |
int pins = portpin & 0x07; |
|
137 |
|
|
138 |
/* |
|
139 |
if you want to set to 0 |
|
140 |
*/ |
|
141 |
if(val == 0) { |
|
142 |
switch(portpin >> 3) { |
|
143 |
case _PORT_A: |
|
144 |
DDRA |= _BV(pins); |
|
145 |
PORTA &= (0XFF - _BV(pins)); |
|
146 |
break; |
|
147 |
case _PORT_B: |
|
148 |
DDRB |= _BV(pins); |
|
149 |
PORTB &= (0XFF - _BV(pins)); |
|
150 |
break; |
|
151 |
case _PORT_C: |
|
152 |
DDRC |= _BV(pins); |
|
153 |
PORTC &= (0XFF - _BV(pins)); |
|
154 |
break; |
|
155 |
case _PORT_D: |
|
156 |
DDRD |= _BV(pins); |
|
157 |
PORTD &= (0XFF - _BV(pins)); |
|
158 |
break; |
|
159 |
case _PORT_E: |
|
160 |
DDRE |= _BV(pins); |
|
161 |
PORTE &= (0XFF - _BV(pins)); |
|
162 |
break; |
|
163 |
case _PORT_F: |
|
164 |
DDRF |= _BV(pins); |
|
165 |
PORTF &= (0XFF - _BV(pins)); |
|
166 |
break; |
|
167 |
case _PORT_G: |
|
168 |
DDRG |= _BV(pins); |
|
169 |
PORTG &= (0XFF - _BV(pins)); |
|
170 |
break; |
|
171 |
} |
|
172 |
} |
|
173 |
else { /* ( val == 1) */ |
|
174 |
switch(portpin >> 3) { |
|
175 |
case _PORT_A: |
|
176 |
DDRA |= _BV(pins); |
|
177 |
PORTA |= _BV(pins); |
|
178 |
break; |
|
179 |
case _PORT_B: |
|
180 |
DDRB |= _BV(pins); |
|
181 |
PORTB |= _BV(pins); |
|
182 |
break; |
|
183 |
case _PORT_C: |
|
184 |
DDRC |= _BV(pins); |
|
185 |
PORTC |= _BV(pins); |
|
186 |
break; |
|
187 |
case _PORT_D: |
|
188 |
DDRD |= _BV(pins); |
|
189 |
PORTD |= _BV(pins); |
|
190 |
break; |
|
191 |
case _PORT_E: |
|
192 |
DDRE |= _BV(pins); |
|
193 |
PORTE |= _BV(pins); |
|
194 |
break; |
|
195 |
case _PORT_F: |
|
196 |
DDRF |= _BV(pins); |
|
197 |
PORTF |= _BV(pins); |
|
198 |
break; |
|
199 |
case _PORT_G: |
|
200 |
DDRG |= _BV(pins); |
|
201 |
PORTG |= _BV(pins); |
|
202 |
break; |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
////////////////////////////////////// |
|
212 |
//////////// button1 ////////////// |
|
213 |
////////////////////////////////////// |
|
214 |
|
|
215 |
/* |
|
216 |
return 1 if button is pressed, 0 otherwise |
|
217 |
*/ |
|
218 |
int button1_read( void ) |
|
219 |
{ |
|
220 |
//return (BTN & (_BV(BTN1)) >> BTN1); |
|
221 |
return (PIN_BTN >> BTN1) & 1; |
|
222 |
} |
|
223 |
|
|
224 |
/* |
|
225 |
similar to button1_read, but hold program until the button is actually pressed |
|
226 |
*/ |
|
227 |
void button1_wait( void ) |
|
228 |
{ |
|
229 |
while(!button1_read() ) { |
|
230 |
delay_ms(15); |
|
231 |
} |
|
232 |
} |
|
233 |
|
|
234 |
|
|
235 |
/* |
|
236 |
same as button1_wait |
|
237 |
However, blink the led while waiting |
|
238 |
|
|
239 |
IMPORTANT: This requires that the LED has been initialized ( init_led ) |
|
240 |
*/ |
|
241 |
void button1_wait_led( void ) |
|
242 |
{ |
|
243 |
int i = 0; |
|
244 |
|
|
245 |
while(!button1_read() ) { |
|
246 |
if( i < 8 ) |
|
247 |
led_user(1); |
|
248 |
else { |
|
249 |
led_user(0); |
|
250 |
} |
|
251 |
//increment i, but restart when i = 15; |
|
252 |
i = (i+1) & 0xF; |
|
253 |
delay_ms(15); |
|
254 |
} |
|
255 |
|
|
256 |
led_user(0); |
|
257 |
} |
|
258 |
|
|
259 |
|
|
260 |
////////////////////////////////////// |
|
261 |
//////////// button2 ////////////// |
|
262 |
////////////////////////////////////// |
|
263 |
//see button1 functions for descriptions |
|
264 |
//same except for which button is used |
|
265 |
int button2_read( void ) |
|
266 |
{ |
|
267 |
return (PIN_BTN >> BTN2) & 1; |
|
268 |
} |
|
269 |
|
|
270 |
void button2_wait( void ) |
|
271 |
{ |
|
272 |
while(!button2_read() ) { |
|
273 |
delay_ms(15); |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
|
|
278 |
void button2_wait_led( void ) |
|
279 |
{ |
|
280 |
int i = 0; |
|
281 |
|
|
282 |
while(!button2_read() ) { |
|
283 |
if( i < 8 ) |
|
284 |
led_user(1); |
|
285 |
else { |
|
286 |
led_user(0); |
|
287 |
} |
|
288 |
//increment i, but restart when i = 15; |
|
289 |
i = (i+1) & 0xF; |
|
290 |
delay_ms(15); |
|
291 |
} |
|
292 |
|
|
293 |
led_user(0); |
|
294 |
} |
|
295 |
|
|
296 |
/* |
|
297 |
// EXTERNAL INTERRUPTS |
|
298 |
/// example code to be used by anyone in need |
|
299 |
/// this example has 2 bump sensors on PE6 and PE7 |
|
300 |
|
|
301 |
// left touch sensor on PE6 |
|
302 |
SIGNAL (SIG_INTERRUPT6) |
|
303 |
{ |
|
304 |
putcharlcd('6'); |
|
305 |
} |
|
306 |
|
|
307 |
// right touch sensor on PE7 |
|
308 |
SIGNAL (SIG_INTERRUPT7) |
|
309 |
{ |
|
310 |
putcharlcd('7'); |
|
311 |
} |
|
312 |
*/ |
trunk/code/projects/colonet/robot/dongle/robot_receiver/serial.c | ||
---|---|---|
1 |
/* |
|
2 |
serial.c - Functions for using the RS232 serial port |
|
3 |
|
|
4 |
author: Robotics Club, Colony Project |
|
5 |
|
|
6 |
much code taken from FWR's library, author: Tom Lauwers |
|
7 |
|
|
8 |
|
|
9 |
general note - serial -> uart |
|
10 |
serial1 -> uart1 |
|
11 |
this is done for clarity purposes |
|
12 |
*/ |
|
13 |
|
|
14 |
|
|
15 |
#include <avr/io.h> |
|
16 |
#include <stdio.h> |
|
17 |
#include "serial.h" |
|
18 |
|
|
19 |
//setup uart0 (serial) |
|
20 |
void serial_init(unsigned int ubrr) |
|
21 |
{ |
|
22 |
/*Set baud rate - baud rates under 4800bps unsupported */ |
|
23 |
UBRR0H = 0x00; |
|
24 |
UBRR0L = (unsigned char)ubrr; |
|
25 |
|
|
26 |
/*Enable receiver and transmitter */ |
|
27 |
UCSR0B |= (1<<RXEN0)|(1<<TXEN0); |
|
28 |
|
|
29 |
/* Set frame format: 8data, 1stop bit, asynchronous normal mode */ |
|
30 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
|
31 |
} |
|
32 |
|
|
33 |
// uart_putchar - Low-level function which puts a value into the |
|
34 |
// Tx buffer for transmission. Automatically injects |
|
35 |
// a \r before all \n's |
|
36 |
int serial_putchar(char c) |
|
37 |
{ |
|
38 |
|
|
39 |
if (c == '\n') |
|
40 |
putchar('\r'); |
|
41 |
// Wait until buffer is clear for sending |
|
42 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
|
43 |
// Load buffer with your character |
|
44 |
UDR0 = c; |
|
45 |
return 0; |
|
46 |
} |
|
47 |
|
|
48 |
// uart_getchar - Low-level function which waits for the Rx buffer |
|
49 |
// to be filled, and then reads one character out of it. |
|
50 |
// Note that this function blocks on read - it will wait until |
|
51 |
// something fills the Rx buffer. |
|
52 |
int serial_getchar(void) |
|
53 |
{ |
|
54 |
char c; |
|
55 |
// Wait for the receive buffer to be filled |
|
56 |
loop_until_bit_is_set(UCSR0A, RXC0); |
|
57 |
|
|
58 |
// Read the receive buffer |
|
59 |
c = UDR0; |
|
60 |
return c; |
|
61 |
} |
|
62 |
|
|
63 |
// uart_getchar_nb - Low-level function which checks if the Rx buffer |
|
64 |
// is filled, and then reads one character out of it. |
|
65 |
// This is a non blocking version uart_getchar |
|
66 |
int serial_getchar_nb(void) |
|
67 |
{ |
|
68 |
char c; |
|
69 |
// Wait for the receive buffer to be filled |
|
70 |
//loop_until_bit_is_set(UCSR0A, RXC0); |
|
71 |
if (UCSR0A & _BV(RXC0)){ |
|
72 |
// Read the receive buffer |
|
73 |
c = UDR0; |
|
74 |
return c; |
|
75 |
} |
|
76 |
return 0; |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
//setup uart1 (serial1) |
|
81 |
void serial1_init( unsigned int ubrr) |
|
82 |
{ |
|
83 |
/*Set baud rate - baud rates under 4800bps unsupported */ |
Also available in: Unified diff