Micom_Electric/CAN 2014. 3. 3. 22:31














방법: 16진수 문자열과 숫자 형식 간 변환(C# 프로그래밍 가이드)

http://msdn.microsoft.com/ko-kr/library/bb311038.aspx


string을 byte[] 배열로 변환? 


CAN ID ---> CAN FILTER MASK 변환


http://www.csharpstudy.com/Tips/Tips-string-encoding.aspx


        private void btnCANID_Click(object sender, EventArgs e)

        {

            int num = Int32.Parse(tbCANID.Text, System.Globalization.NumberStyles.HexNumber);

            int temp;

            temp = num >> 3;

            //tbFilterValue.Text = temp.ToString() + (num << 5 & 0XE0).ToString();

            tbFilterValue.Text = String.Format("{00:X2}", temp) + String.Format("{00:X2}", (num << 5 & 0XE0));

        }


MCP2515 - CAN LIB 수정된거

// ---------------------------------------------------------------------------

/**

 * \brief Set filter mask of given MCP2515 register values

 *

 * \param amr0 Acceptence mask register 0

 * \param amr1 Acceptence mask register 1

 * \param amr2 Acceptence mask register 2

 * \param amr3 Acceptence mask register 3

 *

 * This function has only affect if mcp2515 is in configuration mode.

 * The filter mask is only set for the first 11 bit because of compatibility

 * issues between SJA1000 and MCP2515.

 */

void mcp2515_set_mask(unsigned char amr0, unsigned char amr1, unsigned char amr2, unsigned char amr3) {


    // MCP2515 mask bit definition: 0 = accept without matching, 1 = do matching with acceptance filter

    // -> invert mask


// As MCP2515 is used do not 'NOT' - 2014.03.03

    // mask for filter 1

    mcp2515_write_register(MCP2515_REG_RXM0SIDH, amr0);

    mcp2515_write_register(MCP2515_REG_RXM0SIDL, (amr1) & 0xE0);

    mcp2515_write_register(MCP2515_REG_RXM0EID8, 0x00);

    mcp2515_write_register(MCP2515_REG_RXM0EID0, 0x00);


    // mask for filter 2

    mcp2515_write_register(MCP2515_REG_RXM1SIDH, amr2);

    mcp2515_write_register(MCP2515_REG_RXM1SIDL, (amr3) & 0xE0);

    mcp2515_write_register(MCP2515_REG_RXM1EID8, 0x00);

    mcp2515_write_register(MCP2515_REG_RXM1EID0, 0x00);


}

// ---------------------------------------------------------------------------

// MCP2515 Filter reg is RXF0SIDH(ID10...ID3)/RXF0SIDL(ID2...ID0)

// if want to get CAN ID = 545 (101 0100 0101) --> 1010 1000 101x (=A8Ax)

// so we should set RXF0SIDH = A8, RXF0SIDL = A0 (only upper 3 bits is useful) 

// ---------------------------------------------------------------------------

/**

 * \brief Set filter code of given MCP2515 register values

 *

 * \param amr0 Acceptence code register 0

 * \param amr1 Acceptence code register 1

 * \param amr2 Acceptence code register 2

 * \param amr3 Acceptence code register 3

 * \param amr4 Acceptence code register 4 // extended

 * \param amr5 Acceptence code register 5

 *

 * This function has only affect if mcp2515 is in configuration mode.

 */

void mcp2515_set_filter(unsigned char acr0, unsigned char acr1, unsigned char acr2, 

unsigned char acr3, unsigned char acr4, unsigned char acr5){


    // acceptance code for filter 1

    mcp2515_write_register(MCP2515_REG_RXF0SIDH, acr0);

    mcp2515_write_register(MCP2515_REG_RXF0SIDL, (acr1) & 0xE0); // standard

    mcp2515_write_register(MCP2515_REG_RXF1SIDH, acr0);

    mcp2515_write_register(MCP2515_REG_RXF1SIDL, ((acr1) & 0xE0) | 0x80); // extended


    // acceptance code for filter 2

    mcp2515_write_register(MCP2515_REG_RXF2SIDH, acr2);

    mcp2515_write_register(MCP2515_REG_RXF2SIDL, (acr3) & 0xE0); // standard

    mcp2515_write_register(MCP2515_REG_RXF3SIDH, acr2);

    mcp2515_write_register(MCP2515_REG_RXF3SIDL, ((acr3) & 0xE0) | 0x80); // extended


    // fill remaining filters with zero

    mcp2515_write_register(MCP2515_REG_RXF4SIDH, 0x00);

    mcp2515_write_register(MCP2515_REG_RXF4SIDL, 0x00);

    mcp2515_write_register(MCP2515_REG_RXF5SIDH, 0x00);

    mcp2515_write_register(MCP2515_REG_RXF5SIDL, 0x00);

}

// ---------------------------------------------------------------------------


posted by 털보네i

댓글을 달아 주세요