[AVR单片机] 有调试MCP2510 CAN总线的大侠吗?。。需要一个调试通过的CAN总线程序

[复制链接]
2301|2
 楼主| zhao3462910 发表于 2013-4-19 09:19 | 显示全部楼层 |阅读模式
需要一个调试通过的CAN总线程序,,谢谢。。有可以指导我下。。QQ122503892  
qin552011373 发表于 2013-4-20 09:31 | 显示全部楼层
你可以先调试  遇到问题再来这里  自然会有人帮你
 楼主| zhao3462910 发表于 2013-5-8 09:55 | 显示全部楼层
qin552011373 发表于 2013-4-20 09:31
你可以先调试  遇到问题再来这里  自然会有人帮你
  1. #include "MCP2510.h"
  2. #include "config.h"

  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. #define ulong unsigned long

  6. uchar com_recv[20];
  7. uchar com_len=0;
  8. uchar k=0;
  9. uint com_js=0,com_fs=0;
  10. uchar Rdata[20];

  11. void Put_Char(uchar ch);

  12. //************************************SPI写子程序*******************************
  13. void SPISendByte(uchar Cmd)
  14. {
  15. uchar i,Status=0;
  16. CANCSDW;
  17. DelayUs(50);
  18. for (i=0;i<8;i++)
  19. { //in...
  20. CANSCKDW;
  21. DelayUs(5);
  22. if((Cmd&0x80)==0x80) //高位先出
  23. CANSIUP;
  24. else
  25. CANSIDW;
  26. DelayUs(5);
  27. CANSCKUP;
  28. Cmd=Cmd<<1;
  29. DelayUs(10);
  30. }
  31. }
  32. //************************************SPI读程序*********************************
  33. unsigned int SPIRecvByte(void)
  34. {
  35. uchar i,Status=0;
  36. CANCSDW;
  37. for (i=0;i<8;i++)
  38. { //in...

  39. CANSCKUP;
  40. DelayUs(5);

  41. Status=Status<<1;

  42. if (CHKSO)
  43. {
  44. Status |=0x01;
  45. }
  46. else
  47. {
  48. Status &=0xFE;
  49. }

  50. DelayUs(5);
  51. CANSCKDW;
  52. DelayUs(5);
  53. }

  54. return (Status);
  55. }

  56. //********************************MCP复位程序************************************
  57. void MCP2510_Reset(void)
  58. {
  59. CANCSDW;
  60. SPISendByte(0xc0);
  61. CANCSUP;
  62. }
  63. //*******************************CAN读数据**************************************
  64. uchar CAN_Read_SS(uchar address)
  65. {
  66. uchar ret;
  67. CANCSUP;
  68. DelayUs(4);

  69. SPISendByte(0x03);
  70. SPISendByte(address);
  71. ret = SPIRecvByte();

  72. CANCSUP;
  73. return ret;
  74. }
  75. //********************************CAN写数据*************************************
  76. void CAN_Write_SS(uchar address,uchar Value)
  77. {
  78. CANCSUP;
  79. DelayUs(4);
  80. SPISendByte(0x02);
  81. SPISendByte(address);
  82. SPISendByte(Value);

  83. CANCSUP;
  84. DelayUs(5);
  85. }

  86. //***********读取 MCP2510的状态(包括发送接收中断标志位和各请求发送位)*********
  87. uchar MCP2510_ReadStatus()
  88. {
  89. uchar result;
  90. CANCSDW;
  91. SPISendByte(0xa0);
  92. result = SPIRecvByte();
  93. CANCSUP;
  94. return result;
  95. }
  96. //******************************************************************************
  97. /*--- 修改寄存器的值 ---*/
  98. //mask为掩码
  99. void CAN_Set_Var(uchar address,uchar mask,uchar dat)
  100. {
  101. CANCSUP;
  102. DelayUs(10);
  103. CANCSDW;

  104. SPISendByte(0x05);
  105. SPISendByte(address);//64页
  106. SPISendByte(mask);
  107. SPISendByte(dat);

  108. CANCSUP;
  109. }
  110. /*******************************************************************************************************
  111. 检测是否发送完毕,是否已经可以进行发送
  112. *******************************************************************************************************/
  113. unsigned char mcp_transmit_busy(unsigned send_buf)
  114. {
  115. unsigned char temp;
  116. switch(send_buf)
  117. {
  118. case 0:
  119. {
  120. temp=MCP2510_ReadStatus();//读取当前的MCP2510发送状态
  121. if(temp&0x04)
  122. {
  123. return 1;
  124. }
  125. return 0;
  126. break;
  127. }
  128. case 1:
  129. {
  130. temp=MCP2510_ReadStatus();//读取当前的MCP2510发送状态
  131. if(temp&0x10)
  132. {
  133. return 1;
  134. }
  135. return 0;
  136. break;
  137. }
  138. case 2:
  139. {
  140. temp=MCP2510_ReadStatus();//读取当前的MCP2510发送状态
  141. if(temp&0x40)
  142. {
  143. return 1;
  144. }
  145. return 0;
  146. break;
  147. }
  148. }
  149. }
  150. //******************************CAN初始化***************************************
  151. void Init_Can()
  152. {
  153. unsigned char i,j,a;
  154. CANCSDW;
  155. MCP2510_Reset();
  156. DelayMs(10);
  157. CANCSUP;

  158. //设置为配置模式
  159. CAN_Set_Var(0x0F,0xE0,0x80);

  160. //TJA1050 最低波特率为60K
  161. //设置通信的速率 16M 晶振 125k
  162. CAN_Write_SS(0x2A, 0x03);
  163. CAN_Write_SS(0x29, 0xB9);
  164. CAN_Write_SS(0x28, 0x04);

  165. //0x00 仅接收标准或扩展标识符
  166. //0x60 关闭接收所有数据
  167. CAN_Write_SS(0x60,0x60); //接收缓冲器0 接收所有数据报文

  168. //滤波
  169. CAN_Write_SS(0x00,0);
  170. CAN_Write_SS(0x01,0);
  171. CAN_Write_SS(0x02,0);
  172. CAN_Write_SS(0x03,0);

  173. //屏蔽
  174. CAN_Write_SS(0x20,0);
  175. CAN_Write_SS(0x21,0);
  176. CAN_Write_SS(0x22,0);
  177. CAN_Write_SS(0x23,0);

  178. //接收数据产生中断
  179. CAN_Write_SS(0x2B,0x01); //接收缓冲器0 满中断使能

  180. //回环模式
  181. CAN_Set_Var(0x0F,0xE0,0x40);
  182. //工作模式
  183. //CAN_Set_Var(0x0F,0xE0,0);

  184. a = TXB0CTRL;
  185. for (i = 0; i < 3; i++)
  186. {
  187. for (j = 0; j < 14; j++)
  188. {
  189. CAN_Write_SS(a, 0);
  190. a++;
  191. }
  192. a += 2; // 我们不清 CANSTAT 和 CANCTRL
  193. }
  194. // 并且两个接收缓冲区清零.
  195. CAN_Write_SS(RXB0CTRL, 0);

  196. }
  197. //*******************************************************************************
  198. //////////////////////////////CAN发送部分////////////////////////////////////////

  199. void CAN_Send_End()
  200. {
  201. uchar i;
  202. for(i=1;i<com_len;i++)
  203. {
  204. CAN_Write_SS(0x30+i,com_recv[i]);
  205. }

  206. CAN_Set_Var(0x30,com_recv[0],com_recv[0]);

  207. do
  208. {
  209. i = CAN_Read_SS(0x30);// 读发送缓存0控制寄存器
  210. i = i & 0x08; // 观察发送请求位
  211. }while(i);
  212. }

  213. /*--- 中断的定义 ---*/
  214. //*********************************CAN接收中断入口******************************
  215. #pragma interrupt_handler int1_isr:3
  216. void int1_isr(void)//CAN
  217. {
  218. uchar Sta_Int,i;
  219. Sta_Int = CAN_Read_SS(0x2C); /* 读中断标志寄存器 */
  220. CAN_Write_SS(0x2C,0x00); /* 清除所有中断标志位 */

  221. /*--- 如果为接收缓冲器0中断 ---*/
  222. if(Sta_Int & 0x01)
  223. {
  224. for(i=0;i<14;i++)
  225. {
  226. Put_Char(CAN_Read_SS(0x60+i));
  227. }

  228. com_fs=com_fs+14;
  229. }
  230. }
  231. //******************************************************************************
  232. void Put_Char(uchar ch)
  233. {
  234. //while ( !( UCSRA & (1<<UDRE)) ) ;
  235. UDR = ch;
  236. //while(TI==0);
  237. //TI = 0;
  238. }
  239. //*******************主程序***************
  240. void main(void)
  241. {
  242. WakeUp();
  243. Init_Can();
  244. com_len=0;
  245. MCP2510_Reset();

  246. CANCSDW; // CS片选拉低
  247. DelayUs(5);
  248. SPISendByte(0x02); // 写指令
  249. SPISendByte(0x66); // 写地址
  250. SPISendByte(0x18); // 写数据

  251. CANCSUP;
  252. DelayUs(5);


  253. CANCSUP;
  254. DelayUs(1);
  255. CANCSDW; // 片选拉低
  256. SPISendByte(0xa0); // 写空操作,产生SCK时钟
  257. ret = SPIRecvByte(); // 读数据

  258. CANCSUP;
  259. NOP();
  260. DelayUs(1);

  261. while(1)
  262. {

  263. DelayMs(1);
  264. CANCSUP;
  265. DelayUs(1);
  266. CANCSDW; // 片选拉低

  267. SPISendByte(0xa0); // 读指令
  268. //SPISendByte(0x66); // 地址
  269. //SPISendByte(0x00); // 写空操作,产生SCK时钟
  270. ret = SPIRecvByte(); // 读数据

  271. CANCSUP;
  272. NOP();
  273. DelayUs(1);
  274. }
  275. }

调试了一段时间,遇到了问题,我是mega16+MCP2515..用的模拟SPI。。在测试回环模式时候,我写入数据之后,然后在读数据。。SCK,cs,SI.SO引脚上都有波形。但是读处理的全不是FF。。不知道什么原因?源程序代码如上:


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:物联网改变世界,提高生活品质!

342

主题

1189

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部