Project

General

Profile

Revision 1496

Reverted "libdragonfly" folder back to version before Init Checking was implemented and did "make dist" to recompile the library. BOM LEDs now shine
correctly.

View differences:

analog.c
36 36

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

  
40

  
41
#include "dragonfly_defs.h"
39
#include "analog.h"
42 40
#include "serial.h"
43
#include "analog.h"
44 41
// Internal Function Prototypes
45 42
void set_adc_mux(int which);
46 43

  
......
52 49
 * @{
53 50
 **/
54 51

  
55
unsigned char analog_initd=0;
56

  
57 52
volatile int adc_loop_status = ADC_LOOP_STOPPED;
58 53
volatile int adc_sig_stop_loop = 0;
59 54
volatile int adc_current_port = 0;
......
63 58
 * Initializes the ADC.
64 59
 * Call analog_init before reading from the analog ports.
65 60
 *
66
 * @return returns 0 on success, nonzero on error
67
 *
68 61
 * @see analog8, analog10, analog_get8, analog_get10
69 62
 *
70 63
 * @bug First conversion takes a performance penalty of
......
80 73
 * determine highest clock speed for accurate 8-bit ADC.
81 74
 *
82 75
 **/
83
int analog_init(int start_conversion) {
84
  if(analog_initd)
85
    return ERROR_INIT_ALREADY_INITD;
76
void analog_init(int start_conversion) {
77
	for (int i = 0; i < 11; i++) {
78
		an_val[i].adc10 = 0;
79
		an_val[i].adc8 = 0;
80
	}
86 81

  
87
  for (int i = 0; i < 11; i++) {
88
    an_val[i].adc10 = 0;
89
    an_val[i].adc8 = 0;
90
  }
82
	// ADMUX register
83
	// Bit 7,6 - Set voltage reference to AVcc (0b01)
84
	// Bit 5 - ADLAR set to simplify moving from register
85
	// Bit 4 - X
86
	// Bit 3:0 - Sets the current channel
87
	// Initializes to read from AN1 first (AN0 is reservered for the BOM)
88
	ADMUX = 0;
89
	ADMUX |= ADMUX_OPT | _BV(MUX0);
91 90

  
92
  // ADMUX register
93
  // Bit 7,6 - Set voltage reference to AVcc (0b01)
94
  // Bit 5 - ADLAR set to simplify moving from register
95
  // Bit 4 - X
96
  // Bit 3:0 - Sets the current channel
97
  // Initializes to read from AN1 first (AN0 is reservered for the BOM)
98
  ADMUX = 0;
99
  ADMUX |= ADMUX_OPT | _BV(MUX0);
100

  
101
  // ADC Status Register A
102
  // Bit 7 - ADEN is set (enables analog)
103
  // Bit 6 - Start conversion bit is set (must be done once for free-running mode)
104
  // Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
105
  // Bit 4 - ADC interrupt flag, 0
106
  // Bit 3 - Enable ADC Interrupt (required to run free-running mode)
107
  // Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
108
  ADCSRA = 0;
109
  ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
91
	// ADC Status Register A
92
	// Bit 7 - ADEN is set (enables analog)
93
	// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
94
	// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
95
	// Bit 4 - ADC interrupt flag, 0
96
	// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
97
	// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
98
	ADCSRA = 0;
99
	ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
110 100
	
111
  // Set external mux lines to outputs
112
  DDRG |= 0x1C;
101
	// Set external mux lines to outputs
102
	DDRG |= 0x1C;
113 103
	
114
  // Set up first port for conversions
115
  set_adc_mux(0x00);
116
  adc_current_port = AN1;
104
	// Set up first port for conversions
105
	set_adc_mux(0x00);
106
	adc_current_port = AN1;
117 107

  
118
  //Start the conversion loop if requested
119
  if (start_conversion)
120
    analog_start_loop();
108
	//Start the conversion loop if requested
109
	if (start_conversion)
110
		analog_start_loop();
121 111
		
122
  //Conversion loop disabled by default
123

  
124
  analog_initd=1;
125
  return 0;
112
	//Conversion loop disabled by default
126 113
}	
127 114

  
128 115
/**
......
183 170
 * Starts the analog update loop. Will continue to run
184 171
 * until analog_stop_loop is called.
185 172
 *
186
 * @return returns 0 on success, nonzero on error
187
 *
188 173
 * @see analog_stop_loop, analog_loop_status
189 174
 **/
190
int analog_start_loop(void) {
191
  if(!analog_initd)
192
    return ERROR_LIBRARY_NOT_INITD;
193

  
194
  if(adc_loop_status != ADC_LOOP_RUNNING){
195
    //Start the conversion, enable ADC interrupt
196
    ADCSRA |= _BV(ADIE);
197
    ADCSRA |= _BV(ADSC);
198
    adc_loop_status = ADC_LOOP_RUNNING;
199
  }
200

  
201
  return 0;
175
void analog_start_loop(void) {
176
	if(adc_loop_status != ADC_LOOP_RUNNING){
177
		//Start the conversion, enable ADC interrupt
178
		ADCSRA |= _BV(ADIE);
179
		ADCSRA |= _BV(ADSC);
180
		adc_loop_status = ADC_LOOP_RUNNING;
181
	}
202 182
}
203 183

  
204 184
/**
......
207 187
 * is interrupted. No further updates will be made until
208 188
 * the loop is started again.
209 189
 *
210
 * @return returns 0 on success, nonzero on error
211
 *
212 190
 * @see analog_start_loop, analog_loop_status
213 191
 **/
214
int analog_stop_loop() {
215
  if(!analog_initd)
216
    return ERROR_LIBRARY_NOT_INITD;
217

  
218
  //Signal to stop after the next conversion
219
  adc_sig_stop_loop = 1;
220

  
221
  return 0;
192
void analog_stop_loop() {
193
	//Signal to stop after the next conversion
194
	adc_sig_stop_loop = 1;
222 195
}
223 196

  
224 197
/**

Also available in: Unified diff