STM32F4——IIC bus and SPI bus

Publisher:快乐舞动Latest update time:2018-07-14 Source: eefocusKeywords:STM32F Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Introduction:

    The I2C bus is a two-wire serial bus used to connect microcontrollers and peripheral devices. The bus has a data line SDA and a clock line SCL, which can be used to send and receive data.


2. Signal and Timing:

    The I2C bus has three types of signals: start signal, end signal and response signal.

    Start signal: When SCL is high, SDA jumps from high to low.

    End signal; when SCL is high, SDA jumps from low to high.

    Response signal: After receiving the data, the peripheral device that receives the data sends a response signal to the controller that sends the data to indicate that the data has been received. After sending data to the peripheral device, the controller waits for the response signal from the controller. When the response signal is received, the corresponding judgment is made based on the response signal.

    The relevant timing is as follows:

    Start and stop timing:

    

    Response timing:

    

3. Related code:

    According to the above explanation, you can understand the following signal codes:


//Generate IIC start signal

void IIC_Start(void)

{

SDA_OUT(); //sda line output

IIC_SDA=1;    

IIC_SCL=1;

delay_us(4);

  IIC_SDA=0; 

delay_us(4);

IIC_SCL=0; //Clamp the I2C bus and prepare to send or receive data 

}  

//Generate IIC stop signal

void IIC_Stop(void)

{

SDA_OUT(); //sda line output

IIC_SCL=0;

IIC_SDA=0;

  delay_us(4);

IIC_SCL=1; 

IIC_SDA=1; //Send I2C bus end signal

delay_us(4);  

}

//Wait for the response signal to arrive

//Return value: 1, failed to receive response

// 0, receiving the response successfully

u8 IIC_Wait_Ack(void)

{

u8 ucErrTime=0;

SDA_IN(); //Set SDA to input  

IIC_SDA=1;delay_us(1);   

IIC_SCL=1;delay_us(1);  

while(READ_SDA)

{

ucErrTime++;

if(ucErrTime>250)

{

IIC_Stop();

return 1;

}

}

IIC_SCL=0; //Clock output 0   

return 0;  

//Generate ACK response

void IIC_Ack(void)

{

IIC_SCL=0;

SDA_OUT();

IIC_SDA=0;

delay_us(2);

IIC_SCL=1;

delay_us(2);

IIC_SCL=0;

}

//No ACK response is generated    

void IIC_NAck(void)

{

IIC_SCL=0;

SDA_OUT();

IIC_SDA=1;

delay_us(2);

IIC_SCL=1;

delay_us(2);

IIC_SCL=0;

}     

//IIC sends a byte

//Return whether the slave has a response

//1, there is a response

//0, no response  

void IIC_Send_Byte(u8 txd)

{                        

    u8 t;   

SDA_OUT();    

    IIC_SCL=0; //Pull the clock low to start data transmission

    for(t=0;t<8;t++)

    {              

        IIC_SDA=(txd&0x80)>>7;

        txd<<=1;  

delay_us(2);   

IIC_SCL=1;

delay_us(2); 

IIC_SCL=0;

delay_us(2);

    }  

}    

//Read 1 byte, when ack=1, send ACK, when ack=0, send nACK   

u8 IIC_Read_Byte(unsigned char ack)

{

unsigned char i,receive=0;

SDA_IN(); //SDA is set as input

    for(i=0;i<8;i++ )

{

        IIC_SCL=0; 

        delay_us(2);

IIC_SCL=1;

        receive<<=1;

        if(READ_SDA)receive++;   

delay_us(1); 

    }  

    if (!ack)

        IIC_NAck(); //Send nACK

    else

        IIC_Ack(); //Send ACK   

    return receive;

}

SPI Bus

1. Introduction:

    SPI is a high-speed full-duplex synchronous communication bus with only four chip pins: MISO (master input, slave output), MOSI (master output, slave input), SCLK (clock signal), and CS (chip select).

2. Block diagram and working principle:

    The internal structure of SPI is as follows:

    

    The working principle is as follows: both the host and the slave have a serial shift register inside. The host writes a byte into its serial shift register to initiate transmission. The register sends the byte to the slave through the MOSI signal line, and the slave also sends a byte from its shift register to the host, thus realizing a byte exchange between the two shift registers. If only a write operation is performed, the host ignores the received data. If only a read operation is performed, the host can send an empty byte.

3. Working mode and block diagram:

    There are four working modes of SPI. According to the needs of peripherals, relevant configurations are made, mainly to configure the polarity and phase of the synchronous clock. The polarity configuration is to configure CPOL. When CPOL=0, the idle state of the synchronous clock is low level, and when CPOL=1, the idle state of the synchronous clock is high level; the phase configuration is to configure CPHA. When CPHA=0, data is collected at the first transition edge of the serial synchronous clock, and when CPHA=1, data is collected at the second transition edge of the serial synchronous clock. The relevant timing is as follows:

    

4. Library function configuration code:


//SPI module initialization code, configured as host mode  

//SPI port initialization

void SPI1_Init(void)

{  

GPIO_InitTypeDef GPIO_InitStructure;

SPI_InitTypeDef SPI_InitStructure;

 

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable GPIOA clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //Enable SPI1 clock

 

//GPIOFB3,4,5 initialization settings

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5; //PB3~5 multiplexing function output

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull output

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_100MHz;//100MHz

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Pull-up

GPIO_Init(GPIOB, &GPIO_InitStructure); // Initialization

 

GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); //PB3 is multiplexed as SPI1

GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); //PB4 is multiplexed as SPI1

GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); //PB5 is multiplexed as SPI1

 

//Here is only for SPI port initialization

RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE); //Reset SPI1

RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE); //Stop resetting SPI1

 

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //Set SPI unidirectional or bidirectional data mode: SPI is set to two-line bidirectional full-duplex

SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //Set SPI working mode: set to master SPI

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //Set the SPI data size: SPI sends and receives 8-bit frame structure

SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //The idle state of the serial synchronous clock is high

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //The second transition edge (rising or falling) of the serial synchronous clock is used to sample the data

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS signal is managed by hardware (NSS pin) or software (using SSI bit): internal NSS signal is controlled by SSI bit

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //Define the baud rate prescaler value: the baud rate prescaler value is 256

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Specify whether data transmission starts from the MSB bit or the LSB bit: Data transmission starts from the MSB bit

SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC value calculation polynomial

SPI_Init(SPI1, &SPI_InitStructure); //Initialize the peripheral SPIx registers according to the parameters specified in SPI_InitStruct

 

SPI_Cmd(SPI1, ENABLE); //Enable SPI peripheral

 

SPI1_ReadWriteByte(0xff); //Start transmission  


Keywords:STM32F Reference address:STM32F4——IIC bus and SPI bus

Previous article:The difference between STM32 MCU and 51 MCU
Next article:stm32_GPIO simulates I2C to read and write EEPROM

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号