天堂草原最受欢迎的角色,天堂动漫,天堂在线,色天堂下载,天堂中文在线资源,亚洲男人天堂

技術熱線: 4007-888-234
設計開發

專注差異化嵌入式產品解決方案 給智能產品定制注入靈魂給予生命

開發工具

提供開發工具、應用測試 完善的開發代碼案例庫分享

技術支持

從全面的產品導入到強大技術支援服務 全程貼心伴隨服務,創造無限潛能!

新品推廣

提供新的芯片及解決方案,提升客戶產品競爭力

新聞中心

提供最新的單片機資訊,行業消息以及公司新聞動態

CCSC i2c.c程序訪問MCD2demo 24C02EEPROM

更新時間: 2019-03-23
閱讀量:1857

十年專注單片機方案開發的方案公司英銳恩,分享CCSC i2c.c程序訪問MCD2demo 24C02EEPROM。英銳恩現提供服務產品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。

/*=============================================================================
I2C總線訪問24C02,運行程序:
        24C02 ready   -> LED0亮,
        Test  OK      -> LED7亮,
        Write&Read OK -> LED1/3/5/7亮。
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
// MPLAB IDE V7.11 + CCS C3.18
// i2c access 24C02 for MCD2-demo
//
// by LW7807@163.com
// 2005/06/21
///////////////////////////////////////////////////////////////////////////////
#include <16F877A.h>                            // PIC16F877 header file
#use delay(clock=4000000)                       // for 4Mhz crystal
#fuses XT, NOWDT, NOPROTECT, NOLVP              // for debug mode

#define EEPROM_24C02_SDA        PIN_B5
#define EEPROM_24C02_SCL        PIN_B4
#define EEPROM_24C02_SIZE       256
#use i2c(master, sda=EEPROM_24C02_SDA, scl=EEPROM_24C02_SCL)


///////////////////////////////////////////////////////////////////////////////
//
void init_eeprom_24c02(void)
{
        port_b_pullups(true);                   // !!!internal pullup resister

        output_float(EEPROM_24C02_SCL);
        output_float(EEPROM_24C02_SDA);
}//end init_24c02()

///////////////////////////////////////////////////////////////////////////////
//
int8 eeprom_24c02_ready(void)
{
        int1 ack;

  i2c_start();                            // If write cmd is acknowledged,
        ack = i2c_write(0xa0);                  // then the device is ready.
        i2c_stop();

        return (0==ack) ? 0xff : 0;
}//end eeprom_24c02_ready()

///////////////////////////////////////////////////////////////////////////////
//
void eeprom_24c02_write(int8 addr, int8 data)
{
        while(0 == eeprom_24c02_ready());       // wait 24c02 ready
        i2c_start();
        i2c_write(0xa0);
        i2c_write(addr);
        i2c_write(data);
        i2c_stop();
}//end eeprom_24c02_write()

///////////////////////////////////////////////////////////////////////////////
//
int8 eeprom_24c02_read(int8 addr)
{
        int8 data;

        while(0 == eeprom_24c02_ready());       // wait 24c02 ready
        i2c_start();
        i2c_write(0xa0);
        i2c_write(addr);
        i2c_start();
        i2c_write(0xa1);
        data = i2c_read(0);
        i2c_stop();

        return(data);
}//end eeprom_24c02_read()

///////////////////////////////////////////////////////////////////////////////
//
int8 eeprom_24c02_test(void)
{
        int16 i;

        // write 0x55 test
        for(i=0; i<eeprom_24c02_size;>                {

      eeprom_24c02_write(i, 0x55);
                }
        for(i=0; i<eeprom_24c02_size;>                {
                if(0x55 != eeprom_24c02_read(i)) return 0;
                }

        // write 0xaa test
        for(i=0; i<eeprom_24c02_size;>                {
                eeprom_24c02_write(i, 0xaa);
                }
        for(i=0; i<eeprom_24c02_size;>                {
                if(0xaa != eeprom_24c02_read(i)) return 0;
                }
        
        // write eeprom 0
        for(i=0; i<eeprom_24c02_size;>                {
                eeprom_24c02_write(i, 0x00);
                }
        return 0xff;
}//end eeprom_24c02_test()


/*===========================================================================*/
void main(void)
{
        init_eeprom_24c02();

 // 24c02 is ready
        if(0 != eeprom_24c02_ready())
                {
                output_high(PIN_C0);
                }

        // test 24c02
        if(0 != eeprom_24c02_test())
                {
                output_high(PIN_C7);
                delay_ms(1000);                
                }
        
        // write 24c02
        eeprom_24c02_write(0, 0xaa);
        

// read 24c02
        output_c(eeprom_24c02_read(0));

}//end main()

联系我们: 林州市| 眉山市| 旅游| 西昌市| 南部县| 南皮县| 水城县| 监利县| 威宁| 介休市| 禄劝| 望江县| 平阳县| 庆安县| 宜阳县| 宁强县| 石城县| 无极县| 靖安县| 老河口市| 南岸区| 抚宁县| 大田县| 北京市| 双江| 怀宁县| 通榆县| 赤水市| 临漳县| 巴林左旗| 景德镇市| 大庆市| 满城县| 大宁县| 富民县| 台山市| 清水河县| 绥化市| 米林县| 都兰县| 清河县|