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
}
Previous article:The difference between STM32 MCU and 51 MCU
Next article:stm32_GPIO simulates I2C to read and write EEPROM
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- ADI's smoke detector integrated solution based on ADPD188BI has started the registration for the prize live broadcast~
- Prize-winning live broadcast: TI chip technology is helping motor development in progress!
- C2000 MCU DesignDRIVE Solutions for Industrial Motor Drives
- New predictions for cloud computing in 2019
- Typical applications of Zigbee technology in street light control systems
- Bluetooth BLE - BlueNRG2 VTimer
- Celebrate National Day and wish our motherland a happy 70th birthday!
- [STM32F769Discovery development board trial] SD card file system application & hard-to-decode JPEG decoding code that makes people autistic
- The general form of C language function call
- R329 Development Board Application - Operation Image (1)