C-code with I2C API implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chikas
    New Member
    • Mar 2007
    • 1

    #1

    C-code with I2C API implementation

    hallo all,I need help and any tips shall be appreciated!
    i want to implement a I2C API(from Beck SC12 microcontroller ) in a 1 wire search C-code(use to search devices on 1 wire bus) sothat that i can use Beck SC12 microcontroller to perform a 1 wire search command.
    I tried to implement this I2C API in the search code but my programm this not workout because my programming power is weak

    1) see communication diagram on page one under:
    http://pdfserv.maxim-ic.com/en/an/AN3684.pdf or search AN3684 in the internet
    2) see the 1 wire search C-code from page 12 to 16 under: http://pdfserv.maxim-ic.com/en/an/AN3684.pdf or search AN3684 in the internet.
    3)see below for the I2C API decription and my failed 1 wire search program.
    further explanation for the I2C API is found under:www.goblack.de.

    Code:
    /****
    I2C API DEFINITIONS*************************************** ******************************/
    
    void I2C_init (void);
    void I2C_release (void);
    void I2C_restart (void);
    unsigned char I2C_scan (unsigned char start_addr, unsigned char end_addr);
    int I2C_transmit_block (unsigned char slave, char far * buffer, int length);
    int I2C_receive_block (unsigned char slave, char far * buffer, int length);
    int I2C_transmit_char (unsigned char slave, char c);
    int I2C_receive_char (unsigned char slave, char * c, unsigned char lastchar);
    void I2C_select_clock_pin(unsigned char pio_no);
    void I2C_select_data_pin(unsigned char pio_no);
    
    /************************************************** ***********************/
    //#endif /* __I2CAPI_H__*/
    
    
    /************************************************** **************************/
    //includes
    //I2C definitions
    /************************************************** **************************/
    #include <stdio.h>
    #include <stdlib.h>
    #include <dos.h>
    #include "declarations.h"
    #include "1wire.h"
    
    
    
    /************************************************** ***********************/
    //Init the I2C Interface
    //I2C bus initialisation
    /************************************************** ***********************/
    uchar program = 0; // program-variable
    uchar ROM[8];
    uchar lastDevice=0;
    
    uchar LastDeviceFlag, LastFamilyDiscrepancy;
    int LastDiscrepancy;
    uchar deviceAddress;
    
    uchar resetSearch;
    uchar diir,firsstBit,seccondBit;
    //I2C bus initialisation
    
    void I2C_init (void)
    {
    union REGS inregs;
    union REGS outregs;
    
    
    inregs.h.ah = I2C_INIT;
    
    int86(I2CINT,&inregs,&outregs);
    
    }
    
    /************************************************** ***********************/
    //Release I2C bus
    //I2C bus sends stop condition
    /************************************************** ***********************/
    void I2C_release (void) //
    {
    union REGS inregs;
    union REGS outregs;
    
    
    inregs.h.ah = I2C_RELEASE;
    inregs.h.al = 0;
    int86(I2CINT,&inregs,&outregs);
    
    }
    
    /************************************************** ***********************/
    //Restart I2C
    //I2C bus sends start condition
    /************************************************** ***********************/
    void I2C_restart (void)
    {
    union REGS inregs;
    union REGS outregs;
    
    inregs.h.ah = I2C_RESTART;
    inregs.h.al = 0;
    int86(I2CINT,&inregs,&outregs);
    
    }
    
    
    /************************************************** ***********************/
    //Transmit Block
    // I2C bus transmits a number of bytes length from the buffer to DS2482
    //"slave" is the i2c adress of DS2482 ,7bits + 1bit(R/W)
    //transmission ends up with stop condition
    /************************************************** ***********************/
    int I2C_transmit_block (unsigned char slave, char far * buffer, int length)
    {
    union REGS inregs;
    union REGS outregs;
    struct SREGS sregs;
    
    inregs.h.ah = I2C_TRANS_RECV_BLOCK;
    inregs.h.al = slave & 0xFE;
    inregs.x.cx = length;
    sregs.es = FP_SEG(buffer);
    inregs.x.bx = FP_OFF(buffer);
    int86x(I2CINT,&inregs,&outregs,&sregs);
    
    if (outregs.x.flags & 0x01)
    {
    return outregs.h.al;
    }
    
    return 0;
    
    }
    
    /************************************************** ***********************/
    //Receive Block
    //I2C bus recieve byte(s) sequentially
    //this is oppsite of transmit_block above!
    /************************************************** ***********************/
    int I2C_receive_block (unsigned char slave, char far * buffer, int length)
    {
    union REGS inregs;
    union REGS outregs;
    struct SREGS sregs;
    
    
    inregs.h.ah = I2C_TRANS_RECV_BLOCK;
    inregs.h.al = slave | 0x01;
    inregs.x.cx = length;
    sregs.es = FP_SEG(buffer);
    inregs.x.bx = FP_OFF(buffer);
    int86x(I2CINT,&inregs,&outregs,&sregs);
    
    if (outregs.x.flags & 0x01)
    {
    return outregs.h.al;
    }
    
    return 0;
    
    }
    
    /************************************************** ***********************/
    //Transmit one character
    //I2C bus transmits a byte from µC to DS2482
    //"slave" is DS2482 adress 7bits + 1 bit(R/W)
    /************************************************** ***********************/
    int I2C_transmit_char (unsigned char slave, char c)
    {
    union REGS inregs;
    union REGS outregs;
    
    inregs.h.ah = I2C_TRANS_RECV_CHAR;
    inregs.h.al = slave & 0xFE;
    inregs.h.cl = c;
    int86(I2CINT,&inregs,&outregs);
    
    if (outregs.x.flags & 0x01)
    {
    return (int)outregs.h.al & 0x00FF;
    }
    
    return 0;
    }
    
    /************************************************** ***********************/
    //Receive one Character
    //I2C recieve byte(s) from DS2482
    /************************************************** ***********************/
    int I2C_receive_char (unsigned char slave, char * c, unsigned char lastchar)
    {
    union REGS inregs;
    union REGS outregs;
    
    inregs.h.ah = I2C_TRANS_RECV_CHAR;
    inregs.h.al = slave | 0x01;
    if (lastchar)
    {
    inregs.h.cl = 1; //want more characters
    }
    else
    {
    inregs.h.cl = 0; //only one character
    }
    int86(I2CINT,&inregs,&outregs);
    
    if (outregs.x.flags & 0x01)
    {
    *c = 0;
    return (int)outregs.h.al & 0x00FF;
    }
    
    *c = (char)outregs.h.ch;
    return 0;
    }
    
    /************************************************** ***********************/
    //Select I2C Clock Pin from the µC
    /************************************************** ***********************/
    void I2C_select_clock_pin(unsigned char pio_no)
    {
    union REGS inregs;
    union REGS outregs;
    
    inregs.h.ah = I2C_SELECT_CLK_PIN;
    inregs.h.al = pio_no;
    int86(I2CINT,&inregs,&outregs);
    }
    
    /************************************************** ***********************/
    //Select I2C data Pin from the µC
    /************************************************** ***********************/
    void I2C_select_data_pin(unsigned char pio_no)
    {
    union REGS inregs;
    union REGS outregs;
    
    inregs.h.ah = I2C_SELECT_DATA_PIN;
    inregs.h.al = pio_no;
    int86(I2CINT,&inregs,&outregs);
    }
    
    void writeByte(unsigned char slave,unsigned char data)
    {
    I2C_init();
    I2C_transmit_char(slave,data);
    I2C_release();
    }
    
    
    
    void readbyte();
    {
    }
    
    void onewreset1()
    {
    I2C_init();
    I2C_transmit_char( 0x30, 0xB4);// A5h 1w-wire reset byte
    I2C_release(); // transmission stop condition
    
    
    }
    
    
    uchar onewreset()
    {
    
    I2C_init();
    I2C_transmit_char( 0x30, 0xB4); // 1-wire reset byte
    I2C_release(); // transmission stop condition
    return SUCCESS;
    }
    
    
    void Dreset()
    {
    
    I2C_init();
    I2C_transmit_char( 0x30,0xF0);// 1-wire device reset byte
    I2C_release(); // transmission stop condition
    }
    
    
    uchar OWFirst( uchar resetSearch,uchar lastDevice, uchar deviceAddress)
    {
    LastDiscrepancy=0;
    deviceAddress=0;
    OWSearch1( resetSearch, &lastDevice, &deviceAddress);
    }
    
    
    uchar OWNext(uchar resetSearch, uchar lastdevice, uchar deviceAddress) 
    {
    OWSearch1(resetSearch, &lastdevice, &deviceAddress);
    
    }
    
    
    
    
    // OWSearch
    
    
    //
    uchar OWSearch1(uchar resetSearch, uchar *lastDevice, uchar *deviceAddress)
    {
    uchar bit_number,last_zero,serial_byte_number,serial_byt e_mask;
    uchar firstBit,secondBit,dir,i, buff[3];
    //initialisation for search
    uchar retVal = FAILURE;
    bit_number = 1;
    last_zero = 0;
    serial_byte_number = 0;
    serial_byte_mask = 1;
    firstBit, secondBit, dir;
    i = 0;
    deviceAddress = (unsigned char *)malloc(8*sizeof(char));
    I2C_init();
    I2C_transmit_char(0x30, 0xF0);// perform a global reset of device
    I2C_restart();
    I2C_transmit_block( 0x30, buff,0);
    buff[2]=I2C_receive_char(0x31,buff, 0);
    I2C_release();
    
    // if the last call was not the last one
    if (!(*lastDevice))
    {
    // reset the 1-wire
    // if there are no parts on 1-wire, return FALSE
    if(!onewreset())
    {
    // reset the search
    *lastDevice = 0;
    LastDiscrepancy = 0;
    return FAILURE;
    }
    
    // Issue the Search ROM command
    
    writeByte(0x30,0xF0);
    I2C_restart();
    writeByte1(0x30,0xE1,0xF0); //slave adress+1w set pointer+save in read in status register
    
    // loop to do the search
    do
    {
    if ( bit_number < LastDiscrepancy )
    {
    if( ROM[serial_byte_number] & serial_byte_mask )
    {
    dir = 1;
    }
    else
    {
    dir = 0;
    }
    }
    else
    {
    // if equal to last pick 1, if not then pick 0
    
    
    if(bit_number == LastDiscrepancy)
    {
    
    
    dir =1;
    } 
    
    if(bit_number > LastDiscrepancy)
    {
    
    dir =0;
    } 
    
    }
    
    
    
    
    if(!owTriplet(&dir, &firstBit, &secondBit)) // genrate 3 time slotes..2 read slotes and 1 write slot
    {
    return FAILURE; 
    }
    
    
    
    // if 0 was picked then record its position in LastZero
    if ( firstBit == 0 && secondBit == 0 && dir == 0 )
    {
    last_zero = bit_number;
    
    }
    
    
    // check for no devices on 1-wire
    if ( firstBit == 1 && secondBit == 1 )
    break;
    
    ****i deleted part of my program because of limited space to post message here!!
    
    
    }
    
    return retVal;
    
    }
    
    // owTriplet
    
    //
    // returns SUCCES or FAILURE
    //
    uchar owTriplet(uchar *diir, uchar *firsstBit, uchar *seccondBit)
    small space here!!!refer to AN3684 in the internet,page 13 and 14!




    once more i will be greatful for any help.
    paul
Working...