通信人家园

标题: 24C64 E2ROM的读写C程序  [查看完整版帖子] [打印本页]

时间:  2007-12-14 22:36
作者: 缘份     标题: 24C64 E2ROM的读写C程序

<span class="bold">24C64 E2ROM的读写C程序</span><br/><br/><div style="FONT-SIZE: 12px;">#include <br/>#define uchar unsigned char<br/>#define uint unsigned int<br/>#define ulong unsigned long<br/>sbit WDOG=P3^4;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;//看门狗<br/>sbit I2C_SCK=P1^5;&nbsp; &nbsp;&nbsp;&nbsp;//24cxx的时钟线<br/>sbit I2C_SDA=P1^6;&nbsp; &nbsp;&nbsp;&nbsp;//24CXX的数据线<br/>bit&nbsp; &nbsp;I2C_Start(void);<br/>void&nbsp;&nbsp;I2C_Stop(void);<br/>void&nbsp;&nbsp;I2C_Ack(void);<br/>void&nbsp;&nbsp;I2C_Nack(void);<br/>bit&nbsp; &nbsp;I2C_Send_Byte( uchar);<br/>uchar I2C_Receive_Byte(void);<br/>void&nbsp;&nbsp;AT24C64_R(void *mcu_address,uint AT24C64_address,uint count);<br/>void&nbsp;&nbsp;AT24C64_W(void *mcu_address,uint AT24C64_address,uint count);<br/>void FeedDog(void)<br/>{<br/>&nbsp; &nbsp;&nbsp;&nbsp;WDOG=!WDOG;<br/>}<br/>void&nbsp;&nbsp;Delay_10_uS(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;char i=10;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while(i--);<br/>}<br/>void Delay_N_mS( uint n_milisecond)&nbsp;&nbsp;/* n mS delay */<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;uchar i;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while(n_milisecond--)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; i=37;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; while(i--);<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br/>}<br/>void I2C_Init(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK=0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA=0;<br/>}<br/>bit I2C_Start(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA =1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK =1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if ( I2C_SDA == 0) return 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if ( I2C_SCK == 0) return 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA = 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK = 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return 1;<br/>}<br/>void&nbsp;&nbsp;I2C_Stop(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA = 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK = 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA = 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>}<br/>void I2C_Ack(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA=0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK=1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK=0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>}<br/>void I2C_Nack(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA=1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK=1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK=0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>}<br/>bit I2C_Send_Byte( uchar d)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;uchar i = 8;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;bit bit_ack;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while( i-- )<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if ( d &amp;0x80 )&nbsp; &nbsp;I2C_SDA =1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; else&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_SDA =0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_SCK = 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_SCK = 0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; d = d &lt;&lt; 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA = 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK = 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;bit_ack = I2C_SDA;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SCK =0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return bit_ack;<br/>}<br/>uchar I2C_Receive_Byte(void)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;uchar i = 8, d;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;I2C_SDA = 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while ( i--)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; d = d &lt;&lt; 1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_SCK =1;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if ( I2C_SDA ) d++;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Delay_10_uS();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_SCK =0;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;return d;<br/>}<br/>void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;FeedDog();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while(count--)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Start();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/&nbsp;&nbsp;/* 24C16&nbsp;&nbsp;USE */<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( 0xa0 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte(&nbsp;&nbsp;AT24C64_address/256 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( AT24C64_address %256 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( *(uchar*)mcu_address );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Stop();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; Delay_N_mS(10);&nbsp; &nbsp;&nbsp; &nbsp; /* waiting for write cycle to be completed */<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; ((uchar*)mcu_address)++;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; AT24C64_address++;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}<br/>}<br/>void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)<br/>{<br/>&nbsp; &nbsp;&nbsp; &nbsp; FeedDog();<br/>&nbsp; &nbsp;&nbsp; &nbsp; while(count--)<br/>&nbsp; &nbsp;&nbsp; &nbsp; {<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Start();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/&nbsp; &nbsp;/* 24C16 USE */<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( 0xa0 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( AT24C64_address/256 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( AT24C64_address % 256 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Start();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; /*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Send_Byte( 0xa1 );<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; *(uchar*)mcu_address = I2C_Receive_Byte();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Nack();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; I2C_Stop();<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; ((uchar*)mcu_address)++;<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; AT24C64_address++;<br/>&nbsp; &nbsp;&nbsp; &nbsp; }<br/>}</div>
时间:  2007-12-15 01:01
作者: f12003ga

看到程序就头痛,看来我不适合做技术
时间:  2007-12-16 15:29
作者: kuaileguo

誓将C关拿下




通信人家园 (https://www.txrjy.com/) Powered by C114