Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 272 → Rev 273

/FollowMe/analog.c
0,0 → 1,109
 
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
 
#include "analog.h"
 
volatile int16_t Adc0, Adc1, Adc2, Adc3, Adc4, Adc5, Adc6, Adc7;
volatile uint8_t ADReady = 1;
 
/*****************************************************/
/* Initialize Analog Digital Converter */
/*****************************************************/
void ADC_Init(void)
{
uint8_t sreg = SREG;
// disable all interrupts before reconfiguration
cli();
//ADC0 ... ADC7 is connected to PortA pin 0 ... 7
DDRA = 0x00;
PORTA = 0x00;
// Digital Input Disable Register 0
// Disable digital input buffer for analog adc_channel pins
DIDR0 = 0xFF;
// external reference, adjust data to the right
ADMUX &= ~((1 << REFS1)|(1 << REFS0)|(1 << ADLAR));
// set muxer to ADC adc_channel 0 (0 to 7 is a valid choice)
ADMUX = (ADMUX & 0xE0) | 0x00;
//Set ADC Control and Status Register A
//Auto Trigger Enable, Prescaler Select Bits to Division Factor 128, i.e. ADC clock = SYSCKL/128 = 156.25 kHz
ADCSRA = (0<<ADEN)|(0<<ADSC)|(0<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(0<<ADIE);
//Set ADC Control and Status Register B
//Trigger Source to Free Running Mode
ADCSRB &= ~((1 << ADTS2)|(1 << ADTS1)|(1 << ADTS0));
// Start AD conversion
ADC_Enable();
// restore global interrupt flags
SREG = sreg;
}
 
/*****************************************************/
/* Interrupt Service Routine for ADC */
/*****************************************************/
// runs at 312.5 kHz or 3.2 µs
// if after (60.8µs) all 19 states are processed the interrupt is disabled
// and the update of further ads is stopped
 
 
#define ADC0 0
#define ADC1 1
#define ADC2 2
#define ADC3 3
#define ADC4 4
#define ADC5 5
#define ADC6 6
#define ADC7 7
 
ISR(ADC_vect)
{
static uint8_t ad_channel = ADC0, state = 0;
 
// state machine
switch(state++)
{
case 0:
Adc0 = ADC;
ad_channel = ADC1;
break;
case 1:
Adc1 = ADC;
ad_channel = ADC2;
break;
case 2:
Adc2 = ADC;
ad_channel = ADC3;
break;
case 3:
Adc3 = ADC;
ad_channel = ADC4;
break;
case 4:
Adc4 = ADC;
ad_channel = ADC5;
break;
case 5:
Adc5 = ADC;
ad_channel = ADC6;
break;
case 6:
Adc6 = ADC;
ad_channel = ADC7;
break;
case 7:
Adc7 = ADC;
ad_channel = ADC0;
state = 0;
ADReady = 1;
break;
default:
ad_channel = ADC0;
state = 0;
ADReady = 1;
break;
}
// set adc muxer to next ad_channel
ADMUX = (ADMUX & 0xE0) | ad_channel;
// after full cycle stop further interrupts
if(state != 0) ADC_Enable();
}
/FollowMe/analog.h
0,0 → 1,20
#ifndef _ANALOG_H
#define _ANALOG_H
 
#include <inttypes.h>
 
extern volatile int16_t Adc0, Adc1, Adc2, Adc3, Adc4, Adc5, Adc6, Adc7;
extern volatile uint8_t ADReady;
 
void ADC_Init(void);
 
 
// clear ADC enable & ADC Start Conversion & ADC Interrupt Enable bit
#define ADC_Disable() (ADCSRA &= ~((1<<ADEN)|(1<<ADSC)|(1<<ADIE)))
// set ADC enable & ADC Start Conversion & ADC Interrupt Enable bit
#define ADC_Enable() (ADCSRA |= (1<<ADEN)|(1<<ADSC)|(1<<ADIE))
 
 
#endif //_ANALOG_H
 
 
/FollowMe/main.c
62,7 → 62,12
#include "led.h"
#include "menu.h"
#include "printf_P.h"
#include "analog.h"
 
#ifdef USE_FOLLOWME
int16_t UBat = 120;
#endif
 
int main (void)
{
// disable interrupts global
75,11 → 80,11
 
// initalize modules
LED_Init();
 
LEDRED_ON;
TIMER0_Init();
USART0_Init();
USART1_Init();
ADC_Init();
// enable interrupts global
sei();
LEDRED_OFF;
121,6 → 126,30
{
USART0_ProcessRxData();
USART0_TransmitTxData();
// restart ADConversion if ready
if(ADReady)
{
DebugOut.Analog[0] = Adc0;
DebugOut.Analog[1] = Adc1;
DebugOut.Analog[2] = Adc2;
DebugOut.Analog[3] = Adc3;
DebugOut.Analog[4] = Adc4;
DebugOut.Analog[5] = Adc5;
DebugOut.Analog[6] = Adc6;
DebugOut.Analog[7] = Adc7;
 
#ifdef USE_FOLLOWME
// AVcc = 5V --> 5V = 1024 counts
// the voltage at the voltage divider reference point is 0.8V less that the UBat
// because of the silicon diode inbetween.
// voltage divider R2=10K, R3=1K
// UAdc4 = R3/(R3+R2)*(UBat-0.8V) = 1k/(1k+10k)*(UBat-0.8V) = (UBat-0.8V)/11
UBat = (3 * UBat + (69 * Adc4) / 128 + 8) / 4;
DebugOut.Analog[8] = UBat;
#endif
ADReady = 0;
ADC_Enable();
}
}
return (1);
}
/FollowMe/makefile
76,7 → 76,7
 
##########################################################################################################
# List C source files here. (C dependencies are automatically generated.)
SRC = main.c uart0.c uart1.c printf_P.c timer0.c menu.c led.c ubx.c
SRC = main.c uart0.c uart1.c printf_P.c timer0.c menu.c led.c ubx.c analog.c
#ssc.c sdc.c fat16.c
 
##########################################################################################################
/FollowMe/uart0.c
58,15 → 58,15
const uint8_t ANALOG_LABEL[32][16] =
{
//1234567890123456
"Debug0 ", //0
"Debug1 ",
"Debug2 ",
"Debug3 ",
"Debug4 ",
"Debug5 ", //5
"Debug6 ",
"Debug7 ",
"Debug8 ",
"Analog Ch0 ", //0
"Analog Ch1 ",
"Analog Ch2 ",
"Analog Ch3 ",
"Analog Ch4 ",
"Analog Ch5 ", //5
"Analog Ch6 ",
"Analog Ch7 ",
"UBat ",
"Debug9 ",
"Debug10 ", //10
"Debug11 ",