今天调试的时候发数据CAN卡能接受到,但是CAN卡发的数据却接受不到,可能是什么原因?通讯部分程序如下。
void DELAY(void)
{
for(i=5;--i;)
continue;
}
void __attribute__((__interrupt__)) _T1Interrupt(void);
void __attribute__((__interrupt__)) _T4Interrupt(void);
void __attribute__((__interrupt__)) _ADCInterrupt(void);
void __attribute__((__interrupt__)) _INT0Interrupt(void);
//void __attribute__((__interrupt__)) _INT1Interrupt(void);
//void __attribute__((__interrupt__)) _INT2Interrupt(void);
void __attribute__((__interrupt__)) _C1Interrupt(void);
//
void initial(void)
{
IPC0=0X0000;//所有中断源被禁止
IPC1=0X0000;//所有中断源被禁止
IPC2=0X0000;//所有中断源被禁止
IPC3=0X0000;//所有中断源被禁止
IPC4=0X0000;//所有中断源被禁止
IPC5=0X0000;//所有中断源被禁止
PORTEbits.RE0=1;
TRISEbits.TRISE8=1;
IPC0bits.INT0IP=4;
IFS0bits.INT0IF=0;
IEC0bits.INT0IE=1;
INTCON2=0X000E;
//TMR1
T1CON=0X0000;
TMR1=0X0000;
PR1=0XDFFF;
IPC0bits.T1IP=2;
IFS0bits.T1IF=0;
IEC0bits.T1IE=1;
// T1CONbits.TON=1;
//开关输入
TRISEbits.TRISE1=1;
TRISFbits.TRISF6=1;
TRISCbits.TRISC14=1;
TRISCbits.TRISC13=1;
//TMR4
T4CON=0X0000;
TMR4=0X0000;
PR4=0X0FFF;
IPC5bits.T4IP=5;
IFS1bits.T4IF=0;
IEC1bits.T4IE=1;
T4CONbits.TON=1;
//
//AD
TRISBbits.TRISB0=1;
TRISBbits.TRISB1=1;
TRISBbits.TRISB2=1;
ADPCFG=0XFFF8;
ADCON1=0X00E0;
ADCSSL=0X0000;
ADCSSLbits.CSSL0=1;
ADCSSLbits.CSSL1=1;
ADCSSLbits.CSSL2=1;
ADCON3=0X0F05,
ADCON2=0X0408;
IPC2bits.ADIP=3;
IFS0bits.ADIF=0;
IEC0bits.ADIE=1;
ADCON1bits.ADON=1;
ADCON1bits.ASAM=1;
//
TRISEbits.TRISE0=0;
TRISDbits.TRISD2=0;
PR3=0X03E8;
OC3R=0X01F4;
OC3RS=0X01F4;
PWMOUT3();
//
//can set
TRISF=0X0001;
canset1();
canset2();
canset3();
T1CONbits.TON=1;
}
//
//can set
void canset1(void)
{
C1CTRLbits.REQOP=4;
while(C1CTRLbits.OPMODE!=0x4){}
C1RX0CON=0x0000; //
C1RXF0SID=0x0000;
C1RXF0EIDH=0x0000;
C1RXF0EIDL=0x0000;
C1RXM0SID=0x0000;
C1RXM0EIDH=0x0000;
C1RXM0EIDL=0x0000;
C1CTRLbits.CANCKS=0;
C1CFG1bits.SJW=1-1;
C1CFG1bits.BRP=4;
//C1CFG2=0x024c;
C1CFG2bits.PRSEG=3-1;
C1CFG2bits.SEG1PH=2-1;
C1CFG2bits.SAM=0;
C1CFG2bits.SEG2PHTS=1;
C1CFG2bits.SEG2PH=2-1;
C1CFG2bits.WAKFIL=0;
}
//
void canset2(void)
{
C1CTRLbits.REQOP=4; //配置模式
while(C1CTRLbits.OPMODE!=0x4){}
C1CTRLbits.CANCAP=1;
C1TX0CON=0x0003;//最高报文优先级
}
//设置正常工作模式
void canset3(void)
{
C1CTRLbits.REQOP=0; //设置正常工作模式
IPC6bits.C1IP=1;
// C1INTFbits.RX0IF=0;
C1INTF=0X0000; //接收缓存器0 1中断标识
IFS1bits.C1IF=0;
IEC1bits.C1IE=1;
C1INTE=0X0003; //接收缓存器0 1使能中断
while(C1CTRLbits.OPMODE!=0X0000){}
}
//设置正常工作模式2
void canset4(void)
{
C1CTRLbits.REQOP=0; //设置正常工作模式
while(C1CTRLbits.OPMODE!=0X0000){}
}
//
void chudian(void)
{
if(PORTEbits.RE1==1)
{ k1=1;}
else
{ k1=0;}
if(PORTFbits.RF6==1)
{k2=1;}
else
{ k2=0;}
if(PORTCbits.RC14==1)
{k3=1;}
else
{ k3=0;}
if(PORTCbits.RC13==1)
{ k4=1;}
else
{ k4=0;}
}
//
void _INT0Interrupt(void)
{
DELAY();
IFS0bits.INT0IF=0;
IEC0bits.INT0IE=0;
PORTEbits.RE0=0; //故障指示灯亮
IEC0bits.INT0IE=1;
}
//
void fasong(void)
{
C1TX0CONbits.TXREQ=1; //启动发送
while (C1TX0CONbits.TXREQ==1) //等待发送完成
{for (ii=0;ii<200;ii++){}
}
}
//
void _T1Interrupt(void)
{
T1CONbits.TON=0;
IEC0bits.T1IE=0;
IFS0bits.T1IF=0;
//IEC1bits.C1IE=0;
ticount="TICOUNT+1;
TICOUNT=3;
" if (TICOUNT==1)
{
d1fabuffer01="0x0061;
" c1tx0b1="D1fabuffer01;
" c1tx0b2="D1fabuffer02;
" c1tx0b3="D1fabuffer03;
" c1tx0b4="D1fabuffer04;"
canset2();
C1TX0SIDbits.TXIDE=1;
C1TX0SIDbits.SRR=0;
C1TX0SIDbits.SID10_6=0;
C1TX0SIDbits.SID5_0=0;
C1TX0EIDbits.EID17_14=0;
C1TX0EIDbits.EID13_6=385;
C1TX0DLCbits.EID5_0=36;
C1TX0DLCbits.DLC=8;
C1TX0DLCbits.TXRTR=0;
C1TX0DLCbits.TXRB0=1;
C1TX0DLCbits.TXRB1=1;
canset4();
fasong();
}
else if(TICOUNT==2)
{
// d2fabuffer01="0X1000;
//" d2fabuffer02="0X2000;
//" d2fabuffer03="0X3000;
//" d2fabuffer04="0X4000;
" d2fabuffer01="0x0062;
" c1tx0b1="D2fabuffer01;
" c1tx0b2="D2fabuffer02;
" c1tx0b3="D2fabuffer03;
" c1tx0b4="D2fabuffer04;"
canset2();
C1TX0SIDbits.TXIDE=1;
C1TX0SIDbits.SRR=0;
C1TX0SIDbits.SID10_6=0;
C1TX0SIDbits.SID5_0=0;
C1TX0EIDbits.EID17_14=0;
C1TX0EIDbits.EID13_6=385;
C1TX0DLCbits.EID5_0=37;
C1TX0DLCbits.DLC=8;
C1TX0DLCbits.TXRTR=0;
C1TX0DLCbits.TXRB0=1;
C1TX0DLCbits.TXRB1=1;
canset4();
fasong();
}
//else if(TICOUNT==3)
else
{
//DCfabuffer01=0X0123;
//DCfabuffer02=0X0321;
//DCfabuffer03=0X0213;
//DCfabuffer04=0X0312;
dcfabuffer01="0x1000;
" c1tx0b1="DCfabuffer01;
" c1tx0b2="DCfabuffer02;
" c1tx0b3="DCfabuffer03;
" c1tx0b4="DCfabuffer04;"
canset2();
C1TX0SIDbits.TXIDE=1;
C1TX0SIDbits.SRR=0;
C1TX0SIDbits.SID10_6=0; /
C1TX0SIDbits.SID5_0=0;
C1TX0EIDbits.EID17_14=1;
C1TX0EIDbits.EID13_6=64;
C1TX0DLCbits.EID5_0=17;
C1TX0DLCbits.DLC=8;
C1TX0DLCbits.TXRTR=0
C1TX0DLCbits.TXRB0=1;
C1TX0DLCbits.TXRB1=1;
canset4();
fasong();
ticount="0;
}
"
/*
else
{
dpfabuffer01="0X00CA;
" dpfabuffer02="0X00A1;
" dpfabuffer03="0X00C6;
" dpfabuffer04="0X00B1;"
c1tx0b1="DPfabuffer01;
" c1tx0b2="DPfabuffer02;
" c1tx0b3="DPfabuffer03;
" c1tx0b4="DPfabuffer04;"
canset2();
C1TX0SIDbits.TXIDE=0;
C1TX0SIDbits.SRR=0;
C1TX0SIDbits.SID10_6=20;
C1TX0SIDbits.SID5_0=1;
C1TX0DLCbits.DLC=8;
C1TX0DLCbits.TXRTR=0;
C1TX0DLCbits.TXRB0=1;
C1TX0DLCbits.TXRB1=1;
canset4();
fasong();
ticount="0;
}"
*/
C1INTFbits.TX0IF=0;
IEC0bits.T1IE=1;
T1CONbits.TON=1;
// IEC1bits.C1IE=1;
}
//
void _T4Interrupt(void)
{
IEC1bits.T4IE=0;
T4CONbits.TON=0;
IFS1bits.T4IF=0;
chudian();
IEC1bits.T4IE=1;
T4CONbits.TON=1;
}
此帖出自
小平头技术问答
一周热门 更多>