SPI read-Write summary

Source: Internet
Author: User
The SPI protocol is a master-slave mode: The slave does not actively initiate access and always performs the operation passively.
CSN: Chip selection signal.
SCK: Clock signal.
Mosi:master output slave input, that is, the host outputs from the machine. The host can be understood to write from the device.
Miso:master input slave output, which is the host input from the slave. You can understand the host read from the device.

SPI full Name: Serial Peripheral Interface, which is the serial peripheral interface. The SPI protocol naturally transmits data serially, with each

Press 1 bit to read and write the device, rather than parallel each 1byte (8bit) transmission.


Single-byte read timing:

    /*
    * * Function Name: Spi_read_onebyte
    * * return value: Temp--spi read a byte of data
    * * parameter  : None
    * * Description  : Falling along read data, 1 bit
  */
    uint8 spi_read_onebyte (void)
    {
      uint8 i;
      Uint8 temp = 0;
      
      for (i=0;i<8;i++)
      {
       temp <<= 1;       Read the value of Miso 8 input and save it to temp. The position after the "SCK = 0" statement is not placed because:
                         //After reading the last 1byte (ie, LSB), can not move left
       SCK = 1;         
       if (miso)          //reads the highest bit, saves to the end, reads the entire byte through the left shift
         temp |= 0x01;
        else
         temp &= ~0x01;
       SCK = 0;          The Falling Edge comes (sck from 1-->0), the data on the miso will change, stable after reading into temp
      }
      
      return temp;
    }

Single-byte write timing:

    /*
    * * Function Name: Spi_write_onebyte
    * * return value: None
    * * parameter  : U8_writedata--spi write a byte of data
    * * Description  : Rising edge Write data, each write into 1 bit
    *
    /void Spi_write_onebyte (uint8 u8_writedata)
    {
      uint8 i;
      
      for (i=0;i<8;i++)
      {
        if (U8_writedata & 0x80)      //judge highest bit, always send highest bit
         mosi_on;                    MOSI Output 1, data bus preparation 1
        else
         Mosi_off;                   Mosi output 0, data bus preparation 0
        
        SCK = 1;                     The rising Edge comes (sck from 0-->1), data on the bus is written to the device
        u8_writedata <<= 1;          Left shift discards the highest bit SCK that has been output
        = 0;                     Pull down sck signal, initialize to 0
      }
    }

Nrf24l01 Register Write function:

    /*
    * * Function Name: Nrf24l01_writereg
    * * return value: None
    * * parameter: (1) uint8 addr--Register Address
    * *         (2) uint8 value--Write Value
    * * Description: Nrf24l01 Register Write function
    */
    void Nrf24l01_writereg (uint8 addr, uint8 value)
    {
        csn_off ();                  CS Sheet Selection Pull low
        spi_write_onebyte (addr| WR); SPI Write Address command
        spi_write_onebyte (value);   SPI Write Data
        csn_on ();                   CS Chip Select Pull High
    }
NRF24L01 Read Register function:

    /*
    * * Function Name: Nrf24l01_readreg
    * * return value: value--Read Register value
    * * Parameter: addr--Register Address
    * * Description: NRF24L01 Register read function */
    uint8 Nrf24l01_readreg (uint8 addr)
    {
        uint8 value;
        Csn_off ();                   CS Sheet Selection Pull low
        spi_write_onebyte (addr| RR);  SPI Write address command
        value = Spi_read_onebyte ();  SPI read Data
        csn_on ();                    CS Slice Select Pull high
        return value;
    }

Note: The read-write program can be integrated as follows: (Because the SPI is a ring structure, the input will have output)

    /* * * Function Name: Spi_writeandread_onebyte * * return value: U8_readdata--spi read a byte of data * * Parameter: U8_writedata--spi write a byte of data * * Description: Rising edge write, falling along read * * In this example if U8_writedata is interpreted as an address, the returned u8_readdata is not the value of the address, but rather the value that the slave shift register moves when the address is sent   */uint8
      Spi_writeandread_onebyte (uint8 u8_writedata) {uint8 i;
         
      Uint8 u8_readdata = 0x00;      for (i=0;i<8;i++) {u8_readdata <<= 1;
           
        Read the value of Miso 8 inputs and deposit to u8_readdata.              if (U8_writedata & 0x80)//judgment highest bit, always write highest bit (output highest bit) mosi_on;             MOSI Output 1, data bus preparation 1 else mosi_off;     Mosi output 0, data bus preparation 0 u8_writedata <<= 1;                Left shift discards the highest bit SCK that has been output = 1; The rising Edge comes (sck from 0-->1), data on the bus is written to the device if (miso)//reads the highest bit, saves to the end, reads the entire byte by left shift U8_readdata |= 0x
        01;
            
        else U8_readdata &= ~0x01;                SCK = 0; The Falling Edge comes (sck from 1-->0), miso will generate new data, read into the U8--readdata} return u8_readdata; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.