Slave
This commit is contained in:
parent
a57b1bbcbe
commit
1aa420c1ca
100
SPI/slave.ino
Normal file
100
SPI/slave.ino
Normal file
@ -0,0 +1,100 @@
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#define DDR_SPI DDRB
|
||||
#define DD_MOSI PB3
|
||||
#define DD_MISO PB4
|
||||
#define DD_SCK PB5
|
||||
#define DD_SS PB2
|
||||
|
||||
|
||||
static int index = 0;
|
||||
static byte data[255];
|
||||
|
||||
|
||||
void SPI_SlaveInit(void)
|
||||
{
|
||||
DDR_SPI = (1 << DD_MISO);
|
||||
|
||||
SPCR = (1 << SPE) | (1 << SPIE);
|
||||
}
|
||||
|
||||
ISR(SPI_STC_vect)
|
||||
{
|
||||
char received = SPDR;
|
||||
data[index] = received;
|
||||
index++;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
SPI_SlaveInit();
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AddSymbol(byte *symbols){
|
||||
Serial.print("Received Add: ");
|
||||
|
||||
for(int i = 0; i < index-1; i++) {
|
||||
Serial.print(symbols[i]);
|
||||
Serial.print(" ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Функция для вычисления контрольной суммы XOR
|
||||
byte calculateXORChecksum(byte *data, int length) {
|
||||
byte checksum = 0;
|
||||
for (int i = 0; i < length - 1; i++) {
|
||||
checksum ^= data[i];
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if(PINB & (1 << 2))
|
||||
{
|
||||
if(index > 0)
|
||||
{
|
||||
// byte calculatedChecksum = calculateXORChecksum(data, index);
|
||||
// byte last = data[index - 1];
|
||||
// Serial.println();
|
||||
// Serial.println("--------------");
|
||||
// Serial.print("\n Last: ");
|
||||
// Serial.print(last);
|
||||
// Serial.print("\t Sum: ");
|
||||
// Serial.println(calculatedChecksum);
|
||||
|
||||
// // SPDR = calculatedChecksum;
|
||||
// if (last != calculatedChecksum)
|
||||
// {
|
||||
// Serial.println("\n -----Invalid Checksum!---");
|
||||
|
||||
// index = 0;
|
||||
// return;
|
||||
// }
|
||||
|
||||
SetCommand(data);
|
||||
index=0;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user