新闻  |   论坛  |   博客  |   在线研讨会
c51写的24c02的读写程序
yanqin | 2009-04-16 15:35:18    阅读:1832   发布文章

#include <reg51.h>
%A
%A // 对24C02的读、写
%A // extern void DelayMs(unsigned int);
%A // extern void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
%A // extern void Write24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
%A
%A /***************************************************************************/
%A #define WriteDeviceAddress 0xa0
%A #define ReadDviceAddress 0xa1
%A
%A /***************************************************************************/
%A sbit SCL=P1^4;
%A sbit SDA=P1^3;
%A sbit DOG=P3^7;
%A
%A /***************************************************************************/
%A void DelayMs(unsigned int number) {
%A unsigned char temp;
%A for(;number!=0;number--,DOG=!DOG) {
%A   for(temp=112;temp!=0;temp--) {
%A   }
%A }
%A }
%A
%A /***************************************************************************/
%A void Start() {
%A SDA=1;
%A SCL=1;
%A SDA=0;
%A SCL=0;
%A }
%A
%A /***************************************************************************/
%A void Stop() {
%A SCL=0;
%A SDA=0;
%A SCL=1;
%A SDA=1;
%A }
%A
%A /***************************************************************************/
%A void Ack() {
%A SDA=0;
%A SCL=1;
%A SCL=0;
%A SDA=1;
%A }
%A
%A /***************************************************************************/
%A void NoAck() {
%A SDA=1;
%A SCL=1;
%A SCL=0;
%A }
%A
%A /***************************************************************************/
%A bit TestAck() {
%A bit ErrorBit;
%A SDA=1;
%A SCL=1;
%A ErrorBit=SDA;
%A SCL=0;
%A return(ErrorBit);
%A }
%A
%A /***************************************************************************/
%A bit Write8Bit(unsigned char input) {
%A unsigned char temp;
%A for(temp=8;temp!=0;temp--) {
%A   SDA=(bit)(input&0x80);
%A   SCL=1;
%A   SCL=0;
%A   input=input<<1;
%A }
%A }
%A
%A /***************************************************************************/
%A void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) {
%A Start();
%A Write8Bit(WriteDeviceAddress);
%A TestAck();
%A Write8Bit(RomAddress);
%A TestAck();
%A for(;number!=0;number--) {
%A   Write8Bit(*Wdata);
%A   TestAck();
%A   Wdata++;
%A }
%A Stop();
%A DelayMs(10);
%A }
%A
%A /***************************************************************************/
%A unsigned char Read8Bit() {
%A unsigned char temp,rbyte=0;
%A for(temp=8;temp!=0;temp--) {
%A   SCL=1;
%A   rbyte=rbyte<<1;
%A   rbyte=rbyte|((unsigned char)(SDA));
%A   SCL=0;
%A }
%A return(rbyte);
%A }
%A
%A /***************************************************************************/
%A void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) {
%A unsigned char temp,rbyte;
%A Start();
%A Write8Bit(WriteDeviceAddress);
%A TestAck();
%A Write8Bit(RomAddress);
%A TestAck();
%A Start();
%A Write8Bit(ReadDviceAddress);
%A TestAck();
%A while(bytes!=1) {
%A *RamAddress=Read8Bit();
%A Ack();
%A RamAddress++;
%A bytes--;
%A }
%A *RamAddress=Read8Bit();
%A NoAck();
%A Stop();
%A }
%A
%A /***************************************************************************/
%A
%A%A
%A

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客