interface spi with P89v51rd2

Discussion in 'Embedded Systems and Microcontrollers' started by Darpan747, Feb 26, 2014.

  1. Darpan747

    Thread Starter New Member

    Feb 26, 2014
    8
    0
    Hello, P89v51rd2 is an extended version of 8051 which supports spi communication.
    I have two controllers.
    I have done ss,mosi,miso connections and clock connections.
    When i send some data from master to slave it is working correct as it gets displayed on the lcd.
    The problem I am facing is when I go for data transfer from slave to master that is using miso line.
    Can anyone suggest what changes to be made?
    Or which bit to be set or reset to make data transfer from slave to master in that same code?
     
  2. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    The whole idea of SPI is the circular buffer, whatever is placed in the serial buffer on the slave will be clocked out whilst the new data is being clocked in...

    If your master has binary 00110011 in the serial buffer
    And your slave has 11001100 in its serial buffer
    After 8 clocks they will be swapped... the master will hold 11001100 and the slave will hold 00110011..

    MOSI is master out slave in... and MISO is Master in slave out.. So to answer your question, it's is always done this way...
     
    Darpan747 and absf like this.
  3. Darpan747

    Thread Starter New Member

    Feb 26, 2014
    8
    0
    Hello, I understood the concept you said.
    BUt y data transfer from master to slave is working.
    When i tried to implement the slave to data transfer, the SPIF is not set in slave, that means my data is not transferred from slave.
    This i came to know as i had kept indications at various places in code like displaying on lcd.
    Here are my codes:

    MASTER CODE:

    Code ( (Unknown Language)):
    1.  
    2. #include<p89v51rd2.h>
    3. #include<lcd_ron.h>
    4. #include<DELAY.h>
    5.  
    6.  
    7. #define ss_pin P1_4 //SS(chip select) pin of the master
    8.  
    9. //function to setup the master configuration for the SPI communication
    10. void spi_config_master()
    11. {
    12.  ss_pin=0;  //make the SS(chip select) pin logic low because the SS pin of the slave is active low
    13.  SPCR=0X50; //SPI enable bit=1; MSTR bit=1 (to configure it as master) and frequency for SPI comm. = (F)osc/4
    14.  SPSR=0X00; //disable the SPI interrupt
    15.  SPDAT=0X00;    //initialize the SPI data register to 0X00
    16. }
    17.  
    18. void master_receive()               //receive data from slave
    19. {
    20. while(1)
    21. {
    22. if(SPSR==0X80)                      // if SPIF bit is set
    23. {
    24.     unsigned char val;
    25.     P3_3=0;                         //make led on(active low)
    26. P2=SPDAT;
    27.     val=P2+0x30;
    28.  
    29. DELAY(500);
    30.     lcd_char(val);                  //also display the received data on lcd
    31.     //P3_3=1;
    32.     //DELAY(500);
    33.     SPSR=0X00;                      //clear SPIF bit
    34. }
    35. else
    36. {
    37.     P3_3=1;
    38.     DELAY(500);
    39. }
    40. }
    41. }
    42.  
    43.  
    44.  
    45.  
    46. void main()
    47. {
    48.  //unsigned char m;
    49.     //initialize the port values
    50.  P0=0XFF;
    51.  P1=0xFF;
    52.  P3=0XFF;
    53.  P2=0XFF;
    54. lcd_init();
    55.  spi_config_master();   //setup the master configuration
    56.  //infinite loop
    57. while(1)
    58.     {
    59.  master_receive();
    60. }
    61. //SPDAT=0x00;
    62.  
    63.         //end of while loop
    64. }  
    65.  


    SLAVE CODE:

    Code ( (Unknown Language)):
    1.  
    2. #include<p89v51rd2.h>
    3. #include<lcd_ron.h>
    4. #include<DELAY.h>
    5.  
    6.  
    7. unsigned char val;
    8. void ini()              //spi slave config
    9. {
    10.      SPCR=0X40;                         //SPI enable bit=1; MSTR bit=0 (to configure it as slave) and frequency for SPI comm. = (F)osc/4
    11.  SPSR=0X00;                             //disable the SPI interrupt
    12.     SPDAT=0X00;                         //initialize the SPI data register to 0X00
    13.     //SPIF=0;
    14. }
    15.  
    16.  
    17. void spi_slave_tx(unsigned char data_given_to_master)
    18. {
    19.     //P1_4=1;
    20.     unsigned char temp;             //temporary variable to store the data to be given to slave
    21.  temp=data_given_to_master;             //store the data to be passed to the master
    22.  SPDAT=temp;
    23.  
    24.     P3_3=0;
    25.     DELAY(100);
    26.     if(SPIF==1)
    27. {                                       //wait while the transmission is complete ( the SPIF bit in the SPSR register goes high when the transmission is complete)
    28.  SPSR=0X00;                                     //clear the SPIF bit
    29.  
    30.     P3_3=1;
    31.     DELAY(100);
    32.    
    33. }
    34.  
    35. }
    36.  
    37.  
    38. void main()
    39. {
    40.  
    41.     //initialize the port values
    42.  P0=0XFF;
    43.  P1=0xFF;
    44.  P3=0XFF;
    45.  P2=0XFF;
    46. lcd_init();
    47. ini();  //setup the slave configuration
    48.  //infinite loop to continuously send/transmit data to master
    49. while(1)
    50.     {
    51.        
    52.   spi_slave_tx(0x08);   //send the character(here the number 8 is transmitted)
    53.   DELAY(5);
    54.         //random delay so that the processes on the slave side are completed and hence the slave data buffer (SPDAT of slave) is not filled with the garbage values
    55. }
    56. //SPDAT=0x00;
    57.  
    58.         //end of while loop
    59. }
    60.  
     
    Last edited by a moderator: Feb 27, 2014
  4. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    I noticed you are calling master_recieve() extremely quickly... You might want a delay in there to slow the code down a bit.

    I can't test your code.. I use ISIS to sim but ISIS doesn't support SPI on that model!!
     
  5. Darpan747

    Thread Starter New Member

    Feb 26, 2014
    8
    0
    Thanks for the help Ian.
     
  6. Darpan747

    Thread Starter New Member

    Feb 26, 2014
    8
    0
    Hello Ian,
    Thanks , there was surely a delay issue.
     
Loading...