Project

General

Profile

Revision 487

removed more old files

View differences:

trunk/code/projects/colonet/robot/robot_wireless_relay/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 |= 0x50; //=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/robot_wireless_relay/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/robot_wireless_relay/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/robot_wireless_relay/dio.c
1
/* 
2
dio.c
3
Controls digital input and output
4

  
5
A general note on how this code works:
6
portpin is used to select both the bank and which pin is selected
7
6 bits are used (lower 6, ex: 0b00abcdef)
8
the first 3 (abc in this example) are used to select the bank
9
A = 001
10
B = 010
11
C = 011
12
D = 100
13
E = 101
14
F = 110
15
G = 111
16
		
17
the bank can be found by doing portpin >> 3
18
		
19
the next 3 (def in this example) are used to select the pin number
20
the 3 bits are just the binary representation of the pin number
21
0 = 000
22
1 = 001
23
2 = 010
24
3 = 011
25
4 = 100
26
5 = 101
27
6 = 110
28
7 = 111
29
		
30
the pin number can be found by doing portping & 0b111
31
*/
32

  
33
#include <avr/interrupt.h>
34
#include <dio.h>
35
#include <time.h>
36
#include <lights.h>
37

  
38
/*
39
 * digital_input
40
 * Reads the value on the selected portpin, returns it as 1 or 0
41
 * see general description (above) for definition of portpin
42
 */
43
int digital_input(int portpin){
44
  int pin = portpin & 0x7;
45
  int pin_val = 0;
46
  
47
  switch(portpin >> 3){
48
  case _PORT_A:
49
    DDRA &= ~_BV(pin);
50
    pin_val = PINA;
51
    return (pin_val >> pin) & 1;
52
  case _PORT_B:
53
    DDRB &= ~_BV(pin);
54
    pin_val = PINB;
55
    return (pin_val >> pin) & 1;
56
  case _PORT_C:
57
    DDRC &= ~_BV(pin);
58
    pin_val = PINC;
59
    return (pin_val >> pin) & 1;
60
  case _PORT_D:
61
    DDRD &= ~_BV(pin);
62
    pin_val = PIND;
63
    return (pin_val >> pin) & 1;
64
  case _PORT_E:
65
    DDRE &= ~_BV(pin);
66
    pin_val = PINE;
67
    return (pin_val >> pin) & 1;
68
  case _PORT_F:
69
    if(pin>=4){
70
      MCUSR|=1<<7;
71
      MCUSR|=1<<7;
72
    }
73
    DDRF &= ~_BV(pin);
74
    pin_val = PINF;
75
    return (pin_val >> pin) & 1;
76
  case _PORT_G:
77
    DDRG &= ~_BV(pin);
78
    pin_val = PING;
79
    return (pin_val >> pin) & 1;
80
  default: break;
81
  }
82
  
83
  return -1;
84
}
85

  
86
/*
87
  digital_pull_up
88
  Enables pullup on a pin.  if pin is output, it will make it output 1.
89
*/
90
void digital_pull_up(int portpin) {
91
  int pins = portpin & 0x07;
92

  
93
  switch(portpin >> 3) {
94
  case _PORT_A:
95
    PORTA |= _BV(pins);
96
    break;
97
  case _PORT_B:
98
    PORTB |= _BV(pins);
99
    break;    
100
  case _PORT_C:
101
    PORTC |= _BV(pins);
102
    break;    
103
  case _PORT_D:
104
    PORTD |= _BV(pins);
105
    break;    
106
  case _PORT_E:
107
    PORTE |= _BV(pins);
108
    break;    
109
  case _PORT_F:
110
    PORTF |= _BV(pins);
111
    break;    
112
  case _PORT_G:
113
    PORTG |= _BV(pins);
114
    break;
115
  }
116
}
117

  
118
/*
119
  digital_output
120

  
121
  sets portpin to the value
122
  see general description above for explanation of portpin
123
  val can only be 0 for off, nonzero for on
124
*/
125
void digital_output(int portpin, int val) {
126
  int pins = portpin & 0x07;
127
  
128
  /* if you want to set to 0... */
129
  if(val == 0) {
130
    switch(portpin >> 3) {
131
    case _PORT_A:
132
      DDRA |= _BV(pins);
133
      PORTA &= (0XFF - _BV(pins));
134
      break;
135
    case _PORT_B:
136
      DDRB |= _BV(pins);
137
      PORTB &= (0XFF - _BV(pins));
138
      break;    
139
    case _PORT_C:
140
      DDRC |= _BV(pins);
141
      PORTC &= (0XFF - _BV(pins));
142
      break;    
143
    case _PORT_D:
144
      DDRD |= _BV(pins);
145
      PORTD &= (0XFF - _BV(pins));
146
      break;    
147
    case _PORT_E:
148
      DDRE |= _BV(pins);
149
      PORTE &= (0XFF - _BV(pins));
150
      break;    
151
    case _PORT_F:
152
      DDRF |= _BV(pins);
153
      PORTF &= (0XFF - _BV(pins));
154
      break;    
155
    case _PORT_G:
156
      DDRG |= _BV(pins);
157
      PORTG &= (0XFF - _BV(pins));
158
      break;
159
    }
160
  }else { /* ( val == 1) */ 
161
    switch(portpin >> 3) {
162
    case _PORT_A:
163
      DDRA |= _BV(pins);
164
      PORTA |= _BV(pins);
165
      break;
166
    case _PORT_B:
167
      DDRB |= _BV(pins);
168
      PORTB |= _BV(pins);
169
      break;    
170
    case _PORT_C:
171
      DDRC |= _BV(pins);
172
      PORTC |= _BV(pins);
173
      break;    
174
    case _PORT_D:
175
      DDRD |= _BV(pins);
176
      PORTD |= _BV(pins);
177
      break;    
178
    case _PORT_E:
179
      DDRE |= _BV(pins);
180
      PORTE |= _BV(pins);
181
      break;    
182
    case _PORT_F:
183
      DDRF |= _BV(pins);
184
      PORTF |= _BV(pins);
185
      break;    
186
    case _PORT_G:
187
      DDRG |= _BV(pins);
188
      PORTG |= _BV(pins);
189
      break;
190
    }
191
  }
192
}
193

  
194
//////////////////////////////////////
195
////////////   button1  //////////////
196
//////////////////////////////////////
197

  
198
/*
199
  return 1 if button is pressed, 0 otherwise
200
*/
201
int button1_read( void )
202
{
203
  //return (BTN & (_BV(BTN1)) >> BTN1);
204
  return (PIN_BTN >> BTN1) & 1;
205
}
206

  
207
/* similar to button1_read, but hold program until the button is actually 
208
 * pressed
209
*/
210
void button1_wait( void )
211
{
212
  while(!button1_read() ) {
213
    delay_ms(15);
214
  }
215
}
216

  
217
/*
218
  same as button1_wait
219
  However, blink the led while waiting
220

  
221
  IMPORTANT: This requires that the LED has been initialized (  init_led  )
222
*/
223
void button1_wait_led( void )
224
{
225
  int i = 0;
226
  
227
  while(!button1_read()){
228
    if(i < 8){
229
      led_user(1);
230
    }else{
231
      led_user(0);
232
    }
233
    //increment i, but restart when i = 15;
234
    i = (i+1) & 0xF;
235
    delay_ms(15);
236
  }
237
  
238
  led_user(0);
239
}
240

  
241
//click waits until the button is realesed to return true
242
//return false immediatley
243
int button1_click()
244
{
245
  if(button1_read()){
246
    while(button1_read());
247
    return 1;
248
  }else{
249
    return 0;
250
  }
251
}
252

  
253
//////////////////////////////////////
254
////////////   button2  //////////////
255
//////////////////////////////////////
256
//see button1 functions for descriptions
257
//same except for which button is used
258
int button2_read( void )
259
{
260
  return (PIN_BTN >> BTN2) & 1;
261
}
262

  
263
void button2_wait( void )
264
{
265
  while(!button2_read()){
266
    delay_ms(15);
267
  }
268
}
269

  
270
void button2_wait_led(void)
271
{
272
  int i = 0;
273
  
274
  while(!button2_read()){
275
    if(i < 8){
276
			led_user(1);
277
    }else{
278
			led_user(0);
279
		}
280
		//increment i, but restart when i = 15;
281
		i = (i+1) & 0xF;
282
		delay_ms(15);
283
	}
284
	
285
	led_user(0);
286
}
287

  
288
int button2_click()
289
{
290
	if(button2_read()){
291
    while(button2_read());
292
    return 1;
293
  }else{
294
		return 0;
295
  }
296
}
297

  
298
/*
299
// EXTERNAL INTERRUPTS
300
/// example code to be used by anyone in need
301
/// this example has 2 bump sensors on PE6 and PE7
302

  
303
// left touch sensor on PE6 
304
SIGNAL (SIG_INTERRUPT6)
305
{
306
putcharlcd('6');
307
}
308

  
309
// right touch sensor on PE7
310
SIGNAL (SIG_INTERRUPT7)
311
{
312
	putcharlcd('7');
313
}
314
*/
trunk/code/projects/colonet/robot/robot_wireless_relay/bom.c
1
#include "bom.h"
2

  
3

  
4
const int lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
5

  
6
int getMaxBom(void) {
7
	int max_bom_temp = 0;
8
	int a, i, j, h;
9
    h = 255;
10

  
11
    for (j = 0; j < 16; j++)
12
    {
13
      i = lookup[j];
14

  
15
      if (i&8)
16
        output_high(MONK3);
17
      else
18
        output_low(MONK3);
19

  
20
      if (i&4)
21
        output_high(MONK2);
22
      else
23
        output_low(MONK2);
24

  
25
      if (i&2)
26
        output_high(MONK1);
27
      else
28
        output_low(MONK1);
29

  
30
      if (i&1)
31
        output_high(MONK0);
32
      else
33
        output_low(MONK0);
34

  
35
      a = analog8(MONKI);
36

  
37
      if (a < h)
38
      {
39
        h = a;
40
        max_bom_temp = j;
41
      }
42

  
43
    }
44
	
45
	return max_bom_temp;
46
}
47

  
48
void bom_on(void)
49
{
50
  output_high(MONKL);
51

  
52
}
53

  
54
void bom_off(void)
55
{
56
  output_low(MONKL);
57
}
58

  
59
void output_high(int which) {
60
	digital_output(which, 1);
61
}
62

  
63
void output_low(int which) {
64
	digital_output(which, 0);
65
}
66

  
trunk/code/projects/colonet/robot/robot_wireless_relay/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

  
71
	if (UCSR0A & _BV(RXC0)){
72
		// Read the receive buffer
73
		c = UDR0;
74
		return c;
75
 	}
76

  
77
	return 0;
78
}
79

  
80
//setup uart1 (serial1)
81
void serial1_init( unsigned int ubrr)
82
{
83
	/*Set baud rate - baud rates under 4800bps unsupported */
84
	UBRR1H = 0x00;
85
	UBRR1L = (unsigned char)ubrr;
86

  
87
	/*Enable receiver and transmitter */
88
	UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
89
	
90
	/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
91
	UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
92
  
93
  /* For use with fprintf() and related functions */
94
  serial1_stream = fdevopen(serial1_putchar, serial1_putchar);
95
  
96
  /* To use fprintf():
97
  After calling serial1_init(BAUD115200); 
98
  use the command fprintf(serial1_stream, "Hello World"); 
99
     This call to fdevopen() does give a compile warning
100
  but works fine.
101
  -Greg Tress
102
  */
103
}
104

  
105
//put to uart1 (serial1)
106
int serial1_putchar(char c)
107
{
108

  
109
    if (c == '\n')
110
		serial1_putchar('\r'); //changed from putchar aj
111
	// Wait until buffer is clear for sending
112
    loop_until_bit_is_set(UCSR1A, UDRE1);
113
	// Load buffer with your character
114
    UDR1 = c;
115
    return 0;
116
}
117

  
118
//get from uart1 (serial1)
119
int serial1_getchar(void)
120
{
121
	char c;
122
	// Wait for the receive buffer to be filled
123
    loop_until_bit_is_set(UCSR1A, RXC1);
124
	
125
	// Read the receive buffer
126
    c = UDR1;
127
	return c;
128
}
129

  
130
//get from uart1 non-block
131
int serial1_getchar_nb(void)
132
{
133
	char c;
134
	// Wait for the receive buffer to be filled
135
    //loop_until_bit_is_set(UCSR1A, RXC1);
136
	if (UCSR1A & _BV(RXC1)){
137
		// Read the receive buffer
138
		c = UDR1;
139
		return c;
140
	}
141
	return 0;
142
		
143
}
trunk/code/projects/colonet/robot/robot_wireless_relay/dio.h
1
#ifndef _DIO_H
2
#define _DIO_H
3

  
4
/*
5
these are ALL the pins
6
don't use unless you know what you're doing
7
*/
8

  
9
#define _PORT_A 1
10
#define _PORT_B 2
11
#define _PORT_C 3
12
#define _PORT_D 4
13
#define _PORT_E 5
14
#define _PORT_F 6
15
#define _PORT_G 7
16

  
17
#define _PIN_A0 8
18
#define _PIN_A1 9
19
#define _PIN_A2 10
20
#define _PIN_A3 11
21
#define _PIN_A4 12
22
#define _PIN_A5 13
23
#define _PIN_A6 14 
24
#define _PIN_A7 15
25

  
26
#define _PIN_B0 16
27
#define _PIN_B1 17
28
#define _PIN_B2 18
29
#define _PIN_B3 19
30
#define _PIN_B4 20
31
#define _PIN_B5 21
32
#define _PIN_B6 22
33
#define _PIN_B7 23
34

  
35
#define _PIN_C0 24
36
#define _PIN_C1 25
37
#define _PIN_C2 26
38
#define _PIN_C3 27
39
#define _PIN_C4 28
40
#define _PIN_C5 29
41
#define _PIN_C6 30
42
#define _PIN_C7 31
43

  
44
#define _PIN_D0 32
45
#define _PIN_D1 33
46
#define _PIN_D2 34
47
#define _PIN_D3 35
48
#define _PIN_D4 36
49
#define _PIN_D5 37
50
#define _PIN_D6 38
51
#define _PIN_D7 39
52

  
53
#define _PIN_E0 40
54
#define _PIN_E1 41
55
#define _PIN_E2 42
56
#define _PIN_E3 43
57
#define _PIN_E4 44
58
#define _PIN_E5 45
59
#define _PIN_E6 46
60
#define _PIN_E7 47
61

  
62
#define _PIN_F0 48
63
#define _PIN_F1 49
64
#define _PIN_F2 50
65
#define _PIN_F3 51
66
#define _PIN_F4 52
67
#define _PIN_F5 53
68
#define _PIN_F6 54
69
#define _PIN_F7 55
70

  
71
#define _PIN_G0 56
72
#define _PIN_WR 56
73
#define _PIN_G1 57
74
#define _PIN_RD 57
75
#define _PIN_G2 58
76
#define _PIN_ALE 58
77
#define _PIN_G3 59
78
#define _PIN_TOSC2 59
79
#define _PIN_G4 60
80
#define _PIN_TOSC1 60
81
//#define _PIN_G5 61
82
//#define _PIN_G6 62
83
//#define _PIN_G7 63
84

  
85
/*
86
These are the header pins (the ones you can connect things to)
87
Feel free to use these
88
*/
89
#define PIN_A0 8
90
#define PIN_A1 9
91
#define PIN_A2 10
92
#define PIN_A3 11
93
#define PIN_A4 12
94
#define PIN_A5 13
95
#define PIN_A6 14 
96
#define PIN_A7 15
97

  
98
#define PIN_SS 16
99
#define PIN_SCK 17
100
#define PIN_MOSI 18
101
#define PIN_MISO 19
102
#define PIN_LCD_COMMAND 20
103

  
104
#define PIN_C0 24
105
#define PIN_C1 25
106
#define PIN_C2 26
107
#define PIN_C3 27
108
#define PIN_C4 28
109
#define PIN_C5 29
110
#define PIN_C6 30
111
#define PIN_C7 31
112

  
113
#define PIN_SCL 32
114
#define PIN_SDA 33
115

  
116
#define PIN_RX0 40
117
#define PIN_TX0 41
118
#define PIN_LCD_RESET 42
119
//#define PIN_LEDR 43
120
//#define PIN_LEDG 44
121
//#define PIN_LEDB 45
122
#define PIN_E6 46
123
#define PIN_EXT_DIO1 46
124
#define PIN_E7 47
125
#define PIN_EXT_DIO2 48
126

  
127
#define PIN_AN0 48
128
#define PIN_ADC0 48
129
#define PIN_AN1 49
130
#define PIN_ADC1 49
131
#define PIN_AN2 50
132
#define PIN_ADC2 50
133
#define PIN_AN3 51
134
#define PIN_ADC3 51
135
#define PIN_AN4 52
136
#define PIN_ADC4 52
137
#define PIN_AN5 53
138
#define PIN_ADC5 53
139
#define PIN_AN6 54
140
#define PIN_ADC6 54
141
#define PIN_AN7 55
142
#define PIN_ADC7 55
143

  
144
#ifndef FFPP
145
#define PIN_WHEEL 54
146
#define PIN_BATT 55
147
#endif
148

  
149
#define PIN_BTN1 56
150
#define PIN_BTN2 57
151

  
152
#ifndef FFPP
153
#define PIN_LED1 58
154
#endif
155

  
156
/* Buttons */
157
#define PIN_BTN PING
158
#define BTN2 PING1
159
#define BTN1 PING0
160

  
161
int digital_input(int);
162
void digital_output(int bit, int val);
163
void digital_pull_up(int);
164

  
165
int button1_read( void );
166
int button1_click( void );
167
void button1_wait( void );
168
void button1_wait_led( void );
169

  
170
int button2_read( void );
171
int button2_click( void );
172
void button2_wait( void );
173
void button2_wait_led( void );
174

  
175
#endif
trunk/code/projects/colonet/robot/robot_wireless_relay/buzzer.c
1
/*
2
  buzzer.c - Contains the functions necessary for running the buzzer
3
  
4
  author: Robotics Club, Colony Project
5
  
6
  much taken from FWR's firefly library (author: Tom Lauwers)
7
*/
8

  
9
#include <avr/io.h>
10
#include <buzzer.h>
11
#include <time.h>
12

  
13
/*
14
initializes buffer
15
this function must be called before the buzzer can be used
16
*/
17
void buzzer_init( void )
18
{
19
  // Set pin D7 to output - D7 is buzzer pin
20
  DDRB |= 0x80;
21
  
22
  // Set to Fast PWM mode, toggle the OC0A pin (D6) on output compare
23
  // matches, and select a clock prescalar of 64 for the counter.
24
  // Counter FREQ = 8000000/64 = 125000
25
  //01011000
26
  TCCR2 = 0x1B;
27
  OCR2=100;
28
}
29

  
30
// Set the buzzer value - takes a value from 0-255
31
// Higher values are lower frequency.  Take a look at 
32
// the buzzer frequency table to see how a given value
33
// correlates to a frequency.
34
void buzzer_set_val(unsigned int buzz_value)
35
{ 
36
  OCR2 = buzz_value;
37
}
38

  
39
// Set the buzzer frequency - takes any value and tries to find the nearest 
40
// buzzer frequency
41
void buzzer_set_freq(unsigned int buzz_freq)
42
{
43
  int buzz_value;
44
  
45
  buzz_value = 62500/buzz_freq - 1;
46

  
47
  if(buzz_value > 255){
48
    buzz_value = 255;
49
  }else if(buzz_value < 0){
50
    buzz_value = 0;
51
  }
52
  
53
  buzzer_set_val(buzz_value);
54
}
55

  
56
// Chirps the buzzer for a number of milliseconds
57
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) 
58
{ 
59
  buzzer_set_freq(buzz_freq);
60
  delay_ms(ms);
61
  buzzer_off();
62
}
63

  
64
// Disables timer0 clock counting, turning off the buzzer
65
void buzzer_off()
66
{
67
  // Disable clock, to halt counter
68
  TCCR2 &= 0xF8;
69
  
70
  // Set buzzer pin low in case it's high
71
  PORTB &= 0x7F;
72
}
trunk/code/projects/colonet/robot/robot_wireless_relay/bom.h
1

  
2
#ifndef _BOM_H
3
#define _BOM_H
4

  
5

  
6
#include "firefly+_lib.h"
7

  
8
/*
9

  
10

  
11
 Bk R Y (Analog)
12
---------
13
 Green
14
 Blue
15
 White
16
---------
17
 Blue
18
 White
19

  
20
*/
21

  
22

  
23
#define MONKI AN0         //analog
24
//------------------------//
25
#define MONKL PIN_C2      //green
26
#define MONK1 PIN_C3      //blue
27
#define MONK0 PIN_C4      //white
28
//------------------------//
29
#define MONK3 PIN_C5      //blue
30
#define MONK2 PIN_C6      //white 
31

  
32

  
33

  
34
int getMaxBom( void );
35

  
36
void bom_on(void);
37
void bom_off(void);
38

  
39
void output_high(int which);
40
void output_low(int which);
41

  
42

  
43
#endif
44

  
trunk/code/projects/colonet/robot/robot_wireless_relay/serial.h
1
/*
2
	serial.h - Contains definitions and function prototypes for the RS232 serial port
3
*/
4

  
5
#ifndef _SERIAL_H
6
#define _SERIAL_H
7

  
8
// Tested baud rates
9
#define BAUD9600    103
10
#define BAUD115200  8  //Warning--3.5% error
11

  
12
// Untested baud rates that might be right --aaron
13
#define BAUD1M	     0
14
#define BAUD500K     1
15
#define BAUD250K     3
16
#define BAUD230400   3   //Warning--8.5% error
17
#define BAUD76800    12
18
#define BAUD57600    16  //Warning--2.1% error
19
#define BAUD38400    25
20
#define BAUD28800    34
21
#define BAUD19200    51
22
#define BAUD14400    68
23
#define BAUD4800     207
24
#define BAUD2400     416  //Might not work, since it needs some high bits set
25

  
26
#include <stdio.h>
27
FILE *serial1_stream;  //For use with fprintf() and related functions
28

  
29
  /* NOTE: To use fprintf():
30
  After calling serial1_init(BAUD115200); 
31
  use the command fprintf(serial1_stream, "Hello World"); */
32

  
33
// Function descriptions are available in serial.c
34

  
35
//serial (serial0) is the ttl serial (wireless)
36
void serial_init(unsigned int ubrr);
37
int serial_putchar(char c);
38
int serial_getchar(void);
39
int serial_getchar_nb(void);
40

  
41
//serial1 is the rs-232 port (db9 connector) (except on robot0 where both are serial0)
42
void serial1_init( unsigned int ubrr);
43
int serial1_putchar(char c);
44
int serial1_getchar(void);
45
int serial1_getchar_nb(void);
46

  
47
#endif
trunk/code/projects/colonet/robot/robot_wireless_relay/rangefinder.c
1
/*
2
Authors: James Kong and Greg Tress
3

  
4
Last Modified: 4/30/06 by James
5
-Started log_distance conversion function !!!NOT COMPLETE!!!
6
-Cleaning up comments
7

  
8
-----------------
9
rangefinder.c
10
Using Sharp GP2D02 IR Rangefinder
11

  
12
Vin is the input to the rangefinder, designated RANGE_CTRL.
13
Vout is the output from the rangefinder, designated RANGE_IN# where # is the rangefinder you are reading from
14

  
15
Expected Initial Conditions:
16
Vin is high and Vout should read high.
17

  
18
Usage:
19
1.) Set Vin low. Vout should read low.
20
2.) Wait for high on Vout.
21
3.) Begin clocking Vin and reading 8 bits from Vout (MSB first).
22
4.) Set Vin high for 2ms or more to turn off rangefinder
23

  
24
*/
25

  
26
#include "firefly+_lib.h"
27
#include "rangefinder.h"
28

  
29
/*
30
read_distance returns the 8-bit reading from the rangefinder
31
parameters:
32
range_id - dio pin set as the rangefinder Vout [i.e. RANGE_IN0]
33

  
34
NOTE:
35
The Sharp GD2D02 returns values on a decreasing logrithmic scale.
36
So higher values correspond to closer distances.  Use linearize_distance to convert to normal centimeter scale.
37
Also, when reading distances closer than 8cm, the Sharp GD2D02 will return lower values than the values at 8cm.
38
At this point, we are only reading from one rangefinder [RANGE_IN0].
39
*/
40

  
41
int read_distance (int range_id) {  
42
#ifndef FFPP
43
	int bitcount;
44
	int current = 0;
45
	digital_output(RANGE_CTRL, 0);						//set Vin Low
46
	while(digital_input(range_id) == 0);				//waits until Vout reads high
47
	digital_output(RANGE_CTRL, 1);						//first Vin clock high transition
48
	
49
	for(bitcount = 8; bitcount != 0; bitcount--){
50
		digital_output(RANGE_CTRL, 0);					//Vin clock low transition
51
		//clock delay (unnecessary)
52
		current = current << 1;							//shifts current 8-bit value left
53
		current |= digital_input(range_id);				//LSB set to Vout value
54
		digital_output(RANGE_CTRL, 1);					//Vin clock high transition
55
		//clock delay (unnecessary)
56
	}													//repeats until all 8 bits are stored
57
	return current;
58
#else
59
	enable_IR();
60
	_delay_ms(5);										///////Figure out what this should be!!!!!!  Maybe if not enabled already?
61
	return read_IR_val(range_id);
62
#endif
63
}
64

  
65
/*
66
linearize_distance converts an 8-bit rangefinder reading to a centimeter measurement (truncated to an integer)
67
parameters:
68
reading - 8-bit rangefinder reading
69

  
70
OFFSET, GAIN, and THRESHOLD are experimentally determined constants
71
The conversion is a piecewise defined function with a continuity fix at reading = THRESHOLD3
72

  
73
NOTE:
74
This function is ugly, but it gets the job done without spending a huge amount processing.
75
Ideally, you want to compare with the read_distance value directly.
76
*/
77

  
78
int linearize_distance (int reading) {
79
#ifndef FFPP
80
	int temp = reading;
81
	if(reading >  THRESHOLD1){
82
		temp = temp - OFFSET1;
83
		return GAIN1 / temp;
84
	}else if(reading > THRESHOLD2){
85
		temp = temp - OFFSET2;
86
		return GAIN2 / temp + 1;
87
	}else if(reading > THRESHOLD3){
88
		temp = temp - OFFSET2;
89
		return GAIN2 / temp;
90
	}else if(reading == THRESHOLD3){
91
		return CONTINUITY_FIX;
92
	}else if(reading > THRESHOLD4){
93
		temp = temp - OFFSET3;
94
		return GAIN3 / temp;
95
	}else{
96
		return MAX_DIST;
97
	}
98
#else
99
	return convert_IR_distance(reading);
100
#endif
101
}
102

  
103
/*
104
log_distance converts a centimeter value to a rangefinder comparable value
105
parameters:
106
distance - centimeter measurement
107

  
108
NOTE:
109
This is still incomplete, do not use it.
110
*/
111
int log_distance(int distance) {
112
#ifndef FFPP
113
	if(distance >= MAX_DIST){
114
		return THRESHOLD4;
115
	}
116
	if(distance <= MIN_DIST){
117
		return 255;
118
	}	
119

  
120
	switch(distance){			//Incomplete need to fill out table
121
	case 60:				
122
		return 56;
123
	case 59:
124
		return 89;
125
	}
126
#endif
127
	return 0;
128
}
129

  
130

  
131
#ifdef FFPP
132

  
133
static int IR_dist_conversion[114] = {
134
800,791,751,714,681,651,623,597,574,552,531,512,494,478,462,447
135
,434,421,408,397,386,375,365,356,347,338,330,322,315,307,301,294
136
,288,282,276,270,265,260,255,250,245,241,237,232,228,224,221,217
137
,213,210,207,203,200,197,194,191,189,186,183,181,178,176,173,171
138
,169,166,164,162,160,158,156,154,152,151,149,147,145,144,142,140
139
,139,137,136,134,133,131,130,129,127,126,125,124,122,121,120,119
140
,118,117,115,114,113,112,111,110,109,108,107,106,105,105,104,103
141
,102,101
142
};
143

  
144
void enable_IR()
145
{
146
  // active low, so set enable to low to turn on IR
147
  PORTC &= ~(_BV(IR_ENABLE));
148
  DDRC |= _BV(IR_ENABLE);
149
}
150

  
151
void disable_IR()
152
{
153
  // active low, so set line high and turn to input for good measure (line has pull-up)
154
  PORTC |= _BV(IR_ENABLE);
155
  DDRC &= ~(_BV(IR_ENABLE));
156
}
157

  
158
int read_IR_val(int which)
159
{
160
  return analog8(IR_READ + which);
161
}
162

  
163
int convert_IR_distance(int value)
164
{
165
  if(value < MIN_IR_ADC8)
166
  {
167
    return -1;
168
  }
169
  else if(value > MAX_IR_ADC8)
170
  {
171
    return -1;
172
  }
173
  else
174
  {
175
    return IR_dist_conversion[value - MIN_IR_ADC8];
176
  }
177
}
178

  
179
int get_IR_distance(int which)
180
{
181
  return convert_IR_distance(read_IR_val(which));
182
}
183
#endif
trunk/code/projects/colonet/robot/robot_wireless_relay/buzzer.h
1
/*
2
	buzzer.h - Contains function prototypes for running the buzzer
3

  
4
	author: Robotics Club, Colony Project
5
*/
6
#ifndef _BUZZER_H_
7
#define _BUZZER_H_
8

  
9
//Musical note definitions
10
//Source: http://www.answers.com/topic/piano-key-frequencies
11
#define	C4	260 //Middle C
12
#define	C4s	277 //C sharp
13
#define	D4	294
14
#define	D4s	311
15
#define	E4	330
16
#define	F4	349
17
#define	F4s	370
18
#define	G4	392
19
#define	G4s	415
20
#define	A4	440
21
#define	A4s	466
22
#define	B4	494
23
#define	C5	523
24

  
25
// Function descriptions can be found in buzzer.c
26
void buzzer_init(void);
27
void buzzer_set_val(unsigned int buzz_value);
28
void buzzer_set_freq(unsigned int buzz_freq);
29
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq);
30
void buzzer_off(void); 
31

  
32
#endif
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff