modified adc stuff to implement passing channels, added channels and started implementing read-temp

master
Dario Ernst 15 years ago
parent 1a824d85dd
commit 2d0d119786

@ -8,9 +8,6 @@ Where following values are Valid:
|--------------------------------------------------------------------------------------------------------------------------| |--------------------------------------------------------------------------------------------------------------------------|
| Decimal Hex Name Address ValueL ValueH Reply Description| | Decimal Hex Name Address ValueL ValueH Reply Description|
|--------------------------------------------------------------------------------------------------------------------------| |--------------------------------------------------------------------------------------------------------------------------|
| 0000001 0x01 Read-Temp Number of zeroes/Rand zeroes/Rand A 16Bit Value Reads the |
| Thermometer Representing Temperature|
| Starting with 0 the Temperature |
|--------------------------------------------------------------------------------------------------------------------------| |--------------------------------------------------------------------------------------------------------------------------|
| 0000002 0x02 Read-Var8 Number of the zeroes/Rand zeroes/Rand A 8Bit Value Reads 8Bit | | 0000002 0x02 Read-Var8 Number of the zeroes/Rand zeroes/Rand A 8Bit Value Reads 8Bit |
| var-DEFINE read from VAR Variable | | var-DEFINE read from VAR Variable |
@ -26,5 +23,10 @@ Where following values are Valid:
|--------------------------------------------------------------------------------------------------------------------------| |--------------------------------------------------------------------------------------------------------------------------|
| 0000006 0x06 Call-Func Number of the zeroes/Rand zeroes/Rand zeroes/Rand Calls a| | 0000006 0x06 Call-Func Number of the zeroes/Rand zeroes/Rand zeroes/Rand Calls a|
| func-DEFINE remote | | func-DEFINE remote |
function | | function |
|--------------------------------------------------------------------------------------------------------------------------|
| 0000001 0x01 Read-Temp Number of zeroes/Rand zeroes/Rand A 16Bit Value Reads the |
| Thermometer Representing Temperature|
| Starting with 0 the Temperature |
|--------------------------------------------------------------------------------------------------------------------------| |--------------------------------------------------------------------------------------------------------------------------|

@ -1,5 +1,6 @@
#include "spi_proto.h" #include "spi_proto.h"
#include "spi.h" #include "spi.h"
#include "main.h"
/***** MASTER *****/ /***** MASTER *****/
@ -90,8 +91,9 @@ uint16_t spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) {
//TODO: prevent a function from being run several times because the message gets repeated. //TODO: prevent a function from being run several times because the message gets repeated.
break; break;
case 7: case 7:
//TODO if(addr < 4) {
retval = 0; retval = temperatures[addr];
} else return 0;
break; break;
default: default:
break; break;

@ -6,18 +6,11 @@
#include "debug.h" #include "debug.h"
uint8_t foo;
uint16_t bar;
uint16_t timertmp; uint16_t timertmp;
uint16_t temperatures[4]; uint16_t temperatures[4];
uint8_t sensor_active[4];
int32_t temp_avg_cumul; int32_t temp_avg_cumul[4];
int16_t temp_avg_count; int16_t temp_avg_count[4];
void baz() {
foo++;
}
/* pinout /* pinout
* - i2c: * - i2c:
@ -36,9 +29,8 @@ void baz() {
#define MUXER_CHANNEL_2 1 #define MUXER_CHANNEL_2 1
#define MUXER_CHANNEL_3 3 #define MUXER_CHANNEL_3 3
void hardinit() {
/* initializes the hardware */ /* initializes the hardware */
void hardinit() {
// enable softtimer isr // enable softtimer isr
TCCR1A = _BV(WGM10); TCCR1A = _BV(WGM10);
TCCR1B = _BV(WGM12) | _BV(CS11); // clk/8 prescaler, 4kHz PWM-freq. TCCR1B = _BV(WGM12) | _BV(CS11); // clk/8 prescaler, 4kHz PWM-freq.
@ -47,53 +39,45 @@ void hardinit() {
spi_init(); spi_init();
muxer_init(); muxer_init();
i2c_init(); i2c_init();
sei(); sei();
} }
void softinit() { void softinit() {
//test values
foo = 0x87;
bar= 0xfafa;
mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16); mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16);
} }
int __attribute__((noreturn)) main(void) { int __attribute__((noreturn)) main(void) {
temperatures[2]=1;
hardinit(); hardinit();
temperatures[2]=10;
softinit(); softinit();
muxer_set(MUXER_CHANNEL_0); muxer_set(MUXER_CHANNEL_0);
temperatures[2]=0;
dbgLog("Hallo, Welt!\n");
for(;;){ for(;;){
SOFTTIMER(1,10) { SOFTTIMER(1,10) {
if(mcpadc_has_new_data()) { for(int i=0; i<4; i++) {
temperatures[0] = mcpadc_get_data(); if(sensor_active[i] && mcpadc_has_new_data(i)) {
temperatures[2]++; temperatures[i] = mcpadc_get_data(i);
int16_t temp = temperatures[0]; temp_avg_cumul[i] += temperatures[i];
temp_avg_cumul += temp; temp_avg_count[i] += 1;
temp_avg_count += 1; }
} }
temperatures[1] = 22;
} }
SOFTTIMER(2,500){ SOFTTIMER(2,500){
int32_t temp = temp_avg_cumul; for(int i=0; i<4; i++) {
temp /= temp_avg_count; if(sensor_active[i]) {
temp_avg_count = 0; temp_avg_cumul = 0; int32_t temp = temp_avg_cumul[i];
temp /= temp_avg_count[i];
temp_avg_count[i] = 0; temp_avg_cumul[i] = 0;
int32_t nV = (temp * 625); int32_t nV = (temp * 625);
int32_t mK = nV/39; int32_t mK = nV/39;
dbgLog("temp-%i: %6li (%li µV, %li mK)\n",i,temp,nV/1000, mK);
dbgLog("temp: %6li (%li µV, %li mK)\n",temp,nV/1000, mK); }
}
} }
} }

@ -14,11 +14,7 @@
#include "softtimer.h" #include "softtimer.h"
#include "debug.h" #include "debug.h"
extern uint8_t foo;
extern uint16_t bar;
extern uint16_t timertmp; extern uint16_t timertmp;
extern uint16_t temperatures[]; extern uint16_t temperatures[];
void baz();
#endif //__MAIN_H #endif //__MAIN_H

@ -42,8 +42,10 @@ void mcpadc_init(uint8_t mode)
curmode = mode; curmode = mode;
} }
uint8_t mcpadc_has_new_data(void) uint8_t mcpadc_has_new_data(uint8_t channel)
{ {
// TODO: implement channel
/* this assumes that the first RDY bit read after the sample data indicates the old/new state of the sample we just read */ /* this assumes that the first RDY bit read after the sample data indicates the old/new state of the sample we just read */
if(unread_data_available){ if(unread_data_available){
// we already have new data, don't overwrite that by reading more // we already have new data, don't overwrite that by reading more
@ -69,8 +71,9 @@ uint8_t mcpadc_has_new_data(void)
} }
#if ADC_ENABLE_18_BIT_MODE #if ADC_ENABLE_18_BIT_MODE
int32_t mcpadc_get_data(void) int32_t mcpadc_get_data(uint8_t channel)
{ {
// TODO: implement channel
if(!unread_data_available){ if(!unread_data_available){
return 0; return 0;
} }
@ -86,7 +89,7 @@ int32_t mcpadc_get_data(void)
return value; return value;
} }
#else #else
int16_t mcpadc_get_data(void) int16_t mcpadc_get_data(uint8_t channel)
{ {
if(!unread_data_available){ if(!unread_data_available){
return 0; return 0;

@ -26,15 +26,15 @@ void mcpadc_init(uint8_t mode);
/* tests if the adc has a new sample /* tests if the adc has a new sample
* returns nonzero if new data is aviable, 0 otherwise * returns nonzero if new data is aviable, 0 otherwise
*/ */
uint8_t mcpadc_has_new_data(void); uint8_t mcpadc_has_new_data(uint8_t channel);
/* returns the most recent sample result of the adc /* returns the most recent sample result of the adc
* !! return type depends on defines * !! return type depends on defines
*/ */
#if ADC_ENABLE_18_BIT_MODE #if ADC_ENABLE_18_BIT_MODE
int32_t mcpadc_get_data(void); int32_t mcpadc_get_data(uint8_t channel);
#else #else
int16_t mcpadc_get_data(void); int16_t mcpadc_get_data(uint8_t channel);
#endif #endif
/* instructs the adc to take a new sample now /* instructs the adc to take a new sample now

@ -7,18 +7,9 @@ uint8_t *spi_proto_globals8[] = {
}; };
uint16_t *spi_proto_globals16[] = { uint16_t *spi_proto_globals16[] = {
&timertmp,
&temperatures[0],
&temperatures[1],
&temperatures[2],
&temperatures[3],
}; };
funptr_t spi_proto_funcs[] = { funptr_t spi_proto_funcs[] = {
&dbgHasNew, &dbgHasNew,
&dbgAdvanceChar &dbgAdvanceChar
}; };

Loading…
Cancel
Save