24c64 Read and Write Program

Publisher:凌晨2点369Latest update time:2016-01-06 Source: eefocusKeywords:24c64 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include //Include a 52 standard kernel header file

#define uchar unsigned char //Define for easy use
#define uint unsigned int
#define ulong unsigned long
#define WriteDeviceAddress 0xa0 //Define the address of the device in the IIC bus
#define ReadDviceAddress 0xa1
sbit SCL=P2^7;
sbit SDA=P2^6;
sbit P20=P2^0;
//Timing function
void DelayMs(unsigned int number)
{
unsigned char temp;
for(;number!=0;number--)
{
for(temp=112;temp!=0;temp--) ;
}
}
//Start bus
void Start()
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
//End bus
void Stop()
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
//Send ACK0
void NoAck()
{
SDA=1;
SCL=1;
SCL=0;
}
//Test ACK
bit TestAck()
{
bit ErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit);
}
//Write 8 bits to 24c02
Write8Bit(unsigned char input)
{
unsigned char temp;
for(temp=8;temp!=0;temp--)
{
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input=input<<1;
}
}
//Write a byte to 24c02
void Write24c64(uchar ch,uchar address)
{
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(address);
TestAck();
Write8Bit(ch);
TestAck();
Stop();
DelayMs(10);
}
//Read 8 bits from 24c02
uchar Read8Bit()
{
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--)
{
SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|((unsigned char)(SDA));
SCL=0;
}
return(rbyte);
}
//Read 1 byte from 24c02
uchar Read24c64(uchar address)
{
uchar ch;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(address);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
ch=Read8Bit();
NoAck();
Stop();
return(ch);
}
//This lesson tests writing a byte to 24c02 and reading it out for verification
void main(void) // Main program
{
uchar c1;
uchar address =  0x01;
for ( address =  0x01;address<0xff;address++)
{
Write24c64(0x00,address); // Write 0x88 to the second address space of 24c02
c1=Read24c64(address);
}
P20=0;
while(1); // Program hangs
}

Keywords:24c64 Reference address:24c64 Read and Write Program

Previous article:Output 1KHz and 500Hz audio signals to drive speakers
Next article:Course Design of 51 Single Chip Microcomputer Controlled Traffic Lights

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号