📄 can.lst
字号:
C51 COMPILER V7.10 CAN 06/10/2004 11:06:11 PAGE 1
C51 COMPILER V7.10, COMPILATION OF MODULE CAN
OBJECT MODULE PLACED IN can.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE can.c BROWSE DEFINE(KEIL) DEBUG OBJECTEXTEND TABS(3)
line level source
1 /**
2 * @file $RCSfile: can.c,v $
3 *
4 * Copyright (c) 2004 Atmel.
5 *
6 * Please read file license.txt for copyright notice.
7 *
8 * @brief This file is an example to use can networking with id and mask functionality.
9 *
10 * This file can be parsed by Doxygen for automatic documentation
11 * generation.
12 * Put here the functional description of this file within the software
13 * architecture of your program.
14 *
15 * @version $Revision: 1.0 $ $Name: $
16 */
17
18 /* @section I N C L U D E S */
19 #include "t89c51cc01.h"
20
21 /* baud rate and bit timing parameters at Fosc=12Mhz */
22 /* refert to Xcalculator software on Atmel website */
23 #define BRP_500k 0x00
24 #define SJW_500k 0x00
25 #define PRS_500k 0x02
26 #define PHS1_500k 0x03
27 #define PHS2_500k 0x03
28
29 #define BRP_100k 0x05
30 #define SJW_100k 0x00
31 #define PRS_100k 0x01
32 #define PHS2_100k 0x03
33 #define PHS1_100k 0x02
34
35 #define MSK_CANGCON_ENA 0x02
36 #define MSK_CANGCON_GRES 0x01
37
38 #define DLC_MAX 8
39 #define CH_DISABLE 0x00
40 #define CH_RxENA 0x80
41 #define CH_TxENA 0x40
42 #define MSK_CANGIE_ENRX 0x20
43 #define MSK_CANGIE_ENTX 0x10
44
45 unsigned char num_channel, num_data;
46
47 /**
48 * FUNCTION_PURPOSE: This file set up Can at 100Kbauds with channel 0 id 0x123 in reception
49 * and channel 1 id 0x001 in emission.
50 * FUNCTION_INPUTS: void
51 * FUNCTION_OUTPUTS: void
52 */
53 void main(void)
54 {
55 1 CANGCON |= MSK_CANGCON_GRES;/* reset CAN */
C51 COMPILER V7.10 CAN 06/10/2004 11:06:11 PAGE 2
56 1 /* reset all mailboxes */
57 1 for (num_channel = 0; num_channel < 15; num_channel++)
58 1 {
59 2 CANPAGE = num_channel << 4;
60 2 CANCONCH = CH_DISABLE;
61 2 CANSTCH = 0;
62 2 CANIDT1 = 0;
63 2 CANIDT2 = 0;
64 2 CANIDT3 = 0;
65 2 CANIDT4 = 0;
66 2 CANIDM1 = 0;
67 2 CANIDM2 = 0;
68 2 CANIDM3 = 0;
69 2 CANIDM4 = 0;
70 2 for (num_data = 0; num_data < 8; num_data++) CANMSG = 0;
71 2 }
72 1 /* setup bit timing */
73 1 CANBT1 = BRP_100k << 1; /* BRP=0x00; */
74 1 CANBT2 &= ~0x60; /* reset SJW */
75 1 CANBT2 |= SJW_100k << 5; /* SJW=0x00; */
76 1 CANBT2 &= ~0x0E; /* reset PRS */
77 1 CANBT2 |= PRS_100k << 1; /* PRS=0x02; */
78 1 CANBT3 &= ~0x70; /* reset PHS2 */
79 1 CANBT3 |= PHS2_100k << 4; /* PHS2=0x03;*/
80 1 CANBT3 &= ~0x0E; /* reset PHS1 */
81 1 CANBT3 |= PHS1_100k << 1; /* PHS1=0x03 */
82 1 CANGCON |= MSK_CANGCON_ENA; /* start CAN */
83 1
84 1 /* Channel 0 init */
85 1 CANPAGE = (0 << 4); /* CHNB=0x00; select channel 0 */
86 1 CANSTCH = 0x00; /* reset channel staus */
87 1 CANCONCH = CH_DISABLE; /* reset control and dlc register */
88 1
89 1 /* Channel 0: identifier = 11bits. CANIDT=0x123 */
90 1 CANIDT1 = 0x24;
91 1 CANIDT2 &= ~0x80;
92 1 CANIDT2 |= 0x60;
93 1
94 1 /* Channel 0: mask = 11bits. 0x7F0 */
95 1 CANIDM1 = 0xFE;
96 1 CANIDM2 &= ~0xE0;
97 1 CANIDM4 = 0;
98 1
99 1 /* Channel 0 configuration */
100 1 CANIDT4 &=~0x04; /* clear bit rtr in CANIDT4. */
101 1 CANCONCH |= DLC_MAX; /* Reception 8 bytes.*/
102 1 CANCONCH |= CH_RxENA; /* Reception enabled without buffer.*/
103 1
104 1 /* Channel 1 init */
105 1 CANPAGE = (1 << 4); /* CHNB=0x01; select channel 1 */
106 1 CANSTCH = 0x00; /* reset channel staus */
107 1 CANCONCH = CH_DISABLE; /* reset control and dlc register */
108 1
109 1 /* Channel 1: identifier = 11bits. CANIDT=0x001 */
110 1 CANIDT1 = 0x80;
111 1 CANIDT2 &= ~0xC0;
112 1 CANIDT2 |= 0x20;
113 1
114 1 /* interrupt configuration */
115 1 CANIE2|=0x01; /* IECH0=1 */
116 1 CANGIE |= MSK_CANGIE_ENTX; /* Can_Tx IT enable */
117 1 CANGIE |= MSK_CANGIE_ENRX; /* Can_Rx IT enable */
C51 COMPILER V7.10 CAN 06/10/2004 11:06:11 PAGE 3
118 1 ECAN = 1; /* CAN IT enable */
119 1 EA = 1; /* all IT enable */
120 1
121 1
122 1 while(1); /* endless */
123 1 }
124
125 /**
126 * FUNCTION_PURPOSE: can interrupt. echo receive data on channel 0 reception.
127 * Reception id between 0x120 and 0x12F.
128 * FUNCTION_INPUTS: P4.1(RxDC) can input
129 * FUNCTION_OUTPUTS: P4.0(TxDC) can output
130 */
131 can_it(void) interrupt 7
132 {
133 1 char save_canpage;
134 1 char i; /* can_data index */
135 1 char can_data[8];
136 1
137 1 /* CAUTION can interrupt function modify CANPAGE. Save CANPAGE at beginning
138 1 and restore it at ending */
139 1 save_canpage = CANPAGE; /* save current context */
140 1
141 1 /* echo receive data on channel 0 reception */
142 1 CANPAGE = (0 << 4); /* CHNB=0x00; select channel 0 */
143 1 if(CANSTCH==MSK_CANSTCH_RxOk)
144 1 {
145 2 for (i=0; i<8; i++) can_data[i] = CANMSG; /* save receive data */
146 2 CANPAGE = (1 << 4); /* CHNB=0x00; select channel 1 */
147 2 /* Channel 1 configuration */
148 2 CANCONCH = CH_DISABLE; /* reset channel 1 configuration */
149 2 for (i=0; i<8; i++) CANMSG = can_data[i]; /* load saved data */
150 2
151 2 CANCONCH |= DLC_MAX; /* transmit 8 bytes */
152 2 CANCONCH |= CH_TxENA; /* emission enabled */
153 2 CANEN2 |= (1 << 1); /* channel 1 enable */
154 2 CANSTCH=0x00; /* reset channel 1 status */
155 2 }
156 1
157 1 CANPAGE = (0 << 4); /* CHNB=0x00; select channel 0 */
158 1 CANCONCH = CH_DISABLE; /* reset channel 0 configuration */
159 1 CANCONCH |= DLC_MAX; /* receive 8 bytes */
160 1 CANCONCH |= CH_RxENA; /* reception enable */
161 1 CANEN2 |= (1 << 0); /* channel 0 enable */
162 1 CANSTCH=0x00; /* reset channel 0 status */
163 1
164 1 CANPAGE= save_canpage; /* restore saved context */
165 1
166 1 CANGIT = 0x00; /* reset all flags */
167 1 }
168
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 244 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 2 8
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILER V7.10 CAN 06/10/2004 11:06:11 PAGE 4
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -