Implemented reading multiple analog value.

This commit is contained in:
Marvin Blum
2017-03-26 14:59:59 +02:00
parent 3c4451e1bb
commit 885fe1d624
2 changed files with 12 additions and 13 deletions

8
main.c
View File

@@ -27,7 +27,8 @@ void prepare(){
pin_mode(8, INPUT);*/ pin_mode(8, INPUT);*/
// anlog input example // anlog input example
pin_mode(A4, INPUT); pin_mode(A1, INPUT);
pin_mode(A2, INPUT);
} }
void loop(){ void loop(){
@@ -40,9 +41,10 @@ void loop(){
}*/ }*/
// anlog input example // anlog input example
int analog = analog_read(A4); int analog1 = analog_read(A1);
int analog2 = analog_read(A2);
char out[15]; char out[15];
sprintf(out, "%d", analog); sprintf(out, "%d %d", analog1, analog2);
serial_write(out, 15); serial_write(out, 15);
_delay_ms(25); _delay_ms(25);

17
pins.c
View File

@@ -17,8 +17,6 @@ const unsigned char A5 = 0x13;
const unsigned char A6 = 0x14; const unsigned char A6 = 0x14;
const unsigned char A7 = 0x15; const unsigned char A7 = 0x15;
double dutyCycle = 0; // TODO more than one???
void analog_read_duty_cycle(); void analog_read_duty_cycle();
unsigned char map_analog_pin(unsigned char); unsigned char map_analog_pin(unsigned char);
@@ -114,16 +112,15 @@ unsigned int analog_read(unsigned char pin){
ADCSRA = _BV(ADEN)|_BV(ADIE)|0x07; ADCSRA = _BV(ADEN)|_BV(ADIE)|0x07;
ADMUX = _BV(REFS0)|pin; ADMUX = _BV(REFS0)|pin;
DIDR0 |= _BV(pin); DIDR0 |= _BV(pin);
// while(!(ADCSRA&_BV(ADIF))); ADCSRA |= _BV(ADSC); // start
analog_read_duty_cycle();
// wait for it to finish
while(ADCSRA&_BV(ADSC));
// return 10 bit value
return ADC; return ADC;
} }
void analog_read_duty_cycle(){
ADCSRA |= _BV(ADSC);
}
void analog_write(unsigned char pin, unsigned int value){ void analog_write(unsigned char pin, unsigned int value){
pin = map_analog_pin(pin); pin = map_analog_pin(pin);
@@ -148,7 +145,7 @@ unsigned char map_analog_pin(unsigned char pin){
return pin; return pin;
} }
// analog to digital converter interrupt handler
ISR(ADC_vect){ ISR(ADC_vect){
dutyCycle = ADC; // required but not used...
analog_read_duty_cycle();
} }