弗原創IT教育中心 ∥送停止位sda=0->1 void i2c stop(void) sC=O nop o sda=0 nop_0? SC=1 nosso sda=1 nosso sc=O ∥应答(包含ack:sda=0和 no ack: sda=0) void i2c ack main (bit ack main sc=O f( ack main)sda=0;∥ack主应答 else sda=1;/ no ack无需应答 nops SCIE nosso sc=O
//送停止位 sda=0->1 void i2c_stop(void){ scl=0; _nop_(); sda=0; _nop_(); scl=1; nops(); sda=1; nops(); scl=0; } //主应答 (包含ack:sda=0 和 no_ack:sda=0) void i2c_ack_main(bit ack_main){ scl=0; if(ack_main)sda=0; //ack主应答 els e sda=1; //no_ack无需应答 nops(); scl=1; nops(); scl=0; }
弗原創IT教育中心 ∥}节发送程序 ∥)送c(可以是数据也可是地址),送完后接收 从应答 ∥不考虑从应答位 void send byte(uchar c) uchar i for(=0;<8;++){ sc|=0: f(c)&0x80)sda=1;∥判断发送位 else sda=0 nop SC=1: nops(; sc|=0 nosso sda=1;∥发送完8bit,释放总线准备接收应答位 op_0; nosso;∥sda上数据即是从应答位 scl=0;∥考虑从应答位但要控制好时序
//字节发送程序 //发送c(可以是数据也可是地址 ),送完后接收 从应答 //不考虑从应答位 void send_byte(uchar c){ uchar i; for(i=0;i<8;i++){ scl=0; if((c<<i) & 0x80)sda=1; //判断发送位 els e sda=0; _nop_(); scl=1; nops(); scl=0; } nops(); sda=1; //发送完8bit,释放总线准备接收应答位 _nop_(); scl=1; nops(); //sda上数据即是从应答位 scl=0; //不考虑从应答位|但要控制好时序 }
弗原創IT教育中心 ∥字节接收程序 ∥接收器件传来的数据,此程序应配合主应答函数 ⅰ2 c ack main0使用 ∥ return: uchar型1字节 uchar read byte(void uchar i: uchar c c=0 sc=0; nop sda=1;∥置数据线为输入方式 for(=0;<8++){ nop scl=0;∥置时钟线为低,准备接收数据位 0 nops Scl=1;∥置时钟线为高,使数据线上数据有效 nop_0, C<<=1: fsda)C+=1;∥读数据位,将接收的数据存c SCE 0; return c
//字节接收程序 //接收器件传来的数据,此程序应配合|主应答函数 |i2c_ack_main()使用 //return: uchar 型 1字节 uchar read_byte(void){ uchar i; uchar c; c=0; scl=0; _nop_(); sda=1; //置数据线为输入方式 for(i=0;i<8;i++){ _nop_(); scl=0; //置时钟线为低,准备接收数据位 nops(); scl=1; //置时钟线为高,使数据线上数据有效 _nop_(); c<<=1; if(sda)c+=1; //读数据位,将接收的数据存c } scl=0; return c; }
弗原創IT教育中心 个大水出∥向无子地址器件发送单字节数据 void send to byte uchar ad main, uchar c) i2c start send_byte( ad main);∥发送器件地址 send byte(C);∥)发送数据c i2c stop 大大★大大★★大★大★大大大大★★大★★大大★大大★大大★★大★大★大大★★大★大★★★大大 ∥向有子地址器件发送多字节数据 void send to byte(uchar ad main, uchar ad sub, uchar buf, uchar num) uchar i i2c start(; send_byte( ad main);∥发送器件地址 send byte(ad sub);∥发送器件子地址 for(=0;<num;++){ send byte("bu;∥发送数据棽buf buf++ i2c stop
//向无子地址器件发送单字节数据 void send_to_byte(uchar ad_main,uchar c){ i2c _start(); send_byte(ad_main); //发送器件地址 send_byte(c); //发送数据c i2c _stop(); } //*************************************************** //向有子地址器件发送多字节数据 void send_to_nbyte(uchar ad_main,uchar ad_sub,uchar *buf,uchar num){ uchar i; i2c _start(); send_byte(ad_main); //发送器件地址 send_byte(ad_sub); //发送器件子地址 for(i=0;i<num;i++){ send_byte(*buf); //发送数据*buf buf++; } i2c _stop(); }
弗原創IT教育中心 ∥从人无子地址器件读单字节数据 function:器件地址,所读数据存在接收缓冲区当前字节 void read from byte uchar ad main, uchar *buf) i2c start send byte( ad main);∥发送器件地址 buf=read byte 0: 2 c ack main(no ack);∥无需应答< no ack=0> i2c stop ∥从人有子地址器件读多个字节数据 l/function void read from mbyte(uchar ad main, uchar ad sub, uchar buf, uchar num) uchar i 12c start send byte ( ad main) send byte (ad sub) for(=0;<num-1++){ buf=read byte o 2 c ack main(ack);∥应答<ack=1> buf++
//从无子地址器件读单字节数据 //function:器件地址,所读数据存在接收缓冲区当前字节 void read_from_byte(uchar ad_main,uchar *buf){ i2c_start(); send_byte(ad_main); //发送器件地址 *buf=read_byte(); i2c_ack_main(no_ack); //无需应答<no_ack=0> i2c_stop(); } //*************************************************** //从有子地址器件读多个字节数据 //function: void read_from_nbyte(uchar ad_main,uchar ad_sub,uchar *buf,uchar num){ uchar i; i2c_start(); send_byte(ad_main); send_byte(ad_sub); for(i=0;i<num-1;i++){ *buf=read_byte(); i2c_ack_main(ack); //主应答<ack=1> buf++; }