1、51单片机i2c协议示例程序精品文档#include#include#define uint unsigned int#define uchar unsigned charsbit sda=P35;sbit scl=P34;void start_bit();void stop_bit();void slave_ack();void no_ack();void write_byte(uchar dat);uchar read_byte();void write_date(uchar addr,uchar date);uchar read_date(uchar addr);void delay(u
2、int x);void main()uchar num;write_date(0x01,0xaa);delay(2);num=read_date(0x01);P0=num;while(1);void start_bit() /开始位scl=1;_nop_();sda=1;_nop_();sda=0;_nop_();scl=0;void stop_bit()/停止位sda=0;_nop_();scl=1;_nop_();sda=1;void slave_ack()/应答信号_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();_nop_();scl=1;_nop
3、_();_nop_();while(sda);/P0=0xfe;scl=0;void no_ack() /无应答sda=1;_nop_();scl=1;_nop_();scl=0;void write_byte(uchar dat) /写一个字节uchar i;scl=0;for(i=0;i8;i+)if(dat&0x80)sda=1;elsesda=0;scl=1;dat=1;scl=0;uchar read_byte() /读一个字节uchar i,dat=0;sda=1;for(i=0;i8;i+)dat0;i-)for(j=0x40;j0;j-);收集于网络,如有侵权请联系管理员删除