AT24C0X IIC-EEPROM's Keil C operation procedure.

xiaoxiao2021-03-06  42

The following programs I have passed only at 6 MHz crystal, and the AT89CXX test passed. . As for other crystals, it is not known to do not know. If you can't, adjust the delay yourself. Mainly in understanding the operation of the IIC bus, no matter whether it is AT24C0X or other IIC bus's chip can be applied well.

#include

SBIT SDA = P2 ^ 5; // SDA

SBIT SCL = P2 ^ 4; // SCL

SBIT WP = P2 ^ 3; // WP

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

6MHz crystal, AT89CXX

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

Void delay20ms (void)

{// delay 20ms

INT T = 1240;

Unsigned char i = 11;

For (; i! = 0; I -)

For (; t! = 0; t -);

}

// --------------------

Void MDDELAY (VOID)

{

Unsigned char i = 0;

For (; i <= 10; i );

}

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

Start signal

-------------------------- /

Void start () {

SDA = 1; // data cable high

SCL = 1; // clock high

MDDELAY ();

SDA = 0; // Under data, jump as start signal

SCL = 0; // clock high

MDDELAY ();

}

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

Stop signal

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

Void stop () {

SDA = 0; // data cable high

SCL = 1; // clock high

MDDELAY ();

SDA = 1; // data cable high

MDDELAY ();

}

/ * ----------------------- Send an answer ----------------------- * / void Ack () {SDA = 0; SCL = 1; MDDELAY (); SCL = 0;} / * ---------------------- Send No answer --------------------- * / void noAck () {sda = 1; SCL = 1; mddelay (); SCL = 0;} / * ------------------------------------------------- ------ Write a Byte ----------------------------------------- ------------------- * / void WritebyTe (unsigned char buffyte) {unsigned char i; for (i = 1; i <= 8; i ) {SCL = 1; mddelay (); SDA = (BIT) (bufbyte & 0x80); // Start writing from the high position, output a bit data SCL = 0; MDDELAY (); bufbyte << = 1; // left shift a bit} SCL = 0;} / * --------------------------------------------- ----- Write (Page Wirte) 24C04 --------------------------------------- --------- * / Void Write24C04 (unsigned char Romaddress, unsigned char Arraylen) {start (); // Send Start Signal WritebyTe (0xA0); // "Write" command Slices selected / chip (only one A2, A1, A0 == 0) ACK (); WritebyTe (RomadDress); // Preparation to write EEPROM address information Ack (); for (; arraylen! = 0; arraylen -) {WritebyTe (* arraydata); Ack (); arraydata ; // Move the address of the next arraydata array under the pointer} Stop (); // Send stop signal} / * ---------------------------------------------------------------------------------------------------------------------------------- ---------- Read a BYTE ------------------------------------- * / unsigned char read8bit () {unsigned char i, rbyte = 0; SDA = 1; for (i = 1; i <= 8; i ) {SCL = 1; SCL = 0; RByte << = 1; IF SDA) RBYTE | = 0x01;} return (rbyte);} / * ---------------------------------- ---------------- Read 24C004 (Randowm Read mode) --------------------------- ----------------------- * / void read24c04 (unsigned char Romaddress, unsigned char bYTES) {Unsigned char i; for (i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-55906.html

New Post(0)