prabhakarandhar
Newbie level 2
i did the interface between two pic18f4580 using CAN protocol but i didnt work... i doesnt go to Configure mode please help me
// hitech c
#include<pic18.h>
struct CANMessage
{
unsigned long Address;
unsigned char Data[8];
unsigned char NoOfBytes;
};
struct CANMessage RX_Message, TX_Message, ReturnMessage;
char CANPut(struct CANMessage Message)
{
TXB0D0 = Message.Data[0];
TXB0SIDL = Message.Address<<5&0xff;
TXB0SIDH = Message.Address>>3&0xff;
while(TXB0CONbits.TX0IF!=1);
TXB0CONbits.TX0IF = 0;
}
struct CANMessage CANGet(void)
{
unsigned int Add,Add1;
ReturnMessage.Data[0] = RXB0D0;
Add = RXB0SIDH;
Add1= RXB0SIDL;
Add1= (Add1 >> 5)|(Add<<3);
ReturnMessage.Address = Add1;
}
void Can_isr()
{
if(RXB0IF)
{
RXB0IF =0;
RXB0CONbits.RXFUL =0;
RX_Message = CANGet();
}
}
void Can_Set_Baudrate(void)
{
BRGCON1 = 0X01;
BRGCON2 = 0X90;
BRGCON3 = 0X42;
}
void Can_Set_TxBuffer(void)
{
TXB0CON = 0x03; // Highest Priority
TXB0SIDL = 0xE0;
TXB0SIDH = 0xFF;
TXB0DLC = 0x08; // 8 byte Data length
TXB0D0 = 0X00;
TXB0D1 = 0X01;
TXB0D2 = 0X02;
TXB0D3 = 0X03;
TXB0D4 = 0X04;
TXB0D5 = 0X05;
TXB0D6 = 0X06;
TXB0D7 = 0X07;
}
void Can_Set_RxBuffer(void)
{
RXB0CON = 0X20;
RXB0SIDL = 0XE0;
RXB0SIDH = 0XFF;
RXB0DLC = 0x80;
RXB0D0 = 0X00;
RXB0D1 = 0X00;
RXB0D2 = 0X00;
RXB0D3 = 0X00;
RXB0D4 = 0X00;
RXB0D5 = 0X00;
RXB0D6 = 0X00;
RXB0D7 = 0X00;
}
void Can_Set_AMFilter()
{
RXF0SIDL = 0xE0;
RXF0SIDH = 0xFF;
RXM0SIDH = 0X00;
RXM0SIDL = 0X00;
}
void Can_Init(void)
{
TRISB = 0x08;
PEIE=1;
GIE=1;
ECANCON = 0x00; //Mode-0
CANCON = 0x80;
PORTC = CANSTAT;
while(CANSTAT&0X80==1);
Can_Set_Baudrate();
Can_Set_TxBuffer();
Can_Set_RxBuffer();
Can_Set_AMFilter();
CANCON = 0X00;
while(CANSTAT&0XE0!=0);
PIR3 = 0X00;
PIE3 = 0X01;
IPR3 = 0X01;
}
void main()
{
Can_Init();
while(1)
{
TX_Message.Address = 0x123;
TX_Message.Data[0] = 'A';
TX_Message.NoOfbytes = 1;
CanPut(TX_Message);
}
// hitech c
#include<pic18.h>
struct CANMessage
{
unsigned long Address;
unsigned char Data[8];
unsigned char NoOfBytes;
};
struct CANMessage RX_Message, TX_Message, ReturnMessage;
char CANPut(struct CANMessage Message)
{
TXB0D0 = Message.Data[0];
TXB0SIDL = Message.Address<<5&0xff;
TXB0SIDH = Message.Address>>3&0xff;
while(TXB0CONbits.TX0IF!=1);
TXB0CONbits.TX0IF = 0;
}
struct CANMessage CANGet(void)
{
unsigned int Add,Add1;
ReturnMessage.Data[0] = RXB0D0;
Add = RXB0SIDH;
Add1= RXB0SIDL;
Add1= (Add1 >> 5)|(Add<<3);
ReturnMessage.Address = Add1;
}
void Can_isr()
{
if(RXB0IF)
{
RXB0IF =0;
RXB0CONbits.RXFUL =0;
RX_Message = CANGet();
}
}
void Can_Set_Baudrate(void)
{
BRGCON1 = 0X01;
BRGCON2 = 0X90;
BRGCON3 = 0X42;
}
void Can_Set_TxBuffer(void)
{
TXB0CON = 0x03; // Highest Priority
TXB0SIDL = 0xE0;
TXB0SIDH = 0xFF;
TXB0DLC = 0x08; // 8 byte Data length
TXB0D0 = 0X00;
TXB0D1 = 0X01;
TXB0D2 = 0X02;
TXB0D3 = 0X03;
TXB0D4 = 0X04;
TXB0D5 = 0X05;
TXB0D6 = 0X06;
TXB0D7 = 0X07;
}
void Can_Set_RxBuffer(void)
{
RXB0CON = 0X20;
RXB0SIDL = 0XE0;
RXB0SIDH = 0XFF;
RXB0DLC = 0x80;
RXB0D0 = 0X00;
RXB0D1 = 0X00;
RXB0D2 = 0X00;
RXB0D3 = 0X00;
RXB0D4 = 0X00;
RXB0D5 = 0X00;
RXB0D6 = 0X00;
RXB0D7 = 0X00;
}
void Can_Set_AMFilter()
{
RXF0SIDL = 0xE0;
RXF0SIDH = 0xFF;
RXM0SIDH = 0X00;
RXM0SIDL = 0X00;
}
void Can_Init(void)
{
TRISB = 0x08;
PEIE=1;
GIE=1;
ECANCON = 0x00; //Mode-0
CANCON = 0x80;
PORTC = CANSTAT;
while(CANSTAT&0X80==1);
Can_Set_Baudrate();
Can_Set_TxBuffer();
Can_Set_RxBuffer();
Can_Set_AMFilter();
CANCON = 0X00;
while(CANSTAT&0XE0!=0);
PIR3 = 0X00;
PIE3 = 0X01;
IPR3 = 0X01;
}
void main()
{
Can_Init();
while(1)
{
TX_Message.Address = 0x123;
TX_Message.Data[0] = 'A';
TX_Message.NoOfbytes = 1;
CanPut(TX_Message);
}