4 massive sending

This commit is contained in:
Павел Вершинин 2023-04-17 17:57:11 +03:00
parent 917d0847b3
commit 3827d85437
2 changed files with 82 additions and 14 deletions

View File

@ -25,13 +25,26 @@ void Print(byte *data2, int lenght){
void SPI_MasterTransmit(char *data, int length)
{
PORTB &= ~(1<<2);
// Serial.println();
int size = sizeof(data) / sizeof(data[0]);
if (data[size - 1] == SPDR){
Serial.println("Privet") ;
}
byte check = 0;
byte spdr[length];
Serial.println();
for(int i = 0; i < length; i++) {
SPDR = data[i]; // отправляем байт массива
Serial.print(data[i], HEX);
Serial.print(" ");
while (!(SPSR & (1 << SPIF))); // ждем, пока байт передастся
}
PORTB |= (1<<2);
// Print(spdr, length);
// return check;
// //ждем, пока придет контрольная сумма
@ -69,8 +82,8 @@ byte calculateXORChecksum(byte *data, int length) {
void loop()
{
char data[5] = {6, 1, 2, 8, 0};
PORTB &= ~(1<<2);
char data[5] = {6, 1, 3, 8, 0};
int length = sizeof(data);
const int last = data [ (sizeof(data) / sizeof(data[0]))-1];
@ -81,11 +94,27 @@ void loop()
// Serial.println(last);
SPI_MasterTransmit(data, length);
_delay_ms(200);
PORTB |= (1<<2);
Serial.println("Sent");
// Serial.print(sum);
_delay_ms(2000);
_delay_ms(1000);
char data2[5] = {6, 9, 7, 10, 0};
int length2 = sizeof(data2);
const int last2 = data2 [ (sizeof(data2) / sizeof(data2[0]))-1];
byte checking2 = calculateXORChecksum(data2, length2);
int size2 = sizeof(data2) / sizeof(data2[0]);
data2[size2 - 1] = checking2;
SPI_MasterTransmit(data2, length2);
_delay_ms(1000);
char data3[5] = {6, 11, 12, 13, 0};
int length3 = sizeof(data3);
const int last3 = data3 [ (sizeof(data3) / sizeof(data3[0]))-1];
byte checking3 = calculateXORChecksum(data3, length3);
int size3 = sizeof(data3) / sizeof(data3[0]);
data3[size3 - 1] = checking3;
SPI_MasterTransmit(data3, length3);
_delay_ms(1000);
// ожидаем ответа
// while (!(SPSR & (1 << SPIF)));
// byte response = SPDR;

View File

@ -9,7 +9,12 @@
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)
@ -28,8 +33,9 @@ ISR(SPI_STC_vect)
void setup()
{
Serial.begin(9600);
Serial.begin(2000000);
SPI_SlaveInit();
Serial.println();
Serial.println("Initialization ");
}
@ -42,7 +48,6 @@ void SetCommand(byte *data2){
Serial.print("\nReceived command: ");
Serial.println(command);
switch (command){
case 6:
AddSymbol(result);
break;
@ -70,12 +75,21 @@ byte calculateXORChecksum(byte *data, int length) {
return checksum;
}
void arrayOut(byte *arr,int size){
Serial.print("Array: ");
for(int i = 0;i<size;i++){
Serial.print(arr[i]);
Serial.print(" ");
}
Serial.println(".");
}
void loop() {
if(PINB & (1 << 2))
{
if(index > 0)
{
// byte calculatedChecksum = calculateXORChecksum(data, index);
// byte calculatedChecksum = calculateXORChecksum(data, index-1);
// byte last = data[index - 1];
// Serial.println();
// Serial.println("--------------");
@ -84,16 +98,41 @@ void loop() {
// Serial.print("\t Sum: ");
// Serial.println(calculatedChecksum);
// // SPDR = calculatedChecksum;
// if (last != calculatedChecksum)
// {
// Serial.println("\n -----Invalid Checksum!---");
// SPDR = calculatedChecksum;
// index = 0;
// return;
// }
SetCommand(data);
//SetCommand(data);
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;
Serial.println();
Serial.println("Старт вывода массивов");
arrayOut(ar1,5);
sum = calculateXORChecksum(ar1,5);
Serial.println(sum);
arrayOut(ar2,5);
sum = calculateXORChecksum(ar2,5);
Serial.println(sum);
arrayOut(ar3,5);
sum = calculateXORChecksum(ar3,5);
Serial.println(sum);
Serial.println("Стоп вывода массивов");
index=0;
}
}