>1、24c02电路 2、开始于结束函数 //*****************stop******************** void iic_stop() { scl=0; sda=0; delay4us(); scl=1; delay4us(); sda=1; delay(10); } //*****************start******************* void iic_start() { scl=1; delay4us(); sda=1; delay4us(); sda=0; delay4us(); scl=0; delay4us(); }
3、延时与显示函数
void display(uchar shu,uchar wei) { P2=(P2&0xe3)|(wei<<2); P0=tab[shu]; delay(2); P0=0x00; }
void delay(uint ms) { uint i,j; for(i=ms;i>0;i--) for(j=123;j>0;j--); }
4、从器件应答 //********ack 从器件应答,也可以作为主控器非应答******** bit iic_ack() { bit ack; scl=0; delay4us(); sda=1; delay4us(); scl=1; delay4us(); ack=sda; scl=0; delay4us(); return ack; }
5、主控器件应答 //*****************m_ack 控制器应答****************** void iic_mack() { scl=0; delay4us(); sda=1; delay4us(); sda=0; delay4us(); scl=1; delay4us(); scl=0; delay4us(); sda=1;//释放总线 }
6、读写一个字节
uchar read_byte() { uchar t,temp=0; for(t=0;t<8;t++) { scl=1; delay4us(); temp<<=1; if(sda==1) temp|=1; delay4us(); scl=0; delay4us(); } return temp; }
void write_byte(uchar temp) { uchar t; for(t=0;t<8;t++) { if(temp&0x80) sda=1; else sda=0; scl=1; delay4us(); scl=0; temp<<=1; delay4us(); } }
7、写一个字节到地址add,写多个字节到add
void iic_write_mbyte(uchar add,uchar *pdat,uchar n) { uchar t; iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); for(t=0;t<n;t++) { write_byte(pdat[t]); iic_ack(); } iic_stop(); }
void iic_write_byte(uchar add,uchar dat) { iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); write_byte(dat); iic_ack(); iic_stop(); }
8、直接读一个字节 //*************直接读iic*************** uchar iic_read_byte() { uchar temp; iic_start(); write_byte(0xa1); iic_ack(); temp=read_byte(); iic_ack(); iic_stop(); return temp; }
9、直接读多个字节
void iic_read_mbyte(uchar* pdat,uchar n) { uchar t; iic_start(); write_byte(0xa1); iic_ack(); for(t=0;t<n;t++) { pdat[t]=read_byte(); if(t<n-1) iic_mack(); else iic_ack(); } iic_stop(); }
10、随机读一个字节
//*************随机读*************** uchar iic_readom_read_byte(uchar add) { uchar temp; iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); iic_start(); write_byte(0xa1); iic_ack(); temp=read_byte(); iic_ack(); iic_stop(); return temp; }
11、随机读n个字节
void iic_readom_read_mbyte(uchar add,uchar* pdat,uchar n) { uchar t; iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); iic_start(); write_byte(0xa1); iic_ack(); for(t=0;t<n;t++) { pdat[t]=read_byte(); if(t<(n-1)) iic_mack(); else iic_ack(); } iic_stop(); }
12、完整程序
#include <REGX51.H> #include <intrins.h> #define uchar unsigned char #define uint unsigned int uchar tab[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0x40}; #define delay4us() _nop_();_nop_();_nop_();_nop_() void delay(uint ms); void display(uchar shu,uchar wei); void iic_start(); void iic_stop(); bit iic_ack(); void iic_mack(); void write_byte(uchar temp); uchar read_byte(); void iic_write_byte(uchar add,uchar dat); uchar iic_read_byte(); uchar iic_readom_read_byte(uchar add); void iic_write_mbyte(uchar add,uchar *pdat,uchar n); void iic_readom_read_mbyte(uchar add,uchar* pdat,uchar n); void iic_read_mbyte(uchar* pdat,uchar n); sbit scl=P2^1; sbit sda=P2^0; void main() { uchar a,b; uchar c[8],d[8]; iic_write_mbyte(0,tab,8); iic_write_byte(0,0x75); b=iic_readom_read_byte(0); a=iic_read_byte(); iic_read_mbyte(d,8); iic_readom_read_mbyte(0,c,8); while(1) { display(a/16,7); display(a%16,6); display(c[7]/16,3); display(c[7]%16,2); display(b/16,5); display(b%16,4); display(d[7]/16,1); display(d[7]%16,0); } }
void iic_read_mbyte(uchar* pdat,uchar n) { uchar t; iic_start(); write_byte(0xa1); iic_ack(); for(t=0;t<n;t++) { pdat[t]=read_byte(); if(t<n-1) iic_mack(); else iic_ack(); } iic_stop(); }
void iic_readom_read_mbyte(uchar add,uchar* pdat,uchar n) { uchar t; iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); iic_start(); write_byte(0xa1); iic_ack(); for(t=0;t<n;t++) { pdat[t]=read_byte(); if(t<(n-1)) iic_mack(); else iic_ack(); } iic_stop(); }
uchar iic_readom_read_byte(uchar add) { uchar temp; iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); iic_start(); write_byte(0xa1); iic_ack(); temp=read_byte(); iic_ack(); iic_stop(); return temp; }
uchar iic_read_byte() { uchar temp; iic_start(); write_byte(0xa1); iic_ack(); temp=read_byte(); iic_ack(); iic_stop(); return temp; }
void iic_write_mbyte(uchar add,uchar *pdat,uchar n) { uchar t; iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); for(t=0;t<n;t++) { write_byte(pdat[t]); iic_ack(); } iic_stop(); }
void iic_write_byte(uchar add,uchar dat) { iic_start(); write_byte(0xa0); iic_ack(); write_byte(add); iic_ack(); write_byte(dat); iic_ack(); iic_stop(); }
uchar read_byte() { uchar t,temp=0; for(t=0;t<8;t++) { scl=1; delay4us(); temp<<=1; if(sda==1) temp|=1; delay4us(); scl=0; delay4us(); } return temp; }
void write_byte(uchar temp) { uchar t; for(t=0;t<8;t++) { if(temp&0x80) sda=1; else sda=0; scl=1; delay4us(); scl=0; temp<<=1; delay4us(); } }
void iic_mack() { scl=0; delay4us(); sda=1; delay4us(); sda=0; delay4us(); scl=1; delay4us(); scl=0; delay4us(); sda=1; }
bit iic_ack() { bit ack; scl=0; delay4us(); sda=1; delay4us(); scl=1; delay4us(); ack=sda; scl=0; delay4us(); return ack; }
void iic_stop() { scl=0; sda=0; delay4us(); scl=1; delay4us(); sda=1; delay(10); }
void iic_start() { scl=1; delay4us(); sda=1; delay4us(); sda=0; delay4us(); scl=0; delay4us(); }
void display(uchar shu,uchar wei) { P2=(P2&0xe3)|(wei<<2); P0=tab[shu]; delay(2); P0=0x00; }
void delay(uint ms) { uint i,j; for(i=ms;i>0;i--) for(j=123;j>0;j--); }
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |