Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / analog.c @ 277

History | View | Annotate | Download (4.77 KB)

1
/**
2
 * Copyright (c) 2007 Colony Project
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26
/**
27
 * @file analog.c
28
 * @brief Analog input and output
29
 *
30
 * Contains functions for manipulating the ADC on the Dragonfly board.
31
 *
32
 * @author Colony Project, CMU Robotics Club
33
 * code mostly taken from fwr analog file (author: Tom Lauwers)
34
 **/
35

    
36
#include <util/delay.h>
37
#include <avr/interrupt.h>
38
#include "analog.h"
39

    
40
// Internal Function Prototypes
41
void set_adc_mux(int which);
42

    
43
/**
44
 * @defgroup analog Analog
45
 * Functions for manipulation the ADC on the dragonfly board.
46
 * All definitions may be found in analog.h.
47
 *
48
 * @{
49
 **/
50

    
51

    
52
/**
53
 * Initializes the ADC.
54
 * Call analog_init before reading from the analog ports.
55
 *
56
 * @see analog8, analog10
57
 **/
58
void analog_init(void)
59
{
60
  // ADC Status Register A
61
  // Bit 7 - ADEN is set (enables analog)
62
  // Bit 6 - Start conversion bit is set (must be done once for free-running mode)
63
  // Bit 5 - Enable Auto Trigger (for free running mode)
64
  // Bit 4 - ADC interrupt flag, 0
65
  // Bit 3 - Enable ADC Interrupt (required to run free-running mode)
66
  // Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/128
67
  ADCSRA |= 0xEF;
68

    
69
  // ADC Status Register B
70
  // Bit 7, 5-3 - Must be cleared
71
  // Bit 2:0 - Set mode, currently cleared for free running operation
72
  // Bit 6 - Analog comparator mode - cleared
73
//  ADCSRB = 0x00;
74

    
75
  // ADMUX register
76
  // Bit 7,6 - Set voltage reference to AVcc (0b01)
77
  // Bit 5 - Set ADLAR bit for left adjust to do simple 8-bit reads
78
  // Bit 4 - X
79
  // Bit 3:0 - Sets the current channel, set to ADC7 (the external mux)
80
  ADMUX = 0x67;
81

    
82
  // Set external mux lines to outputs
83
  DDRG |= 0x1C;
84
  set_adc_mux(0x07);
85
}
86

    
87

    
88
/**
89
 * Reads an eight bit number from an analog port.
90
 * analog_init must be called before using this function.
91
 *
92
 * @param which the analog port to read from. One of
93
 * the constants AN0 - AN7.
94
 *
95
 * @return the eight bit input to the specified port
96
 *
97
 * @see analog_init, analog10
98
 **/
99
unsigned int analog8(int which)
100
{
101
  if(which < EXT_MUX) {
102
    ADMUX = 0x60 + which;
103
  } else if(which == EXT_MUX) {
104
    return 0;
105
  } else {
106
    ADMUX = 0x60 + EXT_MUX;
107
    set_adc_mux(which - 8);
108
    _delay_ms(1);
109
  }
110

    
111
  _delay_ms(1); // need at least 130 us between conversions
112
  return ADCH;
113
}
114

    
115
/**
116
 * Reads a ten bit number from the specified port.
117
 * analog_init must be called before using this function.
118
 *
119
 * @param which the analog port to read from. Typically
120
 * a constant, one of AN0 - AN7.
121
 *
122
 * @return the ten bit number input to the specified port
123
 *
124
 * @see analog_init, analog8
125
 **/
126
unsigned int analog10(int which)
127
{
128
  unsigned int adc_h = 0;
129
  unsigned int adc_l = 0;
130

    
131
  if(which < EXT_MUX) {
132
    ADMUX = 0x60 + which;
133
  } else if(which == EXT_MUX) {
134
    return 0;
135
  } else {
136
    ADMUX = 0x60 + EXT_MUX;
137
    set_adc_mux(which - 8);
138
    _delay_ms(1);
139
  }
140

    
141
  _delay_ms(1);
142
  adc_l = ADCL; /* highest 2 bits of ADCL -> least 2 bits of analog val */
143
  adc_h = ADCH; /* ADCH -> 8 highest bits of analog val */
144

    
145
  return (adc_h << 2) | (adc_l >> 6);
146
}
147

    
148
/**
149
 * Returns the current position of the wheel, as an integer
150
 * in the range 0 - 255.
151
 * analog_init must be called before using this function.
152
 *
153
 * @return the orientation of the wheel, as an integer in
154
 * the range 0 - 255.
155
 *
156
 * @see analog_init
157
 **/
158
int wheel(void)
159
{
160
  return analog8(WHEEL_PORT);
161
}
162

    
163
/**@}**/ //end defgroup
164

    
165
SIGNAL (SIG_ADC)
166
{
167
  // This is just here to catch ADC interrupts because ADC is free running.
168
  // No code needs to be in here.
169
}
170

    
171
void set_adc_mux(int which)
172
{
173
  // FIX THIS IN NEXT REVISION
174
  // ADDR2 ADDR1 ADDR0
175
  // G2.G4.G3 set mux to port 0-7 via binary selection
176
  // math would be much cleaner if it was G4.G3.G2
177

    
178
  // mask so only proper bits are possible.
179
  PORTG = (PORTG & 0xE3) | ((which & 0x03) << 3) | (which & 0x04);
180
}