Understanding of SPI bus of 51 series microcontroller

Publisher:科技驿站Latest update time:2019-05-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

* UART, I2C and SPI are the three most commonly used communication protocols in microcontroller systems. *


1. Introduction to SPI:

    SPI is a high-speed, full-duplex, synchronous communication bus. The standard SPI uses only 4 pins and is often used for communication between microcontrollers and EEPROM, FLASH, real-time clocks, digital signal processors and other devices. The SPI communication principle is simpler than I2C. It is mainly a master-slave communication mode. This mode usually has only one host and one or more slaves. The standard SPI has 4 wires, namely SSEL (chip select, also written as SCS), SCLK (clock, also written as SCK), MOSI (Master Output/Slave Input) and MISO (Master Input/Slave Output).


    SSEL: Slave chip select enable signal. If the slave device is low level enabled, when this pin is pulled low, the slave device will be selected, and the host will communicate with the selected slave.

    SCLK: Clock signal, generated by the host, somewhat similar to the SCL of I2C communication.

    MOSI: The channel through which the host sends instructions or data to the slave.

    MISO: The channel through which the host reads the status or data of the slave.



2. Programming

Send a byte

void SPISendByte(unsigned char ch)

    unsigned char idata n = 8; //Send one data byte to SDA, a total of eight bits      

    SCLK = 1; // Set the clock high

    SSEL = 0; //Select slave

    while(n--)

    { 

        delayNOP();

        SCLK = 0; //Clock low

        if((ch & 0x80) == 0x80) //If the highest bit of the data to be sent is 1, send bit 1

        {       

            MOSI = 1; //Transmit bit 1

        }

        else

        {  

            MOSI = 0; //Otherwise transmit bit 0

        }

        delayNOP();

        ch = ch << 1; // data shifted one bit to the left

        SCLK = 1; // Set the clock high 

    }

}


Receive a byte

unsigned char SPIreceiveByte()

{

    unsigned char idata n = 8; // Read a data byte from the MISO line, a total of eight bits

    unsigned char tdata;


    SCLK = 1; //Clock is high

    SSEL = 0; //Select slave

    while(n--)

    {

        delayNOP();

        SCLK = 0; //Clock is low

        delayNOP();

        tdata = tdata << 1; // Shift left by one bit, or _crol_(temp,1)


        if(MISO == 1)

        {

            tdata = tdata | 0x01;    

        }// If the received bit is 1, the last bit of the data is set to 1

        else

        { 

            tdata = tdata & 0xfe;    

        }// Otherwise the last position of the data is 0

        SCLK = 1;

    }

    return(tdata);

}



At the beginning of each time, the clock line SCLK is at a high level, MISO is the data receiving bit, the highest bit is received first, and the data is obtained by shifting left 8 times. MOSI is the data sending bit, and the highest bit of the data is sent first. 

The sending and receiving of data are executed between the statements SCLK=0; and SCLK=1;, that is:


SCLK = 0;

Data transmission, reception and shifting

SCLK = 1;


That is, data transmission and reception start with the falling edge of SCLK and end with the rising edge.

--------------------- 

Author: Turbidity and Clearness 

Source: CSDN 

Original text: https://blog.csdn.net/qq_34706280/article/details/78702106 

Copyright Statement: This article is an original article by the blogger. Please attach the blog link when reprinting!


Reference address:Understanding of SPI bus of 51 series microcontroller

Previous article:8051 MCU baud rate calculation formula (with C language routine)
Next article:Two infrared decoding methods (RC-5)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号