Subversion Repositories MK3Mag

Compare Revisions

Regard whitespace Rev 18 → Rev 19

/branches/MK3Mag V0.14 Code Redesign Killagreg/analog.c
54,29 → 54,42
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//#include <stdlib.h>
#include "analog.h"
 
#include "main.h"
 
//############################################################################
//Init ADC
void ADC_Init(void)
{
ADMUX = 0;
ADCSRA = 0x87;
ADMUX = 0;
ADCSRA |= 0x40; // Start Conversion
// set PortC 0,1,2,3 as input
DDRC &= ~((1<<DDC3)|(1<<DDC2)|(1<<DDC1)|(1<<DDC0));
// set PortC 0,1,2,3 and 6,7 as tri state
PORTC &= ~((1<<PORTC3)|(1<<PORTC2)|(1<<PORTC1)|(1<<PORTC0));
 
// port PD5 and PD6 control the current direction of the test coils of the sensors
DDRD |= ((1<<DDD5)|(1<<DDD6));
FLIP_LOW;
 
// disable internal reference voltage and right adjust bits in ADCW
ADMUX = 0x00;
// clear start conversion bit (ADSC = 0)
// disable ADC Auto Trigger Enable (ADATE = 0)
// disable interrupt (ADIE = 0)
ADCSRA &= ~((1<<ADSC)|(1<<ADATE)|(1<<ADIE));
// Enable ADC (ADEN = 1) with SYSCLK/128 (ADPS2 = 1, ADPS1 = 1, ADPS0 = 1) and clear ready flag (ADIF = 1)
ADCSRA |= ((1<<ADEN)|(1<<ADIF)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));
ADMUX = 0x00; // select ADC0
ADCSRA |= (1<<ADSC); // start conversion
}
 
 
unsigned int MessAD(unsigned char channel)
uint16_t ADC_GetValue(ADChannel_t channel)
{
unsigned int messwert = 0;
ADMUX = channel;
ADCSRA |= 0x10; // Clear Ready-Bit
ADCSRA |= 0x40; // Start Conversion
while (((ADCSRA & 0x10) == 0));
messwert = ADCW;
return(messwert);
uint16_t value = 0;
ADMUX = channel; // set muxer bits
ADCSRA |= (1<<ADIF); // clear ready-flag
ADCSRA |= (1<<ADSC); // start conversion
while (((ADCSRA & (1<<ADIF)) == 0)); // wait for end of conversion
value = ADCW; // read adc result
return(value);
}