Program for controlling AT24C04 by microcontroller STC12C5A60S2 (C language)

Publisher:数据小巨人Latest update time:2023-06-25 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/**************************************

Main chip: STC12C5A60S2 (1T)
Working frequency: 12.000MHz
**************************************** /

#include"REG51.H"
#include"INTRINS.H"


typedefunsignedcharBYTE;
typedefunsignedshortWORD;

sbitSCL=P3^4;//clock of AT24C04
sbitSDA=P3^5;//data of AT24C04

BYTEBUF[16];//data buffer

BYTEcodeTESTDATA[]=
{
0x00,0x11,0x22,0x33,0x44 ,0x55,0x66,0x77,
0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF
};

voidDelay5us();
voidDelay5ms();
voidAT24C04_Start();
voidAT24C04_Stop();
voidAT24C04_SenDACK(bitack);
bitAT24C04_Rec vACK();
voidAT24C04_SendByte (BYTEdat);
BYTEAT24C04_RecvByte();
voidAT24C04_ReadPage();
voidAT24C04_WritePage();

voidmain()
{
AT24C04_WritePage();
Delay5ms();
AT24C04_ReadPage();

while(1);

}

/*********** ***************************
Write 1 page (16 bytes) of data to AT24C04
and write the 16 test data starting from TESTDATA as the device 00~0F address
******************************************/
voidAT24C04_WritePage()
{
BYTEi;

AT24C04_Start();//Start signal
AT24C04_SendByte(0xa0);//Send device address + write signal
AT24C04_SendByte(0x00);//Send storage unit address

for (i=0; i《16; i++)

{

BUF[i] = AT24C04_RecvByte();

if (i == 15)

{

AT24C04_SendACK(1); //The last data needs to be NAK

}

else

{

AT24C04_SendACK(0); //Response ACK

}

}

AT24C04_Stop(); //Stop signal

}

/**************************************

Delay 5 microseconds (STC12C5A60S2@12M)

Different working environments require adjusting this function

This delay function is calculated using 1T instruction cycle, which is different from the traditional 12T MCU.

**************************************/

void Delay5us()

{

BYTE n = 4;

while (n--)

{

_nop_();

_nop_();

}

}

/**************************************

Delay 5 milliseconds (STC12C5A60S2@12M)

Different working environments require adjusting this function

This delay function is calculated using 1T instruction cycle, which is different from the traditional 12T MCU.

**************************************/

void Delay5ms()

{

BE n = 2500;

while (n--)

{

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

}

}

/**************************************

start signal

**************************************/

void AT24C04_Start()

{

SDA = 1; //Pull the data line high

SCL = 1; //pull the clock line high

Delay5us(); //Delay

SDA = 0; //Generate falling edge

Delay5us(); //Delay

SCL = 0; //pull the clock line low

}

/**************************************

stop signal

**************************************/

void AT24C04_Stop()

{

SDA = 0; //Pull the data line low

SCL = 1; //pull the clock line high

Delay5us(); //Delay

SDA = 1; //Generate rising edge

Delay5us(); //Delay

}

/**************************************

Send acknowledgment signal

Entry parameters: ack (0:ACK 1:NAK)

**************************************/

void AT24C04_SendACK(bit ack)

{

SDA = ack; //Write response signal

SCL = 1; //pull the clock line high

Delay5us(); //Delay

SCL = 0; //pull the clock line low

Delay5us(); //Delay

}

/**************************************

receive response signal

**************************************/

bit AT24C04_RecvACK()

{

SCL = 1; //pull the clock line high

Delay5us(); //Delay

CY = SDA; //Read response signal

SCL = 0; //pull the clock line low

Delay5us(); //Delay

return CY;

}

/**************************************

Send one byte of data to the IIC bus

**************************************/

void AT24C04_SendByte(BYTE dat)

{

SWITCH in;

for (i=0; i<8; i++) //8-bit counter

{

dat <<= 1; //Move out the highest bit of data

SDA = CY; //Send data port

SCL = 1; //pull the clock line high

Delay5us(); //Delay

SCL = 0; //pull the clock line low

Delay5us(); //Delay

}

AT24C04_RecvACK();

}

/**************************************

Receive one byte of data from the IIC bus

**************************************/

BYTE AT24C04_RecvByte()

{

SWITCH in;

BYTE dat = 0;

SDA = 1; //Enable internal pull-up and prepare to read data

for (i=0; i<8; i++) //8-bit counter

{

that <<= 1;

SCL = 1; //pull the clock line high

Delay5us(); //Delay

dat |= SDA; //Read data

SCL = 0; //pull the clock line low

Delay5us(); //Delay

}

return that;

}


Reference address:Program for controlling AT24C04 by microcontroller STC12C5A60S2 (C language)

Previous article:51 microcontroller programming skills combining assembly and C language
Next article:CAN bus becomes the bus standard for vehicle electrical systems

Recommended ReadingLatest update time:2024-11-15 08:19

The PCA module of STC12C5A60S2 outputs PWM wave
PCA is a module and a hardware structure, as shown in the following figure The 16-bit PCA timer/counter is equivalent to timer 0/1. It will automatically increase by 1 after each clock cycle. The clock cycle comes from the following parts: A clock cycle may be (SYSclk/1, SYSclk/2...), and the last external input r
[Microcontroller]
The PCA module of STC12C5A60S2 outputs PWM wave
Research on automobile anti-fatigue driving system based on STC12C5A60S2
  As we all know, cars have become a necessary means of transportation in our lives. They are closely related to our lives. However, traffic accidents caused by our fatigue driving have a considerable impact on our lives, both in terms of property and spirit. It causes serious damage, so how to reduce fatigue driving
[Microcontroller]
Research on automobile anti-fatigue driving system based on STC12C5A60S2
STC12C5A60S2 MCU IO port working mode setting
STC12C5A60S2 MCU IO port working mode setting   All I/O ports of STC12C5A60S2 series MCU can be configured by software into one of four working types. The four types are: quasi-bidirectional port (standard 8051 output mode), push-pull output, input only (high impedance) or open-drain output function. Each port i
[Microcontroller]
The enhanced single-chip microcomputer STC12C5A60S2 outputs square wave signals of various frequencies
Assume the clock frequency Fosc = 18.432MHz, design the program to output a clock with a frequency of 124.540KHz from the P1.0/CLKOUT2 pin; output a clock with a frequency of 125KHz from the T0 (P3.4) pin; and output a clock with a frequency of 38.4KHz from the T1 (P3.5) pin. Solution: Use the programmable clock outp
[Microcontroller]
ADC+PWM function of STC12C5A60S2
The A/D conversion port of the STC12C5A60S2 microcontroller is at port P1 (P1.7-P1.0), with 8-channel 10-bit high-speed A/D converters, and the speed can reach 250 KHz (250,000 times/second). Pulse width modulation (PWM) is a technology that uses a program to control the waveform duty cycle, period, and phase waveform
[Microcontroller]
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号