c51单片机编程教程(分享单片机编程步骤)


自己学习记录用,有些乱,

1、IIC,eeprom24c02驱动配置

本实验是基于EEPROM的24C02驱动配置的编程,主要记录一些编程的重点节点的理解:

1、IIC驱动配置:精准的时钟配置,计算每一步所需要的指令周期,相加是10us。

c51单片机嵌入式开发总结2

2、IIC的起始信号配置,根据IIC的时序特性配置。

c51单片机嵌入式开发总结2

3、IIC终止信号配置,根据IIC的时序特性配置。

c51单片机嵌入式开发总结2

4、I2cSendByte(unsigned char dat): 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定。

c51单片机嵌入式开发总结2

5、I2cReadByte(),函数功能 : 使用I2c读取一个字节

c51单片机嵌入式开发总结2

6、void At24c02Write(unsigned char addr,unsigned char dat) 函数功能 :向 24c02的一个地址写入一个数据。

c51单片机嵌入式开发总结2

发送写器件地址是根据24C02的电路特性和地址特性获得。24C02是有E0,E1,E2寻址,根据下面两图片可知是8位寻址,因E0、E1、E2引脚接地,而是写所以,R/W位为零。,即发送器件地址位0xa0,同理接收时,接收器件地址为0xa1.

c51单片机嵌入式开发总结2
c51单片机嵌入式开发总结2

7、unsigned char At24c02Read(unsigned char addr)函数功能 : 读取24c02的一个地址的一个数据

c51单片机嵌入式开发总结2

2、24C02或者24C16进行字节、页读取、页存取

  1. void i2cStart(void) //开始时序
  2. {
  3. SDA=1;
  4. SCL=1;
  5. _delay(2); //延时2μs
  6. SDA=0;
  7. _delay(2);
  8. SCL=0;
  9. }
  1. void i2cStop(void) //结束时序
  2. {
  3. SDA=0;
  4. _delay(2);
  5. SCL=1;
  6. _delay(2);
  7. SDA=1;
  8. }
  1. void i2cAck_MCU(void) //应答时序
  2. {
  3. SDA=0;
  4. _delay(2);
  5. SCL=1;
  6. _delay(2);
  7. SCL=0;
  8. _delay(2);
  9. SDA=1;
  10. _delay(4);
  11. }
  1. void i2cNoAck_MCU(void) //非应答时序
  2. {
  3. SDA=1;
  4. _delay(2);
  5. SCL=1;
  6. _delay(2);
  7. SCL=0;
  8. _delay(2);
  9. }

24C02/16存储空间大小

24C02就是有2Kbit = 2000/8 = 250个字节,24C16就是有16kbit = 2000个字节,24C02一页最多可以写入8个字节,因此可以分成32页,24C16一页最多可以写入16个字节,因此可以分成128页。
1、字节写操作

void WriteByte_24LC16B(unsigned char input)

{

unsigned char bit_count;

for(bit_count=8;bit_count!=0;bit_count–)

{

_delay(2);

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

_delay(2);

SCL=1;

_delay(2);

SCL=0;

input=input<<1;

_delay(2);

}

SDA=1;

SDA_CTRL=1;//这里需要看自己使用的芯片设置SDA的IO口为输入状态,我用的是HT45F67芯片,SDA端口设置为输入状态,用于判断SDA是否接到主机的应答信号

_delay(2);

SCL=1;

_delay(2);

if(SDA == 1)

ack = 0;

else

ack = 1;

SCL=0;

SDA_CTRL=0; //此处根据自己所使用的芯片将SDA所在的Io口设置为输出状态

}

void Write_24LC16B(unsigned char Wdata,unsigned int RomAddress)

{

unsigned char block;

WriteDeviceAddress=0B10100000;

block=RomAddress/256;

RomAddress=RomAddress%256;

WriteDeviceAddress=WriteDeviceAddress|(block<<1);

i2cStart();

WriteByte_24LC16B(WriteDeviceAddress);

WriteByte_24LC16B((unsigned char)RomAddress);

WriteByte_24LC16B(Wdata);

i2cStop();

_delay(5500);

}

2、字节读操作

unsigned char ReadByte_24LC16B()

{

unsigned char bit_count,rbyte=0;

SDA=1;

SDA_CTRL=1;

_delay(10);

for(bit_count=8;bit_count!=0;bit_count–)

{

rbyte=rbyte<<1;

_delay(2);

SCL=1;

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

_delay(2);

SCL=0;

_delay(2);

}

SDA_CTRL=0;

return(rbyte);

}

unsigned char Read_24LC16B(unsigned int RomAddress)

{

unsigned char output,block;

ReadDeviceAddress=0B10100001;

WriteDeviceAddress=0B10100000;

block=RomAddress/256;

RomAddress=RomAddress%256;

WriteDeviceAddress=WriteDeviceAddress|(block<<1);

ReadDeviceAddress=ReadDeviceAddress|(block<<1);

i2cStart();

WriteByte_24LC16B(WriteDeviceAddress);

WriteByte_24LC16B((unsigned char)RomAddress);

i2cStart();

WriteByte_24LC16B(ReadDeviceAddress);

output=ReadByte_24LC16B();

i2cNoAck_MCU();

i2cStop();

_delay(2000);

return(output);

}

3、页写操作

Wdata为输入数组的首地址,RomAddress为需要进行存储的地址,范围在0~2047之间,cnt为一次需要写入的字节个数,建议采用8的倍数的cnt,因为本函数不采用自动分页,不是8的倍数会在超出页写入最多的字节数之后覆盖掉原来的数。

void WritePage_24LC16B(unsigned char *Wdata,unsigned int RomAddress,unsigned char cnt)

{

unsigned char block;

WriteDeviceAddress=0B10100000;

block=RomAddress/256;

RomAddress=RomAddress%256;

WriteDeviceAddress=WriteDeviceAddress|(block<<1);

i2cStart();

WriteByte_24LC16B(WriteDeviceAddress);

WriteByte_24LC16B((unsigned char)RomAddress);

while(cnt–)

{

WriteByte_24LC16B(*Wdata++);

}

i2cStop();

}

注意:连续进行多页写操作,需要在WritePage_24LC16B函数后添加150μs以上的延迟,这段时间,24C02内部需要将数据存储到芯片内部。

例子:

D_buffer[8] = {1,2,3,4,5,6,7,8};

WritePage_24LC16B(D_buffer,0,8);

_delay(150); //延迟150μs以及以上

WritePage_24LC16B(D_buffer,8,8);

4、页读操作
void ReadPage_24LC16B(unsigned char *Rdata,unsigned int RomAddress,unsigned char cnt)

{

unsigned char block;

ReadDeviceAddress=0B10100001;

WriteDeviceAddress=0B10100000;

block=RomAddress/256;

RomAddress=RomAddress%256;

WriteDeviceAddress=WriteDeviceAddress|(block<<1);

ReadDeviceAddress=ReadDeviceAddress|(block<<1);

i2cStart();

WriteByte_24LC16B(WriteDeviceAddress);

WriteByte_24LC16B((unsigned char)RomAddress);

i2cStart();

WriteByte_24LC16B(ReadDeviceAddress);

while(cnt>1)

{

*Rdata++ = ReadByte_24LC16B();

cnt–;

i2cAck_MCU();//发送完读地址后,需要应答一下

}

*Rdata = ReadByte_24LC16B();

i2cNoAck_MCU();//读取最后一个字节需要非应答

i2cStop();

_delay(2000);

二维码

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 394062665@qq.com 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论
客服