www.pudn.com > EEPROM-AT24C02.zip > fg.c, change:2012-11-29,size:4107b


//24C02-多花样流水灯演示程序(C51 ASM)2007-03-02 15:16/*******************************************************************/ 
/* ME300B单片机开发系统演示程序 - 多花样流水灯                                                           
/* LED显示                                             */ 
/* 将演示数据先写入24C02芯片内,再将其数据逐个读出送P0口LED显示。                                
 作者: gguoqing                 时间: 2006/06/20                       
 
/*【版权】Copyright(C)伟纳电子 www.willar.com  All Rights Reserved           */ 
/*【声明】此程序仅用于学习与参考,引用请注明版权和作者信息!                                            
/*******************************************************************/ 
#include <reg51.h> 
#include <intrins.h> 
#define uchar unsigned char 
#define uint unsigned int 
#define OP_WRITE 0xa0// 器件地址以及写入操作 
#define OP_READ  0xa1// 器件地址以及读取操作 
uchar code display[72]= 
{ 
        0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F, 
        0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF, 
        0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00, 
        0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF, 
        0xFC,0xF9,0xF3,0xE7,0xCF,0x9F,0x3F,0x9F, 
		0xCF,0xE7,0xF3,0xF9,0xFC,0xFF,0xE7,0xDB, 
		0xBD,0x7E,0xBD,0xDB,0xE7,0xFF,0xE7,0xC3, 
		0x81,0x00,0x81,0xC3,0xE7,0xFF,0xAA,0x55, 
		0x18,0xFF,0xF0,0x0F,0x00,0xFF,0x00,0xFF,  
}; 
 
sbit SDA = P3^4; 
sbit SCL = P3^3; 
void delayNOP() 
{	 
	int i; 
	for(i=0;i<4;i++) 
		_nop_(); 
} 
/**********************************************************/ 
void delayms(uint ms)  
// 延时子程序 
{ 
   uchar k; 
   while(ms--) 
   { 
      for(k = 0; k < 120; k++); 
   } 
} 
 
/**********************************************************/ 
void start() 
//开始位 
 { 
   SDA = 1; 
   SCL = 1; 
   delayNOP(); 
   SDA = 0; 
   delayNOP(); 
   SCL = 0; 
} 
 
/**********************************************************/ 
void stop() 
// 停止位 
{ 
   SDA = 0; 
   delayNOP(); 
   SCL = 1; 
   delayNOP(); 
   SDA = 1; 
} 
 
/**********************************************************/ 
uchar shin() 
//从AT24C02移出数据到MCU 
{ 
   uchar i,read_data; 
   for(i = 0; i < 8; i++) 
   { 
    SCL = 1; 
    read_data <<= 1; 
    read_data |= SDA; 
    SCL = 0; 
   } 
    return(read_data); 
} 
 
/**********************************************************/ 
bit shout(uchar write_data) 
// 从MCU移出数据到AT24C02  
{ 
   uchar i; 
   bit ack_bit; 
   for(i = 0; i < 8; i++)// 循环移入8个位 
   { 
     SDA = (bit)(write_data & 0x80); 
    _nop_(); 
    SCL = 1; 
    delayNOP(); 
    SCL = 0; 
    write_data <<= 1; 
   } 
   SDA = 1;// 读取应答 
   delayNOP(); 
   SCL = 1; 
   delayNOP(); 
   ack_bit = SDA; 
   SCL = 0; 
   return ack_bit;// 返回AT24C02应答位 
} 
 
/**********************************************************/ 
void write_byte(uchar addr, uchar write_data) 
//在指定地址addr处写入数据write_data 
{ 
   start(); 
   shout(OP_WRITE); 
   shout(addr); 
   shout(write_data); 
   stop(); 
   delayms(10);// 写入周期 
} 
 
/**********************************************************/ 
void fill_byte(uchar fill_size,uchar fill_data) 
// 填充数据fill_data到EEPROM内fill_size字节 
{ 
   uchar i; 
   for(i = 0; i < fill_size; i++) 
   { 
     write_byte(i, fill_data); 
   } 
} 
 
/**********************************************************/ 
uchar read_current() 
// 在当前地址读取 
{ 
   uchar read_data; 
   start(); 
   shout(OP_READ); 
   read_data = shin(); 
   stop(); 
   return read_data; 
} 
 
/**********************************************************/ 
uchar read_random(uchar random_addr) 
// 在指定地址读取 
{ 
   start(); 
   shout(OP_WRITE); 
   shout(random_addr); 
   return(read_current()); 
} 
 
/**********************************************************/ 
 
void main() 
{ 
   uchar i; 
   SDA = 1; 
   SCL = 1; 
   fill_byte(72,0xff);          // 将前72字节填充0xff 
   for(i = 0 ; i < 72; i++)    //写入显示代码到AT24C02 
    { 
      write_byte(i,display[i]); 
    } 
   for(i =0 ;i <72 ; i++)      //从AT24C02移出数据送到P0口显示 
   { 
      P0 = read_random(i); 
      delayms(150); 
   }  
}