Literacy Tutorial: single-chip IIC communication foundation

MCU IIC basic communication

An interface definition
2 the procedure of Example

Reading Tips: This chapter explains the past 24C02- 24C512 circuit uses more memory, for now the mainstream STC15W MCU, usually has an internal comparator and DataFlash memory, it can be directly substituted substitution routine function of each chapter (see section 7 Chapter)
1 Interface Definition

I2C bus is a two wire serial bus (along with the GND line 3), requires only two clock and data lines can transmit data, only need to occupy two IO pins of the microcontroller, is very convenient to use, can I2C bus connected on the same bus, and a plurality of devices, each device has its own device address (as a comparison: SPI bus is not the device address, determine whether the chip is selected by a chip select line provides the CPU), read and write operations need to send the device address , and confirmed matches the device address after performing the corresponding operation, while other devices on the same bus not respond, the device address called.

SCM more peripheral devices using the I2C interface, EEPROM memory is the most common (eg:
24C02) and the ADC chip (such as: MCP3421).
24C02 microcontroller and connected to the circuit as shown in FIG.
Here Insert Picture Description
2 program example
24C series chips now used as a memory, we are most concerned about is how to write the data written to the chip and the success of how to read it.

Example 6.2 using hardware emulation IAP15W4K58S4 24C02 microcontroller emulation chip reader observed results
(also available 24C01 / 04/08/16) . In this case no page limits, fast, read for a plurality of successive bytes, the present chip backwards first set of data is written, then read out, if the data written to the read data indicates the same results is correct .

For simplicity, we directly transplanted mature program code (supporting routine provided).
① In addition to the main program MAIN.C, the rest * .C and * .H files copied to your folder and * .C file to the project, as shown.
Here Insert Picture Description
② Modify the definition of the pin connector according to the actual hardware. In the following statement IIC.H There may be set to any IO port, note
intended to pull.

sbit SCL = P3 ^ 7; // Serial Clock (code package must)

sbit SDA = P4 ^ 1; // serial data (code packets must)

③ The hardware revision slave address. In 24C01_02.H has: #define SlaveADDR 0xA0 // slave address format: 1010 *** 0. *** which corresponds to device pins for setting the device address A2A1A0 ,,, when the hardware A2A1A0 connected to GND, *** = 000.
④ under demanding circumstances, to modify the delay parameters based on the function R / C clock frequency. delay function delay1ms () and delay10ms () myfun.c the delay parameter calculated by the software described in Chapter 1.

⑤ The R / C clock frequency modification macro definition. IIC.H statements "#define tt 26" 26 corresponding to the constant
parameter t myfun.c the delay function void delay (unsigned char t) is, this constant value is determined
5uS delay time, the delay may be longer , can not be shorter, the clock frequency is 22.1184MHz when t = 26.
⑥ example here 24C02 chip is used, if the change with 24C04 / 08/16, in order to improve the multi-byte write speed of data across pages, the need for constant internal 24C01_02.C a function WrToRomPageB 0x07 to 0x0f.

Main code is as follows (note the results observed hardware simulation):

#include "STC15W4K.H" // Note that no macro definition behind semicolon
#include "24C01_02.H"
#include "myfun.h" void main ()
{
U8 I;
U8 TEST_DATA [20 is] = {0x10,0x20,0x30 , 0x40,0x50,0x60,0x70,0x80,0x90,0xa0,
0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81,0x91,0xa1};
port_mode (); // set all of quasi-bidirectional IO port weak the pull mode. WrToRomB (SlaveAddr,. 5, TEST_DATA, 20 is);
// device address, the address storing unit, the data pointer, the number of bytes written
for (I = 0; I <20 is; I ++)
{
TEST_DATA [I] = 0;
}
RdFromROM (SlaveADDR, 5, test_data, 20 ); // 20-byte data continuously read
the while (. 1);
}
Example 6.5 using recording power frequency and the 24C02 microcontroller (working group use mode)

Header code as follows:

#define E2P_RECORD_ADDR 0x00 // address storage unit

#define POWER_UP_MARK 0xAB // mark the first time

struct POWER_UP

{
u32 times;

u8 flag;

};
// main code is as follows:

#include “PowerUP.H”

#include “24C01_02.H”

#include “myfun.h”

struct POWER_UP Power_up;
void main()
{

RdFromROM(SlaveADDR,E2P_RECORD_ADDR,(u8*)&Power_up,sizeof(struct POWER_UP));

// chip hardware address, the address storing unit, the data set, the number of bytes written

if (Power_up.flag! = POWER_UP_MARK) // first memory use
{

}
else
{

}

Power_up.flag = POWER_UP_MARK; Power_up.times = 1;

Power_up.times++;
WrToRomB(SlaveADDR, E2P_RECORD_ADDR,(u8 *)&Power_up,sizeof(struct POWER_UP));

// chip hardware address, the address storing unit, the data set, the number of bytes written UART_init (); UART_Send_StrNum ( "the power frequency:", Power_up.times);
power on // the number of serial output, baud rate: 9600 / 22.1184MHz
the while (. 1);
}
The results shown in FIG.
Here Insert Picture Description

Example 6.6 using 24C02 instant of power to store data. Off moment is important data stored in the RAM to the EEPROM system is powered down, the next time when the power system is read out of the EEPROM data and run down detecting circuit shown in principle in FIG.
Here Insert Picture Description
Microperipherals using AC power interruption detecting presence or absence of AC power into the normal interrupt INT2 every 10mS, interrupt
the program so that the initial value of the timer reload timer T1 has not overflow interrupt is generated, the AC power failure, the timer count increasing the value until the overflow, overflow interrupt program the microcontroller immediately save the data to the EEPROM. The timer is greater than the time required
10mS to, such as 12 or 15mS to take, additionally, paying particular attention must interrupt pin external pull-up resistor.
For now mainstream STC15W MCU typically has an internal comparator and DataFlash memory, direct substitution 24C series memory usage, power failure detecting circuit simpler, and as in FIG microcontroller VCC to GND to two resistors.

Published 72 original articles · won praise 159 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_44212493/article/details/104334099