英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 4050|回复: 0
打印 上一主题 下一主题

简单使用PIC18F458调用CAN的C代码

[复制链接]
跳转到指定楼层
1#
发表于 2009-4-25 15:17:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1、
先在microchip网站上找到00738.zip,这个包中有
can18xx8.hcan18xx8.c,将其复制到一个你要编写程度的子目录中,例如C:\testcan下,这个时候这个东东是不能直接用的。
2、
下载一个MPLAB 7.10,直接安装(要求直接下载)
3、
再下载一个PICC18 (HI-TECH),要正版哦(支持正版,买不起的想其它办法吧)
4、在你的头文件上写上如下的样式:
#include "stdio.h"
#include "can18xx8.h"
#include "pic18fxx8.h"//可能是“pic.h”不太好包含,因此常会报错,直接包含克服之
5、然后设置好PICC和MPLAB,如果MPLAB上没有PICC18,则到PICC的网上下一个安装软件,可以直接运行安装就有了。
6、按照工程的基本要求把你的源代码、添加上两个库(不加也可以)
然后就可以写如下代码了:下面是一个完整版,可以直接使用microchip的库函数,本程序只是仿真调试过,未在多个芯片通信过,仍在制作中。
#include "stdio.h"
#include "can18xx8.h"
#include "pic18fxx8.h"
unsigned long NewMessage;
BYTE NewMessageData[8];
BYTE MessageData[8];
BYTE NewMessageLen;
enum CAN_RX_MSG_FLAGS NewMessageFlags=1;
BYTE RxFilterMatch;
void main()
{
//int a;
//int b;
CANInitialize(1, 5, 7, 6, 2, CAN_CONFIG_VALID_XTD_MSG);
while(1)
{
// Application specific logic here
// Check for CAN message
if ( CANIsRxReady() )
{
CANReceiveMessage(&NewMessage,NewMessageData,&NewMessageLen,&NewMessageFlags);
if ( NewMessageFlags & CAN_RX_OVERFLOW )
{
// Rx overflow occurred; handle it
}
if ( NewMessageFlags & CAN_RX_INVALID_MSG )
{
// Invalid message received; handle it
}
if ( NewMessageFlags & CAN_RX_XTD_FRAME )
{
// Extended Identifier received; handle it
}
else
{
// Standard Identifier received.
}
if ( NewMessageFlags & CAN_RX_RTR_FRAME )
{
// RTR frame received
}
else
{
// Regular frame received.
}
// Extract receiver filter match, if it is to be used
RxFilterMatch = NewMessageFlags & CAN_RX_FILTER_BITS;
}
// Process received message
// Transmit a message due to previously received message or
// due to application logic itself.
if ( CANIsTxReady() )
{
MessageData[0] = 0x01;
CANSendMessage( 0x02,MessageData,1,CAN_TX_PRIORITY_0




&CAN_TX_STD_FRAME &

CAN_TX_NO_RTR_FRAME);
}
// Other application specific logic
} // Do this forever
// End of program
}
以上均是参考<PIC18C CAN Routines in ‘C’.pdf>文档并进行了修正,由于中间总是报错,为了大家让CAN更容易
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-4-27 21:22 , Processed in 0.050861 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表