⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c_demo.lst

📁 用PIC16F73编写的SMBUS通信测试程序
💻 LST
字号:
     1: /* I2C test program that writes & reads data to an I2C EEPROM device. */
     2: 
     3: #include <pic.h>
     4: #include "delay.h"
     5: #include "i2c.h"
     6: #include "delay.c"
     7: #include "i2c.c"
     8: #include "i2c_2.c"
     9: 
    10: #define ROM1 0x16        /* smbus */
    11: #define ROM 0xA0        /* I2C EEPROM */
    12: #define XTAL_FREQ       4MHZ
    13: #define I2C_MODULE 0
    14: 
    15: 
    16: 
    17: 
    18: uchar code1[]={0x06,0x06,0x5a,0xff,0x80,0x00,0x00,0x00,0xff};
    19: uchar code2[]={0x5a,0xff,0x5a,0xff};
    20: uchar code3[]={0x80,0x00,0x5a,0xff};
    21: uchar code4[]={0x00,0x00,0x5a,0xff};
    22: 
    23: void flashled(void) {           /* flash a led on RB0 */
    24:         while(1) {
    25:             RB0 = 1;
    26:             DelayMs(200);
    27:             RB0 = 0;
    28:             DelayMs(200);
    29:         }
    30: }
    31: 
    32: 
    33: void WriteByte(unsigned char addr, unsigned char byte) {
    34:         i2c_WriteTo(ROM);
    35:         if (i2c_PutByte(addr)==I2C_ERROR)
    36:             flashled();
    37:         if (i2c_PutByte(byte)==I2C_ERROR)
    38:             flashled();
    39: }
    40: 
    41: 
    42: 
    43: unsigned int ReadByte_smbus(unsigned char addr) {
    44:         unsigned int data=0;
    45:         unsigned char data1,data2;
    46:         i2c_WriteTo(ROM1);
    47:         if (i2c_PutByte(addr)==I2C_ERROR)
    48:             flashled();
    49:         i2c_ReadFrom(ROM1);
    50:         data1=i2c_GetByte(I2C_MORE);
    51:         data2=i2c_GetByte(I2C_LAST);
    52:         data|=data1;
    53:         data<<=8;
    54:         data|=data2;
    55:         return(data);
    56: }
    57: ////////********************************//////////////
    58: int ReadWord(uchar addr){
    59: uchar data1,data2;
    60: int data;
    61: 
    62:  start_i2c(); 
    63:       send_byte(0x16);      //发送器件地址,即DEVICE ADDRESS。 
    64:         if (ack==0) i2c_error(); //如果24LC02无应答。则进入I2C ERROR错误指示。 
    65:       send_byte(addr);         //发送字地址,即WORD ADDRESS。D口显示数组。 
    66:         if (ack==0) i2c_error(); 
    67:       start_i2c();          //重新启动总线。 
    68:       send_byte(0x17);      //发送读命令和器件地址DEVICE ADDRESS。 
    69:         if (ack==0) i2c_error(); 
    70:       data2=receive_byte();
    71:       i2c_SendAcknowledge(1);
    72:       data1=receive_byte();
    73:       stop_i2c(); 
    74:       //data<<=8;
    75:       //data|=data1;
    76:       return(data1);
    77:       }
    78: 
    79: //////////////////////////////////////////////
    80: static  int  remain,remain1;
    81: 
    82: void main(void) {
    83: unsigned char count,val;
    84: //const unsigned char *add;
    85: //add=0x20;
    86: //TRISB=0;        /* use a led on RB0 - set as output */
    87: //PORTB=0;
    88: //RB0=0;
    89: PORTA=0;
    90: ADCON1=0X06;
    91: TRISA=0x0;        /* use a led on RB0 - set as output */
    92: RA2=0;
    93: remain=0x0;
    94: 
    95: /* initialize i2c */
    96: /*
    97: #ifdef I2C_MODULE
    98:     //SSPMode(MASTER_MODE);
    99:     //SSPEN = 1;
   100:     CKP = 1;
   101: #else
   102:     SCL_DIR = I2C_OUTPUT;
   103:     SDA_DIR = I2C_OUTPUT;
   104:     SDA = 0;
   105:     SCL = 0;
   106: #endif
   107: */
   108:     SCL_DIR = I2C_OUTPUT;
   109:     SDA_DIR = I2C_OUTPUT;
   110:     SDA = 0;
   111:     SCL = 0;
   112: 
   113: while(1) {
   114: 
   115:     RA2=0; 
   116:     remain=0;
   117:     remain1=0;
   118:     count=0x1;
   119: 
   120:     DelayMs(200);       
   121: 
   122:     remain1=ReadByte_smbus(0x15);    //OK
   123:   DelayMs(200);
   124:   DelayMs(200);
   125:       DelayMs(20);
   126: 
   127:      I_send_word(0x16,0x4f,0x5a,0xff);
   128:      I_send_word(0x16,0x4f,0x80,0x00);
   129:      I_send_word(0x16,0x4f,0x00,0x00);
   130:      DelayMs(20);
   131:   DelayMs(200);
   132:   I_send_word(0x16,0x00,0x06,0x06);
   133:   I_send_str(0x16,0x0,code1,2); 
   134:         DelayMs(200);
   135:         DelayMs(200);
   136:         DelayMs(200);
   137:         DelayMs(200);
   138:       remain1=ReadByte_EE(0x4);    //OK
   139:       remain1=ReadByte_EE(0x5);
   140:       remain1=ReadByte_EE(0x6);
   141:       remain1=ReadByte_EE(0x7);
   142:       remain1=ReadByte_EE(0x8);
   143: 
   144:       I_send_str(0xa0,0x0,code,5);    //OK
   145:       DelayMs(20);
   146:       I_send_word(0x16,0x4f,0x5a,0xff);
   147:       I_send_word(0x16,0x4f,0x80,0x00);
   148:       I_send_word(0x16,0x4f,0x00,0x00);
   149:       DelayMs(20);
   150:     remain1=ReadByte_smbus(0x15);    //OK
   151:     remain1=ReadByte_smbus(0x15);    //OK
   152:     DelayMs(200);
   153: /*===================以下直接写EEPROM
   154:     RA2=1;      
   155:     I_send_str(0xa0,0x00,code,5);    //OK
   156:     remain=0;
   157:     remain1=0;
   158:     count=0x1;
   159:     DelayMs(200);
   160:     remain1=ReadByte_EE(0x4);    //OK
   161:     DelayMs(200);
   162: ==================================*/   
   163:     
   164:     
   165: }
   166: }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -