lchjczwlchjczw.blog.chinaunix.net
lchjczw
全部博文(1008)
sysfs(0)
procfs(6)
Java(0)
Linux C(1)
C++(0)
C语言(0)
wifi驱动开发(0)
视频驱动开发(0)
音频驱动开发(2)
网络设备驱动开发(1)
驱动调试(0)
驱动基础(4)
sd卡驱动开发(1)
总线驱动(9)
flash驱动开发(0)
USB驱动开发(1)
2012年(1008)
Lanny_li
zsy77
cynthia
Bsolar
浪花小雨
fengchar
yxl15098
wangfeng
CHLRX
728196
Katherin
分类:
2012-08-01 11:31:12
原文地址:I2C C51 作者:luozhiyong131
#include<reg52.h>#include<intrins.h>#define uchar unsigned char #define uint unsigned int#define NOP _nop_()sbit PCF_SCL=P2^0; sbit PCF_SDA=P2^1;delay();RcvStr(uchar suba,num); ISendStr(uchar suba,uchar mdata); ISendbyte(uchar c);void WaitACK();void Ack_I2c(bit a); uchar RcvByte(); void SendByte(uchar c) ;void Stop_I2c(); void Start_I2c();extern uchar rd[6];IRcvStr(uchar suba,num){ uchar j; Start_I2c(); delay(); SendByte(0xa0); delay(); SendByte(suba); delay(); Start_I2c(); delay(); SendByte(0xa1); delay(); for(j=0;j<num;j++) { rd[j]=RcvByte(); delay(); if(j<num-1)Ack_I2c(0); } Ack_I2c(1); delay(); Stop_I2c(); delay(); }ISendStr(uchar suba,uchar mdata) { Start_I2c(); SendByte(0xa0); delay(); SendByte(suba); delay(); SendByte(mdata); delay(); Stop_I2c(); delay();} void Ack_I2c(bit a){ PCF_SDA=a; delay(); PCF_SCL=1; delay(); PCF_SCL=0; delay(); }uchar RcvByte(){ uchar BitCnt,retc; PCF_SDA=1; for(BitCnt=0;BitCnt<8;BitCnt++) { PCF_SCL=1; delay(); retc=retc<<1; retc|=PCF_SDA; PCF_SCL=0; delay(); } delay(); return(retc); delay();}void SendByte(uchar c){ uchar BitCnt; for(BitCnt=0;BitCnt<8;BitCnt++) { if(c&0x80)PCF_SDA=1; else PCF_SDA=0; c=c<<1; PCF_SCL=1; delay(); PCF_SCL=0; delay(); } delay(); WaitACK(); }void WaitACK(){ uchar errtime=20; PCF_SDA=1; delay(); PCF_SCL=1; delay(); while(PCF_SDA) { errtime--; delay(); if(!errtime) Stop_I2c(); } PCF_SCL=0; delay();}void Stop_I2c(){ PCF_SDA=0; delay(); PCF_SCL=1; delay(); PCF_SDA=1; delay(); PCF_SCL=0; }void Start_I2c(){ PCF_SDA=1; delay(); PCF_SCL=1; delay(); PCF_SDA=0; delay(); PCF_SCL=0; NOP;NOP; }delay(){ uchar cc; for(cc=0;cc<10;cc++);}
上一篇:C51 音乐 (八月桂花)
下一篇:LCD+矩阵按键值
登录 注册