STM8S105S4 I2C

Publisher:jingwenLatest update time:2016-10-08 Source: eefocusKeywords:STM8S105S4  I2C Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STM8S105S4 I2C can send data from the machine with this configuration, but it cannot receive data from the machine. What's the problem? 
//I2C port initialization 
GPIO_Init(GPIOE,GPIO_PIN_1|GPIO_PIN_2, GPIO_MODE_OUT_OD_HIZ_SLOW); 
void I2C_init(void) 

I2C_CCRH = 0; //Standard I2C interface 
I2C_CCRL = 80 ; 
I2C_FREQR = 16; 
I2C_OARL = (I2CAddr<<1); 
I2C_OARH = 0x40 
; I2C_ITR = 0x06; 
I2C_CR1 = 0x01; 
I2C_CR2 |= 0x04; 

@far @interrupt void I2C_Handler (void) 

u8 state1,state2,state3; 
state1 = I2C_SR1; 
state2 = I2C_SR2; 
state3 = I2C_SR3; 
//Address matchif 
((state1&0x02)!=0){I2C_CR2 |= 0x04;} 
        //Data received, need to be processedif 
( (state1& 0x40) != 0) 
     { 
     I2C_CR2 |= 0x04; 
     temp1 = I2C_DR; 
SetOutput(temp1); 
     } 
//Slave needs to send dataif 
((state1 & 0x80) != 0)   
     { 
     I2C_DR = num++; 
     } 
if((state2 & 0x04) != 0)I2C_SR2 &= ~0x04; //Response failed, clear this bitif 
((state1 & 0x10) != 0) 

I2C_CR2 = 0x02; 


I wrote it like this, and found that there is no problem with the slave sending data, but the slave receiving data fails and no interrupt is entered.

After successfully transmitting the correct data once, I can't enter. The program is still running normally

 

 

 

 

 

 

 

 

#define I2CAddr 0x01
in the host
  //The host writes data, and the slave receives it. This does not work.
   Soft_I2C_Start();
    Soft_I2C_Write(0x02);
    Soft_I2C_Write(0xAA);
    Soft_I2C_Stop();
    //The host receives data. Without running the previous program, the host can receive data. Running the previous program, the data received by the host is 255.
    Soft_I2C_Start();
    Soft_I2C_Write(0x03);
    var0=Soft_I2C_Read(1);
    Soft_I2C_Stop();
    This soft I2C has no problem controlling other devices. The chip used by the host is STM32. There should be no problem.

 

 

 

 

 
 

 

 Solve the problem:

That's it. After you succeeded for the first time, if you turned off the response, how can you still receive it? You should be more careful when configuring the register in the future.

The problem is here I2C_CR2 = 0x02; Modify to I2C_CR2 |= 0x02; The reason is that after the modification, there is no reply after the first success

 

 

 

 

1. PC0, PC1 initialization, GPIO_Init(GPIOC, GPIO_Pin_1 | GPIO_Pin_0, GPIO_Mode_Out_OD_HiZ_Fast);
2. Hardware must be connected
3. The bus cannot be busy, it must be idle
The initialization code is as follows:
GPIO_Init(GPIOC, GPIO_Pin_0, GPIO_Mode_Out_OD_HiZ_Slow);
                GPIO_Init(GPIOC, GPIO_Pin_1, GPIO_Mode_Out_OD_HiZ_Slow);
                CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);
                I2C_Init(I2C1, 50000, 0xA5, I2C_Mode_I2C, I2C_DutyCycle_2,
           I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
//Note: I2C is already enabled in I2C_Init(), so I2C_Cmd(I2C1, ENABLE);
                while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

 
 

 

 

The built-in I2C of stm8 was finally debugged successfully.

1. This debugging of I2C has benefited me a lot. First of all, it proves that the official program for writing EEPROM is correct.
2. The reason why the debugging of TW8816 failed at the beginning was that I mistakenly thought that the register address was 16 bits (the chip company's business also said so, and I believed it), which made me see different data, but I think that there is data, and the program runs through using the WHILE waiting method, which means that it is connected to the slave.
3. Do not configure the GPIO of I2C. I saw some colleagues configure the GPIO.
4. In order for everyone to use the I2C of STM8 well, I baked the program.

void IIC_Init(void)
{
        UCHAR temp;
        
        CLK_PCKENR1 |= 0x01;

        I2C_FREQR |= 0x10; //Input peripheral clock frequency is 1MHz
        I2C_CR1 = 0x00; //Disable I2C peripherals
        I2C_CCRH &= ~0xcf;
        I2C_CCRL &= ~0xff;  
  I2C_TRISER = 0x11;
        I2C_CCRL = 0x10;
        I2C_CCRH = 0x00;
        I2C_CR1 |= 0x01; //Enable I2C peripherals
        I2C_CR2 |= 0x04; //Current received byte returns response
        I2C_CR2 &= 0x08;
        I2C_OARL = 0x86; //Own address
        I2C_OARH = 0x40;
}

void Read_8816(UCHAR *pBuffer, UCHAR index, UCHAR NumByteToRead)
{
        UCHAR temp;

        while(I2C_SR3 & 0x02); //Wait for bus to be free
        I2C_CR2 |= 0x01; //Generate start bit
        while(!(I2C_SR1 & 0x01)); //Wait for START to be sent
        I2C_DR = 0x8a; //Send 8816 device address
        while(!(I2C_SR1 & 0x02)); //Wait for special 7-bit device address to be sent
        temp = I2C_SR1;
        temp = I2C_SR3;
        I2C_DR = (UCHAR)(index);
        while(!(I2C_SR1 & 0x84));
        
        I2C_CR2 |= 0x01; //Generate repeated start bit
        while(!(I2C_SR1 & 0x01)); //Wait for START to be sent
        I2C_DR = 0x8b; //Read
        while(!(I2C_SR1 & 0x02)); //Wait for the 7-bit device address to be sent
        temp = I2C_SR1;
        temp = I2C_SR3;
        while(NumByteToRead) //How many bytes to read
        {
                if(NumByteToRead == 1)
                {
                        I2C_CR2 &= ~0x04; //Do not return a response
                        I2C_CR2 |= 0x02; //Generate a stop bit
                }
                if(I2C_SR1 & 0x40)
                {
                        temp = I2C_SR1;
                        Buff[8-NumByteToRead]=I2C_DR;
                        *pBuffer = Buff[8-NumByteToRead];
                        pBuffer++;
                        NumByteToRead--;
                }
        }
        I2C_CR2 |= 0x04;
        I2C_CR2 &= ~0x08; //Enable response for the next receive
}

Keywords:STM8S105S4  I2C Reference address:STM8S105S4 I2C

Previous article:STM8S interrupt priority setting
Next article:STM8S-Discovery third program - DS18B20

Recommended ReadingLatest update time:2024-11-16 13:30

Software simulation I2C read and write X1226 program
Written in C51, the program has passed the test and has been successfully used in the project. It is suitable for all 51 core microcontrollers and can be transplanted to other microcontrollers with very few modifications. I have transplanted it to the 430 microcontroller.     Disclaimer: This code is origina
[Microcontroller]
I2C reading and writing program for PIC microcontroller
TITLE " TWO WIRE/I2C BUS INTERFACE WITH PIC16C5x " ; LIST P=16C54 ; ;***************************************************************************** ;**  Two wire/I2C Bus READ/WRITE Sample Routines of Microchip's ;**  24Cxx / 85Cxx serial CMOS EEPROM interfacing to a  ;**  PIC16C54 8-bit CMOS single chip microcomputer ;
[Microcontroller]
Single chip microcomputer simulation I2C bus and 24C02 reading and writing examples
Microcontroller simulates I2C bus and 24C02 (I2C EEP ROM ) reading and writing examples (c source code) /* When using the 51 series microcontroller, it is sometimes necessary to simulate the I2C bus. */ /* Here is an example (reading and writing the serial EEPROM chip at2402) */ /*********************************
[Microcontroller]
I2C bus ferroelectric memory FM31256 with RTC
  FM31256 is a new generation of multifunctional system monitoring and non-volatile ferroelectric memory chip launched by Ramtron. Compared with other non-volatile memories, it has the following advantages: fast read/write speed, no write waiting time; low power consumption, static current less than 1 mA, write current
[Microcontroller]
I2C bus ferroelectric memory FM31256 with RTC
Integrated programming of MCU I2C and EEPROM
The memory function of TV channels, the setting of traffic light countdown time, and the memory function of outdoor LED advertisements may all use storage devices such as EEPROM. The advantage of this type of device is that the stored data can not only be changed, but also the data is saved and not lost after power fa
[Microcontroller]
C51IO port simulates I2C bus to drive AT24C16 (EEPROM part)
/*  Name: C51IO port simulates I2C bus driver AT24C16  Note: EEPROM, that is, AT24C16 here, is a special form of FLASH memory, but its capacity is generally small. It is more suitable for storing small amounts of data. The communication interface of AT24C16 is standard I2C communication, that is, we need to operate
[Microcontroller]
STM8 simulates I2C to drive 0.91-inch OLED screen
#define OLED_SCLK_Clr() GPIO_ResetBits(I2C_PORT, I2C_SDA_PIN )//SDA IIC interface clock signal #define OLED_SCLK_Set() GPIO_SetBits(I2C_PORT, I2C_SDA_PIN ) #define OLED_SDIN_Clr() GPIO_ResetBits(I2C_PORT, I2C_SCL_PIN)//SCL IIC interface data signal #define OLED_SDIN_Set() GPIO_SetBits(I2C_PORT, I2C_SCL_PIN) /*******
[Microcontroller]
I2C controlled battery power management IC with integrated power devices
Charges high-capacity batteries from any 5V source while maintaining cool operation Designers of portable electronics face the challenge of developing devices that can do everything and run forever on a single battery charge. While it is impossible to completely solve this problem, each generation of battery te
[Power Management]
I2C controlled battery power management IC with integrated power devices
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号