24CXX I2C格式在STM32中连续读写应用

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

PDF 文件使用 "pdfFactory Pro" 试用版本创建 www.fineprint.cn
文件: E:\资料\常用子程序\I2C 跨页读写\store.c void I2C_Nack(void) { DelayUS(10); I2C_DAT_1; DelayUS(10); I2C_SCK_1; DelayUS(25); I2C_SCK_0; DelayUS(10);
PDF 文件使用 "pdfFactory Pro" 试用版本创建 www.fineprint.cn
文件: E:\资料\常用子程序\I2C 跨页读写\store.c }
2014-5-5, 17:43:55
/*********************************************************************************** ************************ // 函数原型: I2C_WriteNByte 数据赋值 // 函数功能: // 输入参数: addr-器件从地址,*pBuffer-待写入数据,length-待写数据长度 // 输出参数: status // 返 回 值: 成功-TURE,失败-FALSH // 按LENGTH长度单字节写 //I2C_suba_num=1,单字节子地址 //I2C_suba_num=2,双字节子地址 //I2C_suba_num=3.8+X字地址 ************************************************************************************ ***********************/ uint8 I2C_WriteNByte(uint16 addr,uint8 *pBuffer,uint8 length) { uint8 status,SendCnt; uint16 PageCnt; PageCnt = length/16; //24c16 page=16B if((length%16)!=0) PageCnt++; //不满16 for(;PageCnt>0;PageCnt--) { if(length < SendCnt = else { length -= SendCnt = } I2C_start(); if(I2C_suba_num==3) { I2C_SendByte(I2CAddr | ((addr/256)<<1));//8+x 器件地址(写) } else { I2C_SendByte(I2CAddr); //单,双字节子地址时的器件地址 } if(I2C_suba_num==2) { I2C_SendByte((uint8)(addr/256)); //双字节字地址,存储地址H } I2C_SendByte((uint8)addr); //存储地址L while(SendCnt != 0x00) { status = I2C_SendByte(*(uint8*)pBuffer); //((uint8*)pBuffer) ++; (uint8*)pBuffer ++; SendCnt --; DelayUS(30); } I2C_stop(); DelayMS(10); 页: 4 16) length; 16; 16; //每满16B重发start,addr
2014-5-5, 17:43:55
} //********************************************************************************** ** //*函数介绍: 发送无应答信号 //*输入参数: NONE //*输出参数: NONE //*返 回: 发无应答(SDA=H)表示为单字节读,有应答(SDA=L)表示连续读 //********************************************************************************** * void I2C_ack(void) { DelayUS(10); I2C_DAT_0; DelayUS(10); I2C_SCK_1; DelayUS(25); I2C_SCK_0; DelayUS(10); } //********************************************************************************** ** //*函数介绍: I2C单字节写 //*输入参数: SendByte //*输出参数: NONE 回: //*返 //********************************************************************************** * uint8 I2C_SendByte(uint8 SendByte) { uint8 count; uint8 status; for(count=8; count>0;count--) { if((SendByte&0x80) == 0x80) { I2C_DAT_1; } //1 else { I2C_DAT_0; } //0 DelayUS(10); SendByte <<= 1; I2C_SCK_1; //1 DelayUS(10); 页: 2 //左移
PDF 文件使用 "pdfFactory Pro" 试用版本创建 www.fineprint.cn
文件: E:\资料\常用子程序\I2C 跨页读写\store.c I2C_SCK_0; //0 DelayUS(10); } I2C_DAT_1; DelayUS(10); I2C_SCK_1; //1
文件: E:\资料\常用子程序\I2C 跨页读写\store.c
2014-5-5, 17:43:55
Байду номын сангаас
//例程使用24C16,8+x结构.16Byte一页,此例程主要针对连续多字节读写操作,也适用单字节操作 //I2C_WriteNByte //I2C_ReadNByte uint8 I2CAddr = 0xA0; uint8 I2C_suba_num = 3; //I2C器件地址 //I2C字地址字节结构 8+X
//********************************************************************************** ** //*函数介绍: I2C开始条件 //*输入参数: NONE //*输出参数: NONE //*返 回: //********************************************************************************** * void I2C_start(void) { I2C_SCK_0; DelayUS(25); I2C_DAT_1; //1 DelayUS(25); I2C_SCK_1; //1 DelayUS(25); I2C_DAT_0; //0 DelayUS(25); I2C_SCK_0; //0 DelayUS(25); } //********************************************************************************** ** //*函数介绍: I2C结束条件 //*输入参数: NONE //*输出参数: NONE //*返 回: //********************************************************************************** * void I2C_stop(void) { I2C_SCK_0; //0 DelayUS(25); I2C_DAT_0; //0 DelayUS(25); I2C_SCK_1; //1 DelayUS(25); I2C_DAT_1; //1 DelayUS(25); } //********************************************************************************** ** //*函数介绍: 发送无应答信号 //*输入参数: NONE //*输出参数: NONE //*返 回: 发无应答(SDA=H)表示为单字节读,有应答(SDA=L)表示连续读 //********************************************************************************** * 页: 1
2014-5-5, 17:43:55
DelayUS(200); //read ACK I2C_SCK_0; //0 DelayUS(10); status = TRUE; return(status); } //********************************************************************************** ** //*函数介绍: I2C单字节读 //*输入参数: NONE //*输出参数: uint8 //*返 回: //********************************************************************************** * uint8 I2C_ReceiveByte(void) { GPIO_InitTypeDef GPIO_InitStructure; uint8 count; uint8 Rdata = 0; GPIO_InitStructure.GPIO_Pin = I2C_DAT; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //模式:IN GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); DelayUS(10); for(count=8; count>0; count--) { Rdata <<= 1; I2C_SCK_1; //1 DelayUS(10); if(GPIO_ReadInputDataBit(GPIOD,I2C_DAT)==1) {Rdata |= 0x01;} I2C_SCK_0; //0 DelayUS(10); } GPIO_InitStructure.GPIO_Pin = I2C_DAT; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //模式:输出 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); return(Rdata); 页: 3
PDF 文件使用 "pdfFactory Pro" 试用版本创建 www.fineprint.cn
文件: E:\资料\常用子程序\I2C 跨页读写\store.c
2014-5-5, 17:43:55
addr += 16; } return(status); } //********************************************************************************** ** I2C_ReadNByte // 函数原型: 数据赋值 // 函数功能: // 输入参数: addr-子地址,*pBuffer-待写入数据,length-待写数据长度 // 输出参数: status // 返 回 值: 成功-TURE,失败-FALSH // 按LENGTH长度连续读 //I2C_suba_num=1,单字节子地址 //I2C_suba_num=2,双字节子地址 //I2C_suba_num=3.8+X字地址 //********************************************************************************** * uint8 I2C_ReadNByte(uint16 addr,uint8 *pBuffer,uint8 length) { uint8 status; I2C_start(); if(I2C_suba_num==3) { I2C_SendByte(I2CAddr+((addr/256)<<1)); //8+x 器件地址(写) } else { I2C_SendByte(I2CAddr); //单,双字节子地址时的器件地址 } if(I2C_suba_num==2) { I2C_SendByte((uint8)(addr/256)); //双字节字地址,存储地址H } I2C_SendByte((uint8)addr); //存储地址L I2C_start(); //读开始
相关文档
最新文档