📄 mcp2510.lst
字号:
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE MCP2510
OBJECT MODULE PLACED IN MCP2510.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE MCP2510.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /************************************************************************************\
2 MCP2510 CAN总线试验演示程序2003.10
3 \************************************************************************************/
4
5
6 #include "MCP2510.h"
7 #include "def.h"
8 #include "regx51.h"
9
10 /********************** MCP2510 Pin *********************************/
11 //#define MCP2510_CS 0x4 //EXIO2
12
13 /********************** MCP2510 Instruction *********************************/
14 #define MCP2510INSTR_RESET 0xc0
15 #define MCP2510INSTR_READ 0x03
16 #define MCP2510INSTR_WRITE 0x02
17 #define MCP2510INSTR_RTS 0x80 //request to send
18 #define MCP2510INSTR_RDSTAT 0xa0 //read status
19 #define MCP2510INSTR_BITMDFY 0x05 //bit modify
20
21
22 //#define MCP2510_Enable() do{CLREXIOBIT(MCP2510_CS);}while(0)
23 //#define MCP2510_Disable() do{SETEXIOBIT(MCP2510_CS); Delay(1);}while(0)
24
25 MCP2510_Enable()
26 {P1_0=0;}
27
28 MCP2510_Disable()
29 {P1_0=1;}
30
31 void MCP2510_Reset()
32 {
33 1 MCP2510_Enable();
34 1
35 1 SendSIOData(MCP2510INSTR_RESET);
36 1
37 1 MCP2510_Disable();
38 1 }
39
40 void MCP2510_Write(int address, int value)
41 {
42 1 MCP2510_Enable();
43 1
44 1 SendSIOData(MCP2510INSTR_WRITE);
45 1 SendSIOData((unsigned char)address);
46 1 SendSIOData((unsigned char)value);
47 1
48 1 MCP2510_Disable();
49 1 }
50
51 unsigned char MCP2510_Read(int address)
52 {
53 1 unsigned char result;
54 1
55 1 MCP2510_Enable();
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 2
56 1
57 1 SendSIOData(MCP2510INSTR_READ);
58 1 SendSIOData((unsigned char)address);
59 1
60 1 //SendSIOData(0);
61 1 result=ReadSIOData();
62 1
63 1 MCP2510_Disable();
64 1
65 1 return result;
66 1 }
67
68 unsigned char MCP2510_ReadStatus()
69 {
70 1 unsigned char result;
71 1
72 1 MCP2510_Enable();
73 1
74 1 SendSIOData(MCP2510INSTR_RDSTAT);
75 1
76 1 result=ReadSIOData();
77 1
78 1 MCP2510_Disable();
79 1
80 1 return result;
81 1 }
82
83 void MCP2510_WriteBits(int address,int value,int mask)
84 { int baddress,bvalue,bmask;
85 1 baddress=address;
86 1 bvalue=value;
87 1 bmask=mask;
88 1 MCP2510_Enable();
89 1
90 1 SendSIOData(MCP2510INSTR_BITMDFY);
91 1 SendSIOData((unsigned char)baddress);
92 1 SendSIOData((unsigned char)bmask);
93 1 SendSIOData((unsigned char)bvalue);
94 1
95 1 MCP2510_Disable();
96 1 }
97
98
99 void MCP2510_SetBandRate(CanBandRate bandrate, BOOL IsBackNormal)
100 {
101 1 //
102 1 // Bit rate calculations.
103 1 //
104 1 //Input clock fre=16MHz
105 1 // In this case, we'll use a speed of 125 kbit/s, 250 kbit/s, 500 kbit/s.
106 1 // If we set the length of the propagation segment to 7 bit time quanta,
107 1 // and we set both the phase segments to 4 quanta each,
108 1 // one bit will be 1+7+4+4 = 16 quanta in length.
109 1 //
110 1 // setting the prescaler (BRP) to 0 => 500 kbit/s.
111 1 // setting the prescaler (BRP) to 1 => 250 kbit/s.
112 1 // setting the prescaler (BRP) to 3 => 125 kbit/s.
113 1 //
114 1 // If we set the length of the propagation segment to 3 bit time quanta,
115 1 // and we set both the phase segments to 1 quanta each,
116 1 // one bit will be 1+3+2+2 = 8 quanta in length.
117 1 // setting the prescaler (BRP) to 0 => 1 Mbit/s.
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 3
118 1
119 1 // Go into configuration mode
120 1 MCP2510_Write(MCP2510REG_CANCTRL, MODE_CONFIG);
121 1
122 1 switch(bandrate){
123 2 case BandRate_125kbps:
124 2 MCP2510_Write(CNF1, SJW1|BRP4); //Synchronization Jump Width Length =1 TQ
125 2 MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
126 2 MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
127 2 break;
128 2 case BandRate_250kbps:
129 2 MCP2510_Write(CNF1, SJW1|BRP2); //Synchronization Jump Width Length =1 TQ
130 2 MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
131 2 MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
132 2 break;
133 2 case BandRate_500kbps:
134 2 MCP2510_Write(CNF1, SJW1|BRP1); //Synchronization Jump Width Length =1 TQ
135 2 MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG4<<3)|SEG7); // Phase Seg 1 = 4, Prop Seg = 7
136 2 MCP2510_Write(CNF3, SEG4);// Phase Seg 2 = 4
137 2 break;
138 2 case BandRate_1Mbps:
139 2 MCP2510_Write(CNF1, SJW1|BRP1); //Synchronization Jump Width Length =1 TQ
140 2 MCP2510_Write(CNF2, BTLMODE_CNF3|(SEG3<<3)|SEG2); // Phase Seg 1 = 2, Prop Seg = 3
141 2 MCP2510_Write(CNF3, SEG2);// Phase Seg 2 = 1
142 2 break;
143 2 }
144 1
145 1 if(IsBackNormal){
146 2 //Enable clock output
147 2 MCP2510_Write(CLKCTRL, MODE_LOOPBACK | CLKEN | CLK1);
148 2 }
149 1 }
150
151
152 /*******************************************\
153 * 序列读取MCP2510数据 *
154 \*******************************************/
155 /*unsigned char MCP2510_SRead( int address)
156 { unsigned char value[8];
157 //int i;
158
159 MCP2510_Enable();
160 SendSIOData(MCP2510INSTR_READ);
161 SendSIOData((unsigned char)address);
162 //for(i=0;i<nlength;i++)
163 value[0]=ReadSIOData();
164 MCP2510_Disable();
165 return value[0];
166 }*/
167
168
169 /*******************************************\
170 * 序列写入MCP2510数据 *
171 \*******************************************/
172 void MCP2510_Swrite(int address,unsigned char value,unsigned char nlength)
173 { //int address;
174 1 int i;
175 1 unsigned char IDarry[1];
176 1 IDarry[0]=value;
177 1 MCP2510_Enable();
178 1
179 1 SendSIOData(MCP2510INSTR_WRITE);
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 4
180 1 SendSIOData((unsigned char)address);
181 1
182 1 for (i=0; i < nlength; i++) {
183 2 SendSIOData(IDarry[i]);
184 2 //pdata++;
185 2 }
186 1 MCP2510_Disable();
187 1 }
188
189 /*******************************************\
190 * 读取MCP2510 CAN总线ID *
191 * 参数: address为MCP2510寄存器地址*
192 * can_id为返回的ID值 *
193 * 返回值 *
194 * TRUE,表示是扩展ID(29位) *
195 * FALSE,表示非扩展ID(11位) *
196 \*******************************************/
197 /*BOOL MCP2510_Read_Can_ID( int address, U32* can_id)
198 {
199 U32 tbufdata;
200 unsigned char* p=(unsigned char*)&tbufdata;
201
202 MCP2510_SRead(address, p, 4);
203 *can_id = (tbufdata<<3)|((tbufdata>>13)&0x7);
204 *can_id &= 0x7ff;
205
206 if ( (p[MCP2510LREG_SIDL] & TXB_EXIDE_M) == TXB_EXIDE_M ) {
207 *can_id = (*can_id<<2) | (p[MCP2510LREG_SIDL] & 0x03);
208 *can_id <<= 16;
209 *can_id |= tbufdata>>16;
210 return TRUE;
211 }
212 return FALSE;
213 }*/
214
215 /***********************************************************\
216 * 读取MCP2510 接收的数据 *
217 * 参数: nbuffer为第几个缓冲区可以为3或者4 *
218 * can_id为返回的ID值 *
219 * rxRTR表示是否是RXRTR *
220 * data表示读取的数据 *
221 * dlc表示data length code *
222 * 返回值 *
223 * TRUE,表示是扩展总线 *
224 * FALSE,表示非扩展总线 *
225 \***********************************************************/
226 /*BOOL MCP2510_Read_Can(U8 nbuffer, BOOL* rxRTR, U32* can_id, U8* data , U8* dlc)
227 {
228
229 U8 mcp_addr = (nbuffer<<4) + 0x31, ctrl;
230 BOOL IsExt;
231
232 IsExt=MCP2510_Read_Can_ID( mcp_addr, can_id);
233
234 ctrl=MCP2510_Read(mcp_addr-1);
235 *dlc=MCP2510_Read( mcp_addr+4);
236 if ((ctrl & 0x08)) {
237 *rxRTR = TRUE;
238 }
239 else{
240 *rxRTR = FALSE;
241 }
C51 COMPILER V7.50 MCP2510 07/11/2005 20:13:03 PAGE 5
242 *dlc &= DLC_MASK;
243 MCP2510_SRead(mcp_addr+5, data, *dlc);
244
245 return IsExt;
246 }*/
247
248
249 /***********************************************************\
250 * 写入MCP2510 发送的数据 *
251 * 参数: nbuffer为第几个缓冲区可以为0、1、2 *
252 * ext表示是否是扩展总线 *
253 * can_id为返回的ID值 *
254 * rxRTR表示是否是RXRTR *
255 * data表示读取的数据 *
256 * dlc表示data length code *
257 * FALSE,表示非扩展总线 *
258 \***********************************************************/
259 /*void MCP2510_Write_Can( U8 nbuffer, BOOL ext, U32 can_id, BOOL rxRTR, U8* data,U8 dlc )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -