diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2011-10-13 20:33:30 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2011-10-13 20:33:30 +0200 |
commit | 5d9d373a959271b498710817d08c2ea8e153aa6e (patch) | |
tree | 6867e887454a850b4e15616000ef342a1c661c7e /firmware/adc.c | |
parent | 35a36b0a6c69ce136fd6ea2a203d34e48e1291e8 (diff) | |
download | mini-octopus-5d9d373a959271b498710817d08c2ea8e153aa6e.tar.gz mini-octopus-5d9d373a959271b498710817d08c2ea8e153aa6e.zip |
correct firmware??
Diffstat (limited to 'firmware/adc.c')
-rw-r--r-- | firmware/adc.c | 152 |
1 files changed, 71 insertions, 81 deletions
diff --git a/firmware/adc.c b/firmware/adc.c index 915dbce..a9449b7 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -37,27 +37,19 @@ #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(buf[2]); - break; - case CMD_ADC_DEINIT_PIN: - adc_deinit_usb(buf[2]); - break; + adc_init_usb((int)buf[2]); + break; case CMD_ADC_GET: - adc_get_usb(buf[2]); - break; + adc_get_usb((int)buf[2]); + break; case CMD_ADC_REF: - adc_ref_usb(buf[2]); - break; + adc_ref_usb((int)buf[2]); + break; default: answer[0]=buf[0]; @@ -67,49 +59,42 @@ void adc_parser(char *buf) } } -int adc_init(uint8_t pin) +int adc_init(int 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; + 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; } -void adc_init_usb(uint8_t pin) +void adc_init_usb(int pin) { answer[0]=CMD_ADC_INIT_PIN; - answer[1]=(unsigned char)adc_init(pin); - answer[2]=0; - - CommandAnswer(3); -} - -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[1]= (unsigned char)adc_init(pin); answer[2]=0; CommandAnswer(3); } -void adc_get_usb(uint8_t pin) +void adc_get_usb(int pin) { + uint16_t result; //uint8_t result[2]; @@ -125,59 +110,64 @@ void adc_get_usb(uint8_t pin) } -int adc_get(uint8_t pin, uint16_t *value) +int adc_get(int pin, uint16_t *value) { - *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; + *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; + } } -void adc_ref_usb(uint8_t ref) +void adc_ref_usb(int ref) { answer[0]=CMD_ADC_REF; answer[1]=adc_ref(ref); - answer[2]=0; - CommandAnswer(3); + CommandAnswer(2); } -int adc_ref(uint8_t ref) +int adc_ref(int 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(uint8_t mux) +uint16_t _adc_read_channel(int 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 = (ADMUX & 0xF0) | mux; // select channel + //ADMUX |= (unsigned char)mux; // Kanal waehlen + ADMUX = (ADMUX & 0xF0) | (unsigned char) mux; // select channel //ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen |