diff --git a/Makefile b/Makefile index a36911e..7983022 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ RESETPORT = /dev/ttyU0 TARGET = sonde -SRC = main.c debug.c analog.c rf24.c spi.c lib/Hamming.c lib/HammingCalculateParitySmallAndFast.c onewire.c +SRC = main.c debug.c rf24.c analog.c spi.c lib/Hamming.c lib/HammingCalculateParitySmallAndFast.c onewire.c CXXSRC = ASRC = uart.S MCU = attiny84a diff --git a/main.c b/main.c index ba4b47e..287a998 100644 --- a/main.c +++ b/main.c @@ -49,7 +49,7 @@ ISR(PCINT0_vect){ pcint_flag = true; } - +// Put in sleep mode until next ISR void sleep(){ #ifdef DEBUG @@ -75,9 +75,9 @@ void sleep(){ #endif } - -void wait_for_next(){ - int cnt=6; +// Sleep for c*8s + 0-6s +void wait_for_next(int c){ + int cnt=c; #ifdef DEBUG cnt=2; #endif @@ -93,18 +93,20 @@ void wait_for_next(){ sleep(); } #ifdef DEBUG - debughex(&totaltime,2); + debughex((char *)(&totaltime),2); totaltime=0; debug("\r"); #endif } +// Energy saving mode void power_down(){ stopADC(); power_all_disable(); } +// Wait until radio tx done & poweroff void wait_for_radio(){ wdt_enable(WDTO_120MS); @@ -131,8 +133,8 @@ void wait_for_radio(){ return; } +// Load nRF lib setup in Ram void setupRadio(){ - // Static values in ram. rf24_CONFIG = 0; rf24_RF_CH = 90; rf24_RF_SETUP = 0x26; @@ -144,6 +146,7 @@ void setupRadio(){ rf24_EN_AA = 0x00; } +// Restart USI & Reinit nRF24 void initRadio(){ power_usi_enable(); rf24_init(); @@ -151,6 +154,8 @@ void initRadio(){ rf24_writeRegister(TX_ADDR,(uint8_t*)ADDR,5); } +// **** +// Transmit data buffer void txData(){ uint8_t crc,cpt,a,b,len; uint8_t *in, *out; @@ -201,8 +206,9 @@ int main(void) debug("Sonde Start \r"); #endif - uint8_t rvcc=0; - + uint8_t rvcc=0,rtx=0,delay=6; + + uint16_t t_i, lt_i=100; int vcc; // Setup power_down(); @@ -219,59 +225,70 @@ int main(void) while(1){ - + // each 10 runs => read VCC if(!(rvcc--)){ initADC(); - // Read VCC vcc = readVcc(); stopADC(); - rvcc=4; + rvcc=9; } - // Do 8 temp reads over 4s - /* int i = 7; - long lt=readtemp(); - while(i--){ - power_down(); - wdt_enable(WDTO_500MS); - WDTCSR |= _BV(WDIE); - sleep(); - power_up(); - initADC(); - lt += readtemp(); - } - wdt_enable(WDTO_8S); - float temp = calctemp(lt / 8);*/ - + // PowerUp DS18B20 w1_start(); + + // Wait 15ms (for DS18B20 to start) wdt_enable(WDTO_15MS); WDTCSR |= _BV(WDIE); sleep(); + // Start DS18B20 Temp conversion start_meas(); + // Wait for 1s wdt_enable(WDTO_1S); WDTCSR |= _BV(WDIE); sleep(); - uint16_t t_i = read_meas();//=temp*100; + // Read temp & stop DS18B20 + t_i = read_meas();//=temp*100; w1_stop(); - // Send data - initRadio(); + // If temp change more than 1 since last tx + if(t_i > lt_i+1 || t_i < lt_i -1){ + // Force Tx + rtx=0; + } else { + delay=6; + } - buffer[0] = 0x0A | CMD_BIN; // 10 octets : - *(uint16_t*)(buffer+4) = 0x0002; // BIN v2 = vcc+tempDS - *(uint16_t*)(buffer+6) = vcc; // 2 o - *(uint16_t*)(buffer+8) = t_i; // 2 o - txData(buffer); + // If temp change more than 2 => reduced delay + if(t_i > lt_i+2 || t_i < lt_i -2){ + // Force Tx + delay=2; + } - //radio off - rf24_powerDown(); + if(!(rtx--)){ + + // Send data + initRadio(); + + buffer[0] = 0x0A | CMD_BIN; // 10 octets : + *(uint16_t*)(buffer+4) = 0x0002; // BIN v2 = vcc+tempDS + *(uint16_t*)(buffer+6) = vcc; // 2 o + *(uint16_t*)(buffer+8) = t_i; // 2 o + txData(buffer); + + // keep tx temp + lt_i = t_i; + rtx=5; + + //radio off + rf24_powerDown(); + } power_down(); - wait_for_next(); + wait_for_next(delay); } }