diff --git a/src/main.c b/src/main.c index 695f8a5..d51f017 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,7 @@ FUSES = {0xE2, 0xDF, 0x07}; #define GATEWAY_CHANNEL 120 -// #define GATEWAY_ADDRESS 0xDDEEFF +#define GATEWAY_ADDRESS 0xDDEEFF #define DEVICE_ID 6 #define DEVICE_TYPE 4 @@ -40,59 +40,40 @@ static uint8_t zh_nrf24_write_buff_register(uint8_t reg, uint8_t *buf, uint8_t c static uint8_t zh_nrf24_send_command(uint8_t cmd); static float zh_get_battery_level_charge(void); -static uint64_t gateway_address = 0xDDEEFF; -static int16_t transmitted_data[7] = {DEVICE_ID, DEVICE_TYPE, 300, 0x00, 0x00, 0x00, 0x00}; - int main(void) { - // cli(); - // zh_pad_interrupt_init(); - // ADCSRA &= ~(1 << ADEN); - _delay_ms(50); cli(); + zh_pad_interrupt_init(); zh_spi_init(); zh_nrf24_init(); + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); sei(); - // set_sleep_mode(SLEEP_MODE_PWR_DOWN); - // sleep_enable(); - // zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data)); - // sei(); for (;;) { - _delay_ms(1000); - // int16_t transmitted_data[7] = {DEVICE_ID, DEVICE_TYPE, 300, 0x00, 0x00, 0x00, 0x00}; - // transmitted_data[2] = (zh_get_battery_level_charge() * 100); - zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data)); - // sleep_cpu(); + sleep_cpu(); } - // return 0; + return 0; } ISR(INT1_vect) { - // cli(); - // // transmitted_data[2] = (zh_get_battery_level_charge() * 100); - // zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data)); - // sei(); + cli(); + int16_t transmitted_data[7] = {DEVICE_ID, DEVICE_TYPE, (zh_get_battery_level_charge() * 100), 0x00, 0x00, 0x00, 0x00}; + zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data)); + sei(); } static void zh_pad_interrupt_init(void) { - // DDRD |= (1 << PORTD3); EICRA |= ((1 << ISC11) | (1 << ISC10)); EIMSK |= (1 << INT1); } static void zh_spi_init(void) { - uint8_t sreg = SREG; - PORTB |= (1 << PORTB2); - DDRB |= (1 << PORTB2); - // PORTB |= (1 << PORTB2); + DDRB |= ((1 << PORTB2) | (1 << PORTB3) | (1 << PORTB5)); SPCR |= ((1 << SPE) | (1 << MSTR)); - DDRB |= ((1 << PORTB3) | (1 << PORTB5)); - // PORTB |= (1 << PORTB2); - SREG = sreg; } static uint8_t zh_spi_transfer(uint8_t data) @@ -106,8 +87,7 @@ static uint8_t zh_spi_transfer(uint8_t data) static void zh_nrf24_init(void) { - // uint64_t gateway_address = GATEWAY_ADDRESS; - // ZH_NRF24_CE_DDR |= (1 << ZH_NRF24_CE_PIN); + uint64_t gateway_address = GATEWAY_ADDRESS; ZH_NRF24_CSN_DDR |= (1 << ZH_NRF24_CSN_PIN); zh_nrf24_write_register(0x00, 0x48); zh_nrf24_write_register(0x01, 0x01); @@ -118,6 +98,7 @@ static void zh_nrf24_init(void) zh_nrf24_write_register(0x06, 0x26); zh_nrf24_write_buff_register(0x0A, (uint8_t *)&gateway_address, 3); zh_nrf24_write_buff_register(0x10, (uint8_t *)&gateway_address, 3); + NRF24_SPI_START; } static void zh_nrf24_send(uint8_t *buf, uint8_t size) @@ -128,17 +109,14 @@ static void zh_nrf24_send(uint8_t *buf, uint8_t size) NRF24_CE_HIGH; _delay_us(15); NRF24_CE_LOW; - while ((zh_nrf24_send_command(0xFF) & 0x30) == 0) + while ((PIND & (1 << PIND2)) != 0) { _delay_ms(1); } - // while ((PORTD & PIND2) != 0) - // { - // _delay_ms(1); - // } zh_nrf24_write_register(0x07, zh_nrf24_send_command(0xFF)); zh_nrf24_send_command(0xE1); zh_nrf24_write_register(0x00, 0x48); + NRF24_SPI_START; } static uint8_t zh_nrf24_write_buffer(uint8_t cmd, uint8_t *buf, uint8_t count)