This commit is contained in:
2025-07-18 16:51:42 +03:00
parent 79b6d1742d
commit 1292243ffc

View File

@@ -1,3 +1,6 @@
// Не работает с 168P
// Работает с RF-Nano
#include "avr/io.h" #include "avr/io.h"
#include "avr/sleep.h" #include "avr/sleep.h"
#include "avr/fuse.h" #include "avr/fuse.h"
@@ -6,16 +9,19 @@
#define ZH_NRF24_CE_DDR DDRB #define ZH_NRF24_CE_DDR DDRB
#define ZH_NRF24_CE_PORT PORTB #define ZH_NRF24_CE_PORT PORTB
#define ZH_NRF24_CE_PIN PORTB2 // #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_DDR DDRB
#define ZH_NRF24_CSN_PORT PORTB #define ZH_NRF24_CSN_PORT PORTB
#define ZH_NRF24_CSN_PIN PORTB1 // #define ZH_NRF24_CSN_PIN PORTB1 // RF-Nano
#define ZH_NRF24_CSN_PIN PORTB2 // 168
FUSES = {0xE2, 0xDF, 0x07}; FUSES = {0xE2, 0xDF, 0x07};
#define GATEWAY_CHANNEL 120 #define GATEWAY_CHANNEL 120
// #define GATEWAY_ADDRESS 0xDDEEFF
#define DEVICE_ID 50 #define DEVICE_ID 6
#define DEVICE_TYPE 4 #define DEVICE_TYPE 4
#define NRF24_CE_HIGH (ZH_NRF24_CE_PORT |= (1 << ZH_NRF24_CE_PIN)) #define NRF24_CE_HIGH (ZH_NRF24_CE_PORT |= (1 << ZH_NRF24_CE_PIN))
@@ -35,45 +41,58 @@ 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 uint64_t gateway_address = 0xDDEEFF;
static int16_t transmitted_data[7] = {DEVICE_ID, DEVICE_TYPE, 0x00, 0x00, 0x00, 0x00, 0x00}; 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();
sei();
// set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// sleep_enable(); // sleep_enable();
sei(); // zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data));
// sei();
for (;;) for (;;)
{ {
transmitted_data[2] = (zh_get_battery_level_charge() * 100); _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)); zh_nrf24_send((uint8_t *)&transmitted_data, sizeof(transmitted_data));
// sleep_cpu(); // sleep_cpu();
_delay_ms(5000);
} }
return 0; // return 0;
} }
ISR(INT1_vect) ISR(INT1_vect)
{ {
cli(); // cli();
transmitted_data[2] = (zh_get_battery_level_charge() * 100); // // transmitted_data[2] = (zh_get_battery_level_charge() * 100);
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)
{ {
DDRB |= ((1 << PORTB2) | (1 << PORTB3) | (1 << PORTB5)); uint8_t sreg = SREG;
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)
@@ -87,7 +106,8 @@ static uint8_t zh_spi_transfer(uint8_t data)
static void zh_nrf24_init(void) static void zh_nrf24_init(void)
{ {
ZH_NRF24_CE_DDR |= (1 << ZH_NRF24_CE_PIN); // 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);
@@ -112,6 +132,10 @@ static void zh_nrf24_send(uint8_t *buf, uint8_t size)
{ {
_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);