英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
用248做CAN通信的程序
[打印本页]
作者:
winnie
时间:
2009-3-17 14:28
标题:
用248做CAN通信的程序
发送模块:
#include
#include
//define
configure bit of PIC18F248
__CONFIG(1, HS & OSCSDIS);
__CONFIG(2, PWRTEN & BORV20 & BORDIS & WDTPS128 & WDTDIS);
__CONFIG(4, BKBUGEN & LVPDIS & STVREN);
__CONFIG(5, UNPROTECT);
__CONFIG(7, TRU);
//define
bit instruction
#define bitset(var,bitno) ((var)|=1<<(bitno))
#define bitclr(var,bitno) ((var)&=~(1<<(bitno)))
volatile char DATA;
void interrupt low_priority LOW_ISR(void);
void interrupt HI_ISR(void);
void main(void)
{
DATA=0;
LVDEN=0;
ADCON1=0xF7;
TRISA=0x00;
RA0=0;
RA1=1;
RA2=1;
RA3=1;
IPEN=1;
INTCON=0xC0;
PIE3=0xFF;
IPR3=0x00;
PORTB=0x00;
LATB=0x00;
TRISB=0x08;
REQOP2=1;
ENDRHI=1;
CANCAP=0;
//AUSART
initial
RCIE=1;
RCIP=1;
SPBRG=129;
BRGH=1;
SPEN=1;
RX9=0;
CREN=1;
ADDEN=0;
TRISC7=1;
TRISC6=0;
TXIE=0;
TX9=0;
SYNC=0;
TXEN=1;
TXREG=0xEB;
NOP();
while (!TXIF);
TXREG=0x90;
NOP();
while (!TXIF);
//TXB0
TXB0PRI1=1;
TXB0PRI0=1;
TXB0SIDH=0b10101111;
TXB0SIDL=0b11100000;
TXB0DLC=00000001;
//RXB0
RXB0M1=0;
RXB0M0=0;
RXF0SIDH=0b10101111;
RXF0SIDL=0b11100000;
RXM0SIDH=0xFF;
RXM0SIDL=0xFF;
//BAUDRAT
BRGCON1=0b00111110;
BRGCON2=0b10010000;
BRGCON3=0b00000010;
//NORMAL
MODE
REQOP2=0;
REQOP1=0;
REQOP0=0;
ABAT=0;
LOOP:
if (DATA==0x01)
{
DATA=0x00;
TXB0D0=0x08;
TXB0REQ=1;
}
if (DATA==0x02)
{
DATA=0x00;
TXB0D0=0x09;
TXB0REQ=1;
}
goto LOOP;
}
void interrupt HI_ISR(void)
{
if((RCIE)&&(RCIF)&&(RCIP))
{
DATA=RCREG;
if(DATA==0x01)
{
RA2=1;
RA1=0;
}
if(DATA==0x02)
{
RA2=0;
RA1=1;
}
}
}
void interrupt low_priority LOW_ISR(void)
{
RA3=0;
TXB0IF=0;
}
接受模块:
#include
#include
//define
configure bit of PIC18F248
__CONFIG(1, HS & OSCSDIS);
__CONFIG(2, PWRTEN & BORV20 & BORDIS & WDTPS128 & WDTDIS);
__CONFIG(4, BKBUGEN & LVPDIS & STVREN);
__CONFIG(5, UNPROTECT);
__CONFIG(7, TRU);
//define
bit instruction
#define bitset(var,bitno) ((var)|=1<<(bitno))
#define bitclr(var,bitno) ((var)&=~(1<<(bitno)))
void interrupt low_priority LOW_ISR(void);
void interrupt HI_ISR(void);
void main(void)
{
LVDEN=0;
ADCON1=0xF7;
TRISA=0x00;
RA0=0;
RA1=1;
RA2=1;
RA3=1;
IPEN=1;
INTCON=0xC0;
PIE3=0xFF;
IPR3=0x00;
PORTB=0x00;
LATB=0x00;
TRISB=0x08;
REQOP2=1;
ENDRHI=1;
CANCAP=0;
RXB0FUL=0;
//RXB0
RXB0M1=0;
RXB0M0=0;
RXF0SIDH=0b10101111;
RXF0SIDL=0b11100000;
RXM0SIDH=0xFF;
RXM0SIDL=0xFF;
//BAUDRAT
BRGCON1=0b00111110;
BRGCON2=0b10010000;
BRGCON3=0b00000010;
//DATA
// TXB0D0=0x09;
//NORMAL
MODE
REQOP2=0;
REQOP1=0;
REQOP0=0;
ABAT=0;
// TXB0REQ=1;
LOOP:
goto LOOP;
}
void interrupt low_priority LOW_ISR(void)
{
RXB0FUL=0;
RXB0IF=0;
if (RXB0D0==0x08)
{
RA2=1;
RA1=0;
}
if (RXB0D0==0x09)
{
RA1=1;
RA2=0;
}
}
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2