STM8S103K3 I2C

Publisher:Bby1978Latest update time:2019-11-08 Source: eefocusKeywords:STM8S103K3  I2C Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following is the .h file:


PA1 is defined as SDA and PA2 is defined as SCL

 

#ifndef __I2C_H

#define __I2C_H

#include "stm8s.h"

#include "stm8s_gpio.h"

#include "tim1.h"

#include "uart.h"

#include

#include

 

#define SCL              PA_ODR_ODR2

#define SDA PA_ODR_ODR1

#define SDAM PA_IDR_IDR1

#define SET_SCL_OUT()    {PA_DDR_DDR2=1; PA_CR1_C12 = 1; PA_CR2_C22 = 0;}

#define SET_SDA_OUT()    {PA_DDR_DDR1=1; PA_CR1_C11 = 1; PA_CR2_C21 = 0;}

#define SET_SDA_IN()     {PA_DDR_DDR1=0; PA_CR1_C11 = 0; PA_CR2_C21 = 0;}

 

void IIC_Init(void);

void Delay_us(u8 z);

void I2C_Start(void);

void I2C_Stop(void);

void IIC_Ack(void);

void IIC_NAck(void);

uint8_t IIC_Wait_Ack(void);

void IIC_Send_Byte(uint8_t txd);

uint8_t IIC_Read_Byte(uint8_t ack);

void  Device_WriteData(uint8_t DeciveAddr,uint8_t DataAddr,uint8_t Data);

void Decive_ReadData(uint8_t DeciveAddr,uint8_t DataAddr,uint8_t *ReciveData,uint8_t num);

 

#endif

The following is the .c file:


#include "I2C.h"

//--------------------------------------------------------------

// Prototype      : void I2C_Start(void)

// Calls          : Delay_5us()

// Description    : Start Singnal

//--------------------------------------------------------------

void IIC_Init(void)

{

   SET_SCL_OUT();

   SET_SDA_OUT(); 

   SCL = 1;

   SDA = 1;

}

//--------------------------------------------------------------

// Prototype      : void Delay_5us(void)

// Description: Delay of about 5us

//--------------------------------------------------------------

void Delay_us(u8 z)

{

   //u8 i; //fcpu 8MHz

   //for (i=50; i>0; i--);

while(z--)

  {

    nop();nop();nop();nop();

  }

}

//--------------------------------------------------------------

// Prototype      : void I2C_Start(void)

// Calls          : Delay_5us()

// Description    : Start Singnal

//--------------------------------------------------------------

void I2C_Start(void)

{

    // SDA 1->0 while SCL High

  //During the high level of SCL, a falling edge appears on SDA to indicate the start signal

  SET_SDA_OUT();

    SDA = 1; //The data line is kept high first, and the start signal needs the falling edge of the port 

Delay_us(4);

    SCL = 1; //The clock line remains high            

    Delay_us(40); //There is a delay of about 5us, which depends on the device            

    SDA = 0; //The data line is pulled low and a falling edge appears           

    Delay_us(4); //Delay for a short while to ensure a reliable falling edge            

    SCL = 0; //Pull the clock line low to ensure that the data line is allowed to change next            

}

 

 

//--------------------------------------------------------------

// Prototype      : void I2C_Stop(void)

// Calls          : Delay_5us()

// Description    : Stop Singnal

//-------------------------------------------------------------- 

void I2C_Stop(void)

{

    // SDA 0->1 while SCL High

    //During the SCL high level period, SDA generates a rising edge to indicate stop

  SET_SDA_OUT();

SCL = 0;

Delay_us(2);

SDA = 0; // Ensure the data line is low level

Delay_us(40);

    SCL = 1; //First ensure that the clock line is high

    Delay_us(10); //Delay to get a reliable level signal            

    SDA = 1; // Rising edge on data line           

    Delay_us(40); //Delay to ensure a reliable high level           

}

 

 

//Response function

void IIC_Ack(void)

{

    //The data line remains at a low level, and a rising edge on the clock line indicates a response

 

SET_SDA_OUT();

Delay_us(10);

    SDA = 0;

    Delay_us(10);

    SCL = 0;

    Delay_us(40);

SCL = 1;

Delay_us(40);

    //After the response is completed, pull the clock line low to allow data modification

    SCL = 0;

}

//Non-response

void IIC_NAck(void)

{

    //Non-response is the opposite of response. The difference is that the data line remains at a high level.

SET_SDA_OUT();

Delay_us(10);

    SDA = 1;

    Delay_us(10);

SCL = 0;

Delay_us(40);

    SCL = 1;

    Delay_us(40);

    //Finally, pull the clock line low to allow data to change

    SCL = 0;

}

//Wait for response

uint8_t IIC_Wait_Ack(void) //0 means there is a response, 1 means there is no response

{

    //Response waiting count

    uint8_t ackTime = 0;

    //First set the data line to input mode. This program does not reflect this. If there is a response, a falling edge will appear.

SCL = 0;

SET_SDA_OUT();

    Delay_us(10);

SDA = 1;//

Delay_us(30);

SET_SDA_IN(); //Switch to input mode

    //Pull the clock line high

    SCL = 1;

    Delay_us(30);

    //Wait for the data line to pull low for response

    while(SDAM){

        //If it is not pulled low within this time

        ackTime ++;

        if(ackTime > 250)

        {

            // Consider it a non-response stop signal

            I2C_Stop();

            return 1;

        }

    }

    SCL = 0;

    return 0 ;

}

 

void IIC_Send_Byte(uint8_t txd)

{

    //Define a counting variable

    uint8_t i;

SET_SDA_OUT();

    //Pull the clock line low to allow data to change

//    SCL = 0;

    //Send data bit by bit

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

    {

  Delay_us(2);

        if((txd&0x80)>>7) //0x80  1000 0000

SDA=1;

else

SDA=0;

        txd<<=1;  

Delay_us(20);   

SCL=1;

Delay_us(20);  

SCL=0;

Delay_us(20); 

    }

}

 

//The return value is the received data

//Parameter is whether to respond 1 is response 0 is not response

uint8_t IIC_Read_Byte(uint8_t ack)

{

    //Define the counting variable

    uint8_t i = 0;

    //Define the receiving variable

    uint8_t receive = 0;

    //At this time, the data line mode should be switched to input mode. This is not reflected in this program.

 

SET_SDA_IN(); //Switch to input mode

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

    {

  Delay_us(50);

  SCL = 0;

Delay_us(50);

        //Pull the clock line high to read the data to ensure that the other party's data does not change

        SCL = 1;

        // Add a delay to ensure the level is reliable

       // Delay_us(5);

        //First left shift the receiving variable to prevent the variable from being changed at the end of the loop

        receive<<=1;

        //Judge the data line level

        if(SDAM)

        {

            //If the level is high, the receiving variable is incremented, and if the level is low, it does not change but only shifts left, which ensures that the bit is 0

            receive++;

        }

        // Delay for a short while to ensure a reliable level

       // Delay_us(1);

        //Pull the clock line low to allow the next bit of data to change

        //SCL = 0;

    }

Delay_us(50);

SCL = 0;

//    if(!ack)

//    {

        //If no response is needed, a non-response signal is given and no further progress is made

//       IIC_NAck(); 

//    }

// else

// {

        //If a response is needed, give a response

//        IIC_Ack();

//    }

return receive;

}

 

void  Device_WriteData(uint8_t DeciveAddr,uint8_t DataAddr,uint8_t Data)

{

    //Start signal

    I2C_Start();    

    //Send device address                         

    IIC_Send_Byte(DeciveAddr);       

    //Wait for response

    IIC_Wait_Ack();                          

    //Send data address

    IIC_Send_Byte(DataAddr);                     

    //Wait for response

    IIC_Wait_Ack();                          

    //send data

    IIC_Send_Byte(Data);                     

    //Wait for response

    IIC_Wait_Ack();                          

    //End signal

    I2C_Stop();     

}

 

//Read data

//Parameter 1 device address

//Parameter 2 data address

//Parameter three receives data storage

//Parameter 4 receiving length

//

void Decive_ReadData(uint8_t DeciveAddr,uint8_t DataAddr,uint8_t *ReciveData,uint8_t num)

{

    //Define the counting variable

    uint8_t i;

    //Start signal

    I2C_Start();

    //Send device address

    IIC_Send_Byte(DeciveAddr);

    //Wait for response

    IIC_Wait_Ack();

    //Send data address

    IIC_Send_Byte(DataAddr);                     

    //Wait for response

    IIC_Wait_Ack();     

 

    //Start signal

    I2C_Start();

    //Send device address read mode

    IIC_Send_Byte(DeciveAddr + 1);

    //Wait for response

    IIC_Wait_Ack();

    //Read data

    for(i = 0;i < (num-1);i ++)

    {

        //The first num-1 bits of data need to be answered because they need to continue reading

        *ReciveData= IIC_Read_Byte(1);

        ReciveData++;

    }

    //The last bit of data does not need to be responded to because it does not need to be read

    *ReciveData = IIC_Read_Byte(0);

    //Stop signal

    I2C_Stop();

}

Note: I2C communication requires an external pull-up resistor!

Keywords:STM8S103K3  I2C Reference address:STM8S103K3 I2C

Previous article:STM8S AWU low power mode
Next article:STM8S TM1650 chip control program

Recommended ReadingLatest update time:2024-11-23 10:38

1.10.4_I2C_I2C controller programming_framework_P
The following figure is a flowchart in I2C Transmitter mode. The following figure is a flowchart in I2C Receiver mode.
[Microcontroller]
1.10.4_I2C_I2C controller programming_framework_P
Measuring with High Voltage I2C Current and Voltage Monitors
introduction As the complexity of today's electronic product designs continues to increase, managing power consumption and optimizing overall efficiency becomes more important. Accurate supply voltage and current monitoring is critical to saving power and ensuring reliability in everything from industrial and t
[Test Measurement]
Measuring with High Voltage I2C Current and Voltage Monitors
I2C communication read and write data process
At the beginning of communication, the master and slave machines must agree on the communication rules according to their own requirements: the definition and location of the command, the number and location of the address. Take reading and writing slave register data as an example: Assume that the slave register ad
[Microcontroller]
Using STM8L's hardware I2C (Part 2) Hardware I2C events and detection
1. Definition of I2C status by STM8L STD library I2C has four modes: Master write, Master read, Slave write, and Slave read. Direct register programming is certainly possible, but you must thoroughly understand the Reference manual, and the code is not intuitive and difficult to understand. The STD standard library
[Microcontroller]
Using STM8L's hardware I2C (Part 2) Hardware I2C events and detection
【stm32f407】I2C experiment
1. Introduction to I2C IIC (Inter-Integrated Circuit) bus is a two-wire serial bus developed by PHILIPS, used to connect microcontrollers and their peripherals. It is a serial bus composed of data line SDA and clock SCL, which can send and receive data. It can transmit data bidirectionally between CPU and controlled
[Microcontroller]
【stm32f407】I2C experiment
I2C signal line isolation circuit
I2C and SMBUS bus master-slave mode is different from 4-wire SPI QSPI and Microwire data interface standards. This bus mode only needs 2 lines for data transmission and sends and receives data on the same line. Figure 1 can realize an isolated 2-wire interface. The isolated 5V power supply can be obtained from the mai
[Analog Electronics]
I2C signal line isolation circuit
MINI2440i2c driver learning 1
int main(int argc, char** argv) {  struct eeprom e;  fprintf(stderr, "Open /dev/i2c/0 with 8bit moden");  die_if(eeprom_open("/dev/i2c/0", 0x50, EEPROM_TYPE_8BIT_ADDR, &e) 0,    "unable to open eeprom device file "    "(check that the file exists and that it's readable)");           fprintf(stderr, "  Reading 256 byt
[Microcontroller]
NiosII I2C Control IP and Its Application in Imaging System
Abstract: This paper introduces the working principle of an I2C control IP and its programmable registers in detail, and gives an application example of this IP in CMOS digital imaging. This example is designed based on the system-on-programmable chip (SOPC) technology. The system functions are realized by writing p
[Industrial Control]
NiosII I2C Control IP and Its Application in Imaging System
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号