📄 发送.lst
字号:
193 1 XBR0 = 0x04; // bit2=1,UART0 TX0配置到P0.0,RX0配置到P0.1
194 1 P0MDOUT = 0X10; // P0口输出模式控制, 1:相应口为推挽输出模式
195 1 }
196
197 ////////////////////////////////////////////////////////////////////////////////
198 //CAN Functions
199 ////////////////////////////////////////////////////////////////////////////////
200
201 //Clear Message Objects
202 void clear_msg_objects (void)//将所有的消息清 0
203 {
204 1 SFRPAGE = CAN0_PAGE;
205 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask Register 1
206 1 CAN0DATL = 0xFF; // Set direction to WRITE all IF registers to Msg Obj
207 1 for (i=1;i<33;i++) // i 上面定义了 i 为全局变量
208 1 {
209 2 CAN0ADR = IF1CMDRQST; // Write blank (reset) IF registers to each msg obj
210 2 CAN0DATL = i;
211 2 }
212 1 }
213
214 //Initialize Message Object for RX
215 void init_msg_object_RX (char MsgNum)//初始化接收消息体
216 {
217 1 SFRPAGE = CAN0_PAGE;
218 1
219 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
220 1 CAN0DAT = 0x00F8; // Set to WRITE, and alter all Msg Obj except ID MASK
221 1 // and data bits
222 1 CAN0ADR = IF1MSK1;
223 1 CAN0DAT = 0xFFFF; //MSK15~0标识符
224 1 CAN0DAT = 0x0000; //不进行报文滤波
225 1
226 1 CAN0ADR = IF1ARB1; // Point to arbitration1 register
227 1 CAN0DAT = 0xBBBB; // Set arbitration1 ID to "0"
228 1 CAN0DAT = 0x8004; // Arb2 high byte:Set MsgVal bit, no extended ID,接收标识符
229 1 // Dir = RECEIVE
230 1 CAN0DAT = 0x0480; // Msg Cntrl: set RXIE, remote frame function disabled,单数据帧
231 1 CAN0ADR = IF1CMDRQST; // Point to Command Request reg.
232 1 CAN0DATL = MsgNum; // Select Msg Obj passed into function parameter list 消息对象与IFX之间的数据传输
-开始
233 1 // --initiates write to Msg Obj
234 1 // 3-6 CAN clock cycles to move IF register contents to the Msg Obj in CAN RAM
235 1 }
236
237 //Initialize Message Object for TX
238 void init_msg_object_TX (char MsgNum,int id)//初始化发送消息体
239 { int temp;
240 1 SFRPAGE = CAN0_PAGE; // 切换到CAN寄存器页
C51 COMPILER V7.50 发送 01/15/2009 10:05:20 PAGE 5
241 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
242 1 CAN0DAT = 0x00B2; // Set to WRITE, & alter all Msg Obj except ID MASK bits
243 1 // 设定读写方向(写),改变所有信息目标但MASK位不变
244 1
245 1 CAN0ADR = IF1ARB1; // Point to arbitration1 register
246 1 CAN0DAT = 0x0000; // Set arbitration1 ID to highest priority
247 1 // 即ID15-0=0
248 1 //CAN0DAT = 0xA008; // Autoincrement to Arb2 high byte:发送标识符设置
249 1
250 1 //CAN0ADR = IF1ARB2 // CAN0ADR会自动加0,所以这一行要不要都可以
251 1 temp=id<<2;
252 1 temp&=0x1fff;
253 1 temp|=0xa000;
254 1 CAN0DAT=temp; // IFx arbitration 2 register = 101 (id) 00 最后两个0是左移2位形成的
255 1 // MsgVal=1,该消息对象参与报文收发 Xtd=0,ID为11位,Dir=1,报文传输方向为发送
256 1
257 1 CAN0DAT = 0x0081; // Msg Cntrl: DLC = 1, remote frame function not enabled(远程帧功能未启用)
258 1 // EoB=1 当消息对象只是应用与单数据帧场合,则此位须置1,书213页
259 1 // 数据帧的数据长度为1个字节(DLC = 1)
260 1
261 1 CAN0ADR = IF1CMDRQST; // Point to Command Request reg.
262 1 CAN0DAT = MsgNum; // Select Msg Obj passed into function parameter list
263 1 // --initiates write to Msg Obj
264 1 // 3-6 CAN clock cycles to move IF reg contents to the Msg Obj in CAN RAM.
265 1 }
266
267 //Start CAN
268 void start_CAN (void) //开can,初始化
269 {
270 1 SFRPAGE = CAN0_PAGE;
271 1 CAN0CN |= 0x41; // 将 CCE and INIT 位置1,开始初始化
272 1 CAN0ADR = BITREG ; // Point to Bit Timing register
273 1 CAN0DAT = 0x2640; // see above
274 1
275 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
276 1 CAN0DAT = 0x0087; // Config for TX : WRITE to CAN RAM, write data bytes,
277 1 // set TXrqst/NewDat, clr IntPnd
278 1
279 1 // RX-IF2 operation may interrupt TX-IF1 operation
280 1 CAN0ADR = IF2CMDMSK; // Point to Command Mask 2
281 1 CAN0DATL = 0x1F; // Config for RX : READ CAN RAM, read data bytes,
282 1 // clr NewDat and IntPnd
283 1 CAN0CN |= 0x06; // 允许全局中断 Global Int. Enable IE and SIE
284 1 CAN0CN &= ~0x41; // Clear CCE and INIT bits, starts CAN state machine
285 1 }
286
287 //Transmit CAN frame to turn other node's LED ON
288 void transmit_data(char MsgNum)//发送消息体发送数据
289 {int pass;
290 1 SFRPAGE = CAN0_PAGE; // IF1 already set up for TX
291 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
292 1 CAN0DAT = 0x0087; // Config to WRITE to CAN RAM, write data bytes,
293 1 // set TXrqst/NewDat开始消息报文发送, Clr IntPnd
294 1 //CAN0ADR=IF2MSGC; //消息接口寄存器2的
295 1 // pass=CAN0DAT;
296 1
297 1 // if(pass&0x8000 !=0)
298 1 CAN0ADR = IF1DATA1; // 指向数据场的第一个字节 Point to 1st byte of Data Field
299 1
300 1 CAN0DATL =sdata[j]; // j 上面定义了 j 为全局变量
301 1
302 1 //CAN0DATL = 0x35; // Ones signals to turn LED's light ON in data A1 field
C51 COMPILER V7.50 发送 01/15/2009 10:05:20 PAGE 6
303 1
304 1 CAN0ADR = IF1CMDRQST; // Point to Command Request Reg.
305 1 CAN0DATL = MsgNum; // Move new data for TX to Msg Obj "MsgNum"
306 1
307 1 }
*** WARNING C280 IN LINE 289 OF 发送.C: 'pass': unreferenced local variable
308
309
310
311
312 // Receive Data from the IF2 buffer
313 void receive_data(char MsgNum)//接收消息体中接收数据
314 {
315 1 char receive_data;
316 1 SFRPAGE = CAN0_PAGE; // IF1 already set up for RX
317 1 CAN0ADR = IF2CMDRQST;// Point to Command Request Reg.
318 1 CAN0DATL = MsgNum; // Move new data for RX from Msg Obj "MsgNum"
319 1 // Move new data to a
320 1 CAN0ADR = IF2DATA1; // Point to 1st byte of Data Field
321 1
322 1 receive_data = CAN0DATL;
323 1 if (receive_data == 0x11) //Ones is signal from other node to turn LED ON
324 1 LED = 1;
325 1 else LED = 0; //Otherwise turn LED OFF (message was one's)
326 1 SFRPAGE = UART0_PAGE; //发送完了后消息体中存储的数据改变了,通过单步调试可以发现
327 1 //所以每次应该先把接收到的数据取出来,另存为一个变量
328 1 SBUF0=receive_data;
329 1 while(TI0==0); TI0=0;
330 1 }
331
332 ////////////////////////////////////////////////////////////////////////////////
333 //Interrupt Service Routine
334 ////////////////////////////////////////////////////////////////////////////////
335 void ISRname (void) interrupt 19 //中断子程序
336 {
337 1 status = CAN0STA;
338 1 if ((status&0x10) != 0)
339 1 {
340 2 CAN0STA = (CAN0STA&0xEF)|0x07;
341 2
342 2 receive_data (0x01);
343 2 }
344 1 if ((status&0x08) != 0)
345 1 {
346 2 CAN0STA = (CAN0STA&0xF7)|0x07;
347 2 }
348 1 if (((status&0x07) != 0)&&((status&0x07) != 7))
349 1 {
350 2
351 2 CAN0STA = CAN0STA|0x07;
352 2 }
353 1 }
354
355 //**************************************************************************/
356 void uart_init()
357 {
358 1 SFRPAGE = UART0_PAGE;
359 1 SCON0 = 0x50; //允许uart0 模式1,8位UART可变波特率异步传输
360 1
361 1 }
362 /*************************************************************************
363 *
C51 COMPILER V7.50 发送 01/15/2009 10:05:20 PAGE 7
364 *定时器1初始化,作为UART0的波特率发生器
365 *************************************************************************/
366 void time_init()
367 {
368 1 SFRPAGE = TIMER01_PAGE;
369 1 TCON = 0X40;
370 1 TMOD = 0x20;
371 1 CKCON = 0X10;
372 1 TH1 = 0xDC; //0xDC=220: 11.0592 M /(256-220) * 1/32=9600
373 1 TR1=1;
374 1
375 1
376 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 516 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 49 2
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -