Ajout Transmission conditionelle

This commit is contained in:
Tzim 2018-01-31 15:43:10 +01:00
parent aad2cc40d4
commit bacca08b9a
2 changed files with 55 additions and 38 deletions

View file

@ -54,7 +54,7 @@ RESETPORT = /dev/ttyU0
TARGET = sonde 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 = CXXSRC =
ASRC = uart.S ASRC = uart.S
MCU = attiny84a MCU = attiny84a

89
main.c
View file

@ -49,7 +49,7 @@ ISR(PCINT0_vect){
pcint_flag = true; pcint_flag = true;
} }
// Put in sleep mode until next ISR
void sleep(){ void sleep(){
#ifdef DEBUG #ifdef DEBUG
@ -75,9 +75,9 @@ void sleep(){
#endif #endif
} }
// Sleep for c*8s + 0-6s
void wait_for_next(){ void wait_for_next(int c){
int cnt=6; int cnt=c;
#ifdef DEBUG #ifdef DEBUG
cnt=2; cnt=2;
#endif #endif
@ -93,18 +93,20 @@ void wait_for_next(){
sleep(); sleep();
} }
#ifdef DEBUG #ifdef DEBUG
debughex(&totaltime,2); debughex((char *)(&totaltime),2);
totaltime=0; totaltime=0;
debug("\r"); debug("\r");
#endif #endif
} }
// Energy saving mode
void power_down(){ void power_down(){
stopADC(); stopADC();
power_all_disable(); power_all_disable();
} }
// Wait until radio tx done & poweroff
void wait_for_radio(){ void wait_for_radio(){
wdt_enable(WDTO_120MS); wdt_enable(WDTO_120MS);
@ -131,8 +133,8 @@ void wait_for_radio(){
return; return;
} }
// Load nRF lib setup in Ram
void setupRadio(){ void setupRadio(){
// Static values in ram.
rf24_CONFIG = 0; rf24_CONFIG = 0;
rf24_RF_CH = 90; rf24_RF_CH = 90;
rf24_RF_SETUP = 0x26; rf24_RF_SETUP = 0x26;
@ -144,6 +146,7 @@ void setupRadio(){
rf24_EN_AA = 0x00; rf24_EN_AA = 0x00;
} }
// Restart USI & Reinit nRF24
void initRadio(){ void initRadio(){
power_usi_enable(); power_usi_enable();
rf24_init(); rf24_init();
@ -151,6 +154,8 @@ void initRadio(){
rf24_writeRegister(TX_ADDR,(uint8_t*)ADDR,5); rf24_writeRegister(TX_ADDR,(uint8_t*)ADDR,5);
} }
// ****
// Transmit data buffer
void txData(){ void txData(){
uint8_t crc,cpt,a,b,len; uint8_t crc,cpt,a,b,len;
uint8_t *in, *out; uint8_t *in, *out;
@ -201,8 +206,9 @@ int main(void)
debug("Sonde Start \r"); debug("Sonde Start \r");
#endif #endif
uint8_t rvcc=0; uint8_t rvcc=0,rtx=0,delay=6;
uint16_t t_i, lt_i=100;
int vcc; int vcc;
// Setup // Setup
power_down(); power_down();
@ -219,59 +225,70 @@ int main(void)
while(1){ while(1){
// each 10 runs => read VCC
if(!(rvcc--)){ if(!(rvcc--)){
initADC(); initADC();
// Read VCC
vcc = readVcc(); vcc = readVcc();
stopADC(); stopADC();
rvcc=4; rvcc=9;
} }
// Do 8 temp reads over 4s // PowerUp DS18B20
/* 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);*/
w1_start(); w1_start();
// Wait 15ms (for DS18B20 to start)
wdt_enable(WDTO_15MS); wdt_enable(WDTO_15MS);
WDTCSR |= _BV(WDIE); WDTCSR |= _BV(WDIE);
sleep(); sleep();
// Start DS18B20 Temp conversion
start_meas(); start_meas();
// Wait for 1s
wdt_enable(WDTO_1S); wdt_enable(WDTO_1S);
WDTCSR |= _BV(WDIE); WDTCSR |= _BV(WDIE);
sleep(); sleep();
uint16_t t_i = read_meas();//=temp*100; // Read temp & stop DS18B20
t_i = read_meas();//=temp*100;
w1_stop(); w1_stop();
// Send data // If temp change more than 1 since last tx
initRadio(); 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 : // If temp change more than 2 => reduced delay
*(uint16_t*)(buffer+4) = 0x0002; // BIN v2 = vcc+tempDS if(t_i > lt_i+2 || t_i < lt_i -2){
*(uint16_t*)(buffer+6) = vcc; // 2 o // Force Tx
*(uint16_t*)(buffer+8) = t_i; // 2 o delay=2;
txData(buffer); }
//radio off if(!(rtx--)){
rf24_powerDown();
// 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(); power_down();
wait_for_next(); wait_for_next(delay);
} }
} }