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

CCS CAN bus demo board EX10.c

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



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

CCS CAN bus demo board EX10.c
PostPosted: Mon Feb 22, 2010 12:21 pm     Reply with quote

Hi,

I'm using the CCS CAN bus demo board and the example code that comes with it.

I reprogram node B (16F876A) because I am going to use something similar to this setup with the example code EX10.c from the CD.

Code:
#include <16F876A.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=2500000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <can-mcp2510.c>

void main() {
   int32 rx_id;
   int i, rx_len, buffer[8];
   struct rx_stat rxstat;
printf("\r\nhello?");
   can_init();
printf("\r\nworld?");
   while(TRUE) {
      if ( can_kbhit() ) {
          if(can_getd(rx_id, &buffer[0], rx_len, rxstat)) {
             printf("%LX:  (%U) ",rx_id,rx_len);
             if (!rxstat.rtr) {
                   for(i=0;i<rx_len;i++)
                      printf("%X ",buffer[i]);
             }
             if (rxstat.rtr) {printf(" R ");}
             if (rxstat.err_ovfl) {printf(" O ");}
             if (rxstat.inv) {printf(" I ");}
             printf("\r\n");
          }
      }
   }
}


The program only runs to the first printf then stucks there. I assumed there is something wrong with the can_init() function. I did not change anything on the hardware side so I'm not sure why it would not work.

Please advice,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 12:40 pm     Reply with quote

Put some printf statements inside the can_init() routine, to find which
line the program is stopping on.
Benzino



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 1:18 pm     Reply with quote

Hi,

I did as you suggested (also changed <> to "" for #include "can-mcp2510.c").

This is what I receive on the hyper terminal

Quote:

hello?
1
.


and below is the part of code that I change in can-mcp2510.c
Code:

void can_init(void) {
   struct struct_RXB0CTRL b_rxb0ctrl;
printf("\r\n 1");
   mcp2510_init();
printf("\r\n 2");
   can_set_mode(CAN_OP_CONFIG);   //must be in config mode before params can be set
printf("\r\n 3");
   can_set_baud();
printf("\r\n 4");
   b_rxb0ctrl=0;
   b_rxb0ctrl.rxm=CAN_RX_VALID;
   b_rxb0ctrl.bukt=CAN_USE_RX_DOUBLE_BUFFER;
   mcp2510_write(RXB0CTRL, (int)b_rxb0ctrl);
   mcp2510_write(RXB1CTRL, (int)b_rxb0ctrl);
printf("\r\n 5");
   //if you want to configure the TXnRTS pins, do it here.  default is off

   can_set_id(RX0MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID);  //set mask 0 (RX BUFFER 0)
   can_set_id(RX0FILTER0, 0, CAN_USE_EXTENDED_ID);  //set filter 0 of mask 0 (RX BUFFER 0)
   can_set_id(RX0FILTER1, 0, CAN_USE_EXTENDED_ID);  //set filter 1 of mask 0 (RX BUFFER 0)
printf("\r\n 6");
   can_set_id(RX1MASK, CAN_MASK_ACCEPT_ALL, CAN_USE_EXTENDED_ID);  //set mask 1 (RX BUFFER 1)
   can_set_id(RX1FILTER2, 0, CAN_USE_EXTENDED_ID);  //set filter 0 of mask 1 (RX BUFFER 1)
   can_set_id(RX1FILTER3, 0, CAN_USE_EXTENDED_ID);  //set filter 1 of mask 1 (RX BUFFER 1)
   can_set_id(RX1FILTER4, 0, CAN_USE_EXTENDED_ID);  //set filter 2 of mask 1 (RX BUFFER 1)
   can_set_id(RX1FILTER5, 0, CAN_USE_EXTENDED_ID);  //set filter 3 of mask 1 (RX BUFFER 1)
printf("\r\n 7");
   can_set_mode(CAN_OP_NORMAL);
printf("\r\n 8");
}

========AND=========


void mcp2510_write(int address, int data) {
   int command[3];
   int i;
printf("\r\n 1A");
   command[2]=0x02;
   command[1]=address;
   command[0]=data;
printf("\r\n 2A");
   output_low(EXT_CAN_CS);
printf("\r\n 3A");
   for (i=0;i<24;i++) {
      output_bit(EXT_CAN_SI, shift_left(&command[0],3,0));
      output_high(EXT_CAN_SCK);
      output_low(EXT_CAN_SCK);
   }
printf("\r\n 4A");
   output_high(EXT_CAN_CS);
printf("\r\n 5A");
}


I'm not sure why hyper terminal shows just a dot there.

Please advice,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 1:25 pm     Reply with quote

It looks like it's stuck in mcp2510_init(). Go into that routine, with more
printfs, and find out where it's getting stuck in there.
Benzino



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 1:41 pm     Reply with quote

Hi,

I did for the mcp2510_init() function and it didn't pass through the first function.

Code:
void mcp2510_init(void) {
printf("\r\n 10");            <---------- Stop here with 1 and a strange character on the hyper terminal that is not '0'
   output_high(EXT_CAN_CS);
   output_low(EXT_CAN_SCK);
printf("\r\n 20");
   #ifdef EXT_CAN_TX0RTS
    output_high(EXT_CAN_TX0RTS);
   #endif
printf("\r\n 30");
   #ifdef EXT_CAN_TX1RTS
    output_high(EXT_CAN_TX1RTS);
   #endif
printf("\r\n 40");
   #ifdef EXT_CAN_TX2RTS
    output_high(EXT_CAN_TX2RTS);
   #endif
printf("\r\n 50");
  #ifdef EXT_CAN_TX0RTS
   output_high(EXT_CAN_RESET);
   output_low(EXT_CAN_RESET);
   output_high(EXT_CAN_RESET);
   delay_ms(5);
  #endif
printf("\r\n 60");
   mcp2510_command(0xC0);   //reset
   delay_ms(5);
}


The program that comes with the board has no problem transmitting serial RS232 information or CAN for the Node B.

The code that I'm using is from the CCS CD and there should not be any problem with the hardware since I got it from CCS not from other demo kit.

I don't understand why these codes would not work.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 1:47 pm     Reply with quote

Look at the schematic for the CCS board. What pins are used by the PIC
to connect to the MCP2515 chip ? Post the list of connections on the
schematic.

Also, look in the can-mcp2510.c file. Post the list of #define statements
that show the PIC pins used by the driver.
Benzino



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 2:12 pm     Reply with quote

Hi,

The pin diagram below shows how the pins on CCS CAN bus demo board are connected.



CS -> RB1
SI -> RC1
SO -> RC0
SCLK -> RC3

and below is the pin define in the can-mcp2510.c

Code:
   #define EXT_CAN_CS   PIN_B1
   #define EXT_CAN_SI   PIN_C1
   #define EXT_CAN_SO   PIN_C0
   #define EXT_CAN_SCK  PIN_C3


It looks right to me.
The diagram is from the manual for the demo kit published in April 2009.
I'm not sure if you have the newer version of this manual.

Please advice,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 2:45 pm     Reply with quote

I will attempt to duplicate the hardware environment, and see if I get
the same problem. I don't have the CCS demo board (or the manual)
but I think I can make the same circuit.

What is your compiler version ?
Benzino



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 2:50 pm     Reply with quote

Hi,

My compiler version is 4.093.
Do you need the circuit diagram? I can upload it on the forum.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 22, 2010 3:20 pm     Reply with quote

It worked for me. I jumpered my PicDem2-Plus board over to a 3M
breadboard that has a MCP2510 on it. I used the CLKOUT signal
from the MCP2510 as the oscillator input into a 16F877A on the PicDem2
Plus board. This is the same as the CCS CAN Bus Demo board.
I got this output on the terminal window (from your original test program):
Quote:

hello?
world?


Look at the solder joints for the chips on the CCS board with a magnifier.
Do they all look OK ? Also look at the joints on the RS-232 connecter.
Benzino



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

PostPosted: Tue Feb 23, 2010 6:32 am     Reply with quote

Hi,

The chip on the board looks ok as well as the joints on the RS-232.
However, strange thing did happen when I open up the PICkit 2 Programmer.
The PIC works every time I open that program. I don't understand why it does that.
Any suggestion of what might cause the PIC to not function properly without open PICkit 2 programmer software?

Thank you,

edited:

I found out the problem afterward. The PICkit 2 programmer was holding up the release/reset pin.
On the setting there is a check box that I have to select [3-State on "Release from Reset"]
Then from the MPLAB IDE programmer menu bar, I have to click [Release from Reset] and it will work.

I also went back and tested the another board that PCM programmer had been helping me with and it works the same.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 23, 2010 12:11 pm     Reply with quote

So is everything working OK now ? (Including the CAN bus) ?
Benzino



Joined: 15 Feb 2010
Posts: 24

View user's profile Send private message

PostPosted: Tue Feb 23, 2010 12:30 pm     Reply with quote

Yes, it is. I have my PIC program like this.

Code:
#include <16F876A.h>
#device ADC=10
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=2500000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include "can-mcp2510.c"

#define RED_LED     PIN_A1
#define YELLOW_LED  PIN_A2
#define GREEN_LED     PIN_A3

int16 ms;

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


void main() {
   int32 rx_id;
   int rx_len, rxstat, buffer[8];
   int out_data[8];
   int16 i;

   output_bit(YELLOW_LED, 1);
   output_bit(GREEN_LED, 1);
   output_bit(RED_LED, 1);
   buffer[0] = 0x00;
   buffer[1] = 0x00;
   buffer[2] = 0x00;
   buffer[3] = 0x00;
   buffer[4] = 0x00;
   buffer[5] = 0x00;
   buffer[6] = 0x00;
   buffer[7] = 0x00;

   setup_timer_2(T2_DIV_BY_4,79,16);   //setup up timer2 to interrupt every 1ms if using 20Mhz clock
   setup_port_a(RA0_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   printf("\r\nStart CAN initialization");
   can_init();
   printf("\r\nCAN initialization complete");

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

   while(TRUE) {
      if ( can_kbhit() ) {
      //printf("\r\nIn the loop");
      if(can_getd(rx_id, &buffer[0], rx_len, rxstat))
      if (rx_id == 0x201) {
         if(buffer[0]==0x01) // buffer[0]&2
         {
            printf("\r\nreceive rx=201 data=1");
            output_bit(YELLOW_LED, 1);
            output_bit(GREEN_LED, 0);
            output_bit(RED_LED, 1);
            delay_ms(100);
            buffer[0] = 0x01;
            buffer[1] = 0x00;
            buffer[2] = 0x00;
            buffer[3] = 0x00;
            buffer[4] = 0x00;
            buffer[5] = 0x00;
            buffer[6] = 0x00;
            buffer[7] = 0x00;
         }
         if(buffer[0]==0x02) // buffer[0]&4
         {
            printf("\r\nreceive rx=201 data=2");
            output_bit(YELLOW_LED, 0);
            output_bit(GREEN_LED, 1);
            output_bit(RED_LED, 1);
            delay_ms(100);
            buffer[0] = 0x02;
            buffer[1] = 0x00;
            buffer[2] = 0x00;
            buffer[3] = 0x00;
            buffer[4] = 0x00;
            buffer[5] = 0x00;
            buffer[6] = 0x00;
            buffer[7] = 0x00;            
         }
         if(buffer[0]==0x0A) // buffer[0]&8
         {
            printf("\r\nreceive rx=201 data=A");
            output_bit(YELLOW_LED, 1);
            output_bit(GREEN_LED, 1);
            output_bit(RED_LED, 0);
            delay_ms(100);
            buffer[0] = 0x0A;
            buffer[1] = 0x00;
            buffer[2] = 0x00;
            buffer[3] = 0x00;
            buffer[4] = 0x00;
            buffer[5] = 0x00;
            buffer[6] = 0x00;
            buffer[7] = 0x00;
         }
      }
      if (rx_id == 0x202){
         printf("\r\nreceive rx=202");
         output_bit(YELLOW_LED, 0);
         output_bit(GREEN_LED, 0);
         output_bit(RED_LED, 0);
         delay_ms(1000);
         output_bit(YELLOW_LED, 1);
         output_bit(GREEN_LED, 1);
         output_bit(RED_LED, 1);
         buffer[0] = 0x1A;
         buffer[1] = 0x2B;
         buffer[2] = 0x3C;
         buffer[3] = 0x4D;
         buffer[4] = 0x5E;
         buffer[5] = 0x6F;
         buffer[6] = 0x00;
         buffer[7] = 0x00;
      }
   }

      //every one second, send new data if transmit buffer is empty
      if ( can_tbe() && (ms > 100))
      {
         ms=0;
         i=can_putd(0x452, buffer, 8,1,0,0); //put data on transmit buffer
       printf("\r\n");
         if (i != 0xFF) { //success, a transmit buffer was open
            for (i=0;i<8;i++) {
               printf("%X ",buffer[i]);
            }
       i=read_adc();
       printf("Sending AD reading: %Lu",i);
       i=can_putd(0x453, &i, 2,1,0,0);
       } else { //fail, no transmit buffer was open
            printf("\r\nFAIL on PUTD\r\n");
         }
      }
   }
}


and it works just fine with the CCS CAN bus demo board.
It was not the code or hardware but the PICkit 2 programmer settings that caused problem.

I haven't tried the CAN bus with my 16F690, however, it should be fine just like you mentioned when I wired all my PIC together. Also the Bit timing might be another problem since it uses different clock frequency but I will give that another try and let you know the results.

Thank you again for all your help. I never expect that the programmer was the problem for the longest time.


Much appreciate,
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