ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:18.66KB ,
资源ID:10232093      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10232093.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(基于CAN总线的通讯子程序.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

基于CAN总线的通讯子程序.docx

1、基于CAN总线的通讯子程序接收程序:/-/ F04x_CAN1.c/-/ DEVICE: C8051F040/ AUTHOR: LS/ TOOLS: Keil C-compiler and Silicon Labs IDE/ CAN1.c and CAN2.c are a simple example of configuring a CAN network to/ transmit and receive data on a CAN network, and how to move information to/ and from CAN RAM message objects. Each

2、C8051F040-TB CAN node is configured/ to send a message when its P3.7 button is depressed/released, with a 0x11/ to indicate the button is pushed, and 0x00 when released. Each node also has/ a message object configured to receive messages. The C8051 tests the/ received data and will turn on/off the t

3、arget boards LED. When one target/ is loaded with CAN2.c and the other is loaded with CAN1.c, one target/ boards push-button will control the other target boards LED, establishing/ a simple control link via the CAN bus and can be observed directly on the/ target boards./ Includes/#include / SFR decl

4、arations/ CAN Protocol Register Index for CAN0ADR, from TABLE 18.1 of the C8051F040/ datasheet/#define CANCTRL 0x00 /Control Register#define CANSTAT 0x01 /Status register#define ERRCNT 0x02 /Error Counter Register#define BITREG 0x03 /Bit Timing Register#define INTREG 0x04 /Interrupt Low Byte Registe

5、r#define CANTSTR 0x05 /Test register#define BRPEXT 0x06 /BRP Extension Register/IF1 Interface Registers/#define IF1CMDRQST 0x08 /IF1 Command Rest Register#define IF1CMDMSK 0x09 /IF1 Command Mask Register#define IF1MSK1 0x0A /IF1 Mask1 Register#define IF1MSK2 0x0B /IF1 Mask2 Register#define IF1ARB1 0

6、x0C /IF1 Arbitration 1 Register#define IF1ARB2 0x0D /IF1 Arbitration 2 Register#define IF1MSGC 0x0E /IF1 Message Control Register#define IF1DATA1 0x0F /IF1 Data A1 Register#define IF1DATA2 0x10 /IF1 Data A2 Register#define IF1DATB1 0x11 /IF1 Data B1 Register#define IF1DATB2 0x12 /IF1 Data B2 Registe

7、r/IF2 Interface Registers/#define IF2CMDRQST 0x20 /IF2 Command Rest Register#define IF2CMDMSK 0x21 /IF2 Command Mask Register#define IF2MSK1 0x22 /IF2 Mask1 Register#define IF2MSK2 0x23 /IF2 Mask2 Register#define IF2ARB1 0x24 /IF2 Arbitration 1 Register#define IF2ARB2 0x25 /IF2 Arbitration 2 Registe

8、r#define IF2MSGC 0x26 /IF2 Message Control Register#define IF2DATA1 0x27 /IF2 Data A1 Register#define IF2DATA2 0x28 /IF2 Data A2 Register#define IF2DATB1 0x29 /IF2 Data B1 Register#define IF2DATB2 0x2A /IF2 Data B2 Register/Message Handler Registers/#define TRANSREQ1 0x40 /Transmission Rest1 Registe

9、r#define TRANSREQ2 0x41 /Transmission Rest2 Register#define NEWDAT1 0x48 /New Data 1 Register#define NEWDAT2 0x49 /New Data 2 Register#define INTPEND1 0x50 /Interrupt Pending 1 Register#define INTPEND2 0x51 /Interrupt Pending 2 Register#define MSGVAL1 0x58 /Message Valid 1 Register#define MSGVAL2 0x

10、59 /Message Valid 2 Registertypedef unsigned char uchar;typedef unsigned int uint;typedef unsigned long ulong;union intchar uint tempval; struct uchar hig; uchar low; bytek; ;xdata union intchar rdata4;xdata uchar sdata8 = 0x32, 0x33,0x34, 0x35, 0xb3, 0xb4, 0xb5, 0xb6;/Global Variables/char MsgNum;c

11、har status;int i;int MOTwoIndex = 0;int MOOneIndex = 0;int StatusCopy;int RXbuffer 4;int TXbuffer 8;int MsgIntNum;int Temperature;sbit BUTTON = P37;sbit LED = P16;sfr16 CAN0DAT = 0xD8;sbit P20 = P20;sbit P21 = P21;sbit WRn = P07;sbit RDn = P06;/ Function PROTOTYPES/ Initialize Message Objectvoid cle

12、ar_msg_objects (void);/void init_msg_object_TX (char MsgNum);void init_msg_object_RX (char MsgNum, uint devID);void start_CAN (void);/void transmit_turn_LED_ON (char MsgNum);/void transmit_turn_LED_OFF (char MsgNum);void receive_data (char MsgNum);void external_osc (void);void config_IO (void);void

13、flash_LED (void);void test_reg_write (char test);void stop_CAN (void);/ MAIN Routine/void main (void) / disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; /configure Port I/O config_IO(); / switch to external oscillator external_osc();/ Configure CAN communications/ IF1 used for procedures calles by

14、 main program/ IF2 used for interrupt service procedure receive_data/ Message Object assignments:/ 0x02: Used to transmit commands to toggle its LED, arbitration number 1/ / Clear CAN RAM clear_msg_objects(); / Initialize message object to transmit data /init_msg_object_TX (0x02); /init_msg_object_T

15、X (0x03); / Initialize message object to receive data /init_msg_object_RX (0x01); init_msg_object_RX (0x01, 65); /ID = 130 / Enable CAN interrupts in CIP-51 EIE2 = 0x20; /Function call to start CAN start_CAN(); /Global enable 8051 interrupts EA = 1; /Loop and wait for interrupts while (1) /*if (BUTT

16、ON = 0) while (BUTTON = 0) transmit_turn_LED_OFF(0x03); else while (BUTTON = 1) transmit_turn_LED_ON(0x03); */ / Set up C8051F040/ Switch to external oscillatorvoid external_osc (void) int n; / local variable used in delay FOR loop. SFRPAGE = CONFIG_PAGE; / switch to config page to config oscillator

17、 OSCXCN = 0x77; / start external oscillator; 22.1 MHz Crystal / system clock is 22.1 MHz / 2 = 11.05 MHz for (n=0;n255;n+); / delay about 1ms while (OSCXCN & 0x80) = 0); / wait for oscillator to stabilize CLKSEL |= 0x01; / switch to external oscillatorvoid config_IO (void) SFRPAGE = CONFIG_PAGE; /Po

18、rt SFRs on Configuration page XBR3 = 0x80; / Configure CAN TX pin (CTX) as push-pull digital output P1MDOUT |= 0x40; / Configure P1.6 as push-pull to drive LED XBR2 = 0x40; / Enable Crossbar/low ports/enable P2.0 as push-pull output P2MDOUT |= 0x03; /enable P0.7 P0MDOUT |= 0x80; /enable P0.6 P0MDOUT

19、 |= 0x40; /enable P3 推挽输出 P3MDOUT = 0xff; /P3 漏极开路输出 /P3MDOUT = 0x00; /P3MDIN = 0xff;/P3 数字输入/set P2.0, P2.1/ P20 = 0; / P21 = 0; /set XMIF wrn WRn = 0; RDn = 1;/CAN Functions/Clear Message Objectsvoid clear_msg_objects (void) SFRPAGE = CAN0_PAGE; CAN0ADR = IF1CMDMSK; / Point to Command Mask Registe

20、r 1 CAN0DATL = 0xFF; / Set direction to WRITE all IF registers to Msg Obj for (i=1;i33;i+) CAN0ADR = IF1CMDRQST; / Write blank (reset) IF registers to each msg obj CAN0DATL = i; /Initialize Message Object for RXvoid init_msg_object_RX (char MsgNum, uint devID) uint temp; SFRPAGE = CAN0_PAGE; CAN0ADR

21、 = IF1CMDMSK; / Point to Command Mask 1 CAN0DAT = 0x00BB; / Set to WRITE, and alter all Msg Obj except ID MASK / and data bits CAN0ADR = IF1ARB1; / Point to arbitration1 register CAN0DAT = 0x0000; / Set arbitration1 ID to 0 /CAN0DAT = 0x8004; / Arb2 high byte:Set MsgVal bit, no extended ID, can2.c C

22、AN0DAT = 0x8000; / Dir = RECEIVE temp = devID 2; temp &= 0x1fff; / 000 ID28-ID16 temp |= 0x8000; CAN0DAT = temp; /CAN0DAT = 0x8000; CAN0DAT = 0x0482; / Msg Cntrl: set RXIE, remote frame function disabled / DLC = 4; CAN0ADR = IF1CMDRQST; / Point to Command Request reg. CAN0DATL = MsgNum; / Select Msg

23、 Obj passed into function parameter list / -initiates write to Msg Obj / 3-6 CAN clock cycles to move IF register contents to the Msg Obj in CAN RAM/Initialize Message Object for TX/*void init_msg_object_TX (char MsgNum) SFRPAGE = CAN0_PAGE; CAN0ADR = IF2CMDMSK; / Point to Command Mask 1 CAN0DAT = 0

24、x00B2; / Set to WRITE, & alter all Msg Obj except ID MASK bits CAN0ADR = IF2ARB1; / Point to arbitration1 register CAN0DAT = 0x0000; / Set arbitration1 ID to highest priority CAN0DAT = 0xA000; / Autoincrement to Arb2 high byte: can2.c CAN0DAT = 0xA004; / Set MsgVal bit, no extended ID, Dir = WRITE CAN0DAT = 0x0081; / Msg Cntrl: DLC = 1, remote frame function not enabled CAN0ADR = IF2CMDRQST; / Point to Command Req

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1