This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathanalog.c
executable file
·62 lines (48 loc) · 1.61 KB
/
analog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ADC functions for attiny13.
* Author: Guido Socher, Copyright: GPL
* http://tuxgraphics.org/electronics/
*/
#include <avr/io.h>
#include <math.h>
void adcOn(void)
{
// Set up the ADC and enable the interrupt.
// Use Vcc as Vref.
ADMUX = 0x00;
DIDR0 = (1<<ADC0D)|(1<<ADC1D); // Disable the digital circuitry on the ADC inputs.
// Enable the A/D Converter, set the prescaler div8 (125kHz), enable the interrupt.
ADCSRA=(1<<ADEN)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE);
}
void adcOff(void)
{
ADCSRA &= ~((1<<ADEN)|(1<<ADIE));
// Shut off the ADC and disable the interrupt.
// This lowers the power consumption of the uC.
}
// convert adc reading to voltage (readout is multiplied by 10)
uint8_t analog2v1(uint16_t aval)
{
//double adcval = (double)aval;
// ADCout * Vref * Vdiv
// Voltage = ---------------------- * Calibration Value
// 1024
//
//return (uint8_t)(((double)aval * 1.2) - 73.6); // These numbers were generated by calibrating against two known voltage reads.
return (uint8_t)(((double)aval * (double)0.7143) + (double)1.5714);
//return((uint8_t)r);
}
uint8_t analog2v2(uint16_t aval)
{
return (uint8_t)(((double)aval * (double)0.7143) + (double)1.5714); // These numbers were generated by calibrating against two known voltage reads.
}
// Start the ADC conversion. Results are handled by an interrupt in main.c.
void startConvert(uint8_t channel)
{
// Set channel
ADMUX &= ~0x1F; // Clear out existing channel value.
ADMUX |= (channel & 0x1F); // Fill in new channel.
// start conversion
if(bit_is_clear(ADCSRA,ADSC))
ADCSRA |= (1<<ADSC);
}