From a71fd231fc80a599ee810b5321a91921ebc63ec1 Mon Sep 17 00:00:00 2001 From: Soundflog Date: Wed, 26 Apr 2023 12:38:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20CRC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SPI/master/master.ino | 92 ++++++------------------ SPI/slave/slave.ino | 164 +++++++++++++++--------------------------- 2 files changed, 79 insertions(+), 177 deletions(-) diff --git a/SPI/master/master.ino b/SPI/master/master.ino index 0819cda..e2d1643 100644 --- a/SPI/master/master.ino +++ b/SPI/master/master.ino @@ -10,16 +10,17 @@ void SPI_MasterInit(void) { - DDR_SPI = (1 << DD_MOSI) | (1 << DD_SCK) | (1 << DD_SS); - DDRB |= (1< #include -#include +// #include #define DDR_SPI DDRB #define DD_MOSI PB3 @@ -8,66 +8,62 @@ #define DD_SCK PB5 #define DD_SS PB2 -GyverOLED oled; +// GyverOLED oled; static int index = 0; static int arIndex = 0; static byte data[255]; -static byte ar1[5]; -static byte ar2[5]; -static byte ar3[5]; - void SPI_SlaveInit(void) { - DDR_SPI = (1 << DD_MISO); + DDR_SPI = (1 << DD_MISO); - SPCR = (1 << SPE) | (1 << SPIE); + SPCR = (1 << SPE) | (1 << SPIE); } ISR(SPI_STC_vect) { - char received = SPDR; - data[index] = received; - index++; + char received = SPDR; + data[index] = received; + index++; } void setup() { Serial.begin(2000000); SPI_SlaveInit(); - oled.init(); // инициализация - oled.clear(); // очистка + // oled.init(); // инициализация + // oled.clear(); // очистка Serial.println(); Serial.println("Initialization "); } void SetCommand(byte *data2){ - byte command = data2[0]; - // Отрезать 1 элемент от оставшихся - byte result[index]; - byte *ptr = &data2[1]; - strcpy(result, ptr); - Serial.print("\nReceived command: "); - Serial.println(command); - switch (command){ - case 6: - AddSymbol(result); - break; + byte command = data2[0]; + // Отрезать 1 элемент от оставшихся + byte result[index]; + byte *ptr = &data2[1]; + strcpy(result, ptr); + Serial.print("\nReceived command: "); + Serial.println(command); + switch (command){ + case 6: + AddSymbol(result); + break; - } + } } void AddSymbol(byte *symbols){ Serial.print("Received Add: "); - oled.setScale(3); // масштаб текста (1..4) - oled.home(); // курсор в 0,0 - oled.print("Привет!"); + // oled.setScale(3); // масштаб текста (1..4) + // oled.home(); // курсор в 0,0 + // oled.print("Привет!"); for(int i = 0; i < index - 1; i++) { - Serial.print(symbols[i]); + Serial.print(symbols[i], HEX); Serial.print(" "); } } @@ -75,18 +71,29 @@ void AddSymbol(byte *symbols){ // Функция для вычисления контрольной суммы XOR byte calculateXORChecksum(byte *data, int length) { - byte checksum = 0; - for (int i = 0; i < length - 1; i++) { - checksum ^= data[i]; - } - return checksum; + byte crc = 0x00; + byte poly = 0x07; // полином для CRC8 + + for (int i = 0; i < length-2; i++) { + crc ^= data[i]; // XOR текущего байта с crc + + for (int j = 0; j < length; j++) { + if (crc & 0x80) { // если старший бит crc равен 1 + crc = (crc << 1) ^ poly; // сдвигаем crc на 1 бит влево и XOR с полиномом + } else { + crc <<= 1; // иначе просто сдвигаем на 1 бит влево + } + } + } + + return crc; } // Вывод массива void arrayOut(byte *arr,int size){ Serial.print("Array: "); - for(int i = 0;i 0) - { + if(PINB & (1 << 2)){ + if(index > 0){ - if (arIndex == 0){ - arIndex = 1; - memcpy(ar1,data,5); - }else{ - if(arIndex == 1){ - arIndex = 2; - memcpy(ar2,data,5); - }else{ - arIndex = 0; - memcpy(ar3,data,5); - } - } byte sum = 0; + sum = calculateXORChecksum(data, index-1); - //arrayOut(ar1,5); - int lenght = 5; - sum = calculateXORChecksum(ar1, lenght); bool checkNull = false; - - byte last_1 = ar1[index - 1]; - if (last_1 == sum) - { + byte last_1 = data[index - 1]; + //arrayOut(data, index); + + if (last_1 == sum){ Serial.println(); Serial.println("Старт вывода массивов"); - Serial.println(sum); - checkNull = checkArray(ar1, lenght); - if(checkNull == true) - { - SetCommand(ar1); + Serial.println(sum, HEX); + checkNull = checkArray(data, index); + + if(checkNull == true){ + SetCommand(data); } Serial.println("Стоп вывода массивов"); index = 0; return; } - - //arrayOut(ar2,5); - sum = calculateXORChecksum(ar2, lenght); - - byte last_2 = ar2[index - 1]; - if (last_2 == sum) - { - Serial.println(); - Serial.println("Старт вывода массивов"); - Serial.println(sum); - checkNull = checkArray(ar2, lenght); - if(checkNull == true) - { - SetCommand(ar2); - } - - Serial.println("Стоп вывода массивов"); - index = 0; - return; - } - - //arrayOut(ar3,5); - sum = calculateXORChecksum(ar3, lenght); - - byte last_3 = ar3[index - 1]; - if (last_3 == sum) - { - Serial.println(); - Serial.println("Старт вывода массивов"); - Serial.println(sum); - checkNull = checkArray(ar3, lenght); - if(checkNull == true) - { - SetCommand(ar3); - } - - Serial.println("Стоп вывода массивов"); - index = 0; - return; - } - Serial.println("Nothing ..."); - index=0; + + index = 0; } } }