PIC18F45K80 CAN通信实例,可通过模拟调试
2019-10-24 来源:51hei
PIC18F 45K80 CAN 通信实例,可通过模拟调试,注意选编译器,C18是不能通过。
单片机源程序如下:
#pragma config XINST = OFF // CONFIG1L
#pragma config FOSC = INTIO2, FCMEN = OFF, IESO = OFF ,PLLCFG = OFF // CONFIG1H
#pragma config BOREN = OFF, BORV = 0 // CONFIG2L
#pragma config WDTEN = OFF // CONFIG2H
/** I N C L U D E S **************************************************/
#include 'main.h'
unsigned char CAN_RecFLAG=0;
unsigned char Flag50ms = 0;
// AD采样的数据变量
unsigned int VoltageEdlc[5];
unsigned int VoltageBat;
unsigned int Current1;
unsigned int Current2;
unsigned int Temperature1;
unsigned int Temperature2;
void InterruptHandler(void);
#pragma code high_vector=0x08
void high_interrupt (void)
{
_asm GOTO InterruptHandler _endasm
}
#pragma code
#pragma code low_vector=0x18
void low_interrupt (void)
{
_asm GOTO InterruptHandler _endasm
}
#pragma code
#pragma interruptlow InterruptHandler
#pragma interrupt InterruptHandler
void InterruptHandler(void)
{
static unsigned char Tick = 0;
static unsigned char ChannelNo = 0;
// 定时器0
if(INTCONbits.TMR0IF&&INTCONbits.TMR0IE) // timer0 = 10ms
{
INTCONbits.TMR0IF = 0;
TMR0H = 0x63;
TMR0L = 0xBF;
Tick++;
if(Tick ==200)
{
Flag50ms = 1; // 5s发送CAN消息到ECU
Tick = 0;
LATCbits.LATC5 = ~LATCbits.LATC5;
}
}
// CAN接收数据
if(PIR5bits.RXB0IF==1)
{
CAN_RecFLAG=1;
PIR5bits.RXB0IF=0; // 清接收中断标志
RXB0CONbits.RXFUL=0; // 清除标志,接收新报文
}
}
#pragma code
void main(void)
{
unsigned int EDLC_temp[4];
INTCON=0x00;
InitInterOSC();
InitPORT();
InitCan();
Timer0_Init();
// INTCON=0xc0; //使能中断 1100 0000
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
// INTCONbits.GIEL=1;
// TXB0CONbits.TXREQ = 0;
while(1)
{
if(CAN_RecFLAG==1)
{
CAN_RecFLAG = 0;
TXB0D0=1; /* 写发送缓冲器数据区数据初值 */
TXB0D1=2;
TXB0D2=3;
TXB0D3=4;
TXB0D4=5;
TXB0D5=6;
TXB0D6=7;
……………………