diff options
Diffstat (limited to 'firmware/adc.c')
-rw-r--r-- | firmware/adc.c | 152 |
1 files changed, 81 insertions, 71 deletions
diff --git a/firmware/adc.c b/firmware/adc.c index a9449b7..915dbce 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -37,19 +37,27 @@ #include "protocol.h" #include "adc.h" +#include <stdio.h> +#include "debug.h" + + void adc_parser(char *buf) { + //debugf("adc parser CMD %02x\n",buf[0]); switch(buf[0]) { case CMD_ADC_INIT_PIN: - adc_init_usb((int)buf[2]); - break; + adc_init_usb(buf[2]); + break; + case CMD_ADC_DEINIT_PIN: + adc_deinit_usb(buf[2]); + break; case CMD_ADC_GET: - adc_get_usb((int)buf[2]); - break; + adc_get_usb(buf[2]); + break; case CMD_ADC_REF: - adc_ref_usb((int)buf[2]); - break; + adc_ref_usb(buf[2]); + break; default: answer[0]=buf[0]; @@ -59,42 +67,49 @@ void adc_parser(char *buf) } } -int adc_init(int pin) +int adc_init(uint8_t pin) { - if (pin >= 33 && pin <= 40) - { - octopus.ports[pin] = PIN_AD; - - switch(pin) - { - case 40: break; - case 39: break; - case 38: break; - case 37: break; - case 36: break; - case 35: break; - case 34: break; - case 33: break; - default: - return RSP_IMPOSSIBLE_PIN_CONFIG; - } - return RSP_OK; - } - return RSP_UNKOWN_PIN; + if (pin >= 10 && pin <= 17) + { + octopus.ports[pin] = PIN_AD; + ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen + return RSP_OK; + } + + return RSP_UNKOWN_PIN; } -void adc_init_usb(int pin) +void adc_init_usb(uint8_t pin) { answer[0]=CMD_ADC_INIT_PIN; - answer[1]= (unsigned char)adc_init(pin); + answer[1]=(unsigned char)adc_init(pin); answer[2]=0; CommandAnswer(3); } -void adc_get_usb(int pin) +int adc_deinit(uint8_t pin) +{ + if (octopus.ports[pin] == PIN_AD) + { + octopus.ports[pin] = PIN_NONE; + return RSP_OK; + } + + return RSP_UNKOWN_PIN; +} + +void adc_deinit_usb(uint8_t pin) +{ + answer[0]=CMD_ADC_DEINIT_PIN; + answer[1]= (unsigned char)adc_deinit(pin); + answer[2]=0; + + CommandAnswer(3); +} + +void adc_get_usb(uint8_t pin) { - uint16_t result; //uint8_t result[2]; @@ -110,64 +125,59 @@ void adc_get_usb(int pin) } -int adc_get(int pin, uint16_t *value) +int adc_get(uint8_t pin, uint16_t *value) { - *value = 0; - - if (octopus.ports[pin] == PIN_AD) - { - switch (pin) - { - case 40: *value =_adc_read_channel(0); break; - case 39: *value =_adc_read_channel(1); break; - case 38: *value =_adc_read_channel(2); break; - case 37: *value =_adc_read_channel(3); break; - case 36: *value =_adc_read_channel(4); break; - case 35: *value =_adc_read_channel(5); break; - case 34: *value =_adc_read_channel(6); break; - case 33: *value =_adc_read_channel(7); break; - default: - return RSP_IMPOSSIBLE_PIN_CONFIG; - } - return RSP_OK; - } - else - { - return RSP_WRONG_PIN_CONFIG; - } + *value = 0; + + if (octopus.ports[pin] == PIN_AD) + { + // 40 => 0 + // 39 => 1 + // 38 => 2 + // pin => 40-pin + *value =_adc_read_channel(17 - pin); + debugf("Wert %i\r\n",*value); + return RSP_OK; + } + else + return RSP_WRONG_PIN_CONFIG; } -void adc_ref_usb(int ref) +void adc_ref_usb(uint8_t ref) { answer[0]=CMD_ADC_REF; answer[1]=adc_ref(ref); - CommandAnswer(2); + answer[2]=0; + CommandAnswer(3); } -int adc_ref(int ref) +int adc_ref(uint8_t ref) { - ADMUX = 0x00; - - switch(ref){ - case PARAM_ADC_AREF: break; - case PARAM_ADC_AVCC: ADMUX |= (1<<REFS0); break; - case PARAM_ADC_INTERNAL: ADMUX |= (1<<REFS1) | (1<<REFS0); break; - default: - return RSP_ERROR; - } - return RSP_OK; + ADMUX = 0x00; + + switch(ref) + { + case PARAM_ADC_AREF: break; + case PARAM_ADC_AVCC: ADMUX |= (1<<REFS0); break; + case PARAM_ADC_INTERNAL: ADMUX |= (1<<REFS1) | (1<<REFS0); break; + default: + return RSP_ERROR; + } + + return RSP_OK; } -uint16_t _adc_read_channel(int mux) +uint16_t _adc_read_channel(uint8_t mux) { int i; uint16_t result; + + //ADMUX |= mux; // Kanal waehlen ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // Frequenzvorteiler // setzen auf 8 (1) und ADC aktivieren (1) - //ADMUX |= (unsigned char)mux; // Kanal waehlen - ADMUX = (ADMUX & 0xF0) | (unsigned char) mux; // select channel + ADMUX = (ADMUX & 0xF0) | mux; // select channel //ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen |