• jessxj78
    了解作者
  • C/C++
    开发工具
  • 1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 20
    下载次数
  • 2009-11-25 12:02
    上传日期
51系列iic接口模拟,24c01测试通过
IIC.rar
  • IIC.c
    1.9KB
内容介绍
#include<reg51.h> #define HIGHT 1 #define LOW 0 #define FALSE 0 #define TRUE 1 #define WRITE 0xa0 #define READ 0xa1 typedef unsigned char uchar; sbit SCL=P2^1; sbit SDA=P2^2; void delay1() { ; } void start(void) { SCL = HIGHT; delay1(); SDA = LOW; delay1(); SCL = LOW; delay1(); } void stop(void) { SDA = LOW; delay1(); SCL = HIGHT; delay1(); SDA = HIGHT; delay1(); SCL = LOW; delay1(); } void init24xx(void) { SCL=LOW; stop(); } bit clock(void) { bit sample; SCL=HIGHT; delay1(); sample=SDA; SCL=LOW; delay1(); return(sample); } bit send(uchar I_data) { char i; for(i=0;i<8;i++) { SDA=(bit)(I_data & 0x80); I_data = I_data <<1; clock(); } SDA=HIGHT; return(~clock()); } uchar receive(void) { char I_data = 0; char i; for(i=0;i<8;i++) { I_data *= 2; if (clock()) { I_data++; } } return(I_data); } /* void Ack(void) { SDA=LOW; clock(); SDA=HIGHT; } */ /* bit address(int Address) { uchar hbyte; uchar lbyte; lbyte = Address % 256; hbyte=Address/256; start(); if (send(WRITE | (hbyte<<1))) return(send(lbyte)); else return(FALSE); } */ bit address(unsigned Address) { start(); if(send(WRITE)) return send(Address); else return (FALSE); } void wait_5ms(void) { int i; for (i=0;i<1000;i++) ; } bit write( int Address,uchar buf) { if (address(Address) && send (buf)) { stop(); wait_5ms(); return(TRUE); } else return(FALSE); } uchar read (int Address) { char a; address(Address); start(); send (READ); a=receive(); return(a); }
评论
    相关推荐