Go Down

Topic: SPI Master-Slave Operation between Two ATmega328 under ArduinoUNO (Read 131 times) previous topic - next topic

GolamMostafa

With reference to the drawing of the attached file and the codes stated below, I am experiencing difficulty like:

The Slave receives 75 from the Master correctly and shows it on the 7-segmment display unit (DP0-DP1); but, the Master does not receive 23 from the Slave (LCD shows FF). Why?



Quote
//-- Master Initialization

#include <SPI.h>
#include <LiquidCrystal.h>

// APin           PC4 PC5 PC0 PC1 PC2 PC3     
LiquidCrystal lcd(A4,   A5,  A0, A1, A2, A3); //<--- APin
// LCD Signal     D-I/  E    D4  D5  D6  D7
// LCD Pin----->  4    6   11  12  13  14     R-W/(5) = tied to LL       

unsigned char  x1;

void setup()
{

  pinMode(8, INPUT_PULLUP);
 
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("SPI Interface");
 
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
 
  SPI.begin();  //SPI bus is enabled, SPI interrupt disable, SPI bus enabled, Master SPI     
  // Ouputs: SCK, MOSI, SS/; LL-->SCK, MOSI; LH---> SS/
  //what's about MISO(PB4)?  No need to do anything
   //  SPSR = 0x00;           
  //PB2(16):SS/---DPin-10 ----->
  // PB3(17): MOSI--DPin-11 ----->
  // PB4(18): MISO---DPin-12 <---
  // PB5(19):SCK ----DPin-13 ---->
  /*------------------------------------------------------------*/

  SPI.setBitOrder(MSBFIRST);  // MSB-bit will be transmitted first                  2.
  SPI.setClockDivider(SPI_CLOCK_DIV128); //TX Rate = 16 MHz/128 = 125 KBit          3.
  SPI.setDataMode(SPI_MODE1); //Clock Polarity & Phase (must match with peripheral) 4. 

  /*-------------------------------------------------------*/
  digitalWrite(10, LOW); //activate slave
  delay(10);

  while(digitalRead(8) !=0)    // Check that K1 is pressed down
       ;
  x1 = SPI.transfer(0x75);
 
 lcd.setCursor(0, 1);
 lcd.print(x1, 16);
   
  SPI.end();                // SPI bus is disabled                                 

}


void loop()
{

}


//--- Slave Initialization

#include <SPI.h>
 
unsigned char lupTable[] = {
                              0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,
                              0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C,
                              0x39, 0x5E, 0x79, 0x71
                           };   
 
unsigned int x1;
 
 
  void  setup()
  {
      DDRD = 0xFF;          // PORTD is output
      pinMode(8, OUTPUT);
      pinMode(9, OUTPUT);
     
      pinMode(10, INPUT_PULLUP);  //SS/-pin Slave SPI is enabled
       
      SPI.begin();        //int disabled, SPI enable
      bitClear(SPCR, 4);  //SPI slave selection; must be here
      SPI.setBitOrder(MSBFIRST);  // MSB-bit to be transmitted first   
      SPI.setDataMode(SPI_MODE1); //Clock Pol& Phase Mode1 
      SPI.setClockDivider(SPI_CLOCK_DIV128); //TX Rate = 16 MHz/128 = 125 KBit   

      SPDR = 0x23;
           
      while (bitRead(SPSR, 7) !=1)
          ;
      x1 = SPDR;     // x1 = 75; it has come from Master
   
  }

  void  loop()
  {
   
    x2 = x1;
   x2 = x2>>4;
   PORTD = lupTable[x2];
   bitWrite(PORTB, 0,0);    // = 0b11111110;
   bitWrite(PORTB,1,1);
   
    delay (5);
    //------------
    x2 = x1;
    x2 = x2 & 0b00001111;
    PORTD = lupTable[x2];
    PORTB = 0b11111101;
    delay (5);
    //;-------------- 
 
  }




Go Up