- #ifndef __IIC_H__
- #define __IIC_H__
- #include "reg51.h"
- #define u8 unsigned char
- #define u16 unsigned int
-
- extern void IIC_Delay(u16 x);
- extern void IIC_Start();
- extern void IIC_Stop ();
- extern void IIC_SendByte(u8 dat);
- extern u8 IIC_WaitACK ();
- extern u8 IIC_ReciveByte();
- extern void IIC_SendACK();
- extern void IIC_SendNoACK ();
- #endif
- #include "IIC.h"
- sbit IIC_SCL = P1^4;
- sbit IIC_SDA = P1^3;
- /**
- * 1010 000 1 -->0xA1 read
- * 1010 000 0 -->0xA0 write
- */
- void IIC_Delay(u16 x) {
- while(x--);
- }
-
- void IIC_Start() {//产生IIC启动时序
- IIC_SCL = 1;
- IIC_SDA = 1;
- IIC_Delay(4);
- IIC_SDA = 0;
- IIC_Delay(4);
- IIC_SCL = 0;
- }
-
- void IIC_Stop () {//产生IIC停止时序
- IIC_SCL = 0;
- IIC_Delay(4);
- IIC_SDA = 0;
- IIC_Delay(4);
- IIC_SCL = 1;
- IIC_Delay(4);
- IIC_SDA = 1;
- }
-
- void IIC_SendByte(u8 dat) {
- u8 i;
- for(i=0;i<8;i++) {
- IIC_SCL = 0;
- if(dat>>7) {
- IIC_SDA = 1;
- } else {
- IIC_SDA = 0;
- }
- IIC_SCL = 1;
- dat<<=1;
- }
- }
-
- u8 IIC_WaitACK () {
- u16 t=0;
- IIC_SCL = 0;
- IIC_SDA = 1;
- IIC_SCL = 1;
-
- while(IIC_SDA) { //等待从机拉低SDA
- if(t++>=10000) {
- return 1;//等待应答失败
- }
- }
- IIC_SCL = 0;
- return 0;//等待应该成功
- }
-
- u8 IIC_ReciveByte() {
- u8 d,i;
- for(i=0;i<8;i++) {
- d<<=1;
- IIC_SCL = 1;
- if(IIC_SDA) {
- d++;//d|=0x01;
- }
- IIC_SCL = 0;
- }
- return d;
- }
-
- void IIC_SendACK () { //主机发送收到数据确认应答
- IIC_SCL = 0;
- IIC_SDA = 0;
- IIC_SDA = 1;
- IIC_SCL = 1;
- }
-
- void IIC_SendNoACK () { //主机发送收到数据确认非应答
- IIC_SCL = 0;
- IIC_SDA = 0;
- IIC_SDA = 1;
- }
- #ifndef __24C02_H__
- #define __24C02_H__
- #include "iic.h"
- extern void Write_24c02(u8 add, u8 dat);
- extern u8 Read_24c02(u8 add);
- extern void Save_u16(u8 add, u16 n);
- extern u16 Read_u16(add);
- extern void Save_Float(u8 add, float n);
- extern float Read_Float(add);
- #endif
- #include "24c02.h"
- typedef union {
- float t;
- u8 s[4];
- } fltcom; //定义一个共用体
-
- void Write_24c02(u8 add, u8 dat) {//将dat 写入到24c02的add地址
- u8 st;
- IIC_Start();
- IIC_SendByte(0xA0);//1010 000 0
- st = IIC_WaitACK();
-
- if(st != 0) {//等待应答失败,故障提示
- return;
- }
-
- IIC_SendByte(add);
- IIC_WaitACK();
-
- IIC_SendByte(dat);
- IIC_WaitACK();
- IIC_Stop();
- //IIC_Delay(20000); //10ms 需要等10ms
- do {
- IIC_Start();
- IIC_SendByte(0xA0);
- st = IIC_WaitACK();
- } while(st == 1);
- }
- u8 Read_24c02(u8 add) { //读取24c02的数据
- u8 dat, st;
- IIC_Start();
- IIC_SendByte(0xA0);
- st = IIC_WaitACK();
- if(st != 0) {//错误提示
- }
- IIC_SendByte(add);
- IIC_WaitACK();
-
- IIC_Start();
- IIC_SendByte(0xA1);
- IIC_WaitACK();
- dat = IIC_ReciveByte();
- IIC_SendNoACK();
- IIC_Stop();
- return dat;
- }
-
- void Save_u16(u8 add, u16 n) {
- Write_24c02(add, n/256);
- Write_24c02(add+1, n%256);
- }
-
- u16 Read_u16(add) {
- u16 d;
- d = Read_24c02(add);
- d <<=8;
- d |= Read_24c02(add+1);
- return d;
- }
-
-
- void Save_Float(u8 add, float n) {
- fltcom a;
- a.t = n; //将数据写入
- Write_24c02(add++, a.s[0]);// 每字节存储写入的数据
- Write_24c02(add++, a.s[1]);
- Write_24c02(add++, a.s[2]);
- Write_24c02(add, a.s[3]);
- }
-
- float Read_Float(add) {
- fltcom a;
- //将字节的数据读取到共用体数组中
- a.s[0] = Read_u16(add++);
- a.s[1] = Read_u16(add++);
- a.s[2] = Read_u16(add++);
- a.s[3] = Read_u16(add);
- return a.t;//返回共用体float t;
- }