CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem using can-dsPIC30.c [solved]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
djani



Joined: 19 Dec 2012
Posts: 5

View user's profile Send private message Send e-mail

Problem using can-dsPIC30.c [solved]
PostPosted: Wed Dec 19, 2012 3:37 am     Reply with quote

Hi there,

I am using a dsPIC30F4013 with PCA82C250 and watch the output on PCAN view device.

In my code as below. I am just transmitting some data but I cant see this on PCAN view device.

Please help

My code:
Code:

//#if defined(__PCD__)
#include <30F4013.h>
#device ADC=12
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=16000000)
//#endif

//#include <math.h>
//#include <stdio.h>
//#include <string.h>


#include "can-dsPIC30.c"
//#include "can-dsPIC30.h"

//#include "adc.c"
//#include "DEFS.h"


void main(void)
{
   struct rx_stat rxstat;
   int32 rx_id;
   int32 tx_id;
   int8 rx_len;

   
   //unsigned int8 i=0;
   unsigned int8 out_buffer = 0x06;

   can_init();

   do
   {
      can_putd(0X0301,out_buffer,1,1,TRUE,FALSE);   
       delay_ms(100);

   }   
   while(1);
}


Last edited by djani on Wed Dec 19, 2012 7:36 am; edited 1 time in total
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Dec 19, 2012 3:56 am     Reply with quote

The default settings in all CCS's can support "drivers", including can-dsPIC30.c, give 125kbs on a processor clock of 20MHz. I suspect your CAN timing is wrong.

Given that is the case:

1) Are you expecting 125kbs? What does your CAN monitor hardware/software default to?

2) Your clock is 16MHz, so the defaults will mean the CAN will actually run at 100kbps (i.e. (125/20)*16), which is another common speed, but again may not be what your monitoring/testing hardware is set to.

You can set the speed you want for the clock rate you have by defining the settings in your code before including can-dsPIC30.c. DO NOT alter can-dsPIC30.c or can-dsPIC30.h, just define the relevant parameters in your code before including can-dsPIC30.c. Take a look at can_set_baud() and can-dsPIC30.h.

Setting the CAN bit rate correctly is fairly complex and can be tricky. I advise you use the MBTime utility available from I used the CAN bus bit timing calculator call MBTime, from
http://intrepidcs.com/modules/CmodsDownload/upload/Software/MBTime.zip

RF Developer
djani



Joined: 19 Dec 2012
Posts: 5

View user's profile Send private message Send e-mail

PostPosted: Wed Dec 19, 2012 4:56 am     Reply with quote

Hi there,

1) Are you expecting 125kbs? What does your CAN monitor hardware/software default to?

Yes, I was but eventually I want to reach 1Mbps. I can select the baud rate in PCAN view to 125kbps but still nothing.

2) Your clock is 16MHz, so the defaults will mean the CAN will actually run at 100kbps (i.e. (125/20)*16), which is another common speed, but again may not be what your monitoring/testing hardware is set to.

I selected the baud rate in PCAN view to 100kbps but still nothing.

I also tried the following in my code after getting details from the software
you suggested. I didnt understand what the BRP meant but i guess it is the prescaler

my code:

Code:
//#if defined(__PCD__)
#include <30F4013.h>
#device ADC=12
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=16000000)
//#endif

//#include <math.h>
//#include <stdio.h>
//#include <string.h>

#define   CAN_BRG_PRESCALAR         3
#define CAN_BRG_SYNCH_JUMP_WIDTH   1

#define CAN_BRG_PROPAGATION_TIME   1
#define CAN_BRG_PHASE_SEGMENT_1      8
#define CAN_BRG_PHASE_SEGMENT_2      6
   
#include "can-dsPIC30.c"
//#include "can-dsPIC30.h"

//#include "adc.c"
//#include "DEFS.h"


void main(void)
{
   struct rx_stat rxstat;
   int32 rx_id;
   int32 tx_id;
   int8 rx_len;

   
   //unsigned int8 i=0;
   unsigned int8 out_buffer = 0x06;

   can_init();

   do
   {
      can_putd(0X0301,out_buffer,1,1,TRUE,FALSE);   
       delay_ms(100);

   }   
   while(1);
}
djani



Joined: 19 Dec 2012
Posts: 5

View user's profile Send private message Send e-mail

Problem using can-dsPIC30.c
PostPosted: Wed Dec 19, 2012 7:34 am     Reply with quote

Hi there,

This will be a nice example for anybody trying to work at 1Mbps. The following example seem to work as I can see output on PCANview.

As described by the person above the problem lies in getting the correct setting for the Can Baud rate.

Many thanks for your advice.

My code:
Code:

#define CAN_DO_DEBUG TRUE


#if defined(__PCD__)
#include <30F4013.h>
#device ADC=12
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=16MHz)
#use rs232(UART1, baud=9600)
#endif

//#include <math.h>
//#include <stdio.h>
//#include <string.h>

#define CAN_BRG_PRESCALAR           0    //Set CAN Baud Rate to 125K
#define CAN_BRG_PHASE_SEGMENT_1     2    //Tq = (2(1+PRESCALAR))/(Fosc/2)
#define CAN_BRG_PHASE_SEGMENT_2     2    //Tq = (2(1+4)/(20000000/2) = 0.000001
#define CAN_BRG_PROPAGATION_TIME    0    //Baud Rate = 1/(((PHASE_SEGMENT_1+1)+(PHASE_SEGMENT_2+1)+(PROPAGATION_TIME+1)+(JUMP_WIDTH+1))*Tq)
#define CAN_BRG_SYNCH_JUMP_WIDTH    2    //Baud Rate = 1/(((1+1)+(1+1)+(2+1)+(0+1))*0.000001) = 125000
   
#include "can-dsPIC30.c"
//#include "can-dsPIC30.h"

//#include "adc.c"
//#include "DEFS.h"

int16 ms;

#int_timer2
void isr_timer2(void) {
   ms++; //keep a running timer that increments every milli-second
}


void main(void)
{
    struct rx_stat rxstat;
   unsigned int32 rx_id;
   unsigned int8 buffer[8];
   int8 rx_len;

   unsigned int16 i;

   buffer[0]=0x01;
   buffer[1]=0x02;   
   buffer[2]=0x03;
   buffer[3]=0x04;   
   buffer[4]=0x05;   
   buffer[5]=0x06;   
   buffer[6]=0x07;
    buffer[7]=0x08;
       
   setup_timer2(TMR_INTERNAL,1000);   //setup timer2 to interrupt every 1ms if using 20MHz clock

   can_init();

   enable_interrupts(INT_TIMER2);
   enable_interrupts(INTR_GLOBAL);
    
      
    while(TRUE)
      {
         if ( can_tbe() )       //every two seconds, send new data if transmit buffer is empty
         {
            ms=0;
           can_putd(0x301,&buffer[0], 8, 3, 1, 0);
         }       
      }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group