From: Philipp Klaus Date: Thu, 6 Aug 2015 09:30:23 +0000 (+0200) Subject: statemachine and ADC (internal and SPI) added X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=ee77d4b4b5ba7d4223f454487e44ff1fd6c1181a;p=avr.git statemachine and ADC (internal and SPI) added --- diff --git a/pt100/ads_adc.h b/pt100/ads_adc.h new file mode 100644 index 0000000..9e53877 --- /dev/null +++ b/pt100/ads_adc.h @@ -0,0 +1,23 @@ + + +//// ADC ADS1018 Definitions +// SS "Single-shot conversion start" +#define ADS_SConv (1 << 15) +// MUX "Input multiplexer" +#define ADS_MUX_AIN1_GND (0b101 << 12) +#define ADS_MUX_AIN0_AIN3 (0b001 << 12) +// PGA "programmable gain amplifier" +#define ADS_GAIN_4V (0b001 << 9) +#define ADS_GAIN_2V (0b010 << 9) +#define ADS_GAIN_1V (0b011 << 9) +#define ADS_GAIN_025V (0b111 << 9) +// MODE "device operating mode" +#define ADS_MODE_S (1 << 8) +// DR "Data rate" +#define ADS_1600SPS (0b100 << 5) +#define ADS_128SPS (0b000 << 5) +// PULL_UP_EN "Pull-up enable" +#define ADS_PULLUP_DRDY (1 << 3) +// NOP "No operation" +#define ADS_WRITE_CONFIG (0b01 << 1) + diff --git a/pt100/main.c b/pt100/main.c index e54de4d..c2b4f07 100644 --- a/pt100/main.c +++ b/pt100/main.c @@ -67,12 +67,12 @@ void init(void) { //CPU Regs //Init ADC SPI - SPCR = (0 << SPIE) | (1 << SPE ) | (0 << DORD) | (1 << MSTR) | (0 << CPOL) | (0 << CPHA) | (0 << SPR0); - SPSR = (1 << SPI2X); //fcpu/8 + SPCR = (0 << SPIE) | (1 << SPE ) | (0 << DORD) | (1 << MSTR) | (0 << CPOL) | (1 << CPHA) | (0 << SPR0); + SPSR = (1 << SPI2X); //fcpu/2 DDRB |= (1 << PB7) | (1 << PB5) | (1 << PB4); //ADC has three control lines //Init ADC for presence measurement - ADMUX = (3 << REFS0) | (1 << ADLAR) | (3 << MUX0); + ADMUX = (3 << REFS0) | (1 << ADLAR) | (3 << MUX0); // internal2.56V ADC3 ADCSRA = (1 << ADEN) | (0 << ADIE) | (5 << ADPS0); //fcpu/32 DIDR0 = 0x08; //PA3 is analog only diff --git a/pt100/tempmeas.c b/pt100/tempmeas.c index 8a1b0c9..93d93d3 100644 --- a/pt100/tempmeas.c +++ b/pt100/tempmeas.c @@ -1,8 +1,10 @@ #include "main.h" +#include "ads_adc.h" +#define CONNECTED_SENSORS 8 -uint8_t connected_sensors = 0x1a; uint8_t measurement_step; +uint8_t current_channel = 0; //factors: gain_current has 2^29, ADC has 2^7 steps per mV @@ -62,14 +64,76 @@ int32_t res_to_temp(int16_t res) { //34 read ADC temperature as 9th channel //35 send ADC temperature - void do_measurement_step(void) { - if(measurement_step == 0) { - LED1_ON(); - } - else if(measurement_step == 36) { - measurement_active = 0; - LED1_OFF(); + if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x0) { + LED1_ON(); + select_channel(); + } + else if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x1) { + ADCSRA |= (1< compare to 2.27 V or (2.27/2.56)*2^16 = 58112 + if (x < 58112) { + // read value + x = SPI_transceive_16bit(0x0000); + x = res_to_temp(adc_to_res(x)); + void send_answer_hex(0, 0, x); + } + else { + void send_answer_hex(0, 0, (1<< (5*4))); + } + else if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x3) { + selected_channel += 1; + } + measurement_step += 1; + if(measurement_step == 36) measurement_step = 0; + selected_channel = 0 + LED1_OFF(); + measurement_active = 0; + } + + +void set_inhibit_signals(int on_off) { + PORTC = (PORTC & 0x3F) | (on_off << 6); + } + +void select_channel() { + if (current_channel == 8) { + // temperature measurement + set_inhibit_signals(0); } + else { + // update the select outputs + PORTA = (PORTA & 0x8F) | (current_channel << 4); + // clear inhibit signals + set_inhibit_signals(0); + } + } + +uint16_t SPI_transceive_16bit(uint16_t data){ + char high, low; + high = SPI_transceive(data>>8); + low = SPI_transceive(data&0xFF); + return (high << 8) | low; } + +char SPI_transceive(unsigned char cData){ + SPDR = cData; + while (!(SPSR & (1<