单片机
返回首页

AT89S52单片机并行端口模拟I2C总线协议读写AT24C04的设计

2023-05-19 来源:elecfans

I2C总线是2条线总线。数据线SDA,时钟线SCL.结构简单。

AT24C04是具有I2C总线接口的EEPROM.大小为512*8bit.单片机AT89S52本身不具有I2C总线结口,所以可编写程序用并行端口模拟I2C总线协议读写AT24C04.


多个设备通信的重点(1.电平的区别,如串口通信中PC与单片机通信,PC机串口电平值为+12V~-12V,单片机为TTL电平0V~+5V.,所以要用电平转换芯片转电平.2,通信协议。(串口通信协议))具体的协议内容与数据格式可查资料。


代码如下:

#include

#define WriteDeviceAddress 0xa0

#define ReadDeviceAddress 0xa1

sbit SCL = P3^4;

sbit SDA = P3^5;

sbit DOG = P0^0;

sbit PP = P0^1;

sbit DOG1 = P0^7;

void DelayMs(unsigned int number)

{

unsigned char tmp;

for(;number!=0;number--,DOG1=!DOG1)

{

for(tmp=112;tmp!=0;tmp--)

{

}

}

}

void Start()

{

SDA = 1;

DelayMs(1);

SCL = 1;

DelayMs(1);

SDA = 0;

DelayMs(1);

SCL = 0;

DelayMs(1);

}

bit Write8bit(unsigned char input)

{

unsigned char tmp;

for(tmp =8;tmp!=0;tmp--)

{

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

DelayMs(1);

SCL = 1;

DelayMs(1);

SCL = 0;

DelayMs(1);

input = input 《《 1;

}

return 1;

}

bit TestAck()

{

bit ErrorBit;

SDA = 1;

DelayMs(1);

SCL = 1;

DelayMs(1);

ErrorBit = SDA;

DelayMs(1);

SCL = 0;

DelayMs(1);

return(ErrorBit);

}

void Stop()

{

SCL = 0;

DelayMs(1);

SDA = 0;

DelayMs(1);

SCL = 1;

DelayMs(1);

SDA = 1;

DelayMs(1);

}

void WriteI2C(unsigned char *Wdata, unsigned char RomAddress, unsigned char number)

{

Start();

Write8bit(WriteDeviceAddress);

TestAck();

Write8bit(RomAddress);

TestAck();

for(;number!=0;number--)

{

Write8bit(*Wdata);

TestAck();

Wdata++;

}

Stop();

DelayMs(1);

}

unsigned char Read8Bit()

{

unsigned char tmp,rbyte = 0;

for(tmp=8;tmp!=0;tmp--)

{

SCL = 1;

DelayMs(1);

rbyte = rbyte 《《 1;

DelayMs(1);

rbyte = rbyte|((unsigned char)(SDA));

SCL = 0;

DelayMs(1);

}

return(rbyte);

}

void Ack()

{

SDA = 0;

DelayMs(1);

SCL = 1;

DelayMs(1);

SCL = 0 ;

DelayMs(1);

SDA = 1;

DelayMs(1);

}

void NoAck()

{

SDA = 1;

DelayMs(1);

SCL = 1;

DelayMs(1);

SCL = 0 ;

DelayMs(1);

}

void ReadI2C(unsigned char* RamAddress,unsigned char RomAddress,unsigned char bytes)

{

Start();

Write8bit(WriteDeviceAddress);

TestAck();

Write8bit(RomAddress);

TestAck();

Start();

Write8bit(ReadDeviceAddress);

TestAck();

while(bytes != 1)

{

*RamAddress = Read8Bit();

Ack();

RamAddress++;

bytes--;

}

*RamAddress = Read8Bit();

NoAck();

Stop();

}

void main()

{

unsigned char writeByte[8] = {0xC0,0X34,0X12,0X22,0X11,0X01,0X00,0X00};

unsigned char readByte[8];

unsigned char *addw;

unsigned char *addr;

unsigned char i;

unsigned char ok = 0;

bit write = 1;

DOG = 1;

while(1)

{

if(write == 1)

{

addw = writeByte;

addr = readByte;

WriteI2C(addw,0x00,8);

ReadI2C(addr,0x00,8);

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

{

if(writeByte[i] == readByte[i])

{

ok++;

}

}

if(ok == 8)

{

DOG = 0; //一样P0.0亮

}

else

{

PP = 0; //不一样P0.1亮

}

write = 0;

}

}

}


进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 光控电路设计与分析

  • IGBT模块通过控制门极阻断过电流

  • CCFL的工作原理及电子驱动电路解析

  • 开关电源的基本组成及工作原理

  • 基于M66T旋律发​​生器的电路图解析

  • 基于CA3193的热电偶放大器电路

    相关电子头条文章