Example code for pic18f4580 can transmitter and receiver

Status
Not open for further replies.

krishnas

Newbie level 3
Joined
Feb 7, 2013
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,306
Hello fren,

can any one send the code of PIC18F4580 CAN transmitter and receiver .... i almost finished code.. but not working due to some error.. pls help me to recover that problem...

thanks
krishna
 

Can you show us your code so that we can spot the error?

#include<pic18f4580.h>
#include<htc.h>
void delay(unsigned int x)
{
unsigned int i;
for(i=0;i<x;i++);
}

void CAN_TX_BUFFER()
{
TRISD=0X00;

CIOCON=0X20;
TXB0CON = 0X03;
TXB0DLC=0X01; // DATA BYTE LENTH (page no 291)

TXB0EIDH=0X00;
TXB0EIDL=0X00;
TXB0SIDH=0X63; // standard identifier address (page 289)
TXB0SIDL=0XC0; // standard identified address
CANCON=0X00; // Request to Normal mode and TX buf 0
while(CANSTAT&0xE0!=0X00);
PIR3 = 0X00;
PIE3 = 0X04;
IPR3 = 0X04;
IPEN=1;

INTCON = 0xC0;
TXB0IF = 0;
TXB0D0=0x55; // Load data
TXB0D1=0x00;
TXB0D2=0x00;
TXB0D3 = 0x00;
TXB0D4 = 0x00;
TXB0D5 = 0x00;
TXB0D6 = 0x00;
TXB0D7 = 0x00;

PORTD=0X04;
delay(65000);
delay(65000);

TXB0CON |= 0x08; // Request for transmission. Normal priority. TXREQ bit set.


while(TXB0IF!=1);
PORTD=0X55;
delay(65000);
delay(65000);
delay(65000);
PORTD=0X00;
delay(65000);
delay(65000);
delay(65000);

}


void CAN_BAUD_RATE()
{

BRGCON1 = 0x40; //0100 0000 //SJW=1TQ BRP 4
BRGCON2 = 0x09; //0000 1001 //SEG2PHTS 1 sampled once PS1=8TQ PropagationT 1TQ
BRGCON3 = 0x00; //0000 0000 //PS2 6TQ
}


void CAN_INIT()
{
CANCON=0X80; // Config mode REQOP2 bit set, in mode 0 -- transmit buf 0
while(CANSTAT&0xE0!=0X80);
// while(!(OPMODE2==1));

ECANCON=0X00; // Legacy mode 0 TO BE CHANGED TO FIFO MODE
//CANCON=0X88; // After configuration mode modify this reg as required. REQOP2 bit & Tx buf 0 bits(WIN2,WIN1,WIN0)

CAN_BAUD_RATE();
CAN_TX_BUFFER();
}
void main()
{
while(1)
{
RBPU=0;
INTCON = 0;
TRISB=0x08; //can tx and rx pin enable.
CAN_INIT();
}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…