📄 main.lst
字号:
207 1 SFRPAGE = CONFIG_PAGE; // switch to config page to config oscillator
208 1 OSCXCN = 0x77; // start external oscillator; 22.1 MHz Crystal
209 1 // system clock is 22.1 MHz / 2 = 11.05 MHz
210 1 for (n=0;n<255;n++); // delay about 1ms
211 1 while ((OSCXCN & 0x80) == 0); // wait for oscillator to stabilize
212 1 CLKSEL |= 0x01; // switch to external oscillator
213 1 }
214
215 void config_IO (void)
216 {
217 1 SFRPAGE = CONFIG_PAGE; //Port SFR's on Configuration page
218 1 XBR3 = 0x80; // Configure CAN TX pin (CTX) as push-pull digital output
219 1
220 1 P5MDOUT |= 0x0f;
221 1 P5=0xfe;
222 1
223 1 P2MDOUT |= 0x18; // Configure P2.3 as push-pull to drive LED
224 1
225 1 XBR2 = 0x40; // Enable Crossbar/low ports
226 1 }
227
228 ////////////////////////////////////////////////////////////////////////////////
229 //CAN Functions
230 ////////////////////////////////////////////////////////////////////////////////
231
232
233 //Clear Message Objects
234 void clear_msg_objects (void)
235 {
236 1 SFRPAGE = CAN0_PAGE;
237 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask Register 1
238 1 CAN0DATL = 0xFF; // Set direction to WRITE all IF registers to Msg Obj
239 1 for (i=1;i<33;i++)
240 1 {
241 2 CAN0ADR = IF1CMDRQST; // Write blank (reset) IF registers to each msg obj
C51 COMPILER V7.50 MAIN 01/21/2007 21:09:42 PAGE 5
242 2 CAN0DATL = i;
243 2 }
244 1 }
245
246 //Initialize Message Object for RX
247 void init_msg_object_RX (char MsgNum)
248 {
249 1 SFRPAGE = CAN0_PAGE;
250 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
251 1 CAN0DAT = 0x00B8; // Set to WRITE, and alter all Msg Obj except ID MASK
*** ERROR C202 IN LINE 251 OF MAIN.C: 'CAN0DAT': undefined identifier
252 1 // and data bits
253 1 CAN0ADR = IF1ARB1; // Point to arbitration1 register
254 1 CAN0DAT = 0x0000; // Set arbitration1 ID to "0"
*** ERROR C202 IN LINE 254 OF MAIN.C: 'CAN0DAT': undefined identifier
255 1 CAN0DAT = 0x8000; // Arb2 high byte:Set MsgVal bit, no extended ID,
*** ERROR C202 IN LINE 255 OF MAIN.C: 'CAN0DAT': undefined identifier
256 1 // Dir = RECEIVE
257 1 CAN0DAT = 0x0480; // Msg Cntrl: set RXIE, remote frame function disabled
*** ERROR C202 IN LINE 257 OF MAIN.C: 'CAN0DAT': undefined identifier
258 1 CAN0ADR = IF1CMDRQST; // Point to Command Request reg.
259 1 CAN0DATL = MsgNum; // Select Msg Obj passed into function parameter list
260 1 // --initiates write to Msg Obj
261 1 // 3-6 CAN clock cycles to move IF register contents to the Msg Obj in CAN RAM
262 1 }
263
264 //Initialize Message Object for TX
265 void init_msg_object_TX (char MsgNum)
266 {
267 1 SFRPAGE = CAN0_PAGE;
268 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
269 1 CAN0DAT = 0x00B2; // Set to WRITE, & alter all Msg Obj except ID MASK bits
*** ERROR C202 IN LINE 269 OF MAIN.C: 'CAN0DAT': undefined identifier
270 1 CAN0ADR = IF1ARB1; // Point to arbitration1 register
271 1 CAN0DAT = 0x0000; // Set arbitration1 ID to highest priority
*** ERROR C202 IN LINE 271 OF MAIN.C: 'CAN0DAT': undefined identifier
272 1 CAN0DAT = 0xA004; // Autoincrement to Arb2 high byte:
*** ERROR C202 IN LINE 272 OF MAIN.C: 'CAN0DAT': undefined identifier
273 1 // Set MsgVal bit, no extended ID, Dir = WRITE
274 1 CAN0DAT = 0x0081; // Msg Cntrl: DLC = 1, remote frame function not enabled
*** ERROR C202 IN LINE 274 OF MAIN.C: 'CAN0DAT': undefined identifier
275 1 CAN0ADR = IF1CMDRQST; // Point to Command Request reg.
276 1 CAN0DAT = MsgNum; // Select Msg Obj passed into function parameter list
*** ERROR C202 IN LINE 276 OF MAIN.C: 'CAN0DAT': undefined identifier
277 1 // --initiates write to Msg Obj
278 1 // 3-6 CAN clock cycles to move IF reg contents to the Msg Obj in CAN RAM.
279 1 }
280
281 //Start CAN
282 void start_CAN (void)
283 {
284 1 /* Calculation of the CAN bit timing :
285 1
286 1 System clock f_sys = 22.1184 MHz/2 = 11.0592 MHz.
287 1 System clock period t_sys = 1/f_sys = 90.422454 ns.
288 1 CAN time quantum tq = t_sys (at BRP = 0)
289 1
290 1 Desired bit rate is 1 MBit/s, desired bit time is 1000 ns.
291 1 Actual bit time = 11 tq = 996.65ns ~ 1000 ns
292 1 Actual bit rate is 1.005381818 MBit/s = Desired bit rate+0.5381%
293 1
294 1 CAN bus length = 10 m, with 5 ns/m signal delay time.
C51 COMPILER V7.50 MAIN 01/21/2007 21:09:42 PAGE 6
295 1 Propagation delay time : 2*(transceiver loop delay + bus line delay) = 400 ns
296 1 (maximum loop delay between CAN nodes)
297 1
298 1 Prop_Seg = 5 tq = 452 ns ( >= 400 ns).
299 1 Sync_Seg = 1 tq
300 1
301 1 Phase_seg1 + Phase_Seg2 = (11-6) tq = 5 tq
302 1 Phase_seg1 <= Phase_Seg2, => Phase_seg1 = 2 tq and Phase_Seg2 = 3 tq
303 1 SJW = (min(Phase_Seg1, 4) tq = 2 tq
304 1
305 1 TSEG1 = (Prop_Seg + Phase_Seg1 - 1) = 6
306 1 TSEG2 = (Phase_Seg2 - 1) = 2
307 1 SJW_p = (SJW - 1) = 1
308 1
309 1 Bit Timing Register = BRP + SJW_p*0x0040 = TSEG1*0x0100 + TSEG2*0x1000 = 2640
310 1
311 1 Clock tolerance df :
312 1
313 1 A: df < min(Phase_Seg1, Phase_Seg2) / (2 * (13*bit_time - Phase_Seg2))
314 1 B: df < SJW / (20 * bit_time)
315 1
316 1 A: df < 2/(2*(13*11-3)) = 1/(141-3) = 1/138 = 0.7246%
317 1 B: df < 2/(20*11) = 1/110 = 0.9091%
318 1
319 1 Actual clock tolerance is 0.7246% - 0.5381% = 0.1865% (no problem for quartz)
320 1 */
321 1
322 1 SFRPAGE = CAN0_PAGE;
323 1 CAN0CN |= 0x41; // Configuration Change Enable CCE and INIT
324 1 CAN0ADR = BITREG ; // Point to Bit Timing register
325 1 CAN0DAT = 0x2640; // see above
*** ERROR C202 IN LINE 325 OF MAIN.C: 'CAN0DAT': undefined identifier
326 1
327 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
328 1 CAN0DAT = 0x0087; // Config for TX : WRITE to CAN RAM, write data bytes,
*** ERROR C202 IN LINE 328 OF MAIN.C: 'CAN0DAT': undefined identifier
329 1 // set TXrqst/NewDat, clr IntPnd
330 1
331 1 // RX-IF2 operation may interrupt TX-IF1 operation
332 1 CAN0ADR = IF2CMDMSK; // Point to Command Mask 2
333 1 CAN0DATL = 0x1F; // Config for RX : READ CAN RAM, read data bytes,
334 1 // clr NewDat and IntPnd
335 1 CAN0CN |= 0x06; // Global Int. Enable IE and SIE
336 1 CAN0CN &= ~0x41; // Clear CCE and INIT bits, starts CAN state machine
337 1 }
338
339 /***********************************************************************************
340 * 函数名称:Timer0_ISR;
341 *
342 * 函数功能描述:定时器0中断响应;
343 *
344 * 输入参数:none;
345 *
346 * 返回数据:none;
347 *
348 * 注意: none;
349 ************************************************************************************/
350 void Timer0_ISR (void) interrupt 1
351 {
352 1 char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
353 1 SFRPAGE = SFR_PAGE_0; // set SFR page F
354 1
C51 COMPILER V7.50 MAIN 01/21/2007 21:09:42 PAGE 7
355 1 i++;
356 1 if(i==10)
357 1 {
358 2 i=0;
359 2 flag=~flag;
360 2 }
361 1
362 1 TF0 = 0; // 清除中断标志
363 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
364 1 }
365
366
367 /***********************************************************************************
368 * 函数名称:LED_state;
369 *
370 * 函数功能描述:LED开关控制 0:关闭,其他:开启;
371 *
372 * 输入参数:0:关闭,其他:开启;
373 *
374 * 返回数据:none;
375 *
376 * 注意: none;
377 ************************************************************************************/
378
379 void LED_state(unsigned char flag)
380 {
381 1 if(flag)
382 1 transmit_Byte(0x01);
383 1 // else
384 1 // LED=0;
385 1 }
386
387
388 void transmit_Byte (char MsgNum)
389 {
390 1 SFRPAGE = CAN0_PAGE; // IF1 already set up for TX
391 1 CAN0ADR = IF1CMDMSK; // Point to Command Mask 1
392 1 CAN0DAT = 0x0087; // Config to WRITE to CAN RAM, write data bytes,
*** ERROR C202 IN LINE 392 OF MAIN.C: 'CAN0DAT': undefined identifier
393 1 // set TXrqst/NewDat, Clr IntPnd
394 1 CAN0ADR = IF1DATA1; // Point to 1st byte of Data Field
395 1 // CAN0DATL = bytedata; // Ones signals to transmit byte data
396 1 for(i=0;i<=100;i++)
397 1 { CAN0DATL = i; // Ones signals to transmit byte data
398 2
399 2 CAN0ADR = IF1CMDRQST; // Point to Command Request Reg.
400 2 CAN0DATL = MsgNum; // Move new data for TX to Msg Obj "MsgNum"
401 2 }
402 1 }
403 /***********************************************************************************/
404 // 文件结束
405 /***********************************************************************************/
C51 COMPILATION COMPLETE. 0 WARNING(S), 12 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -