#include"i2c.h"
/*******************************************************************************
* 函数名 : Delay10us()
* 函数功能 : 延时10us
* 输入 : 无
* 输出 : 无
*******************************************************************************/
void Delay10us()
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
/*******************************************************************************
* 函数名 : I2cStart()
* 函数功能 : 起始信号:在SCL时钟信号在高电平期间SDA信号产生一个下降沿
* 输入 : 无
* 输出 : 无
* 备注 : 起始之后SDA和SCL都为0
*******************************************************************************/
void I2cStart()
{
SDA=1;
Delay10us();
SCL=1;
Delay10us();//建立时间是SDA保持时间>4.7us
SDA=0;
Delay10us();//保持时间是>4us
SCL=0;
Delay10us();
}
/*******************************************************************************
* 函数名 : I2cStop()
* 函数功能 : 终止信号:在SCL时钟信号高电平期间SDA信号产生一个上升沿
* 输入 : 无
* 输出 : 无
* 备注 : 结束之后保持SDA和SCL都为1;表示总线空闲
*******************************************************************************/
void I2cStop()
{
SDA=0;
Delay10us();
SCL=1;
Delay10us();//建立时间大于4.7us
SDA=1;
Delay10us();
}
/*******************************************************************************
* 函数名 : I2cSendByte(unsigned char dat)
* 函数功能 : 通过I2C发送一个字节。在SCL时钟信号高电平期间,保持发送信号SDA保持稳定
* 输入 : num
* 输出 : 0或1。发送成功返回1,发送失败返回0
* 备注 : 发送完一个字节SCL=0,SDA=1
*******************************************************************************/
unsigned char I2cSendByte(unsigned char dat)
{
unsigned char a=0,b=0;//最大255,一个机器周期为1us,最大延时255us。
for(a=0;a<8;a++)//要发送8位,从最高位开始
{
SDA=dat>>7; //起始信号之后SCL=0,所以可以直接改变SDA信号
dat=dat<<1;
Delay10us();
SCL=1;
Delay10us();//建立时间>4.7us
SCL=0;
Delay10us();//时间大于4us
}
SDA=1;
Delay10us();
SCL=1;
while(SDA)//等待应答,也就是等待从设备把SDA拉低
{
b++;
if(b>200) //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
{
SCL=0;
Delay10us();
return 0;
}
}
SCL=0;
Delay10us();
return 1;
}
/*******************************************************************************
* 函数名 : I2cReadByte()
* 函数功能 : 使用I2c读取一个字节
* 输入 : 无
* 输出 : dat
* 备注 : 接收完一个字节SCL=0,SDA=1.
*******************************************************************************/
unsigned char I2cReadByte()
{
unsigned char a=0,dat=0;
SDA=1; //起始和发送一个字节之后SCL都是0
Delay10us();
for(a=0;a<8;a++)//接收8个字节
{
SCL=1;
Delay10us();
dat<<=1;
dat|=SDA;
Delay10us();
SCL=0;
Delay10us();
}
return dat;
}
/*******************************************************************************
* 函数名 : void At24c02Write(unsigned char addr,unsigned char dat)
* 函数功能 : 往24c02的一个地址写入一个数据
* 输入 : 无
* 输出 : 无
*******************************************************************************/
void At24c02Write(unsigned char addr,unsigned char dat)
{
I2cStart();
I2cSendByte(0xa0);//发送写器件地址
I2cSendByte(addr);//发送要写入内存地址
I2cSendByte(dat); //发送数据
I2cStop();
}
/*******************************************************************************
* 函数名 : unsigned char At24c02Read(unsigned char addr)
* 函数功能 : 读取24c02的一个地址的一个数据
* 输入 : 无
* 输出 : 无
*******************************************************************************/
unsigned char At24c02Read(unsigned char addr)
{
unsigned char num;
I2cStart();
I2cSendByte(0xa0); //发送写器件地址
I2cSendByte(addr); //发送要读取的地址
I2cStart();
I2cSendByte(0xa1); //发送读器件地址
num=I2cReadByte(); //读取数据
I2cStop();
return num;
}
没有合适的资源?快使用搜索试试~ 我知道了~
51单片机基础例程源码学习
共200个文件
c:33个
uvproj:25个
template:25个
需积分: 0 0 下载量 61 浏览量
2023-07-23
09:27:33
上传
评论
收藏 443KB ZIP 举报
温馨提示
关于51单片机的代码学习
资源推荐
资源详情
资源评论
收起资源包目录
51单片机基础例程源码学习 (200个子文件)
ds1302.__i 55B
XPT2046.__i 37B
template.uvgui.Administrator 169KB
template.uvgui.Administrator 136KB
template.uvgui.Administrator 87KB
template.uvgui.Administrator 86KB
template.uvgui.Administrator 86KB
template.uvgui.Administrator 70KB
template.uvgui.Administrator 69KB
template.uvgui.Administrator 69KB
template.uvgui.Administrator 69KB
template.uvgui_Administrator.bak 136KB
template.uvgui_Administrator.bak 69KB
template.uvgui_Administrator.bak 69KB
template.uvgui_Administrator.bak 69KB
template_uvproj.bak 14KB
template_uvproj.bak 13KB
template_uvproj.bak 13KB
template_uvproj.bak 13KB
template_uvopt.bak 6KB
template_uvopt.bak 5KB
template_uvopt.bak 5KB
template_uvopt.bak 5KB
template_uvopt.bak 5KB
i2c.c 4KB
main.c 4KB
temp.c 4KB
lcd.c 4KB
main.c 3KB
main.c 3KB
ds1302.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
XPT2046.c 2KB
XPT2046.c 2KB
XPT2046.c 2KB
main.c 2KB
main.c 2KB
main.c 1KB
XPT2046.c 1KB
main.c 1KB
main.c 1KB
main.c 1KB
main.c 970B
main.c 753B
template_Target 1.cfg 10B
lcd.h 963B
XPT2046.h 540B
XPT2046.h 540B
XPT2046.h 540B
XPT2046.h 540B
ds1302.h 534B
temp.h 452B
i2c.h 322B
template.hex 5KB
template.hex 2KB
template.hex 2KB
template.hex 1KB
template.hex 1KB
template.hex 1KB
template.hex 1KB
template.hex 1KB
template.hex 958B
template.hex 829B
template.hex 790B
template.hex 622B
template.hex 509B
template.hex 483B
template.hex 286B
template.hex 284B
template.hex 284B
template.hex 283B
template.hex 266B
template.hex 230B
template.hex 191B
template.hex 172B
template.hex 170B
template.hex 139B
template.hex 90B
template.build_log.htm 1KB
template.build_log.htm 1KB
template.build_log.htm 458B
template.build_log.htm 417B
template.build_log.htm 252B
template.build_log.htm 251B
template.build_log.htm 243B
template.lnp 59B
template.lnp 56B
共 200 条
- 1
- 2
资源评论
小黑爱上了小白
- 粉丝: 2
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功