I2C read and write operation experiment based on single chip microcomputer

Publisher:岭南布衣Latest update time:2012-11-03 Source: 21ic Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

[ Experimental task ]
Using the characteristic of 24C08 that the stored data does not disappear after power failure, you can make a power failure protection device. First, use the single-chip microcomputer to make an automatic timer of 0-99 seconds. Then randomly turn off the power supply, and after power is turned on, the timer continues to count from the state before power failure.

[ Experimental Principle ]
First, briefly explain the following I2C bus. The I2C bus is a serial data bus with only two signal lines, one is the bidirectional data line SDA, and the other is the clock line SCL. A data byte transmitted on the I2C bus consists of eight bits. The bus has no limit on the number of bytes transmitted each time, but each byte must be followed by an acknowledge bit. Data transmission first transmits the highest bit (MSB), and data transmission is performed in the format shown in Figure 1. First, the host sends a start signal "S" (SDA jumps from high level to low level during the high level of SCL), and then the host sends a byte of data. The first byte of data after the start signal has a special meaning: the upper seven bits are the address of the slave, and the eighth bit is the transmission direction bit. 0 means that the host sends data (write), and 1 means that the host receives data (read). The addressed slave device is set to the corresponding working mode according to the transmission direction bit. The devices of the standard I2C bus have a seven-bit address. All devices connected to the I2C bus receive the first byte after the start signal and compare the received address with their own address. If the address matches, it is the slave that the host wants to visit, and it should send a low level to the SDA line as a response during the ninth clock pulse. In addition to the first byte being a general call address or a ten-bit slave address, the second byte is the data byte. After the data transmission is completed, the host sends a stop signal "P" (SDA jumps from a low level to a high level during the high level of SCL).
AT24C series serial E2PROM has I2C bus interface function, low power consumption, wide power supply voltage (2.5V~6.0V according to different models), working current is about 3mA, static current varies with power supply voltage, 30μA~110μA, AT24C series serial E2PROM parameters are as follows
Model Capacity Device addressing byte (8 bits) Number of bytes loaded at a time
AT24C01 128×8 1010A2A1A0 R/W 4
AT24C02 256×8 1010A2A1A0 R/W 8
AT24C04 512×8 1010A2A1P0 R/W 16
AT24C08 1024×8 1010A2P1P0 R/W 16
AT24C16 2048×8 1010P2P1P0 R/W 16Since
the I2C bus can connect multiple serial interface devices, each device in the I2C bus should have a unique device address. According to the I2C bus rules, the device address is 7-bit data (that is, in theory, 128 devices with different addresses can be connected to an I2C bus system). It and the 1-bit data direction bit constitute a device addressing byte, and the lowest bit D0 is the direction bit (read/write). The highest 4 bits (D7~D4) in the device addressing byte are the device model address. The model addresses of different I2C bus interface devices are given by the manufacturer. For example, the model address of the AT24C series E2PROM is 1010. The lower 3 bits in the device address are the pin addresses A2 A1 A0, corresponding to the D3, D2, and D1 bits in the device addressing byte, which are given by the connected pin level during hardware design.
The read and write operations of the AT24C series E2PROM fully comply with the I2C bus's master-receive-slave-transmit and master-transmit-slave-receive rules.

[ C language source program ]
#include
#include
#include
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07, 0x7f,0x6f,};
unsigned char sec; //Define the count value, every second, sec increases by 1
unsigned int tcnt; //Number of timer interrupts
bit write=0; //Write the flag of 24C08;
sbit gewei=P2^0; //Ones bit selection definition
sbit shiwei=P2^1; //Tens bit selection definition
//////////24C08 read and write driver /////////////////////
sbit scl=P3^4; // 24c08 SCL
sbit sda=P3^5; // 24c08 SDA
void delay1(unsigned char x)
{ unsigned int i;
for(i=0;i ;}
void flash()
{ ; ; }
void x24c08_init() //24c08 initialization subroutine
{scl=1; flash(); sda=1; flash();}
void start() //Start I2C bus
{sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();}
void stop() //Stop I2C bus
{sda=0; flash(); scl=1; flash(); sda=1; flash();}
void writex(unsigned char j) //write a byte
{ unsigned char i,temp;
temp=j;
for (i=0;i<8;i++)
{temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();}
scl=0; flash(); sda=1; flash();
}
unsigned char readx() //read a byte
{
unsigned char i,j,k=0;
scl=0; flash(); sda=1;
for (i=0;i<8;i++)
{
flash(); scl=1; flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=0;}
flash(); return(k);
}
void clock() // I2C bus clock
{
unsigned char i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
////////Read a byte of data from address address of 24c02/////
unsigned char x24c08_read(unsigned char address)
{
unsigned char i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(10);
return(i);
}
//////Write one byte of data to the address of 24c02 info/////
void x24c 08_write(unsigned char address,unsigned char info)
{
EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
EA=1;
delay1(50);
}
/////////////24C08 read and write driver completed/////////////////////
void Delay(unsigned int tc) //Delay program
{
while( tc != 0 )
{unsigned int i;
for(i=0; i<100; i++);
tc--;}
}
void LED() //LED display function
{
shiwei=0; P0=table[sec/10]; Delay(8); shiwei=1;
gewei=0; P0=table[sec%10]; Delay(5); gewei=1;
}
void t0(void) interrupt 1 using 0 //Timed interrupt service function
{
TH0=(65536-50000)/256; //Assign TH0 TL0
TL0=(65536-50000)%256; //Reload the initial value of the count
tcnt++; //Every 250ust, tcnt increases by one
if(tcnt==20) //When the count reaches 20 times (1 second)
{
tcnt=0; //Recount
sec++;
write=1; //Write to 24C08 once per second
if(sec==100) //Timer is 100 seconds, then start the count from zero
{sec=0;}
}
}
void main(void)
{
TMOD=0x01; //Timer works in mode 1
ET0=1; EA=1;
x24c08_init(); //Initialize 24C08
sec=x24c08_read(2);//Read the saved data and assign it to sec
TH0=(65536-50000)/256; //Assign TH0 TL0
TL0=(65536-50000)%256; //Make the timer interrupt once every 0.05 seconds
TR0=1; //Start timing
while(1)
{
LED();
if(write==1) //Judge whether the timer has counted for one second
{
write=0; //Clear
x24c08_write(2,sec); //Write data sec at address 2 of 24c08
}
}
}

[ Hardware Circuit Diagram ]


Keywords:MCU Reference address:I2C read and write operation experiment based on single chip microcomputer

Previous article:I2C read and write operation experiment based on single chip microcomputer
Next article:Application experiment of analog-to-digital conversion DAC0832

Recommended ReadingLatest update time:2024-11-16 17:46

The influence of parameter type on delay in single chip microcomputer delay program
      Today someone asked a question about Tianxiang's delay program.       void delay(unsigned int z)       {               unsigned int x,y;               for(x=z;x 0;x--)                     for(y=110;y 0;y--);         }         For this delay function, the system clock is 11.0592MHz. When z is 1, the d
[Microcontroller]
YuanCheng communication between microcontroller and PC based on MODEM
    Abstract: The public telephone network technology is mature and covers a wide range. It is a very effective method to use the existing public telephone network to realize remote communication between microcontrollers and PCs. This article introduces in detail the hardware, software design and implementation metho
[Industrial Control]
Design of low voltage reactive power compensation device based on single chip microcomputer control
O Introduction In power production, there are two types of power output by the generator, namely active power and reactive power. In the process of AC power transmission and use, part of the energy used to convert into mechanical energy, heat energy, light energy, etc. is called active power, and part of the ene
[Microcontroller]
Design of low voltage reactive power compensation device based on single chip microcomputer control
Design and implementation of a high cost-effective frequency meter based on single-chip microcomputer
1 System measurement principle The measurement method adopts multi-cycle synchronous measurement method to ensure the measurement accuracy. The principle of multi-cycle synchronous measurement is different from the traditional frequency and period measurement principle. The clock signal (f0) is s
[Microcontroller]
Design and implementation of a high cost-effective frequency meter based on single-chip microcomputer
What is the difference between the bidirectional port and the quasi-bidirectional port of the 51 microcontroller?
The main difference between a bidirectional port and a quasi-bidirectional port is that the quasi-bidirectional port I/O port needs to be set to 1 when inputting data, otherwise if the previous bit is low level and the next bit input level is high, the MOS tube cannot be pulled up, resulting in an error. The bidirectio
[Microcontroller]
UIP of MCU--TCP checksum calculation
TCP data verification, I searched a lot on the Internet and in the literature, and summarized it as follows: The data captured by the TCP packet capture tool is as follows:   according to:   1. Set the checksum field to 0; 2. Perform binary summation on every 16 bits in the TCP header; 3. If the high 16 bits of the
[Microcontroller]
UIP of MCU--TCP checksum calculation
Low-cost solar battery charger design without MCU
  The leakage problem of solar panels can traditionally be solved by using a Schottky diode in series with the solar panel, but the forward voltage drop of the Schottky diode causes it to consume a lot of power under high current conditions. Therefore, expensive heat sinks and sophisticated layouts are required to kee
[Microcontroller]
Low-cost solar battery charger design without MCU
Development of Pulse Measuring Instrument Based on 8098 Single Chip Microcomputer
1 Measurement principle The 8098 microcontroller has high-speed input and output channels with excellent performance. HSO0~HSO5 are high-speed output channels, which can generate pulse waves (PWM) with adjustable output width and period. HSI0~HSI3 are high-speed input channels. The CPU can simultaneously re
[Test Measurement]
Development of Pulse Measuring Instrument Based on 8098 Single Chip Microcomputer
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号