电子制作天地网站欢迎您的光临,欢迎提出宝贵意见或建议。本站信息产业部备案: 粤ICP备05114582号
单片机模拟I2C总线及24C02读写实例(c源代码)
24C02 read / write process

时间:2010-1-25 21:18:42 来源:互联网 作者:不详 编辑: 【关闭


单片机模拟I2C总线及24C02读写实例(c源代码),24C02 read / write process

关键字:单片机模拟I2C总线及24C02读写实例(c源代码)


      void Write_One_Byte(char addr,char thedata)
      {
                bit acktemp=1;
                /*write a byte to mem*/
                I2C_Start();
                WriteI2CByte(0xa0);
                acktemp=Check_Acknowledge();
                WriteI2CByte(addr);/*address*/
                acktemp=Check_Acknowledge();
                WriteI2CByte(thedata);/*thedata*/
                acktemp=Check_Acknowledge();
                I2C_Stop();
      }
      void Write_A_Page(char *buffer,char addr)
      {
                bit acktemp=1;
                bit wrtmp;
                int i;
                /*write a page to at24c02*/
                I2C_Start();
                WriteI2CByte(0xa0);
                acktemp=Check_Acknowledge();
                WriteI2CByte(addr);/*address*/
                acktemp=Check_Acknowledge();
                for(i=0;i<7;i++)
                {
                  WriteI2CByte(buffer[i]);
                  if(!Check_Acknowledge())
                      {
                          I2C_Stop();
                      }
                 }

                I2C_Stop();
      
      }
      char Read_One_Byte(char addr)
      {         bit acktemp=1;
                char mydata;
                 /*read a byte from mem*/
                I2C_Start();
                WriteI2CByte(0xa0);
                acktemp=Check_Acknowledge();
                WriteI2CByte(addr);/*address*/
                acktemp=Check_Acknowledge();
                I2C_Start();
                WriteI2CByte(0xa1);
                acktemp=Check_Acknowledge();
                mydata=ReadI2CByte();
                acktemp=Check_Acknowledge();
                return mydata;
                I2C_Stop();
      }
      void Read_N_Bytes(char *buffer,char n,char addr)
      {
                bit acktemp=1;
                int i=0;
                 /*read 8 bytes from mem*/
                I2C_Start();
                WriteI2CByte(0xa0);
                acktemp=Check_Acknowledge();
                WriteI2CByte(addr);/*address*/
                acktemp=Check_Acknowledge();
                I2C_Start();
                WriteI2CByte(0xa1);
                acktemp=Check_Acknowledge();
      
                for(i=0;i                {
                      buffer[i]=ReadI2CByte();
                      if(i!=n-1)
                          SEND_0();    /*发送应答*/
                      else
                          SEND_1();    /*发送非应答*/
                }
         I2C_Stop();
      }
      void main()
      {
                int i;
                char mybyte;
                char myarray[8];
                char myarray2[8];
                char rdarray[16];
               
                for(i=0;i<8;i++)
                {
                      myarray[i]=i;
                      myarray2[i]=i+0x08;    
                }
                Write_One_Byte(0x20,0x28);
                Write_A_Page(myarray,0x10);
                Write_A_Page(myarray2,0x18);
                mybyte=Read_One_Byte(0x20);
                Read_N_Bytes(rdarray,16,0x10);
      }
 


◆相关文章◆
◆ 编辑推荐 ◆
用户:
更多评论
您要为您所发的言论的后果负责,故请各位遵纪守法并注意语言文明。60个字以内
留言:
验证码:

免责声明:
●本站所有内容均来自网络,以学习为目的,如果侵犯了您的利益,请来信告知,立即删除,站长邮件为"diy+dzdiy.com",请将"+"变更为"@"
●电子制作存在风险,请注意人身安全,如果您在根据本站电路进行制作过程中发生伤害,本站不负任何责任。

版权所有:www.dzdiy.com ? 2001-2019 本站信息产业部备案: 粤ICP备05114582号