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

J1939 library CCS

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



Joined: 03 Sep 2018
Posts: 21

View user's profile Send private message MSN Messenger

J1939 library CCS
PostPosted: Mon Sep 03, 2018 7:05 am     Reply with quote

I'm working with PIC18F4520, which does not have hardware for CAN, so I'm using the MCP2515 and MCP2551 integrated circuit for CAN network access.
I'm trying to see the PGN 65.248 (total distance from the vehicle) but I can not. I am using the sample library that comes with the version 5 compiler.
Do I need to make any changes to the library?
Code:

void J1939Task(void)
{
   uint8_t i;
   uint8_t Data[8];
   uint8_t Length;
   J1939_PDU_STRUCT Message;
   
   TICK_TYPE CurrentTick;
   static TICK_TYPE PreviousTick;

   CurrentTick = GetTick();

 
  J1939ReceiveTask();  //J1939ReceiveTask() needs to be called often
  J1939XmitTask();     //J1939XmitTask() needs to be called often
  // delay_ms(1000);
   if(J1939Kbhit())  //Checks for new message in J1939 Receive buffer
   {
      J1939GetMessage(Message,Data,Length);  //Gets J1939 Message from receive buffer     

         printf("\r\nName: 0x");
         for(i=0;i<Length;i++){
            printf("%02X",Data[I]);
         }
         printf(", Address: %03u",Message.SourceAddress);
      //}
   }     
   if(GetTickDifference(CurrentTick,PreviousTick) >= (TICK_TYPE)TICKS_PER_SECOND)
   {
      //send message to other unit once every second to toggle pin
      Message.SourceAddress = g_MyJ1939Address;          //set PDU Source Address, this units address (g_MyJ1939Address)
      Message.DestinationAddress = J1939_PF_BROADCAST;   //set PDU Destination Address, address of other unit
      Message.PDUFormat = J1939_PF_REQUEST;                   //set PDU Formate, LED_TOGGLE command
      Message.DataPage = 0;                              //set PDU Data Page can be either 0 or 1, this message uses 0
      Message.ExtendedDataPage = 0;                      //set PDU Extended Data Page, must be zero for J1939 Messages
      Message.Priority = J1939_CONTROL_PRIORITY;         //set Priority, can be 0 to 7 (0 highest priority) Control default is 3
   
      //Load PGN of Message (refer to J1939 documentation for correct format)
      Data[0] = 0xe0;//FE
      Data[1] = 0Xfe;//E0
      Data[2] = 0x0;
       printf("Enviando\r\n");
      while(!J1939PutMessage(Message,&Data,3));    //loads J1939 Message into Xmit buffer     
      PreviousTick =  CurrentTick;
      printf("Enviado\r\n");
   }
   
   
 
}
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Sep 03, 2018 8:21 am     Reply with quote

Other will know for sure, I don't use CAN way too much overhead...)
and timing is critical, so be sure PIC has a REAL xtal/caps NOT the internal osc !
but I'd start small....
..cut 'simple' code to access some registers and confirm you can change and see the updates. then I'd read some registers that you know the data. after that you should be able to do what you want.

small steps, confirm, then move on....
Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: J1939 library CCS
PostPosted: Mon Sep 03, 2018 9:00 am     Reply with quote

fernandokestering wrote:

Do I need to make any changes to the library?

The J1939.h file has this section in it, near the start of the file:
Quote:

#ifndef USE_INTERNAL_CAN
#define USE_INTERNAL_CAN TRUE
#endif

This means the driver defaults to using a PIC with internal CAN bus support.
You should edit that line and change it to FALSE. Then re-compile.



temtronic, he said this:
Quote:
so I'm using the MCP2515 and MCP2551 integrated circuit
for CAN network access.

The MCP2515 is an external CAN controller chip and has its own crystal
oscillator circuit.
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Sep 03, 2018 9:00 am     Reply with quote

All of the parameters have to change, dependant on the clock rate of the chips, and the clock rate your bus is running at.
There are on-line tools to tell you the parameter values to use depending on these figures.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 03, 2018 9:09 am     Reply with quote

I noticed one thing. If you are using the CCS example file ex_j1939b.c
then it defaults to USE_INTERNAL_CAN as FALSE. So you don't have to
change the .h file.

But, if you are using ex_j1939.c (without the 'b' on the end), then it defaults
to using a PIC with an internal CAN controller.

So in your case, you should use the ex_j1939b.c file. You didn't say which
one you are using (if any).
fernandokestering



Joined: 03 Sep 2018
Posts: 21

View user's profile Send private message MSN Messenger

PostPosted: Mon Sep 03, 2018 9:54 am     Reply with quote

I am using the speed of 250kbits / sec as shown below

#define J1939_BAUD_RATE 250000
#define USE_INTERNAL_CAN false

On the oscilloscope I can verify that data is being written to the CAN bus, but I do not get a return on the J1939GetMessage function from the J1939 library provided by CCS
fernandokestering



Joined: 03 Sep 2018
Posts: 21

View user's profile Send private message MSN Messenger

PostPosted: Mon Sep 03, 2018 10:18 am     Reply with quote

////////////////////////////////////////////////////////////////////////////////
//// EX_J1939.c ////
//// ////
//// Example of CCS's J1939 driver. This example was tested using the CCS ////
//// CAN Bus 24 Development kit. ////
//// ////
//// This example will send a message once every second commanding Node B ////
//// to toggle it's LED once every second. Requires that EX_J1939B.c be ////
//// programmed onto Node B of the development kit. Also pressing the push ////
//// will cause Node A to send out a Global Address Request, causing all ////
//// device to respond with their claimed address. Which will be displayed ////
//// over RS232. ////
//// ////
//// This example will work with the PCM, PCH and PCD compilers. The ////
//// is written to work with the PCD and PCH compiler. Change the device, ////
//// clock, RS232 settings, PIN defines and tick timer setting for hardware ////
//// if needed. ////
//// ////
////////////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2012 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
////////////////////////////////////////////////////////////////////////////////

#if defined(__PCD__)
#include <24HJ256GP610.h>
#elif defined(__PCH__)
#include <18F4520.h>
#endif
#fuses NOWDT
#use delay(crystal=20MHz)
#use rs232(baud=9600,xmit=pin_c1,rcv=pin_c0)


#include <stdint.h>

void InitJ1939Address(void);
void InitJ1939Name(void);

//////////////////////////////////////////////////////////////////////////////// Tick Timer
#define TICKS_PER_SECOND 1000

typedef uint32_t TICK_TYPE;
static TICK_TYPE TimerTick;

TICK_TYPE GetTick(void)
{
return(TimerTick);
}

TICK_TYPE GetTickDifference(TICK_TYPE current,TICK_TYPE previous)
{
return(current - previous);
}

#INT_TIMER2
void tick_time_isr(void)
{
TimerTick++;
}

//////////////////////////////////////////////////////////////////////////////// J1939 Settings

//Following Macros used to initialize unit's J1939 Address and Name - Required
#define J1939InitAddress() InitJ1939Address()
#define J1939InitName() InitJ1939Name()

//Following defines what the CAN's baud rate it, not required defaults to 250Kbit
//only necessary if using non standard baud rate. Was changed to 125Kbit to
//work on CCS CAN Bus and CAN Bus 24 development kit.
#define J1939_BAUD_RATE 250000
#define USE_INTERNAL_CAN false
//Following defines sets up the CAN baud rate, not required if using 250Kbit or
//500Kbit and clock rates of 8, 16, 20, 32 or 40MHz.
#if defined(__PCD__)
#define CAN_BRG_PRESCALAR 4
#define CAN_BRG_PHASE_SEGMENT_1 2
#define CAN_BRG_PHASE_SEGMENT_2 2
#define CAN_BRG_SYNCH_JUMP_WIDTH 0
#define CAN_BRG_PROPAGATION_TIME 0
#elif defined(__PCH__)
#define CAN_BRG_PRESCALAR 4
#define CAN_BRG_PHASE_SEGMENT_1 6
#define CAN_BRG_PHASE_SEGMENT_2 6
#define CAN_BRG_SYNCH_JUMP_WIDTH 0
#define CAN_BRG_PROPAGATION_TIME 0
#endif

//Following defines/macros used to associate your tick timer to J1939 tick timer
// defines/macro's - Required
#define J1939GetTick() GetTick()
#define J1939GetTickDifference(a,b) GetTickDifference(a,b)
#define J1939_TICKS_PER_SECOND TICKS_PER_SECOND
#define J1939_TICK_TYPE TICK_TYPE

//Include the J1939 driver
#include <j1939_1.c>

//Defines for J1939 Commands used in this example


//Define for other Node's J1939 address used in this example


//Function used to initialize this unit's J1939 Address
void InitJ1939Address(void)
{
g_MyJ1939Address = 128;
}

//Function used to initialize this unit's J1939 Name
void InitJ1939Name(void)
{
g_J1939Name[0] = 0;
g_J1939Name[1] = 0;
g_J1939Name[2] = 0;
g_J1939Name[3] = 0;
g_J1939Name[4] = 0;
g_J1939Name[5] = 0;
g_J1939Name[6] = 0;
g_J1939Name[7] = 128;
}

//J1939 Task function for this example
void J1939Task(void)
{
uint8_t i;
uint8_t Data[8];
uint8_t Length;
J1939_PDU_STRUCT Message;

TICK_TYPE CurrentTick;
static TICK_TYPE PreviousTick;

CurrentTick = GetTick();


J1939ReceiveTask(); //J1939ReceiveTask() needs to be called often
J1939XmitTask(); //J1939XmitTask() needs to be called often

if(J1939Kbhit()) //Checks for new message in J1939 Receive buffer
{
J1939GetMessage(Message,Data,Length); //Gets J1939 Message from receive buffer
for(i=0;i<Length;i++){
printf("%02X",Data[I]);
}
printf(", Address: %03u",Message.SourceAddress);
//}
}
if(GetTickDifference(CurrentTick,PreviousTick) >= (TICK_TYPE)TICKS_PER_SECOND/4)
{
//send message to other unit once every second to toggle pin
Message.SourceAddress = g_MyJ1939Address; //set PDU Source Address, this units address (g_MyJ1939Address)
Message.DestinationAddress = J1939_PF_BROADCAST; //set PDU Destination Address, address of other unit
Message.PDUFormat = J1939_PF_REQUEST; //set PDU Formate, LED_TOGGLE command
Message.DataPage = 0; //set PDU Data Page can be either 0 or 1, this message uses 0
Message.ExtendedDataPage = 0; //set PDU Extended Data Page, must be zero for J1939 Messages
Message.Priority = J1939_CONTROL_PRIORITY; //set Priority, can be 0 to 7 (0 highest priority) Control default is 3

//Load PGN of Message (refer to J1939 documentation for correct format)
Data[0] = 0xe0;//FE
Data[1] = 0Xfe;//E0
Data[2] = 0x0;
printf("Enviado\r\n");

while(!J1939PutMessage(Message,Data,3)); //loads J1939 Message into Xmit buffer
PreviousTick = CurrentTick;

}
}
void main()
{
#if defined(__PCD__)
setup_timer2(TMR_INTERNAL,10000); //1ms tick
#else
setup_timer_2(T2_DIV_BY_4, 250, 5); //1ms tick
#endif
enable_interrupts(INT_TIMER2);
#if defined(__PCD__)
enable_interrupts(INTR_GLOBAL);
#else
enable_interrupts(GLOBAL);
#endif

J1939Init(); //Initialize J1939 Driver must be called before any other J1939 function is used

printf("##########\r\n");

while(TRUE)
{

J1939Task();



}
}
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