I have an issue with the SPI connection of this project. It should display a starting number on the 7-digit display circuits and I should increment/decrement with the buttons. The purpose of the SPI connection is that the second Arduino UNO it should work like a temperature sensor (more or less) but I cannot display something (at least) on the 7-digit display circuits which makes me think that it's something wrong with the SPI connection. What should I do? I had tried different changes but with no solution.
//Master
#define SS 2
#define MOSI 3
#define MISO 4
#define CLK 5
int counter = 0;
int time = 0;
void SPI_Begin(){
DDRB = (1<<MOSI) | (1<<SCK) | (1<<SS) | (0<<MISO);
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
PORTB = (1<<SS);
}
int SPI_Transfer(int data){
SPDR = data;
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
void setup(){
SPI_Begin();
PORTB |= 0b00000011;
TCCR0A = (1<<WGM01) | (0<<WGM00);
OCR0A = 0xF9;
TIMSK0 |= (1<<OCIE0A);
TCCR0B = (1<<CS02) | (0<<CS01) | (0<<CS00);
SREG |= (1<<SREG_I);
}
void loop()
{
static int R, S = 25;
if(counter >= time + 5){
R = SPI_Transfer(S);
time = counter;
}
if((PINB&(1<<0))==0)
{
while((PINB&(1<<0))==0)
{}
S=S+1;
}
if((PINB&(1<<1))==0)
{
while((PINB&(1<<1))==0)
{}
S=S+2;
}
}
ISR(TIMER0_COMPA_vect)
{
SREG &= ~(1<<SREG_I);
++counter;
if(counter>=99999)
counter = 0;
SREG |= (1 << SREG_I);
}
//Slave
#define SS 2
#define MOSI 3
#define MISO 4
#define CLK 5
int counter = 0;
int time = 0;
int delay_f = 0;
int number = 0;
int threshold = 100;
static int Send;
void SPI_Begin_Slave(){
DDRB = (0<<MOSI) | (0<<SCK) | (0<<SS) | (1<<MISO);
SPCR = (1<<SPE);
}
int SPI_Transfer(int data){
SPDR = data;
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
void setup(){
SPI_Begin_Slave();
DDRC = 0b00011100;
DDRD = 0b11111111;
TCCR0A = (1<<WGM01) | (0<<WGM00);
OCR0A = 0xF9;
TIMSK0 |= (1<<OCIE0A);
TCCR0B = (1<<CS02) | (0<<CS01) | (0<<CS00);
SREG |= (1<<SREG_I);
number = SPI_Transfer(Send);
}
void loop(){
displaynumber(number);
displaynumber2(threshold);
}
void displaynumber(int Send){
int decimal = number % 10;
int units = ((number - decimal) %100)/10;
int number_f = (number - decimal - units*10) /100;
if((counter>=time+50) && (delay_f == 0)){
number = SPI_Transfer(Send);
PORTD &= 0b11111000;
PORTD |= 0b00001000;
PORTB &= 0b11111100;
PORTB |= 0b00000000;
displaydigit(number_f);
delay_f = 1;
time = counter;
}
if((counter>=time+50) && (delay_f == 1)){
PORTD &= 0b11110100;
PORTD |= 0b00000100;
PORTB &= 0b11111100;
PORTB |= 0b00000000;
displaydigit(units);
delay_f = 2;
time = counter;
}
if((counter>=time+50) && (delay_f == 2)){
PORTD &= 0b11110010;
PORTD |= 0b00000010;
PORTB &= 0b11111100;
PORTB |= 0b00000000;
displaydigit(decimal);
delay_f = 3;
time = counter;
}
}
void displaynumber2(int secondnumber){
int seconddecimal = secondnumber % 10;
int secondunits = ((secondnumber - seconddecimal) %100)/10;
int numar_f2 = (secondnumber - seconddecimal - secondunits*10) /100;
if((counter>=time+50) && (delay_f == 3)){
PORTD &= 0b11110000;
PORTD |= 0b00000000;
PORTB &= 0b11111110;
PORTB |= 0b00000010;
displaydigit(numar_f2);
delay_f = 4;
time = counter;
}
if((counter>=time+50) && (delay_f == 4)){
PORTD &= 0b11110000;
PORTD |= 0b00000000;
PORTB &= 0b11111101;
PORTB |= 0b00000001;
displaydigit(secondunits);
delay_f = 5;
time = counter;
}
if((counter>=time+50) && (delay_f == 5)){
PORTD &= 0b11110001;
PORTD |= 0b00000001;
PORTB &= 0b11111100;
PORTB |= 0b00000000;
displaydigit(seconddecimal);
delay_f = 0;
time = counter;
}
}
void displaydigit(int digit){
if(digit == 0){
PORTD &= 0b00001111;
PORTD |= 0b00000000;
}
else if(digit == 1){
PORTD &= 0b00011111;
PORTD |= 0b00010000;
}
else if(digit == 2){
PORTD &= 0b00101111;
PORTD |= 0b00100000;
}
else if(digit == 3){
PORTD &= 0b00111111;
PORTD |= 0b00110000;
}
else if(digit == 4){
PORTD &= 0b01001111;
PORTD |= 0b01000000;
}
else if(digit == 5){
PORTD &= 0b01011111;
PORTD |= 0b01010000;
}
else if(digit == 6){
PORTD &= 0b01101111;
PORTD |= 0b01100000;
}
else if(digit == 7){
PORTD &= 0b01111111;
PORTD |= 0b01110000;
}
else if(digit == 8){
PORTD &= 0b10001111;
PORTD |= 0b10000000;
}
else if(digit == 9){
PORTD &= 0b10011111;
PORTD |= 0b10010000;
}
}
ISR(TIMER0_COMPA_vect)
{
SREG &= ~(1<<SREG_I);
++counter;
if(counter>=99999)
counter = 0;
SREG |= (1 << SREG_I);
}