#include <util/atomic.h>
and in the void update_ppm () function we remove ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
Instead of curly brackets we write __disable_interrupt();
and __enable_interrupt();
#include <EEPROM.h>
and add the MspFlash library (Sketch-> Import Library ... -> MspFlash), add to which segment of the FLASH memory we will write data #define flash SEGMENT_D
In the function void selectProtocol () we change strings else current_protocol = constrain(EEPROM.read(ee_PROTOCOL_ID),0,PROTO_END-1); // update eeprom EEPROM.update(ee_PROTOCOL_ID, current_protocol);
on // update eeprom Flash.write(flash+ee_PROTOCOL_ID, & current_protocol,1);
We remove completely reading the protocol identifier (why, read below). void set_txid(bool renew) { uint8_t i; for(i=0; i<4; i++) transmitterID[i] = EEPROM.read(ee_TXID0+i); if(renew || (transmitterID[0]==0xFF && transmitterID[1]==0x0FF)) { for(i=0; i<4; i++) { transmitterID[i] = random() & 0xFF; EEPROM.update(ee_TXID0+i, transmitterID[i]); } } }
void set_txid(bool renew) { uint8_t i; unsigned char p; for(i=0; i<4; i++) { Flash.read(flash+ee_TXID0+i,&p,1); transmitterID[i] =p; } Flash.read(flash+ee_PROTOCOL_ID,&p,1); current_protocol = constrain(p,0,PROTO_END-1); Flash.erase(flash); if(renew || (transmitterID[0]==0xFF && transmitterID[1]==0x0FF)) { for(i=0; i<4; i++) { transmitterID[i] = random(0xff) & 0xFF; p = transmitterID[i]; Flash.write(flash+ee_TXID0+i, &p,1); } }else{ for(i=0; i<4; i++) { p = transmitterID[i]; Flash.write(flash+ee_TXID0+i, &p,1); } } }
Here we read the value of the transmitter identifiers and the protocol, erase the segment, and if a command is given to update the transmitter identifier, we write the new value, otherwise we write the old one. The FLASH memory byte with the flash + ee_PROTOCOL_ID address remained clean (0xFF), so we don’t read it in the selectProtocol () function, and immediately write the protocol identifier there. #define PPM_pin 2 // PPM in //SPI Comm.pins with nRF24L01 #define MOSI_pin 3 // MOSI - D3 #define SCK_pin 4 // SCK - D4 #define CE_pin 5 // CE - D5 #define MISO_pin A0 // MISO - A0 #define CS_pin A1 // CS - A1 #define ledPin 13 // LED - D13
on #define PPM_pin P1_5 // PPM in //SPI Comm.pins with nRF24L01 #define MOSI_pin P2_0 // MOSI #define SCK_pin P2_1 // SCK #define CE_pin P2_2 // CE #define MISO_pin P2_3 // MISO #define CS_pin P2_4 // CS #define ledPin P1_4 // LED
and #define MOSI_on PORTD |= _BV(3) // PD3 #define MOSI_off PORTD &= ~_BV(3)// PD3 #define SCK_on PORTD |= _BV(4) // PD4 #define SCK_off PORTD &= ~_BV(4) // PD4 #define CE_on PORTD |= _BV(5) // PD5 #define CE_off PORTD &= ~_BV(5) // PD5 #define CS_on PORTC |= _BV(1) // PC1 #define CS_off PORTC &= ~_BV(1) // PC1 // SPI input #define MISO_on (PINC & _BV(0)) // PC0
on #define MOSI_on P2OUT |= _BV(0)// P2_0 #define MOSI_off P2OUT &= ~_BV(0)// P2_0 #define SCK_on P2OUT |= _BV(1)// P2_1 #define SCK_off P2OUT &= ~_BV(1)// P2_1 #define CE_on P2OUT |= _BV(2)// P2_2 #define CE_off P2OUT &= ~_BV(2)// P2_2 #define CS_on P2OUT |= _BV(4)// P2_4 #define CS_off P2OUT &= ~_BV(4) // P2_4 // SPI input #define MISO_on (P2IN & _BV(3)) // P2_3
#define _BV(val) 1<<val
In the void setup () function, we change the values ​​of the pins of the analog inputs to the free ones with randomSeed((analogRead(A4) & 0x1F) | (analogRead(A5) << 5));
for example on randomSeed((analogRead(A0) & 0x1F) | (analogRead(A1) << 5));
In MSP430, the analog inputs A0, A1, A2, ..., A7 correspond to the P1_0, P1_1, P1_2, ..., P1_7 pins. attachInterrupt(PPM_pin - 2, ISR_ppm, CHANGE);
on attachInterrupt(PPM_pin , ISR_ppm, CHANGE);
In Arduino Uno, Nano, Mini, and others, on mega328 only 2 pins are available for connecting interrupts (2, 3) and in the attachInterrupt function, the first argument is the interrupt number, and not a pin like MSP430. Learn more about attachInterrupt () . TCCR1A = 0; //reset timer1 TCCR1B = 0; TCCR1B |= (1 << CS11); //set timer1 to increment every 1 us @ 8MHz, 0.5 us @16MHz
On TACTL = TASSEL_2 + ID_3 + MC_2 + TACLR; //16000000 / 8
The timer is needed to determine the pulse duration in the PPM. We set it to direct counting with a frequency of 2 MHz (16 MHz clock frequency and a divisor by 8). counterPPM = TCNT1; TCNT1 = 0;
on counterPPM = TAR; TAR = 0;
Registers TCNT1 and TAR timer counters. random() & 0xFF;
on random(0xFF);
and random() % 0x42;
on random(0x41);
Source: https://habr.com/ru/post/392609/
All Articles