24C02 storage and reading

Publisher:MysticalGardenLatest update time:2016-09-23 Source: eefocusKeywords:24C02 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*

------Storage and reading of 24C02-----------
1. Use 24C02 to save the content of an array, and then read it out;
2. Be proficient in the operation of the IIC bus;
3. Start signal, stop signal, response and non-response signals, busy signal,
   read a byte data subroutine from the current position, write a byte subroutine from the current position,
   read a byte data subroutine from the specified position, and write a byte subroutine from the specified position.

4. Operation timing of various signals;
5. This program can also be used as a general subroutine of IIC;
6. This program has been tested on the Puzhong MCU training box;

*/

#include
#include
#define uint unsigned int
#define uchar unsigned char
unsigned char code dula[] = {0x3f,0x06,0x5b,0x4f,
     0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uchar wula[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
sbit sda=P2^1; //define serial data port
sbit scl=P2^0; //define serial clock port
void delay(uint z);
void start();
void stop();
void ack(bit aa);
uchar iic_readbyte();
void iic_writebyte(uchar date1);
void checkack();
uchar iic_addr_read(uchar addr);
void iic_addr_write(uchar addr,uchar date1);
void iic_addr_readn(uchar addr,uchar *s,uchar n);
void iic_addr_writen(uchar addr,uchar *s,uchar n);
void disp();
void nop5();
uchar write1[]={1,2,10,0,0,10,4,8,9,3};
uchar read1[]={0,0,0,0,0,0,0,0,0,0};
bit ack1;
uchar temp3;
//uchar flag1,flag2,num1,num2;
void main()
{
/* The first method to read and write multiple bytes from 24C02
uchar jj;
for(jj=0;jj<8;jj++)
{
iic_addr_write(jj+5,write1[jj]);
delay(10); //This delay is very important;
}

delay(10);
for(jj=0;jj<8;jj++)
{
read1[jj]=iic_addr_read(jj+5);
delay(10);//This delay is very important; 
} */
//
//The second method reads and writes multiple bytes from 24C02
iic_addr_writen(8,write1,8);
 delay(50);
 iic_addr_readn(8,read1,8);

 while(1)
 {disp();
 }
}


//-----5US delay subroutine--------------
void nop5()
{
_nop_();_nop_();_nop_();_nop_();_nop_();
}
//-------XMS delay subroutine----------------
void delay(uint z)
{
 uint x,y;
for(x=0;x for(y=0;y<112;y++);
}

//-------Start signal-----------
void start()
{
   sda=1;
   nop5();
   scl=1;
   nop5();
   sda=0;
   nop5();
   scl=0;
   nop5();

//------Stop signal-----------
void stop()
{
sda=0;
nop5();
scl=1;
nop5();
sda=1;
nop5();
scl=1;
nop5();

//=------Acknowledgement and non-acknowledgement signals---------
void ack(bit aa)
{
   scl=0;
   nop5();
   if(aa==1)
   sda=1;
   else
   sda=0;
   scl=1;
   nop5();
   scl=0;
   nop5(); 
}
//-------Busy signal------------
void checkack()
{ uchar i;   
   sda=1;
 scl=0;
 nop5();
 scl=1;
 nop5();
 while((sda==1)&&(i<250))
 {i++;}
 scl=0;
 nop5();
}
//-------Read a byte of data from the current position---------------

uchar iic_readbyte()
{  
  uchar i,temp;
  sda=1;
  scl=0;
  nop5();
  for(i=0;i<8;i++)
  {
   temp=temp<<1;
   scl=1;
   nop5();
   temp=temp|sda;
   scl=0;
  // nop5();
  // nop5();
   }
  scl=0;
  nop5();
  return(temp);
}
//-----Write a byte from the current position subroutine-------------
void iic_writebyte(uchar date1 )
{
 uchar i,temp;
 temp=date1;
 for(i=0;i<8;i++)
 {
   temp=temp<<1;
   sda=CY;
   scl=1;
   nop5();
   scl=0;
   //nop5();
  // nop5();
  }
  checkack();
  delay(1);
  scl=0;
  nop5();
}
//---Read a byte data subroutine from the specified position-----------------
uchar iic_addr_read(uchar addr)
{ uchar k2;
start();
iic_writebyte(0xa0);
delay(1);
iic_writebyte(addr);
delay(1);
start();
delay(10);
iic_writebyte(0xa1);
delay(10);
k2=iic_readbyte();
ack(1);
stop();
return(k2);
}

//---Write a byte from the specified position subroutine-------------
void iic_addr_write(uchar addr,uchar date1)
{
start();
iic_writebyte(0xa0);
delay(1);
iic_writebyte(addr);
delay(1);
iic_writebyte(date1);
delay(1);
stop();
}
//----------Write multiple bytes from the specified position subroutine-------------------
void iic_addr_writen(uchar addr,uchar *s,uchar n)
{
  uchar i;
  start();
  iic_writebyte(0xa0);
  //delay(1);
  iic_writebyte(addr);
 // delay(1);
  for(i=0;i    {
   iic_writebyte(*s);
   delay(20);//This delay must be longer, otherwise it is easy to make mistakes
   s++; //Because the device reacts slowly, give it enough time;
   }
   stop();
}
//-------Read multiple bytes from the specified position subroutine-------------------------
void iic_addr_readn(uchar addr,uchar *s,uchar n)
{
uchar i;
start();
iic_writebyte(0xa0);
//delay(1);
iic_writebyte(addr);
//delay(10);
start();
//delay(15);
iic_writebyte(0xa1);
//delay(10);
for(i=0;i  {
   *s=iic_readbyte();
   delay(1);
   ack(0);
   delay(20);//This delay must be longer, otherwise it is easy to make mistakes
   ++; //Because the device reacts slowly, give it enough time;
 }
*s=iic_readbyte();
ack(1);
stop();
}

//-----显示数据----------
void disp()
{uchar i;
 for(i=0;i<8;i++)
 {P0=dula[read1[i]];
  P1=wula[i];
    delay(3);
 P0=0;
     }
}

Keywords:24C02 Reference address:24C02 storage and reading

Previous article:51 MCU I2C bus driver
Next article:Simulate traffic light control (MCU C program)

Recommended ReadingLatest update time:2024-11-16 22:54

PIC microcontroller IIC communication read 24C02 program example 16F877A main frequency 4M
The text name is iic.h #ifndef _iic_h_ #define _iic_h_ //PIC microcontroller IIC communication initialization function declaration void iiccsh(void); //PIC microcontroller IIC communication read peripheral device function declaration //Function: send an 8-bit address and return an 8-bit data unsigned char iicread(uns
[Microcontroller]
24c02 c51 driver
#include AT89X51.H //#include stdio.h //#include absacc.h #include intrins.h #define uchar unsigned char #define uint unsigned int sbit led=P1^0; sbit led2=P1^1; sbit SCL=P3^5; //24c02 SCL sbit SDA=P3^4; //24c02 SDA //sbit DOG=P1^7; //狗 uchar x24c02_read(uchar address); //Read a byte of data from the address add
[Microcontroller]
24c02 c51 driver
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号