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

技術熱線: 4007-888-234

技術支持

CCSC i2c.c程序訪問MCD2demo 24C02EEPROM

更新時間: 2019-03-23

十年專注單片機方案開發的方案公司英銳恩,分享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()

404
返回首頁 |  返回上一頁
联系我们: 迁西县| 平利县| 息烽县| 留坝县| 买车| 钟山县| 崇明县| 益阳市| 柳江县| 兴城市| 馆陶县| 扶余县| 苏尼特左旗| 怀安县| 甘洛县| 裕民县| 铜梁县| 丹东市| 南华县| 健康| 枞阳县| 千阳县| 会理县| 府谷县| 禄丰县| 奉节县| 增城市| 永靖县| 西充县| 车致| 丹东市| 佛坪县| 图木舒克市| 舒城县| 夏津县| 彰武县| 玉林市| 临猗县| 酉阳| 台中县| 集安市|