機電之家資源網(wǎng)
單片機首頁|單片機基礎(chǔ)|單片機應(yīng)用|單片機開發(fā)|單片機文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
單片機24C02 EEPROM存儲器的C語言程序
單片機24C02 EEPROM存儲器的C語言程序
 更新時間:2010-1-26 16:37:19  點擊數(shù):0
【字體: 字體顏色
#include <reg51.h> 
#ifndef  false 
  #define  false 0 
#endif 
#ifndef  true 
  #define true 1 
#endif 
#define WriteDeviceAddress 0xa0 
#define ReadDviceAddress 0xa1  
sbit    SDA       =    P3 ^ 5; //根據(jù)實際連接的管腳定義 
sbit    SCL       =    P3 ^ 4; 
sbit    led  = P1^0; 
sbit    led2 = P1^1; 
unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80, 
                            0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00, 
                            0x10,0x89};  
unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF}; 
unsigned char Disbuf[4]; 
unsigned char code write_data[5] = { 5,2,5,1,8 }; 
unsigned char read_data[5]; 
//-------------------------------------------------------------------------- 
void Delayus(unsigned int number)  

 for(;number!=0;number--) 
 { 
 } 

//-------------------------------------------------------------------------- 
void DelayMs(unsigned int number)  

 unsigned char temp; 
 for(;number!=0;number--) 
 { 
  for(temp=112;temp!=0;temp--) 
  { 
  } 
 } 

//-------------------------------------------------------------------------- 
void Start()     

 SDA=1; 
 Delayus(4); 
 SCL=1; 
 Delayus(4); 
 SDA=0; 
 Delayus(4); 
 SCL=0; 
 Delayus(4); 

//-------------------------------------------------------------------------- 
void Stop()     

 SCL=0; 
 Delayus(4); 
 SDA=0; 
 Delayus(4); 
 SCL=1; 
 Delayus(4); 
 SDA=1; 
 Delayus(4); 

//-------------------------------------------------------------------------- 
void Ack()    

 SDA=0; 
 Delayus(4); 
 SCL=1; 
 Delayus(4); 
 SCL=0; 
 Delayus(4); 
 SDA=1; 
 Delayus(4); 

//-------------------------------------------------------------------------- 
void NoAck()   

 SDA=1; 
 Delayus(4); 
 SCL=1; 
 Delayus(4); 
 SCL=0; 
 Delayus(4); 
 SDA=0; // 

//-------------------------------------------------------------------------- 
bit TestAck()      

 bit ErrorBit; 
 SDA=1; 
 Delayus(4); 
 SCL=1; 
 Delayus(4); 
 ErrorBit=SDA; 
 Delayus(4); 
 SCL=0; 
 return(ErrorBit); 

//-------------------------------------------------------------------------- 
void Write8Bit(unsigned char input)  

 unsigned char temp; 
 for(temp=8;temp!=0;temp--) 
 { 
  SDA=(bit)(input&0x80); 
  Delayus(4); 
  SCL=1; 
  Delayus(4); 
  SCL=0; 
  Delayus(4); 
  input=input<<1; 
 } 

//-------------------------------------------------------------------------- 
unsigned char Read8Bit()     

 unsigned char temp,rbyte=0; 
 for(temp=8;temp!=0;temp--) 
 { 
  SCL=1; 
  Delayus(4); 
  rbyte=rbyte<<1; 
  rbyte=rbyte|((unsigned char)(SDA)); 
  SCL=0; 
 } 
 return(rbyte); 

void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) 

 Start(); 
 Write8Bit(WriteDeviceAddress); 
 TestAck(); 
 Write8Bit(RomAddress); 
 TestAck(); 
 for(;number!=0;number--)  
 { 
  Write8Bit(*Wdata); 
  TestAck(); 
  Wdata++; 
 } 
 Stop(); 
 DelayMs(10); 

void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) 

 Start(); 
 Write8Bit(WriteDeviceAddress); 
 TestAck(); 
 Write8Bit(RomAddress); 
 TestAck(); 
  
 Start(); 
 Write8Bit(ReadDviceAddress); 
 TestAck(); 
 while(bytes!=1)  
 { 
  *RamAddress=Read8Bit(); 
  Ack(); 
  RamAddress++; 
  bytes--; 
 } 
 *RamAddress=Read8Bit(); 
 NoAck(); 
 Stop(); 

void Display(void)     //顯示 

unsigned int i = 0; 
unsigned int temp,count; 
        temp = Disdigit[count];     
  P2 =temp; 
  temp = Disbuf[count]; 
  temp = Num[temp]; 
        P0 =temp; 
        count++;       
    if  (count==4) 
        count=0;         
}    
void time1() interrupt 3 using 2 

       
   Display();     
   TH1 = (65535 - 2000)/256; 
   TL1 = (65535 - 2000)%256; 

void main() 

 TMOD = 0x15; 
 TR1 = 1; 
 EA = 1; 
 ET1 = 1;  
  
 Write24c02(write_data,0x00,3); 
  DelayMs(200);  
  Read24c02(read_data,0x00,4); 
 while(1) 
  { 
        Disbuf[0] = read_data[0]; 
     Disbuf[1] = read_data[1]; 
     Disbuf[2] = read_data[2]; 
     Disbuf[3] = read_data[3]; 
  } 
}

 

  • 上一篇: 51單片機驅(qū)動DS1302時間模塊+DS18B20溫度傳感器模塊+12864液晶顯示完整程序設(shè)計
  • 下一篇: 沒有了
  • 發(fā)表評論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機電之家 | 會員助手 | 免費鏈接

    點擊這里給我發(fā)消息66821730(技術(shù)支持)點擊這里給我發(fā)消息66821730(廣告投放) 點擊這里給我發(fā)消息41031197(編輯) 點擊這里給我發(fā)消息58733127(審核)
    本站提供的機電設(shè)備,機電供求等信息由機電企業(yè)自行提供,該企業(yè)負責信息內(nèi)容的真實性、準確性和合法性。
    機電之家對此不承擔任何保證責任,有侵犯您利益的地方請聯(lián)系機電之家,機電之家將及時作出處理。
    Copyright 2007 機電之家 Inc All Rights Reserved.機電之家-由機電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機電一體化學會
    中國行業(yè)電子商務(wù)100強網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1