📄 iic.c
字号:
/*
本项目专门为本站的51 PIC AVR 多功能学习开发板而写,仅供参考,请勿做商业用途,欢迎转载但要保留本站信息
志博电子专注于8位单片机开发板设计和教学
让人耳目一心的经济又豪华型开发板
性价比最高
仅售378!
欢迎访问我们的网站 www.elecmcu.com
*/
#define IIC_READ 0XA1 //定义读指令
#define IIC_WRITE 0XA0 //定义写指令
#define NOP _nop_()
#define uchar unsigned char
#define uint unsigned int
#include "reg52.h"
#define IIC_READ 0XA1 //定义读指令
#define IIC_WRITE 0XA0 //定义写指令
#define NOP _nop_()
uchar readbyteseq(uint add,uchar *ptr,uint readlen);
uchar writebyteseq(uint add,uchar *ptr,uint writelen);
void delay10ms(void);
//#define SDA INT0
//#define SCL T0
//==============================
uchar xdata buff[2000]; //save the byte read out form iic device in test operation
uchar xdata readbuff[232];//测试数组,暂存写入读出数据
//uchar writebuff[32];
//===============================
sbit SDA=P3^5;//P2^7; //THE SDA BIT IS PORT 2 BIT 7
sbit SCL=P3^4;//P2^6; //THE SCL BIT IS PORT 2 BIT 6
sbit SEN=P3^3;//P2^6; //THE SCL BIT IS PORT 2 BIT 6
sbit STE=P3^2;//P2^6; //THE SCL BIT IS PORT 2 BIT 6
uchar bdata bbyte;//定义位操作用数组,采用此方法可提高位操作速度
sbit a0=bbyte^0;
sbit a1=bbyte^1;
sbit a2=bbyte^2;
sbit a3=bbyte^3;
sbit a4=bbyte^4;
sbit a5=bbyte^5;
sbit a6=bbyte^6;
sbit a7=bbyte^7;
//========================================
bit IFACK; //record the SDA state to confirn if ACK has happened
bit NO_ACK; //no ack flag
bit BUS_FAULT; //bus fault flag
//========================================
void start(void)
{
SCL=0;
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
//=======================================
//FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL
void stop(void)
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO PROVIDE ACK SINGAL
void ack(void)
{
SCL=0;
SDA=0;
SCL=1;
SCL=0;
}
//=====================================
//FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL
// OR TO PROVIDE A NO_ACK SIGNAL
//type=1等待应答信号
//type=0 产生无应答信号
void nack(uchar type)
{
SCL=0;
SDA=1;
SCL=1;
IFACK=SDA;
SCL=0;
if(type)
{
if(IFACK)//如果无应答信号,则置标志位NO_ACK,程序中止
{
NO_ACK=1;//用户可以加入自己的异常处理程序
//while(1);
}
else NO_ACK=0;
}
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE TO THE MASTER
uchar inbyte(void) {//从IIC器件中读出数据
SDA=1;
SCL=1;
a7=SDA;
SCL=0;
SCL=1;
a6=SDA;
SCL=0;
SCL=1;
a5=SDA;
SCL=0;
SCL=1;
a4=SDA;
SCL=0;
SCL=1;
a3=SDA;
SCL=0;
SCL=1;
a2=SDA;
SCL=0;
SCL=1;
a1=SDA;
SCL=0;
SCL=1;
a0=SDA;
SCL=0;
return(bbyte);
}
//=======================================================
//FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
void outbyte(uchar outdata) {//将数据写放IIC器件
bbyte=outdata;
SDA=a7;
SCL=1;
SCL=0;
SDA=a6;
SCL=1;
SCL=0;
SDA=a5;
SCL=1;
SCL=0;
SDA=a4;
SCL=1;
SCL=0;
SDA=a3;
SCL=1;
SCL=0;
SDA=a2;
SCL=1;
SCL=0;
SDA=a1;
SCL=1;
SCL=0;
SDA=a0;
SCL=1;
SCL=0;
}
/*
//======================================================
//FUNCTION:BYTE WRITE. 'add' THE WRITE ADDRESS, 'wbyte' THE DATA WANT TO WRITE
void writebyte(uint add,uchar wbyte)//add为写入地址,wbyte为写入数据
{
uchar temph,templ;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
if(!STE)
return;
SEN=0;
if(!STE)
{
SEN=1;
SCL=1;
SDA=1;
return;
}
start(); //开始信号
outbyte(IIC_WRITE|(temph<<1)); //写命令
nack(1); //等待应答
outbyte(templ); //写地址
nack(1);
outbyte(wbyte); //写数据
nack(1); //等待应答
stop(); //停止信号
SCL=1;
SDA=1;
SEN=1;
}
//=====================================================
//FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
uchar readbyte (uint add)//add为读地址
{
uchar temp,temph,templ;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
if(!STE)
return 0XFF;
SEN=0;
if(!STE)
{
SCL=1;
SDA=1;
SEN=1;
return 0XFF;
}
start(); //开始信号
outbyte(IIC_WRITE|(temph<<1)); //写命令
nack(1); //等待应答
outbyte(templ); //写地址
nack(1);
start(); //开始信号
outbyte(IIC_READ|(temph<<1)); //读命令
nack(1); //等待应答
temp=inbyte(); //读数据
nack(0); //无应答
stop(); //停止信号
SEN=1;
SCL=1;
SDA=1;
return(temp);
}
*/
//=================================================
//连写函数
//add为读起始地址,ptr数据保存指针,writelen为写入数据长度
uchar writebyteseq(uint add,uchar *ptr,uint writelen)
{
//uchar temp;
data uchar temph,templ;
data uint i;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
if(!STE)
return 0XFF;
SEN=0;
if(!STE)
{
SCL=1;
SDA=1;
SEN=1;
return 0XFF;
}
start();
outbyte(IIC_WRITE|(temph<<1));
nack(1);
outbyte(templ);
nack(1);
for(i=0;i<writelen;i++)
{
outbyte(*(ptr+i));
SCL=0;
SDA=1;
SCL=1;
SCL=0;
//nack(1);
}
stop();
SCL=1;
SDA=1;
SEN=1;
return 0;
}
//=================================================
//连读函数
//add为读起始地址,ptr数据保存指针,writelen为读出数据长度
uchar readbyteseq(uint add,uchar *ptr,uint readlen)
{
xdata uchar temph,templ;
uint i;
temph=(uchar)(add/256);
templ=(uchar)(add%256);
if(!STE)
{
SCL=1;
SDA=1;
SEN=1;
return 0XFF;
}
SEN=0;
if(!STE)
{
SCL=1;
SDA=1;
SEN=1;
return 0XFF;
}
start();
outbyte(IIC_WRITE|(temph<<1));
nack(1);
outbyte(templ);
nack(1);
start();
outbyte(IIC_READ|(temph<<1));
nack(1);
for(i=0;i<readlen-1;i++)
{
*(ptr+i)=inbyte();
ack();
}
*(ptr+readlen-1)=inbyte();
nack(0);
stop();
SCL=1;
SDA=1;
SEN=1;
return 0;
}
void delay10ms(void)
{
xdata int i;
for(i=0;i<1000;i++)
;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -