This commit is contained in:
2025-07-18 17:59:40 +03:00
parent 35cc9be530
commit 757b2c4343

View File

@@ -1,34 +1,16 @@
// Не работает с 168P
// Работает с RF-Nano
#include "avr/io.h"
#include "avr/sleep.h"
#include "avr/fuse.h"
#include "avr/interrupt.h"
#include "util/delay.h"
#define ZH_NRF24_CE_DDR DDRB
#define ZH_NRF24_CE_PORT PORTB
// #define ZH_NRF24_CE_PIN PORTB2 // RF-Nano
#define ZH_NRF24_CE_PIN PORTB1 // 168
#define ZH_NRF24_CSN_DDR DDRB
#define ZH_NRF24_CSN_PORT PORTB
// #define ZH_NRF24_CSN_PIN PORTB1 // RF-Nano
#define ZH_NRF24_CSN_PIN PORTB2 // 168
FUSES = {0xE2, 0xDF, 0x07};
#define GATEWAY_CHANNEL 120
#define GATEWAY_ADDRESS 0xDDEEFF
#define DEVICE_ID 6
#define DEVICE_TYPE 4
#define NRF24_CE_HIGH (ZH_NRF24_CE_PORT |= (1 << ZH_NRF24_CE_PIN))
#define NRF24_CE_LOW (ZH_NRF24_CE_PORT &= ~(1 << ZH_NRF24_CE_PIN))
#define NRF24_SPI_START (ZH_NRF24_CSN_PORT &= ~(1 << ZH_NRF24_CSN_PIN))
#define NRF24_SPI_STOP (ZH_NRF24_CSN_PORT |= (1 << ZH_NRF24_CSN_PIN))
static void zh_pad_interrupt_init(void);
static void zh_spi_init(void);
static uint8_t zh_spi_transfer(uint8_t data);
@@ -59,8 +41,8 @@ int main(void)
ISR(INT1_vect)
{
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));
int16_t data[] = {DEVICE_ID, DEVICE_TYPE, (zh_get_battery_level_charge() * 100), 0, 0, 0, 0};
zh_nrf24_send((uint8_t *)&data, sizeof(data));
sei();
}
@@ -88,7 +70,7 @@ static uint8_t zh_spi_transfer(uint8_t data)
static void zh_nrf24_init(void)
{
uint64_t gateway_address = GATEWAY_ADDRESS;
ZH_NRF24_CSN_DDR |= (1 << ZH_NRF24_CSN_PIN);
DDRB |= (1 << PORTB2);
zh_nrf24_write_register(0x00, 0x48);
zh_nrf24_write_register(0x01, 0x01);
zh_nrf24_write_register(0x02, 0x01);
@@ -98,7 +80,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;
PORTB &= ~(1 << PORTB2);
}
static void zh_nrf24_send(uint8_t *buf, uint8_t size)
@@ -106,9 +88,9 @@ static void zh_nrf24_send(uint8_t *buf, uint8_t size)
zh_nrf24_write_register(0x00, 0x4A);
_delay_ms(2);
zh_nrf24_write_buffer(0xA0, buf, size);
NRF24_CE_HIGH;
PORTB |= (1 << PORTB1);
_delay_us(15);
NRF24_CE_LOW;
PORTB &= ~(1 << PORTB1);
while ((PIND & (1 << PIND2)) != 0)
{
_delay_ms(1);
@@ -116,27 +98,27 @@ static void zh_nrf24_send(uint8_t *buf, uint8_t size)
zh_nrf24_write_register(0x07, zh_nrf24_send_command(0xFF));
zh_nrf24_send_command(0xE1);
zh_nrf24_write_register(0x00, 0x48);
NRF24_SPI_START;
PORTB &= ~(1 << PORTB2);
}
static uint8_t zh_nrf24_write_buffer(uint8_t cmd, uint8_t *buf, uint8_t count)
{
NRF24_SPI_START;
PORTB &= ~(1 << PORTB2);
uint8_t status = zh_spi_transfer(cmd);
while (count-- != 0)
{
zh_spi_transfer(*(buf++));
}
NRF24_SPI_STOP;
PORTB |= (1 << PORTB2);
return status;
}
static uint8_t zh_nrf24_write_register(uint8_t reg, uint8_t val)
{
NRF24_SPI_START;
PORTB &= ~(1 << PORTB2);
uint8_t status = zh_spi_transfer(reg | 0x20);
zh_spi_transfer(val);
NRF24_SPI_STOP;
PORTB |= (1 << PORTB2);
return status;
}
@@ -147,9 +129,9 @@ 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)
{
NRF24_SPI_START;
PORTB &= ~(1 << PORTB2);
uint8_t status = zh_spi_transfer(cmd);
NRF24_SPI_STOP;
PORTB |= (1 << PORTB2);
return status;
}