This commit is contained in:
2025-07-18 17:42:28 +03:00
parent 1292243ffc
commit 35cc9be530

View File

@@ -19,7 +19,7 @@
FUSES = {0xE2, 0xDF, 0x07}; FUSES = {0xE2, 0xDF, 0x07};
#define GATEWAY_CHANNEL 120 #define GATEWAY_CHANNEL 120
// #define GATEWAY_ADDRESS 0xDDEEFF #define GATEWAY_ADDRESS 0xDDEEFF
#define DEVICE_ID 6 #define DEVICE_ID 6
#define DEVICE_TYPE 4 #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 uint8_t zh_nrf24_send_command(uint8_t cmd);
static float zh_get_battery_level_charge(void); 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) int main(void)
{ {
// cli();
// zh_pad_interrupt_init();
// ADCSRA &= ~(1 << ADEN);
_delay_ms(50);
cli(); cli();
zh_pad_interrupt_init();
zh_spi_init(); zh_spi_init();
zh_nrf24_init(); zh_nrf24_init();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sei(); sei();
// set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// sleep_enable();
// zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data));
// sei();
for (;;) for (;;)
{ {
_delay_ms(1000); sleep_cpu();
// 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();
} }
// return 0; return 0;
} }
ISR(INT1_vect) ISR(INT1_vect)
{ {
// cli(); cli();
// // transmitted_data[2] = (zh_get_battery_level_charge() * 100); 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)); zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data));
// sei(); sei();
} }
static void zh_pad_interrupt_init(void) static void zh_pad_interrupt_init(void)
{ {
// DDRD |= (1 << PORTD3);
EICRA |= ((1 << ISC11) | (1 << ISC10)); EICRA |= ((1 << ISC11) | (1 << ISC10));
EIMSK |= (1 << INT1); EIMSK |= (1 << INT1);
} }
static void zh_spi_init(void) static void zh_spi_init(void)
{ {
uint8_t sreg = SREG; DDRB |= ((1 << PORTB2) | (1 << PORTB3) | (1 << PORTB5));
PORTB |= (1 << PORTB2);
DDRB |= (1 << PORTB2);
// PORTB |= (1 << PORTB2);
SPCR |= ((1 << SPE) | (1 << MSTR)); SPCR |= ((1 << SPE) | (1 << MSTR));
DDRB |= ((1 << PORTB3) | (1 << PORTB5));
// PORTB |= (1 << PORTB2);
SREG = sreg;
} }
static uint8_t zh_spi_transfer(uint8_t data) 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) static void zh_nrf24_init(void)
{ {
// uint64_t gateway_address = GATEWAY_ADDRESS; uint64_t gateway_address = GATEWAY_ADDRESS;
// ZH_NRF24_CE_DDR |= (1 << ZH_NRF24_CE_PIN);
ZH_NRF24_CSN_DDR |= (1 << ZH_NRF24_CSN_PIN); ZH_NRF24_CSN_DDR |= (1 << ZH_NRF24_CSN_PIN);
zh_nrf24_write_register(0x00, 0x48); zh_nrf24_write_register(0x00, 0x48);
zh_nrf24_write_register(0x01, 0x01); 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_register(0x06, 0x26);
zh_nrf24_write_buff_register(0x0A, (uint8_t *)&gateway_address, 3); zh_nrf24_write_buff_register(0x0A, (uint8_t *)&gateway_address, 3);
zh_nrf24_write_buff_register(0x10, (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) 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; NRF24_CE_HIGH;
_delay_us(15); _delay_us(15);
NRF24_CE_LOW; NRF24_CE_LOW;
while ((zh_nrf24_send_command(0xFF) & 0x30) == 0) while ((PIND & (1 << PIND2)) != 0)
{ {
_delay_ms(1); _delay_ms(1);
} }
// while ((PORTD & PIND2) != 0)
// {
// _delay_ms(1);
// }
zh_nrf24_write_register(0x07, zh_nrf24_send_command(0xFF)); zh_nrf24_write_register(0x07, zh_nrf24_send_command(0xFF));
zh_nrf24_send_command(0xE1); zh_nrf24_send_command(0xE1);
zh_nrf24_write_register(0x00, 0x48); 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) static uint8_t zh_nrf24_write_buffer(uint8_t cmd, uint8_t *buf, uint8_t count)