通信人家园
标题:
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; //看门狗<br/>sbit I2C_SCK=P1^5; //24cxx的时钟线<br/>sbit I2C_SDA=P1^6; //24CXX的数据线<br/>bit I2C_Start(void);<br/>void I2C_Stop(void);<br/>void I2C_Ack(void);<br/>void I2C_Nack(void);<br/>bit I2C_Send_Byte( uchar);<br/>uchar I2C_Receive_Byte(void);<br/>void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count);<br/>void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count);<br/>void FeedDog(void)<br/>{<br/> WDOG=!WDOG;<br/>}<br/>void Delay_10_uS(void)<br/>{<br/> char i=10;<br/> while(i--);<br/>}<br/>void Delay_N_mS( uint n_milisecond) /* n mS delay */<br/>{<br/> uchar i;<br/> while(n_milisecond--)<br/> {<br/> i=37;<br/> while(i--);<br/> }<br/>}<br/>void I2C_Init(void)<br/>{<br/> I2C_SCK=0;<br/> I2C_SDA=0;<br/>}<br/>bit I2C_Start(void)<br/>{<br/> Delay_10_uS();<br/> I2C_SDA =1;<br/> Delay_10_uS();<br/> I2C_SCK =1;<br/> Delay_10_uS();<br/> if ( I2C_SDA == 0) return 0;<br/> if ( I2C_SCK == 0) return 0;<br/> I2C_SDA = 0;<br/> Delay_10_uS();<br/> I2C_SCK = 0;<br/> Delay_10_uS();<br/> return 1;<br/>}<br/>void I2C_Stop(void)<br/>{<br/> Delay_10_uS();<br/> I2C_SDA = 0;<br/> Delay_10_uS();<br/> I2C_SCK = 1;<br/> Delay_10_uS();<br/> I2C_SDA = 1;<br/> Delay_10_uS();<br/>}<br/>void I2C_Ack(void)<br/>{<br/> Delay_10_uS();<br/> I2C_SDA=0;<br/> Delay_10_uS();<br/> I2C_SCK=1;<br/> Delay_10_uS();<br/> I2C_SCK=0;<br/> Delay_10_uS();<br/>}<br/>void I2C_Nack(void)<br/>{<br/> Delay_10_uS();<br/> I2C_SDA=1;<br/> Delay_10_uS();<br/> I2C_SCK=1;<br/> Delay_10_uS();<br/> I2C_SCK=0;<br/> Delay_10_uS();<br/>}<br/>bit I2C_Send_Byte( uchar d)<br/>{<br/> uchar i = 8;<br/> bit bit_ack;<br/> while( i-- )<br/> {<br/> Delay_10_uS();<br/> if ( d &0x80 ) I2C_SDA =1;<br/> else I2C_SDA =0;<br/> Delay_10_uS();<br/> I2C_SCK = 1;<br/> Delay_10_uS();<br/> I2C_SCK = 0;<br/> d = d << 1;<br/> }<br/> Delay_10_uS();<br/> I2C_SDA = 1;<br/> Delay_10_uS();<br/> I2C_SCK = 1;<br/> Delay_10_uS();<br/> bit_ack = I2C_SDA;<br/> I2C_SCK =0;<br/> Delay_10_uS();<br/> return bit_ack;<br/>}<br/>uchar I2C_Receive_Byte(void)<br/>{<br/> uchar i = 8, d;<br/> Delay_10_uS();<br/> I2C_SDA = 1;<br/> while ( i--)<br/> {<br/> d = d << 1;<br/> Delay_10_uS();<br/> I2C_SCK =1;<br/> if ( I2C_SDA ) d++;<br/> Delay_10_uS();<br/> I2C_SCK =0;<br/> }<br/> return d;<br/>}<br/>void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)<br/>{<br/> FeedDog();<br/> while(count--)<br/> {<br/> I2C_Start();<br/> /*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/ /* 24C16 USE */<br/> I2C_Send_Byte( 0xa0 );<br/> I2C_Send_Byte( AT24C64_address/256 );<br/> I2C_Send_Byte( AT24C64_address %256 );<br/> I2C_Send_Byte( *(uchar*)mcu_address );<br/> I2C_Stop();<br/> Delay_N_mS(10); /* waiting for write cycle to be completed */<br/> ((uchar*)mcu_address)++;<br/> AT24C64_address++;<br/> }<br/>}<br/>void AT24C64_R(void *mcu_address,uint AT24C64_address,uint count)<br/>{<br/> FeedDog();<br/> while(count--)<br/> {<br/> I2C_Start();<br/> /*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/ /* 24C16 USE */<br/> I2C_Send_Byte( 0xa0 );<br/> I2C_Send_Byte( AT24C64_address/256 );<br/> I2C_Send_Byte( AT24C64_address % 256 );<br/> I2C_Start();<br/> /*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/<br/> I2C_Send_Byte( 0xa1 );<br/> *(uchar*)mcu_address = I2C_Receive_Byte();<br/> I2C_Nack();<br/> I2C_Stop();<br/> ((uchar*)mcu_address)++;<br/> AT24C64_address++;<br/> }<br/>}</div>
时间:
2007-12-15 01:01
作者:
f12003ga
看到程序就头痛,看来我不适合做技术
时间:
2007-12-16 15:29
作者:
kuaileguo
誓将C关拿下
通信人家园 (https://www.txrjy.com/)
Powered by C114