diff --git a/avrrfm.c b/avrrfm.c index 45db55d..53ed900 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -48,7 +48,9 @@ #endif /* 1 int = 8 seconds */ -static volatile uint8_t ints = 0; +static volatile uint8_t wdInts = 0; + +static volatile uint8_t t0Ints = 0; /* Temp. label coordinates */ static x_t xl = 0; @@ -58,7 +60,11 @@ static width_t width = 0; ISR(WDT_vect) { - ints++; + wdInts++; +} + +ISR(TIMER0_COMPA_vect) { + t0Ints++; } /** @@ -132,6 +138,20 @@ } /** + * Sets up the timer. + */ +static void initTimer(void) { + // timer0 clear timer on compare match mode, TOP OCR0A + TCCR0A |= (1 << WGM01); + // timer0 clock prescaler/1024/255 ~ 46 Hz @ 12 MHz ~ 61 Hz @ 16 MHz + TCCR0B |= (1 << CS02) | (1 << CS00); + OCR0A = 255; + + // enable timer0 compare match A interrupt + TIMSK0 |= (1 << OCIE0A); +} + +/** * Enables SPI. */ static void enableSPI(void) { @@ -212,6 +232,7 @@ if (!RECEIVER) { // used only for tx initWatchdog(); + initTimer(); } // enable global interrupts @@ -220,7 +241,6 @@ printString("Hello Radio!\r\n"); uint8_t node = RECEIVER ? NODE1 : NODE2; - initRadio(868600, node); if (RECEIVER) { initDisplay(); @@ -235,13 +255,24 @@ // _delay_ms(1000); if (!RECEIVER) { - if (ints % MEASURE_INTS == 0) { - ints = 0; + if (wdInts % MEASURE_INTS == 0) { + wdInts = 0; enableSPI(); wakeTSens(); wakeRadio(); transmitTemp(NODE1); + + // TODO timeout + /* + uint8_t response[1]; + receivePayload(response, sizeof (response)); + // receiver RSSI + int8_t rssi = divRoundNearest(response[0], 2); + printUint(rssi); + _delay_ms(10); + */ + sleepTSens(); sleepRadio(); disableSPI(); @@ -251,6 +282,11 @@ if (flags.ready) { uint8_t rssi = readRssi(); uint16_t raw = readTemp(); + + _delay_ms(10); + uint8_t payload[] = {rssi}; + transmitPayload(payload, sizeof (payload), NODE2); + displayTemp(rssi, flags.crc, raw); startReceive(); } diff --git a/rfm69.c b/rfm69.c index 090a798..8f77849 100644 --- a/rfm69.c +++ b/rfm69.c @@ -125,7 +125,7 @@ regWrite(RX_BW, 0x54); // RX_BW during AFC (default 0x8b) - regWrite(AFC_BW, 0x8a); + regWrite(AFC_BW, 0x54); // AFC auto on // regWrite(AFC_FEI, 0x04); @@ -183,6 +183,10 @@ printString("Radio init done\r\n"); } +void setTimeoutRxStart(uint8_t value) { + regWrite(RX_TIMEOUT1, value); +} + void sleepRadio(void) { setMode(MODE_SLEEP); } diff --git a/rfm69.h b/rfm69.h index 67847e4..050307a 100644 --- a/rfm69.h +++ b/rfm69.h @@ -84,6 +84,13 @@ void initRadio(uint64_t freq, uint8_t node); /** + * Sets "RxStart" timeout to the given value. + * + * @param value + */ +void setTimeoutRxStart(uint8_t value); + +/** * Shuts down the radio. */ void sleepRadio(void);