1 | /*******************************************************************************
|
2 |
|
3 | MCU: PIC18F66K80
|
4 | Oscillator: HS, 18.0000 MHz
|
5 | ******************************************************************************/
|
6 |
|
7 |
|
8 | char SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg;
|
9 | unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
|
10 | unsigned short init_flag, send_flag, dt, len, read_flag;
|
11 | unsigned char Rx_Data_Len; // received data length in bytes
|
12 | char RxTx_Data[8]; // can rx/tx data buffer
|
13 | char Msg_Rcvd; // reception flag
|
14 | const long ID_1st = 1365, ID_2nd = 170; // node IDs
|
15 | long Rx_ID, mask;
|
16 |
|
17 | void main() {
|
18 | TRISD = 0;
|
19 | TRISE = 16; // RE4 as input, RE5 as output
|
20 | PORTC = 0; // clear PORTC
|
21 | TRISC = 0; // set PORTC as output
|
22 |
|
23 | Can_Init_Flags = 0; //
|
24 | Can_Send_Flags = 0; // clear flags
|
25 | Can_Rcv_Flags = 0; //
|
26 |
|
27 | Can_Send_Flags = _CAN_TX_PRIORITY_0 & // form value to be used
|
28 | _CAN_TX_XTD_FRAME & // with CANWrite
|
29 | _CAN_TX_NO_RTR_FRAME;
|
30 |
|
31 | Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE & // form value to be used
|
32 | _CAN_CONFIG_PHSEG2_PRG_ON & // with CANInit
|
33 | _CAN_CONFIG_XTD_MSG &
|
34 | _CAN_CONFIG_DBL_BUFFER_ON &
|
35 | _CAN_CONFIG_VALID_XTD_MSG;
|
36 |
|
37 | SJW = 1;
|
38 | BRP = 3;
|
39 | Phase_Seg1 = 7;
|
40 | Phase_Seg2 = 8;
|
41 | Prop_Seg = 7;
|
42 |
|
43 | init_flag = _CAN_CONFIG_SAMPLE_THRICE &
|
44 | _CAN_CONFIG_PHSEG2_PRG_ON &
|
45 | _CAN_CONFIG_STD_MSG &
|
46 | _CAN_CONFIG_DBL_BUFFER_ON &
|
47 | _CAN_CONFIG_VALID_STD_MSG &
|
48 | _CAN_CONFIG_LINE_FILTER_OFF;
|
49 |
|
50 | send_flag = _CAN_TX_PRIORITY_0 &
|
51 | _CAN_TX_STD_FRAME &
|
52 | _CAN_TX_NO_RTR_FRAME;
|
53 |
|
54 | read_flag = 0;
|
55 |
|
56 | //
|
57 | // Initialise CAN module
|
58 | //
|
59 | CANInitialize(SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg, init_flag);
|
60 | //
|
61 | // Set CAN CONFIG mode
|
62 | //
|
63 | CANSetOperationMode(_CAN_MODE_CONFIG, 0xFF);
|
64 |
|
65 | mask = -1;
|
66 | //
|
67 | // Set all MASK1 bits to 1's
|
68 | //
|
69 | CANSetMask(_CAN_MASK_B1, mask, _CAN_CONFIG_STD_MSG);
|
70 |
|
71 | //
|
72 | // Set all MASK2 bits to 1's
|
73 | //
|
74 | CANSetMask(_CAN_MASK_B2, mask, _CAN_CONFIG_STD_MSG);
|
75 | // mask
|
76 | // Set id of filter B1_F1 to 3
|
77 | //
|
78 | CANSetFilter(_CAN_FILTER_B1_F1, 0x3,_CAN_CONFIG_STD_MSG); // receive messages with identifier = 3
|
79 | CANSetFilter(_CAN_FILTER_B1_F2, 0x4,_CAN_CONFIG_STD_MSG); // receive messages with identifier = 4
|
80 | CANSetFilter(_CAN_FILTER_B2_F1, 0x5,_CAN_CONFIG_STD_MSG); // receive messages with identifier = 5
|
81 | CANSetFilter(_CAN_FILTER_B2_F2, 0x6,_CAN_CONFIG_STD_MSG); // receive messages with identifier = 6
|
82 | CANSetFilter(_CAN_FILTER_B2_F3, 0x7,_CAN_CONFIG_STD_MSG); // receive messages with identifier = 7
|
83 | CANSetFilter(_CAN_FILTER_B2_F4, 0x8,_CAN_CONFIG_STD_MSG); // receive messages with identifier = 8
|
84 | //
|
85 | // Set CAN module to NORMAL mode
|
86 | //
|
87 | CANSetOperationMode(_CAN_MODE_NORMAL, 0xFF);
|
88 | RxTx_Data[0] = 9;
|
89 | PORTC = 0x2;
|
90 | //CANWrite(Rx_ID, RxTx_Data, 1, send_flag);
|
91 | for(;;) // Endless loop
|
92 | {
|
93 | //
|
94 | // Wait until a request is received
|
95 | //
|
96 | dt = 0;
|
97 | while(!dt) dt = CANRead(&Rx_ID, RxTx_Data, &len, &read_flag);
|
98 | if(Rx_ID == 507)
|
99 | {
|
100 | PORTC = 0xFF;
|
101 | }
|
102 | else
|
103 | {PORTC = 0x1;}
|
104 | }
|
105 | }
|