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 support@ccsinfo.com

Using a simple CAN BUS protocol example.

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



Joined: 06 Oct 2013
Posts: 4

View user's profile Send private message

Using a simple CAN BUS protocol example.
PostPosted: Fri Nov 15, 2013 6:28 am     Reply with quote

Hi, I'm making a simple example code based on the example file of CCS, "EX_CAN.c". I am using PICF4580. My code is as below.
Code:

#include "set4580.h"
#include "can-18F4580.h"
#byte p_d = 0xf83
#byte PIR1 = 0xf9e
int8 count;

#int_timer2
void isr_timer2(void) {
bit_clear(PIR1,1);
   count++; //keep a running timer that increments every milli-second
}

void main() {
   struct rx_stat rxstat; //구조체 정의는 어디서?
   int32 rx_id;
   int in_data[8];
   int rx_len;

//send a request (tx_rtr=1) for 8 bytes of data (tx_len=8) from id 24 (tx_id=24)
   int out_data[8];
   int32 tx_id=24;
   int1 tx_rtr=1;
   int1 tx_ext=0;
   int tx_len=8;
   int tx_pri=3;

   int i;

   for (i=0;i<8;i++) {
      out_data[i]=0;
      in_data[i]=0;
   }

   printf("\r\n\r\nCCS CAN EXAMPLE\r\n");

   setup_timer_2(T2_DIV_BY_16,255,16);   //1초마다 발생.

   can_init();//Initializes PIC18xxx8 CAN peripheral.

   enable_interrupts(INT_TIMER2);   //enable timer2 interrupt
   enable_interrupts(GLOBAL);       //enable all interrupts (else timer2 wont happen)

   printf("\r\nRunning...");

   while(TRUE)
   {
//Data receive
      if ( can_kbhit() )   //if data is waiting in buffer...
      {
         if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) { //...then get data from buffer
            printf("\r\nGOT: BUFF=%U ID=%LU LEN=%U OVF=%U ", rxstat.buffer, rx_id, rx_len, rxstat.err_ovfl);
            printf("FILT=%U RTR=%U EXT=%U INV=%U", rxstat.filthit, rxstat.rtr, rxstat.ext, rxstat.inv);
            printf("\r\n    DATA = ");
            for (i=0;i<rx_len;i++) {
               printf("%X ",in_data[i]);
            }
            printf("\r\n");
         }
         else {
            printf("\r\nFAIL on GETD\r\n");
         }

      }

      //every two seconds, send new data if transmit buffer is empty
      if ( can_tbe() && (count > 153))
      {
         count=0;
         i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
         if (i != 0xFF) { //success, a transmit buffer was open
            printf("\r\nPUT %U: ID=%LU LEN=%U ", i, tx_id, tx_len);
            printf("PRI=%U EXT=%U RTR=%U\r\n   DATA = ", tx_pri, tx_ext, tx_rtr);
            for (i=0;i<tx_len;i++) {
               printf("%X ",out_data[i]);
            }
            printf("\r\n");
         }
         else { //fail, no transmit buffer was open
            printf("\r\nFAIL on PUTD\r\n");
         }
      }
   }
}

Ignore some Korean comments Very Happy
EX_CAN.C file is a source file and this includes head files can-18F4580.h, set4580.h(Default setting file for rom writing). And can-18F4580.h includes can-18F4580.c.
I am writing this list because I am not sure whether this header files are right or not.
Anyway, when I tried to test this CODE using USB-to-CAN with Minimon program, no data is received and transmitted....
I don't know what problem exists in my code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 15, 2013 11:52 pm     Reply with quote

Post the contents of this file:
Quote:
#include "set4580.h"

Post a link to the schematic of your PIC board. Post your compiler version.
sonyoungdong



Joined: 06 Oct 2013
Posts: 4

View user's profile Send private message

About set4580
PostPosted: Tue Nov 19, 2013 5:54 am     Reply with quote

set4580.h
Code:

#include <18F4580.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES H4 //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV21 //Brownout reset at 2.1V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES BBSIZ2K //2K words Boot Block size
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=40000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


18F4580.h

Code:

//////// Standard Header file for the PIC18F4580 device ////////////////
#device PIC18F4580

[header file deleted]

Version is 4.068
thank you for your interest


+++++++++++++++++++++++
Do not post header files. Read CCS Forum Policy and Guidelines.
http://www.ccsinfo.com/forum/viewtopic.php?t=26245
-Forum Moderator
+++++++++++++++++++++++
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 19, 2013 12:13 pm     Reply with quote

Quote:
I'm making a simple example code based on the example file of CCS, "EX_CAN.c".

Anyway, when I tried to test this CODE using USB-to-CAN with Minimon
program, no data is received and transmitted....

Ex_Can.c is written to work with the CCS CAN Bus Development kit:
http://www.ccsinfo.com/product_info.php?products_id=CANbuskit
The program is designed to work with the MCP25050 CAN Bus i/o
expander chips:
http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010402

You are not using an MCP25050, so I think you need to make the
changes listed below:

Comment out the two lines shown below, and substitute the new
initialization lines shown after them:
Code:

//   int1 tx_rtr=1;
//   int1 tx_ext=0;

int1 tx_rtr=0;  // *** Changed to 0 
int1 tx_ext=1;  // *** Changed to 1
 


Also there are some problems with your #fuses:
Quote:

#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET

Change the first one in bold to NOLVP. I doubt that you are using a Low
voltage programmer. Those programmers are hand-built from
instructions on hobbyist websites. Almost everyone uses a normal
programmer, which should use NOLVP.
Also, I don't see that you are using PortB as analog inputs, so it's best
to keep PortB as digital i/o. Change the next fuse in bold to NOPBADEN.


Quote:
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

Also, ideally the #use rs232() line should always have the ERRORS
parameter as shown below:
Code:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

The compiler defaults to 8 bits and No parity. You don't need to specify it.
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