谢谢大家帮忙啦。下面分别是发送单片机和接收单片机的程序,肯请帮我看看哪里出错了, //发送数据,第一片PIC18F2580的发送数据程序 #include "p18f2580.h" char CAN_TX_Adress_H,CAN_TX_Adress_L;
void initial() { // TRISC=0; INTCON=0x00; ADCON1=0X07; PIE1=0; PIE2=0; PIE3=0; }
void initcan() { TXB0CONbits.TXREQ=0; CAN_TX_Adress_H=0x33; CAN_TX_Adress_L=0xe0; CAN_TX_Adress_L=CAN_TX_Adress_L&0xe0; TRISB=(TRISB|0x08)&0XFB; CANCON=0X80; while((CANSTAT&0X80)==0) { ; } BRGCON1=0; BRGCON2=0X90; BRGCON3=0X42;
TXB0CON=0X03; TXB0SIDL=CAN_TX_Adress_L&0xe0; TXB0SIDH=CAN_TX_Adress_H; TXB0DLC=0X08; TXB0D0=0; TXB0D1=0; TXB0D2=0; TXB0D3=0; TXB0D4=0; TXB0D5=0; TXB0D6=0; TXB0D7=0;
CANCON=0X00; while((CANSTAT&0XE0)!=0) { ; } }
void main() { initial(); initcan(); RCONbits.IPEN=1; INTCON=INTCON|0xc0; TXB0CONbits.TXREQ=1; while(1) { TXB0CONbits.TXREQ=0; if(TXB0CONbits.TXABT==1) {
TXB0D0=0xff; TXB0D1=0xff; TXB0D2=0xff; TXB0D3=0xff; TXB0D4=0xff; TXB0D5=0xff; TXB0D6=0xff; TXB0D7=0xff;
TXB0CONbits.TXREQ=1; } } } //收数据,另一片18F2580的数据接收程序 #include "p18f2580.h" char CAN_RX_Adress_H,CAN_RX_Adress_L; int CAN_FLAG; int i; void InterruptHandlerHigh(void );
void initial() { INTCON=0x00; ADCON1=0X07; PIE1=0; PIE2=0; PIE3=0; i=0; CAN_FLAG=0; }
void initcan() { TXB0CONbits.TXREQ=0; CAN_RX_Adress_H=0x33; CAN_RX_Adress_L=0xe0; CAN_RX_Adress_L=CAN_RX_Adress_L&0xe0; TRISB=(TRISB|0x08)&0XFB; CANCON=0X80; while((CANSTAT&0X80)==0) { ; } BRGCON1=0; BRGCON2=0X90; BRGCON3=0X42;
RXB0SIDL=CAN_RX_Adress_L&0xe0; RXB0SIDH=CAN_RX_Adress_H; RXB0CON=0x20; RXB0DLC=0x08; RXB0D0=0; RXB0D1=0; RXB0D2=0; RXB0D3=0; RXB0D4=0; RXB0D5=0; RXB0D6=0; RXB0D7=0; RXF0SIDH=CAN_RX_Adress_H; RXF0SIDL=CAN_RX_Adress_L&0xE0; RXM0SIDH=0xFF; RXM0SIDL=0xE0;
CANCON=0X00; while((CANSTAT&0XE0)!=0) { ; }
PIR3=0; PIE3=PIE3|0x01; IPR3=IPR3|0x01; }
# pragma code InterruptVectorHigh=0x08 void InterruptVectorHigh (void) { _asm goto InterruptHandlerHigh _endasm }
# pragma code # pragma interrupt InterruptHandlerHigh void InterruptHandlerHigh( ) { if(PIR3bits.RXB0IF==1) { CAN_FLAG=1; PIR3bits.RXB0IF=0; RXB0CONbits.RXFUL=0; } }
void main() { initial(); initcan(); RCONbits.IPEN=1; INTCON=INTCON|0xc0; while(1) { if(CAN_FLAG=1) { i=++i; CAN_FLAG=0; } } } 急死人啦,各位一定要帮忙啊 |