How to read and write CAT24C32 using C51 microcontroller

Publisher:陈风102Latest update time:2021-02-03 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include


#include //My custom LCD1602 header file


/*---------------------------------------------------------------


24C32 can store 4K (1024*4) bytes (8 bits), so the maximum addressing address is 0x0FFF, and 24C32 is a slave.


----------------------------------------------------------------*/


#defineWriteDeviceAddress0x0A2 //Define the write address of the device in the I2C bus (Note: change according to the custom slave address interface)


#defineReadDviceAddress0x0A3 //Define the read address of the device in the I2C bus (Note: change according to the custom slave address interface)


sbitSCL=P0^4; //My 24C32 interface settings


How to read and write CAT24C32 using C51 microcontroller


sbitSDA=P3^7;


//2us delay subroutine


voiddelay_2us(uchari)


{


while(--i);


}


// Start the bus


voidStart()


{


SDA=1;


SCL=1;


delay_2us(1);


SDA=0;


delay_2us(1);


SCL=0;


}


//End the bus


voidStop()


{


SDA=0;


SCL=1;


delay_2us(1);


SDA=1;


delay_2us(1);


SDA=0;


SCL=0;


}


//Send the response signal


//voidMACK()


//{


//SDA=0;


//SCL=1;


//delay_2us(1);


//SCL=0;


//SDA=1;


//}


//Send a no-acknowledge bit signal


voidMNACK()


{


SDA=1;


SCL=1;


delay_2us(1);


SCL=0;


SDA=0;


}


//Detect slave response signal


bitCACK()


{


bitflag;


SDA=1;


SCL=1;


delay_2us(1);


flag=SDA;


SCL=0;


return(flag);


}


/*--------------------------------------------------------------------------


//Write 8 bits to 24C32


---------------------------------------------------------------------------*/


Write8Bit(ucharinput)


{


uchartemp;


for(temp=0;temp《8;temp++)


{


SDA=(bit)(input&0x80);


SCL=1;


delay_2us(1);


SCL=0;


input=input《《1;


}


}


/*--------------------------------------------------------------------------


//Write a byte into 24C32


---------------------------------------------------------------------------*/


voidWrite24C32(ucharch,uintaddress)


{


Start();


Write8Bit(WriteDeviceAddress);


while(CACK());


Write8Bit(address/256); (Note: the address is sent twice, with the high bit first!)


while(CACK());


Write8Bit(address%256);


while(CACK());


Write8Bit(ch);


while(CACK());


Stop();


}


/*--------------------------------------------------------------------------


//Read 8 bits from 24C32


---------------------------------------------------------------------------*/


ucharRead8Bit()


{


unsignedchartemp,rbyte=0;


for(temp=0;temp《8;temp++)


{


SDA=1;


SCL=1;


rbyte=rbyte|((unsignedchar)(SDA));


if (temp < 7) // Note: only shift left 7 times


{


rbyte=rbyte《《1;


}


SCL=0;


}


return(rbyte);


}


/*--------------------------------------------------------------------------


Read 1 byte from 24c32


---------------------------------------------------------------------------*/


ucharRead24C32(uintaddress)


{


ucharch;


Start();


Write8Bit(WriteDeviceAddress);


while(CACK());


Write8Bit(address/256);


while(CACK());


Write8Bit(address%256);


while(CACK());


Start();


Write8Bit(ReadDviceAddress);


while(CACK());


ch=Read8Bit();


MNACK();


Stop();


return(ch);


}


/*--------------------------------------------------------------------------


Main function


---------------------------------------------------------------------------*/


voidmain(void) //main program


{


high rise;


LCD1602_init(); //1602 initialization


Write24C32 ('8', 0x01FF); // write '5' into 0x1FF


LCD1602_puts(0,0,“24C32W_TestOK!”);


newchar = Read24C32 (0x01FF); // Receive data from 0x1FF


LCD1602_puts(0,1,“ReadValue=”);


LCD1602_puts(10,1,newchar);


while(1);


}


Keywords:C51 Reference address:How to read and write CAT24C32 using C51 microcontroller

Previous article:nRF24L01 wireless module schematic and connection with 5V microcontroller
Next article:Sharing of DC motor PWM speed control program based on AT89C51 single chip microcomputer

Latest Microcontroller Articles
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号