ÕªÒª: ʵʱÐÔÊǺâÁ¿CANÏÖ³¡×ÜÏßϵͳÐÔÄܵÄÒ»¸öÖØÒª±ê×¼¡£±¾ÎÄÌá³öÒ»ÖÖ²ÉÓÃμC/OSII²Ù×÷ϵͳºÍICANÐÒ飬ÔÚÓ¦ÓòãÃæÉÏÌá¸ßCAN×ÜÏßϵͳʵʱÐÔµÄÇý¶¯³ÌÐòÉè¼Æ·½°¸£¬²¢ÒÔ·Ö²ãµÄ·½Ê½Öð²ã²ûÊöCANÇý¶¯³ÌÐòµÄÉè¼Æ¹ý³Ì¡£
¹Ø¼ü´Ê: CAN×ÜÏߣ» μC/OSII£» ʵʱÐÔ£» Çý¶¯³ÌÐò
CAN Driver Design Based on μC/OSII¡ù
Cheng Jin£¬Shi Guoliang
(College of Electronic and Information,Soochow University,Suzhou 215006,China)
Abstract£º Realª²time performance is an important measuring standard for the performance of CAN field bus system. A CAN driver design is proposed to improve the realª²time performance of CAN bus system at the application level by using μC/OSII and Iª²CAN protocol. The design process of CAN driver is introduced by a layered approach.
Key words: CAN bus; μC/OSII; realª²time performance; driver
ÒýÑÔ
CAN×ÜÏßÊǵ¹úBosch¹«Ë¾ÓÚ1983ÄêÕë¶ÔÆû³µÓ¦Óöø¿ª·¢µÄ£¬Ò»ÖÖÄÜÓÐЧ֧³Ö·Ö²¼Ê½¿ØÖƺÍʵʱ¿ØÖƵĴ®ÐÐͨÐÅÍøÂ磬ÊôÓÚÏÖ³¡×ÜÏߵķ¶³ë¡£ÆäͨОàÀëÓ벨ÌØÂÊÓйأ¬×î´óͨОàÀë¿É´ï10 km£¬×î´óͨÐŲ¨ÌØÂÊ¿É´ï1 Mbps¡£CAN×ÜÏßÖٲòÉÓÃ11루CAN2.0AÐÒ飩ºÍ29루CAN2.0BÐÒ飩±êÖ¾£¬ÒÔ¼°·ÇÆÆ»µÐÔÖÙ²Ã×ÜÏ߽ṹ»úÖÆ£¬¿ÉÒÔÈ·¶¨Êý¾Ý¿éµÄÓÅÏȼ¶£¬±£Ö¤ÔÚÍøÂç½Úµã³åͻʱ×î¸ßÓÅÏȼ¶½Úµã²»ÐèÒª³åÍ»µÈ´ý¡£CAN×ÜÏßÉϵÄÈκνڵã¾ù¿ÉÔÚÈÎÒâʱ¿Ì£¬Ö÷¶¯ÏòÍøÂçÉÏÆäËû½Úµã·¢ËÍÐÅÏ¢¶ø²»·ÖÖ÷´Î£¬´Ó¶øʵÏÖ¸÷½ÚµãÖ®¼äµÄ×ÔÓÉͨÐÅ¡£Ä¿Ç°£¬CAN×ÜÏßÐÒéÒѱ»¹ú¼Ê±ê×¼»¯×éÖ¯ÈÏÖ¤£¬¼¼Êõ±È½Ï³ÉÊ죬Òѹ㷺ӦÓÃÓÚÆû³µ¡¢¹¤Òµ¡¢¸ßËÙÍøÂçºÍµÍ¼Ûλ¶à·Á¬ÏßµÈÁìÓòÖС£
μC/OSIIÊÇJean J.Labrosse¿ª·¢µÄÒ»ÖÖСÐÍǶÈëʽ²Ù×÷ϵͳ¡£ËüʵÖÊÉÏÊÇ»ùÓÚÓÅÏȼ¶µÄ¿É°þ¶áÐÍÄںˣ¬ÏµÍ³ÖеÄËùÓÐÈÎÎñ¶¼ÓÐÒ»¸öΨһµÄÓÅÏȼ¶±ð£¬ÊʺÏÓ¦ÓÃÓÚʵʱÐÔÒªÇó½ÏÇ¿µÄ³¡ºÏ¡£±¾ÎIJÉÓÃμC/OSIIÀ´Éè¼ÆCANµÄÇý¶¯³ÌÐò£¬ÒÔÂú×ãϵͳµÄʵʱҪÇó¡£
1 CAN½ÚµãµÄÓ²¼þÉè¼Æ
ͼ1 CAN½Úµã»ù±¾½á¹¹
CAN½ÚµãÊÇ·Ö²¼ÔÚCANÍøÂçÖнøÐÐÏ໥ͨÐŵĻù±¾µ¥Ôª£¬Ö÷ÒªÓÉÖ÷¿ØÖÆÆ÷¡¢CAN¿ØÖÆÆ÷ºÍCANÊÕ·¢Æ÷×é³É¡£±¾Éè¼ÆÖУ¬½ÚµãµÄ»ù±¾½á¹¹Èçͼ1Ëùʾ¡£ÔÚCANÍøÂçÖУ¬ECU(Electronic Control Unit)ÊÇÖ¸Ò»¸ö¾ßÓÐÍêÕû¹¦ÄܵÄCAN½Úµã¡£
²ÉÓÃNXP¹«Ë¾µÄLPC2368×÷ΪCAN½ÚµãµÄÖ÷¿ØÖÆÆ÷¡£LPC2368ÊÇÒ»¿î»ùÓÚARM7TDMISÄں˵ÄRISC´¦ÀíÆ÷£¬°üº¬2¸ö¼æÈÝCAN2.0B¹æ·¶µÄCAN¿ØÖÆÆ÷¡£Ã¿¸öCAN¿ØÖÆÆ÷ÓµÓÐË«ÖؽÓÊÕ»º³åÆ÷ºÍÈý̬·¢ËÍ»º³åÆ÷£¬¾ßÓпìËÙµÄÓ²¼þʵÏÖµÄËÑË÷Ëã·¨£¬¿ÉÒÔÖ§³Ö´óÁ¿µÄCAN±êʶ·û¡£
LPC2368ÊÇÒ»¿î3.3 VÆ÷¼þ£¬ËäÈ»Æä¶ÔÓ¦µÄCANÊÕ·¢Æ÷½Ó¿ÚÒý½ÅÄܹ»³ÐÊÜ5 Vµçѹ£¬µ«ÎªÁËÈÃCAN½ÚµãÄܹ»¸üÎȶ¨µØÔËÐУ¬ÕâÀï²ÉÓÃTI¹«Ë¾µÄ3.3 V CANÊÕ·¢Æ÷SN65HVD230DÓëÖ®ÅäºÏʹÓá£Æ¾½è¸ßÊäÈë×迹ÌØÐÔ£¬SN65HVD230D¿ÉÒÔÔÚÒ»Ìõ×ÜÏßÉÏÖ§³Ö¶à´ï120¸öCAN½Úµã£¬²¢ÇÒÄܹ»ºÍ5 VµÄCANÊÕ·¢Æ÷Á¼ºÃµØ¼æÈÝ¡£±¾ÎÄÖصã½éÉÜCANÇý¶¯³ÌÐòµÄÉè¼Æ·½·¨¡£
2 CANÇý¶¯³ÌÐòÉè¼Æ×ÜÌå˼Ïë
ͼ2 Çý¶¯³ÌÐò·Ö²ã½á¹¹
ΪÁËʹÈí¼þ¿ÉÒÆÖ²ÐÔÇ¿¡¢Ò×ÓÚά»¤£¬²ÉÓ÷ֲãµÄ·½·¨±àдCANÇý¶¯³ÌÐò¡£Çý¶¯³ÌÐò·Ö²ã½á¹¹Èçͼ2Ëùʾ¡£Í¼ÖУ¬Ë«Ïò¼ýÍ·±íʾʵʱ²Ù×÷ϵͳμC/OSIIÓëCANÇý¶¯³ÌÐòÖ®¼äµÄÊý¾Ý½»»»£¬µ¥Ïò¼ýÍ·±íʾÉϲãÈí¼þ¶ÔϲãÈí¼þµÄµ÷Óá£
3 CANÉ豸¿ØÖƲãºÍCAN½Ó¿Ú¿ØÖƲã
CANÉ豸¿ØÖƲãµÄÖ÷ÒªÈÎÎñÊÇ£º³õʼ»¯Ö÷¿ØÖÆÆ÷ÓëCAN¿ØÖÆÆ÷Ö®¼äµÄÁ¬½ÓÅäÖ㬸´Î»CAN¿ØÖÆÆ÷£¬½¨Á¢Ö÷¿ØÖÆÆ÷ºÍCAN¿ØÖÆÆ÷Ö®¼äµÄͨÐź¯Êý¡£ÓÉÓÚLPC2368ÄÚ²¿¼¯³ÉÁËCAN¿ØÖÆÆ÷£¬CPU¿ÉÒÔͨ¹ýÄÚ²¿APB×ÜÏß½Ó¿Ú¶ÔCAN¿ØÖÆÆ÷µÄËùÓмĴæÆ÷½øÐзÃÎÊ£¬ËùÒÔ²»ÔÙÐèÒª±àдÉ豸¿ØÖÆÇý¶¯²ã³ÌÐò£¬ÒѾÍêÈ«ÓÉÓ²¼þʵÏÖÁË¡£
CAN½Ó¿Ú¿ØÖƲãÖ÷ÒªÈÎÎñÊÇ£ºÊµÏÖCAN¿ØÖÆÆ÷µÄ¸÷ÖÖ¹¦ÄÜ£¬ÈçÉèÖÿØÖÆģʽ¡¢·¢ËÍÊý¾Ý¡¢ÊͷŽÓÊÕ»º³åÇø¡¢ÅäÖÃÑéÊÕÂ˲¨Æ÷µÈ¡£ÕâЩ²Ù×÷¶¼ÊÇͨ¹ý¶ÁдCAN¿ØÖÆÆ÷µÄÄÚ²¿Ïà¹Ø¼Ä´æÆ÷À´ÊµÏֵġ£
CAN¿ØÖÆÆ÷³õʼ»¯³ÌÐò£¨ÔÚÓ¦ÓòãÖÐʵÏÖ£¬ÄÚ²¿µ÷Óõĺ¯ÊýÒ²¶¼ÊÇÔڸòãÖбàдµÄ£©ÈçÏ£º
voidCAN20B_Init() {
¡¡¡¡#ifCAN1_EN > 0
¡¡¡¡while((CAN1MOD & CAN_MOD_RM)!=1)
¡¡¡¡CAN1_MOD_RM ();//½øÈ븴λģʽ
¡¡¡¡CAN1_BTR ();//ÅäÖÃ×ÜÏ߶¨Ê±¼Ä´æÆ÷
¡¡¡¡ID_RAM ();//ÅäÖÃÑéÊÕÂ˲¨Æ÷
¡¡¡¡while((CAN2MOD & CAN_MOD_NM)!=1)
¡¡¡¡CAN1_MOD_NM_SET();//½øÈëÕý³£Ä£Ê½
¡¡¡¡CAN1_INT_EN ();//ÖжÏʹÄܼĴæÆ÷ÉèÖÃ
¡¡¡¡#endif
}
ΪÁËʹ³ÌÐò¸ü¼Ó¼ò½à¡¢¿É¶ÁÐÔ¸üÇ¿£¬¿ÉÒÔͨ¹ýºê¶¨ÒåµÄÐÎʽ½øÐбàд¡£ÀýÈ磺
#define CAN_MOD_RM () CAN1MOD |= 1
CAN1MODÊÇCAN¿ØÖÆÆ÷µÄģʽ¼Ä´æÆ÷£¬×îµÍλÖÃ1¿ÉʹCAN¿ØÖÆÆ÷½øÈ븴λģʽ¡£ÕâÖÖģʽÏ£¬¿ÉÒÔ¶Ô¿ØÖÆÆ÷µÄËùÓмĴæÆ÷½øÐÐд²Ù×÷¡£ÆäËû¶ÔCAN¿ØÖÆÆ÷ÄÚ²¿¼Ä´æÆ÷µÄ²Ù×÷¿ÉÒÔ²ÎÕÕLPC2368µÄ¼¼ÊõÊֲᡣ
×÷Õߣº³É½ø£¬Ê©¹úÁº ËÕÖÝ´óѧ À´Ô´£ºµ¥Æ¬»úÓëǶÈëʽϵͳ