📄 iic read an d write.txt
字号:
C语言读写IIC设备程序(通过测试)
| 网站首页 | 文章中心 | 下载中心 | 图片中心 | 雁过留声 | 动力论坛 |
|
文章中心首页
|
自己搭建智能家
|
机器人
|
单片机
|
嵌入式系统
|
USB设备
|
CPLDFPGA
|
电子电路
|
EDA
|
程序设计
|
您现在的位置: 我们的家 >> 文章中心 >> 程序设计 >> 文章正文 用户登录 新用户注册
C语言读写IIC设备程序(通过测试) 【字体:小 大】
C语言读写IIC设备程序(通过测试)
作者:黄盈鑫 文章来源:本站原创 点击数:9 更新时间:2006-3-16
#include <reg51.h>
#ifndef false
#define false 0
#endif
#ifndef true
#define true 1
#endif
#define WriteDeviceAddress 0xa0
#define ReadDviceAddress 0xa1
sbit SDA = P3 ^ 7; //根据实际连接的管脚定义
sbit SCL = P3 ^ 5;
//--------------------------------------------------------------------------
void Delayus(unsigned int number)
{
for(;number!=0;number--)
{
}
}
//--------------------------------------------------------------------------
void DelayMs(unsigned int number)
{
unsigned char temp;
for(;number!=0;number--)
{
for(temp=112;temp!=0;temp--)
{
}
}
}
//--------------------------------------------------------------------------
void Start()
{
SDA=1;
Delayus(4);
SCL=1;
Delayus(4);
SDA=0;
Delayus(4);
SCL=0;
Delayus(4);
}
//--------------------------------------------------------------------------
void Stop()
{
SCL=0;
Delayus(4);
SDA=0;
Delayus(4);
SCL=1;
Delayus(4);
SDA=1;
Delayus(4);
}
//--------------------------------------------------------------------------
void Ack()
{
SDA=0;
Delayus(4);
SCL=1;
Delayus(4);
SCL=0;
Delayus(4);
SDA=1;
Delayus(4);
}
//--------------------------------------------------------------------------
void NoAck()
{
SDA=1;
Delayus(4);
SCL=1;
Delayus(4);
SCL=0;
Delayus(4);
SDA=0; //
}
//--------------------------------------------------------------------------
bit TestAck()
{
bit ErrorBit;
SDA=1;
Delayus(4);
SCL=1;
Delayus(4);
ErrorBit=SDA;
Delayus(4);
SCL=0;
return(ErrorBit);
}
//--------------------------------------------------------------------------
void Write8Bit(unsigned char input)
{
unsigned char temp;
for(temp=8;temp!=0;temp--)
{
SDA=(bit)(input&0x80);
Delayus(4);
SCL=1;
Delayus(4);
SCL=0;
Delayus(4);
input=input<<1;
}
}
//--------------------------------------------------------------------------
unsigned char Read8Bit()
{
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--)
{
SCL=1;
Delayus(4);
rbyte=rbyte<<1;
rbyte=rbyte|((unsigned char)(SDA));
SCL=0;
}
return(rbyte);
}
void Write24c02(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(10);
}
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned
char bytes) {
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
while(bytes!=1)
{
*RamAddress=Read8Bit();
Ack();
RamAddress++;
bytes--;
}
*RamAddress=Read8Bit();
NoAck();
Stop();
}
void main()
{
unsigned char write_data[5] = {0,1,2,3,4};
unsigned char read_data[5];
Write24c02(write_data,0x00,5);
Read24c02(read_data,0x00,5);
while(1)
{
}
}
文章录入:admin 责任编辑:admin
上一篇文章: 没有了
下一篇文章: 没有了
【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】
最新热点最新推荐相关文章
没有热门文章
没有推荐文章
nRF24E1原版源程序
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
没有任何评论
| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 |
本网站版权属我们的家所有。 站长:senxin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -