Check my code for doing CAN nodes with DSPIC 30F4012

Status
Not open for further replies.

WhyNot

Newbie level 2
Joined
Dec 17, 2007
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,295
Hi, anyone tried to do CAN nodes with this DSPIC ? I'm new too DSPICs, I have worked AVR, but not with DSPICs


I read that I need MCP2551 CAN transceiver to work with it, but I don't manage to test the cricuit,

does anyone have an example code or schematic for a starting point ? Like turn on he LED if the message is received correctly or something like that?

I tried to read the examples from Microchip but didn't find that very helpfull :\


Thks

Added after 9 minutes:

here its the Code I was testing with

Code:
#define __dsPIC30F4012__
#include <p30f4012.h>
#include <can.h>

//#define dataarray 0x1020

_FOSC(CSW_FSCM_OFF & HS);  //Run this project using an external crystal
                                //routed via the PLL in 16x multiplier mode
                                //For the 7.3728 MHz crystal we will derive a
                                //throughput of 7.3728e+6*16/4 = 29.4 MIPS(Fcy)
                                //,~33.9 nanoseconds instruction cycle time(Tcy).

_FWDT(WDT_OFF);                 //Turn off the Watch-Dog Timer.
_FBORPOR(MCLR_EN & PWRT_OFF); 
_FGS(CODE_PROT_OFF);            //Disable Code Protection

//unsigned int Speed;
//int ReadADC(unsigned int channel);

#define FCY 		20000000             		// 20 MHz
#define BITRATE 	700000			 			// 700kHz
#define NTQ 		15							// Number of Tq cycles which will make the 
												//CAN Bit Timing .
#define BRP_VAL		((FCY/(2*NTQ*BITRATE))-1)  //Formulae used for C1CFG1bits.BRP 
#define dataarray 0x1820


int main(void)
{
	unsigned char datalen;
	unsigned char Txdata1[]={1},Txdata2[]={0};
	unsigned int TXConfig,RXConfig;
	unsigned long MaskID, MessageID;
	char FilterNo, tx_rx_no;
	unsigned char *datareceived=(unsigned char *)dataarray;

	TRISE=0xFFFF;
	TRISB=0x0000;
	PORTB=0x0000;


//Set request for configuration Mode

	CAN1SetOperationMode(CAN_IDLE_CON & CAN_MASTERCLOCK_1 & CAN_REQ_OPERMODE_CONFIG & CAN_CAPTURE_DIS);
	while(C1CTRLbits.OPMODE<=3);

//Load Configuration register

	CAN1Initialize(CAN_SYNC_JUMP_WIDTH2 & CAN_BAUD_PRE_SCALE(BRP_VAL),CAN_WAKEUP_BY_FILTER_DIS & CAN_PHASE_SEG2_TQ(5) & CAN_PHASE_SEG1_TQ(4) & 
					CAN_PROPAGATIONTIME_SEG_TQ(4) & CAN_SEG2_FREE_PROG & CAN_SAMPLE1TIME);

//Load acceptance filter register

	FilterNo=0;
	CAN1SetFilter(FilterNo, CAN_FILTER_SID(1920) & CAN_RX_EID_DIS,CAN_FILTER_EID(1));

//Load Mask filter register

	CAN1SetMask(FilterNo, CAN_MASK_SID(0) & CAN_MATCH_FILTER_TYPE, CAN_MASK_EID(0));

//Set transmitter and receiver mode

	tx_rx_no = 0;
	CAN1SetTXMode(tx_rx_no, CAN_TX_STOP_REQ & CAN_TX_PRIORITY_HIGH);
	CAN1SetRXMode(tx_rx_no, CAN_RXFUL_CLEAR & CAN_BUF0_DBLBUFFER_EN);


//Set request for normal mode

	CAN1SetOperationMode(CAN_IDLE_STOP & CAN_CAPTURE_EN & CAN_MASTERCLOCK_1 & CAN_REQ_OPERMODE_NOR);
	while(C1CTRLbits.OPMODE != 0);	

	PORTBbits.RB0=1;
	datalen=8;
while (1)
{
	if(PORTEbits.RE0==1)
	{
		
	//Load message ID, Data into transmit buffer and set transmit request bit
		CAN1SendMessage((CAN_TX_SID(1920)) & (CAN_TX_EID_DIS) & (CAN_SUB_NOR_TX_REQ), (CAN_TX_EID(1)) & (CAN_NOR_TX_REQ), Txdata1, datalen, tx_rx_no);
	
		while(!CAN1IsTXReady(0));
			PORTBbits.RB0=0;
	}
	else
	{
		CAN1SendMessage((CAN_TX_SID(1920)) & (CAN_TX_EID_DIS) & (CAN_SUB_NOR_TX_REQ), (CAN_TX_EID(1)) & (CAN_NOR_TX_REQ), Txdata2, datalen, tx_rx_no);
		while(!CAN1IsTXReady(0));
	}
	if(CAN1IsRXReady(0))
	{
		CAN1ReceiveMessage(datareceived, datalen, tx_rx_no);
		if(datareceived==Txdata1)
			PORTBbits.RB0=1;
		else 
			PORTBbits.RB0=0;
	}
}


}





/*


void __attribute__((interrupt, no_auto_psv)) _C1Interrupt(void)
{
      
	       
}


/*	TRISE=0xFFFF;
	TRISB=0x0000;
	PORTB=0x0000;
	T1CON 	= 0x0030;					/* Timer with 0 prescale */

/*	TMR1	= 0;					/* Clear the Timer counter */
//	PR1		= 20000;		    /* Load the period register */	
//	T1CONbits.TON 	= 1;			/* Start the Timer */

// Initialize ADC

/*	ADCON1 = 0;
	ADCON2 = 0;
	ADCON3 = 0;
	ADPCFG = 0;
	ADCHS = 0x0007;
	ADCON1bits.ADON = 1;


while(1)
{

	Speed = ReadADC(5);
	if(Speed>=500)
		PORTBbits.RB0=1;
	else
		PORTBbits.RB0=0;
}

}

int ReadADC(unsigned int channel)
{
int Delay;

if(channel > 0x000F) return(0);
ADCHS = channel;
ADCON1bits.SAMP = 1;
for(Delay = 0; Delay < 20; Delay++);
IFS0bits.ADIF = 0;
ADCON1bits.SAMP = 0;
while(!IFS0bits.ADIF);
return(ADCBUF0);
}
*/
 

30f4012

Thanks for the reply, but then what is your sggestion to start?, I really need to get some CAN nodes, just to send some sensors readings, I have ADC working, and some test codes with LEDS and ADCs on-off, I need to get CAN working,


I have MCP2551, kinda lost :\
 

can dspic

Attached is my Can library for dsPics. It just provides the functions you need for using Can.
In the 'h' file you will find out the order to call the functions.
The only problem you may have is with the baud rate.
This sets baud rate to 500Kbps with 20Tq for a clock of 10MHz.
The baud rate setting and Tq configuration are normally dictated by the other nodes in the system. You will have to read the manual to find out how to set other baud rates.

You need at least two nodes for can to work.
To use the lib, configure can as in the order in the 'h' file.
Define a CANMSG structure.

CANMSG Canmsg;

and pass the address of the structure to the send message and recieve message function calls.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…