winnie 发表于 2009-3-20 16:47:21

CAN总线

我使用PIC18F248单片机,请问怎么调试CAN,这个型号的单片机能不能给自己发数据,该怎么做?如果可以给自己发送数据是不是要接PCA82C250驱动。
驱动PCA82C250的Rs脚是不是接地就可以了?

winnie 发表于 2009-3-20 16:47:33

#include
#include "../var.h"
#include "../can/caninit.h"

FLAGS Flags;

#pragma interrupt Isr
#pragma code low_ISR = 0x18
void low_ISR()
{
_asm
goto Isr
_endasm
}

#pragma code
void Isr()
{
if(PIR3bits.RXB0IF)
{
Flags.bits.CAN_FLAG = 1;
PIR3bits.RXB0IF = 0;
RXB0CONbits.RXFUL = 0;
}
}

void main(void)
{
INTCON = 0x00;
ClrWdt();
ReadIDFlag.Size16 = 1000;
WriteIDFlag.Size16 = 2000;
InitCAN();
INTCON = 0xC0;
ClrWdt();

while(1)
{
TXB0CONbits.TXREQ = 1;
while(PIR3bits.TXB0IF != 1) ClrWdt();
PIR3bits.TXB0IF = 0;
while(Flags.bits.CAN_FLAG == 0) ClrWdt();
Flags.bits.CAN_FLAG = 0;

TXB0D0 = RXB0D0 + 1;
TXB0D1 = RXB0D1 + 1;
TXB0D2 = RXB0D2 + 1;
TXB0D3 = RXB0D3 + 1;
TXB0D4 = RXB0D4 + 1;
TXB0D5 = RXB0D5 + 1;
TXB0D6 = RXB0D6 + 1;
TXB0D7 = RXB0D7 + 1;
}
}

winnie 发表于 2009-3-20 16:48:23

上面是1号机的main.c

2号机的main.c
#include <p18f248.h>
#include "../var.h"
#include "../can/caninit.h"

FLAGS Flags;

#pragma interrupt Isr
#pragma code low_ISR = 0x18
void low_ISR()
{
_asm
goto Isr
_endasm
}

#pragma code
void Isr()
{
if(PIR3bits.RXB0IF)
{
Flags.bits.CAN_FLAG = 1;
PIR3bits.RXB0IF = 0;
RXB0CONbits.RXFUL = 0;
}
}

void main(void)
{
INTCON = 0x00;
ClrWdt();
ReadIDFlag.Size16 = 1000;
WriteIDFlag.Size16 = 2000;
InitCAN();

while(1)
{
while(Flags.bits.CAN_FLAG == 0) ClrWdt();
Flags.bits.CAN_FLAG = 0;

TXB0D0 = RXB0D0;
TXB0D1 = RXB0D1;
TXB0D2 = RXB0D2;
TXB0D3 = RXB0D3;
TXB0D4 = RXB0D4;
TXB0D5 = RXB0D5;
TXB0D6 = RXB0D6;
TXB0D7 = RXB0D7;

TXB0CONbits.TXREQ = 1;
while(PIR3bits.TXB0IF != 1);
ClrWdt();
PIR3bits.TXB0IF = 0;
}
}

winnie 发表于 2009-3-20 16:49:00

公共的var.h
#ifndef VAR_H
#define VAR_H

typedef union _word
{
unsigned char Size8;
unsigned intSize16;
}WORD;

typedef union _flags
{
struct
{
unsigned None:0;
unsigned CAN_MODE:6;
unsigned CAN_FLAG:7;
}bits;
unsigned char Byte;
}FLAGS;

extern WORD ReadIDFlag;
extern WORD WriteIDFlag;

#endif

winnie 发表于 2009-3-20 16:49:16

CAN的初始化文件caninit.c
#include <p18f248.h>
#include "../var.h"
#include "caninit.h"

WORD ReadIDFlag;
WORD WriteIDFlag;

void InitCAN(void)
{
ReadIDFlag.Size16 = ReadIDFlag.Size16 << 5;
WriteIDFlag.Size16 = WriteIDFlag.Size16 << 5;

TRISB = 0x08;
CANCON = 0x80;

while(CANSTAT & 0x80 == 0);
ClrWdt();

BRGCON1 = 0x01;
BRGCON2 = 0x90;
BRGCON3 = 0x42;

TXB0CON = 0x03;
TXB0SIDH = WriteIDFlag.Size8;
TXB0SIDL = WriteIDFlag.Size8 & 0xE0;
TXB0DLC = 0x08;

TXB0D0 = 0x00;
TXB0D1 = 0x01;
TXB0D2 = 0x02;
TXB0D3 = 0x03;
TXB0D4 = 0x04;
TXB0D5 = 0x05;
TXB0D6 = 0x06;
TXB0D7 = 0x07;

RXB0SIDH = ReadIDFlag.Size8;
RXB0SIDL = ReadIDFlag.Size8 & 0xE0;
RXB0CON = 0x20;
RXB0DLC = 0x08;

RXB0D0 = 0;
RXB0D1 = 0;
RXB0D2 = 0;
RXB0D3 = 0;
RXB0D4 = 0;
RXB0D5 = 0;
RXB0D6 = 0;
RXB0D7 = 0;

RXF0SIDH = ReadIDFlag.Size8;
RXF0SIDH = ReadIDFlag.Size8 & 0xE0;
RXM0SIDH = 0x00;
RXM0SIDL = 0x00;

CIOCON = 0x00;

#ifdef CANBUS_DEBUG

CANCON = 0x40; //自检方式
while(CANSTAT & 0x40 == 0);

#else

CANCON = 0x00; //工作方式
while(CANSTAT & 0xE0 != 0);

#endif

ClrWdt();

PIR3 = 0x00;
PIE3 = 0x01;
IPR3 = 0x01;
}

winnie 发表于 2009-3-20 16:49:50

问题已解决
在2号机的程序有问题改为

#include <p18f248.h>
#include "../var.h"
#include "../can/caninit.h"

FLAGS Flags;

#pragma interrupt Isr
#pragma code low_ISR = 0x18
void low_ISR()
{
_asm
goto Isr
_endasm
}

#pragma code
void Isr()
{
if(PIR3bits.RXB0IF)
{
Flags.bits.CAN_FLAG = 1;
PIR3bits.RXB0IF = 0;
RXB0CONbits.RXFUL = 0;
}
}

void main(void)
{
INTCON = 0x00;
ClrWdt();
ReadIDFlag.Size16 = 1000;
WriteIDFlag.Size16 = 2000;
InitCAN();

Flags.bits.CAN_FLAG = 0;
while(1)
{
while(Flags.bits.CAN_FLAG == 0) ClrWdt();
Flags.bits.CAN_FLAG = 0;

TXB0D0 = RXB0D0;
TXB0D1 = RXB0D1;
TXB0D2 = RXB0D2;
TXB0D3 = RXB0D3;
TXB0D4 = RXB0D4;
TXB0D5 = RXB0D5;
TXB0D6 = RXB0D6;
TXB0D7 = RXB0D7;

TXB0CONbits.TXREQ = 1;
while(PIR3bits.TXB0IF != 1);
ClrWdt();
PIR3bits.TXB0IF = 0;
}
}
就可以了
页: [1]
查看完整版本: CAN总线