PIC单片机论坛论坛展示区(非讨论区)(这个版块不能发新贴)『 单片机类精华贴子展示区 』 → CCSC i2c.c程序访问MCD2demo 24C02EEPROM


  共有15538人关注过本帖树形打印

主题:CCSC i2c.c程序访问MCD2demo 24C02EEPROM

帅哥哟,离线,有人找我吗?
lw7807
  1楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:初中二年级 帖子:54 积分:1892 威望:0 精华:11 注册:2005-6-17 19:44:00
CCSC i2c.c程序访问MCD2demo 24C02EEPROM  发帖心情 Post By:2005-6-22 21:17:00

/*=============================================================================
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; ++i)
                {
                eeprom_24c02_write(i, 0x55);
                }
        for(i=0; i<EEPROM_24C02_SIZE; ++i)
                {
                if(0x55 != eeprom_24c02_read(i)) return 0;
                }

        // write 0xaa test
        for(i=0; i<EEPROM_24C02_SIZE; ++i)
                {
                eeprom_24c02_write(i, 0xaa);
                }
        for(i=0; i<EEPROM_24C02_SIZE; ++i)
                {
                if(0xaa != eeprom_24c02_read(i)) return 0;
                }
        
        // write eeprom 0
        for(i=0; i<EEPROM_24C02_SIZE; ++i)
                {
                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()


支持(0中立(0反对(0回到顶部
帅哥哟,离线,有人找我吗?
lw7807
  2楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:初中二年级 帖子:54 积分:1892 威望:0 精华:11 注册:2005-6-17 19:44:00
附上程序lst文件  发帖心情 Post By:2005-6-22 21:19:00

CCS PCM C Compiler, Version 3.187, 16465

Filename: D:\myPIC\ccsc_I2c.LST

ROM used: 456 (6%) Largest free fragment is 2048 RAM used: 7 (4%) at main() level 13 (7%) worst case Stack: 4 locations

* 0000: MOVLW 00 0001: MOVWF 0A 0002: GOTO 193 0003: NOP .................... /*============================================================================= .................... 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 .................... //////// Standard Header file for the PIC16F877A device //////////////// .................... #device PIC16F877A .................... #list .................... .................... #use delay(clock=4000000) // for 4Mhz crystal * 017C: MOVLW 22 017D: MOVWF 04 017E: MOVF 00,W 017F: BTFSC 03.2 0180: GOTO 190 0181: MOVLW 01 0182: MOVWF 78 0183: CLRF 77 0184: DECFSZ 77,F 0185: GOTO 184 0186: DECFSZ 78,F 0187: GOTO 183 0188: MOVLW 4A 0189: MOVWF 77 018A: DECFSZ 77,F 018B: GOTO 18A 018C: NOP 018D: NOP 018E: DECFSZ 00,F 018F: GOTO 181 0190: BCF 0A.3 0191: BCF 0A.4 0192: GOTO 1B8 (RETURN) .................... #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) * 000C: MOVLW 08 000D: MOVWF 78 000E: NOP 000F: NOP 0010: BCF 06.4 0011: BSF 03.5 0012: BCF 06.4 0013: NOP 0014: BCF 03.5 0015: RLF 26,F 0016: BCF 06.5 0017: BTFSS 03.0 0018: GOTO 01C 0019: BSF 03.5 001A: BSF 06.5 001B: BCF 03.5 001C: BTFSC 03.0 001D: GOTO 021 001E: BSF 03.5 001F: BCF 06.5 0020: BCF 03.5 0021: BSF 03.5 0022: BSF 06.4 0023: BCF 03.5 0024: BTFSC 06.4 0025: GOTO 028 0026: BSF 03.5 0027: GOTO 023 0028: DECFSZ 78,F 0029: GOTO 00E 002A: NOP 002B: BCF 06.4 002C: BSF 03.5 002D: BCF 06.4 002E: NOP 002F: BSF 06.5 0030: NOP 0031: NOP 0032: BSF 06.4 0033: BCF 03.5 0034: BTFSC 06.4 0035: GOTO 038 0036: BSF 03.5 0037: GOTO 033 0038: CLRF 78 0039: NOP 003A: BTFSC 06.5 003B: BSF 78.0 003C: BCF 06.4 003D: BSF 03.5 003E: BCF 06.4 003F: BCF 03.5 0040: BCF 06.5 0041: BSF 03.5 0042: BCF 06.5 0043: BCF 03.5 0044: RETLW 00 * 00A7: MOVLW 08 00A8: MOVWF 26 00A9: MOVF 77,W 00AA: MOVWF 27 00AB: BSF 03.5 00AC: BSF 06.5 00AD: NOP 00AE: BSF 06.4 00AF: BCF 03.5 00B0: BTFSC 06.4 00B1: GOTO 0B4 00B2: BSF 03.5 00B3: GOTO 0AF 00B4: BTFSC 06.5 00B5: BSF 03.0 00B6: BTFSS 06.5 00B7: BCF 03.0 00B8: RLF 78,F 00B9: NOP 00BA: BSF 03.5 00BB: BCF 06.4 00BC: BCF 03.5 00BD: BCF 06.4 00BE: DECFSZ 26,F 00BF: GOTO 0AB 00C0: BSF 03.5 00C1: BSF 06.5 00C2: NOP 00C3: BCF 03.5 00C4: BCF 06.5 00C5: MOVF 27,W 00C6: BTFSC 03.2 00C7: GOTO 0CB 00C8: BSF 03.5 00C9: BCF 06.5 00CA: BCF 03.5 00CB: NOP 00CC: BSF 03.5 00CD: BSF 06.4 00CE: BCF 03.5 00CF: BTFSC 06.4 00D0: GOTO 0D3 00D1: BSF 03.5 00D2: GOTO 0CE 00D3: NOP 00D4: BCF 06.4 00D5: BSF 03.5 00D6: BCF 06.4 00D7: NOP 00D8: BCF 03.5 00D9: BCF 06.5 00DA: BSF 03.5 00DB: BCF 06.5 00DC: BCF 03.5 00DD: BCF 0A.3 00DE: BCF 0A.4 00DF: GOTO 10D (RETURN) .................... .................... .................... /////////////////////////////////////////////////////////////////////////////// .................... // .................... void init_eeprom_24c02(void) .................... { .................... port_b_pullups(true); // !!!internal pullup resister * 0004: BSF 03.5 0005: BCF 01.7 .................... .................... output_float(EEPROM_24C02_SCL); 0006: BSF 06.4 .................... output_float(EEPROM_24C02_SDA); 0007: BSF 06.5 .................... }//end init_24c02() 0008: BCF 03.5 0009: BCF 0A.3 000A: BCF 0A.4 000B: GOTO 19F (RETURN) .................... .................... /////////////////////////////////////////////////////////////////////////////// .................... // .................... int8 eeprom_24c02_ready(void) .................... { .................... int1 ack; .................... .................... i2c_start(); // If write cmd is acknowledged, * 0045: BSF 03.5 0046: BSF 06.5 0047: NOP 0048: BSF 06.4 0049: NOP 004A: BCF 03.5 004B: BCF 06.5 004C: BSF 03.5 004D: BCF 06.5 004E: NOP 004F: BCF 03.5 0050: BCF 06.4 0051: BSF 03.5 0052: BCF 06.4 .................... ack = i2c_write(0xa0); // then the device is ready. 0053: MOVLW A0 0054: BCF 03.5 0055: MOVWF 26 0056: CALL 00C 0057: MOVF 78,W 0058: BTFSS 03.2 0059: GOTO 05C 005A: BCF 25.0 005B: GOTO 05D 005C: BSF 25.0 .................... i2c_stop(); 005D: BSF 03.5 005E: BCF 06.5 005F: NOP 0060: BSF 06.4 0061: BCF 03.5 0062: BTFSC 06.4 0063: GOTO 066 0064: BSF 03.5 0065: GOTO 061 0066: NOP 0067: NOP 0068: NOP 0069: NOP 006A: BSF 03.5 006B: BSF 06.5 006C: NOP .................... .................... return (0==ack) ??0xff : 0; 006D: MOVLW 00 006E: BCF 03.5 006F: BTFSC 25.0 0070: MOVLW 01 0071: XORLW 00 0072: BTFSS 03.2 0073: GOTO 076 0074: MOVLW FF 0075: GOTO 077 0076: MOVLW 00 0077: MOVWF 78 .................... }//end eeprom_24c02_ready() 0078: RETLW 00 .................... .................... /////////////////////////////////////////////////////////////////////////////// .................... // .................... void eeprom_24c02_write(int8 addr, int8 data) .................... { .................... while(0 == eeprom_24c02_ready()); // wait 24c02 ready 0079: CALL 045 007A: MOVF 78,F 007B: BTFSC 03.2 007C: GOTO 079 .................... i2c_start(); 007D: BSF 03.5 007E: BSF 06.5 007F: NOP 0080: BSF 06.4 0081: NOP 0082: BCF 03.5 0083: BCF 06.5 0084: BSF 03.5 0085: BCF 06.5 0086: NOP 0087: BCF 03.5 0088: BCF 06.4 0089: BSF 03.5 008A: BCF 06.4 .................... i2c_write(0xa0); 008B: MOVLW A0 008C: BCF 03.5 008D: MOVWF 26 008E: CALL 00C .................... i2c_write(addr); 008F: MOVF 23,W 0090: MOVWF 26 0091: CALL 00C .................... i2c_write(data); 0092: MOVF 24,W 0093: MOVWF 26 0094: CALL 00C .................... i2c_stop(); 0095: BSF 03.5 0096: BCF 06.5 0097: NOP 0098: BSF 06.4 0099: BCF 03.5 009A: BTFSC 06.4 009B: GOTO 09E 009C: BSF 03.5 009D: GOTO 099 009E: NOP 009F: NOP 00A0: NOP 00A1: NOP 00A2: BSF 03.5 00A3: BSF 06.5 00A4: NOP .................... }//end eeprom_24c02_write() 00A5: BCF 03.5 00A6: RETLW 00 .................... .................... /////////////////////////////////////////////////////////////////////////////// .................... // .................... int8 eeprom_24c02_read(int8 addr) .................... { .................... int8 data; .................... .................... while(0 == eeprom_24c02_ready()); // wait 24c02 ready * 00E0: CALL 045 00E1: MOVF 78,F 00E2: BTFSC 03.2 00E3: GOTO 0E0 .................... i2c_start(); 00E4: BSF 03.5 00E5: BSF 06.5 00E6: NOP 00E7: BSF 06.4 00E8: NOP 00E9: BCF 03.5 00EA: BCF 06.5 00EB: BSF 03.5 00EC: BCF 06.5 00ED: NOP 00EE: BCF 03.5 00EF: BCF 06.4 00F0: BSF 03.5 00F1: BCF 06.4 .................... i2c_write(0xa0); 00F2: MOVLW A0 00F3: BCF 03.5 00F4: MOVWF 26 00F5: CALL 00C .................... i2c_write(addr); 00F6: MOVF 23,W 00F7: MOVWF 26 00F8: CALL 00C .................... i2c_start(); 00F9: BSF 03.5 00FA: BSF 06.5 00FB: NOP 00FC: BSF 06.4 00FD: NOP 00FE: BCF 03.5 00FF: BCF 06.5 0100: BSF 03.5 0101: BCF 06.5 0102: NOP 0103: BCF 03.5 0104: BCF 06.4 0105: BSF 03.5 0106: BCF 06.4 .................... i2c_write(0xa1); 0107: MOVLW A1 0108: BCF 03.5 0109: MOVWF 26 010A: CALL 00C .................... data = i2c_read(0); 010B: CLRF 77 010C: GOTO 0A7 010D: MOVF 78,W 010E: MOVWF 24 .................... i2c_stop(); 010F: BSF 03.5 0110: BCF 06.5 0111: NOP 0112: BSF 06.4 0113: BCF 03.5 0114: BTFSC 06.4 0115: GOTO 118 0116: BSF 03.5 0117: GOTO 113 0118: NOP 0119: NOP 011A: NOP 011B: NOP 011C: BSF 03.5 011D: BSF 06.5 011E: NOP .................... .................... return(data); 011F: BCF 03.5 0120: MOVF 24,W 0121: MOVWF 78 .................... }//end eeprom_24c02_read() 0122: RETLW 00 .................... .................... /////////////////////////////////////////////////////////////////////////////// .................... // .................... int8 eeprom_24c02_test(void) .................... { .................... int16 i; .................... .................... // write 0x55 test .................... for(i=0; i<EEPROM_24C02_SIZE; ++i) 0123: CLRF 22 0124: CLRF 21 0125: MOVF 22,W 0126: SUBLW 00 0127: BTFSS 03.0 0128: GOTO 132 .................... { .................... eeprom_24c02_write(i, 0x55); 0129: MOVF 21,W 012A: MOVWF 23 012B: MOVLW 55 012C: MOVWF 24 012D: CALL 079 .................... } 012E: INCF 21,F 012F: BTFSC 03.2 0130: INCF 22,F 0131: GOTO 125 .................... for(i=0; i<EEPROM_24C02_SIZE; ++i) 0132: CLRF 22 0133: CLRF 21 0134: MOVF 22,W 0135: SUBLW 00 0136: BTFSS 03.0 0137: GOTO 146 .................... { .................... if(0x55 != eeprom_24c02_read(i)) return 0; 0138: MOVF 21,W 0139: MOVWF 23 013A: CALL 0E0 013B: MOVF 78,W 013C: SUBLW 55 013D: BTFSC 03.2 013E: GOTO 142 013F: MOVLW 00 0140: MOVWF 78 0141: GOTO 179 .................... } 0142: INCF 21,F 0143: BTFSC 03.2 0144: INCF 22,F 0145: GOTO 134 .................... .................... // write 0xaa test .................... for(i=0; i<EEPROM_24C02_SIZE; ++i) 0146: CLRF 22 0147: CLRF 21 0148: MOVF 22,W 0149: SUBLW 00 014A: BTFSS 03.0 014B: GOTO 155 .................... { .................... eeprom_24c02_write(i, 0xaa); 014C: MOVF 21,W 014D: MOVWF 23 014E: MOVLW AA 014F: MOVWF 24 0150: CALL 079 .................... } 0151: INCF 21,F 0152: BTFSC 03.2 0153: INCF 22,F 0154: GOTO 148 .................... for(i=0; i<EEPROM_24C02_SIZE; ++i) 0155: CLRF 22 0156: CLRF 21 0157: MOVF 22,W 0158: SUBLW 00 0159: BTFSS 03.0 015A: GOTO 169 .................... { .................... if(0xaa != eeprom_24c02_read(i)) return 0; 015B: MOVF 21,W 015C: MOVWF 23 015D: CALL 0E0 015E: MOVF 78,W 015F: SUBLW AA 0160: BTFSC 03.2 0161: GOTO 165 0162: MOVLW 00 0163: MOVWF 78 0164: GOTO 179 .................... } 0165: INCF 21,F 0166: BTFSC 03.2 0167: INCF 22,F 0168: GOTO 157 .................... .................... // write eeprom 0 .................... for(i=0; i<EEPROM_24C02_SIZE; ++i) 0169: CLRF 22 016A: CLRF 21 016B: MOVF 22,W 016C: SUBLW 00 016D: BTFSS 03.0 016E: GOTO 177 .................... { .................... eeprom_24c02_write(i, 0x00); 016F: MOVF 21,W 0170: MOVWF 23 0171: CLRF 24 0172: CALL 079 .................... } 0173: INCF 21,F 0174: BTFSC 03.2 0175: INCF 22,F 0176: GOTO 16B .................... return 0xff; 0177: MOVLW FF 0178: MOVWF 78 .................... }//end eeprom_24c02_test() 0179: BCF 0A.3 017A: BCF 0A.4 017B: GOTO 1AA (RETURN) .................... .................... .................... /*===========================================================================*/ .................... void main(void) .................... { * 0193: CLRF 04 0194: MOVLW 1F 0195: ANDWF 03,F 0196: MOVLW 06 0197: BSF 03.5 0198: MOVWF 1F 0199: MOVLW 07 019A: BCF 03.5 019B: MOVWF 1F .................... init_eeprom_24c02(); * 019E: GOTO 004 .................... .................... // 24c02 is ready .................... if(0 != eeprom_24c02_ready()) .................... { 019F: CALL 045 01A0: MOVF 78,F 01A1: BTFSC 03.2 01A2: GOTO 1A9 .................... output_high(PIN_C0); * 019C: MOVLW FF 019D: MOVWF 20 * 01A3: BCF 20.0 01A4: MOVF 20,W 01A5: BSF 03.5 01A6: MOVWF 07 01A7: BCF 03.5 01A8: BSF 07.0 .................... } .................... .................... // test 24c02 .................... if(0 != eeprom_24c02_test()) .................... { 01A9: GOTO 123 01AA: MOVF 78,F 01AB: BTFSC 03.2 01AC: GOTO 1BA .................... output_high(PIN_C7); 01AD: BCF 20.7 01AE: MOVF 20,W 01AF: BSF 03.5 01B0: MOVWF 07 01B1: BCF 03.5 01B2: BSF 07.7 .................... delay_ms(1000); 01B3: MOVLW 04 01B4: MOVWF 21 01B5: MOVLW FA 01B6: MOVWF 22 01B7: GOTO 17C 01B8: DECFSZ 21,F 01B9: GOTO 1B5 .................... } .................... .................... // write 24c02 .................... eeprom_24c02_write(0, 0xaa); 01BA: CLRF 23 01BB: MOVLW AA 01BC: MOVWF 24 01BD: CALL 079 .................... .................... // read 24c02 .................... output_c(eeprom_24c02_read(0)); 01BE: CLRF 23 01BF: CALL 0E0 01C0: MOVLW 00 01C1: MOVWF 20 01C2: BSF 03.5 01C3: MOVWF 07 01C4: BCF 03.5 01C5: MOVF 78,W 01C6: MOVWF 07 .................... .................... }//end main() .................... 01C7: SLEEP

Configuration Fuses: Word 1: 3F79 XT NOWDT NOPUT NODEBUG NOPROTECT BROWNOUT NOLVP NOCPD NOWRT


支持(0中立(0反对(0回到顶部
Google 全部 本站