资源描述:
《I2C总线读写程序通用.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、//==========================头文件加载===============================#include//加载52系列单片机头文件//===========================端口声明================================sbitCLK=P3^6;//74hc574时钟信号线sbitG=P2^4;//74hc574使能sbitIIC_SDA=P2^6;//声明IIC总线的数据线接在单片机的P2.5端口。sbitIIC_SCL=P2^5;//声明IIC总线的时钟线接在单片机的
2、P2.7端口。unsignedchartabl[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x0BF,0x8C};//0,1,2,3,4,5,6,7,8,9,-,P//===========================函数声明================================voiddisplay(unsignedcharaa);voiddelay(unsignedintt);voiddelay_IIC(void);voidIIC_Init(void);voidIIC_start(void
3、);voidIIC_stop(void);bitIIC_Tack(void);voidIIC_single_byte_write(unsignedcharDaddr,unsignedcharWaddr,unsignedcharData);unsignedcharIIC_single_byte_read(unsignedcharDaddr,unsignedcharWaddr);voidIIC_write_byte(unsignedcharData);unsignedcharIIC_read_byte(void);//============================
4、主函数=================================voidmain()//主函数{unsignedcharData=2,addr=0x01;//---------------------------系统初始化--------------------------IIC_Init();//初始化IIC总线。//P1=0x7f;//LED8先亮。while(1)//死循环{IIC_single_byte_write(0xa0,0x02,Data);//保存LED的状态delay(50000);//延时约0.5SData=IIC_single_byte_r
5、ead(0xa0,0x02);//读出LED的状态if(Data<10)Data++;elseData=0;display(Data);delay(50000);//延时约0.5S}}//==========================功能子函数===============================voiddelay(unsignedintt){//延时函数while(t){t--;};//延时循环计数}//-------------------------------------------------------------------voiddelay
6、_IIC(void){//IIC总线限速延时函数。//该函数是空函数,延时4个机器周期。;;}//-------------------------------------------------------------------voidIIC_Init(void){//IIC总线初始化函数IIC_SDA=1;//释放IIC总线的数据线。IIC_SCL=1;//释放IIC总线的时钟线。}//-------------------------------------------------------------------voidIIC_start(void){//I
7、IC总线产生起始信号函数IIC_SDA=1;//拉高数据线IIC_SCL=1;//拉高时钟线delay_IIC();IIC_SDA=0;//在时钟线为高电平时,拉低数据线,产生起始信号。delay_IIC();IIC_SCL=0;//拉低时钟线}//-------------------------------------------------------------------voidIIC_stop(void){//IIC总线产生停止信号函数IIC_SDA=0;//拉低数据线delay_IIC();IIC_SCL=1;//拉高时钟线