新聞中心

IIC總線通信模擬

作者: 時(shí)間:2012-08-13 來(lái)源:網(wǎng)絡(luò) 收藏
作為一種通信規(guī)范,廣泛應(yīng)用于各種芯片之間以及。可以毫不夸張的講,不懂的就不算是學(xué)過電子!
    /**************************************************************************/#include"reg51.h"#include "intrins.h"/**************************端口定義************************************/sbit SDA=P1^0;             //IIC數(shù)據(jù)總線sbit SCL=P1^1;             //IIC時(shí)鐘總線/**************************數(shù)組定義************************************/unsigned char wdata[8]={0x32,0xb5,0xab,0xd0,0xd6,0x50,0x3a,0xd3};unsigned char rdata[8];  /**************************函數(shù)聲明************************************/void iic_start(void);void iic_stop(void);bit iic_write_byte(unsigned char wdata);unsigned char iic_read_byte(bit re_data);bit iic_read_some_bytes(unsigned reg_add,unsigned char *rdata,unsigned char len);bit iic_write_some_bytes(unsigned reg_add,unsigned char *wdata,unsigned char len);   /**************************主函數(shù)**************************************/void main(void){..................}/*************************IIC函數(shù)**************************************/void iic_start(void)                            {SDA=1;_nop_();SCL=1;_nop_();_nop_();SDA=0;_nop_();_nop_();SCL=0;_nop_();}//void iic_stop(void)                                    {SDA=0;_nop_();_nop_();SCL=1;_nop_();_nop_();SDA=1;_nop_();_nop_();_nop_();_nop_();}//_______________________________________________________________________bit iic_write_byte( unsigned char wdata )            {unsigned char i=0;for ( i =0 ;i  8 ;i ++ ){if ( wdata  0x80 ){SDA=1;_nop_();}else{SDA=0;_nop_();}wdata = 1;SCL=1;   _nop_();SCL=0;_nop_();}_nop_();SDA=1;_nop_();SCL=1;_nop_();if(!SDA==1){    SCL=0;_nop_();return 1;         }else             {SCL=0;_nop_();return 0;}}//_______________________________________________________________________// iic_read_byte re_data -- whether send ack bitunsigned char iic_read_byte(bit re_data)    {unsigned char rdata=0,i=0;SDA=1;for(i=0;i8;i++){rdata =1;SCL=0;_nop_();SCL=1;_nop_();if(SDA==1){rdata++;}_nop_();}_nop_();SCL=0;_nop_();if(re_data){SDA=1;_nop_();}else{SDA=0;_nop_();}_nop_();SCL=1;_nop_();SCL=0;_nop_();return(rdata);}//bit iic_write_some_bytes(unsigned reg_add,unsigned char *wdata,unsigned char len) {unsigned i;iic_start();if(!iic_write_byte(0xA0)){iic_stop();_nop_();return 0;}if(!iic_write_byte(reg_add)){iic_stop();_nop_();return 0;}for(i=0;ilen;i++){if(!iic_write_byte(wdata[i])){iic_stop();_nop_();return 0;}}iic_stop();_nop_();return 1;}//bit iic_read_some_bytes(unsigned reg_add,unsigned char *rdata,unsigned char len)   {unsigned char i;iic_start();if(!iic_write_byte(0xA0)){iic_stop();_nop_();return 0;}if(!iic_write_byte(reg_add)){iic_stop();return 0;}iic_stop();iic_start();if(!iic_write_byte(0xA0|1)){iic_stop();return 0;}for(i=0;ilen-1;i++){rdata[i]=iic_read_byte(0);_nop_();}rdata[i]=iic_read_byte(1);iic_stop();return 1;} 
				
            
                
			
							
上一頁(yè) 1 2 下一頁(yè)

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉