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

help with 18F4550, SPI, MCP2510

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



Joined: 19 Jan 2006
Posts: 11

View user's profile Send private message

help with 18F4550, SPI, MCP2510
PostPosted: Fri Mar 09, 2007 10:47 am     Reply with quote

Hi, I'm having trouble with connecting the MCP2510. my program crashes on the can_init() function. Actually I've never interface anything via SPI before. Anyways here is what I have

The MCP is interfaced as follows

PIC MCP
C7/SDO -> pin15 SO
B1/SCK -> pin13 SCK
B0/SDI -> pin14 SI

other MCP connections
Vdd -> +5
reset -> pulled +5 and connected to PIC reset button
CS_ -> GND (only using the one SPI device)
Vss ->GND
TXCAN - > TXD trans
RXCAN -> RXD trans

The pic code is based on some USB code I was, so that is were the fuse settings come from. I also have a USB bootloader.

I removed PIN_C7 here because its being used for SPI
#use rs232(baud=19200, xmit=PIN_C6)//, rcv=PIN_C7)

Code:


#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
   
#use delay(clock=48000000)

// for USB boot loader
#build(reset=0x800, interrupt=0x808)
#org 0x000, 0x7ff { }

#use rs232(baud=19200, xmit=PIN_C6)//, rcv=PIN_C7)


#include "lcd420D.c"
#include <can-mcp2510.c>

#define DEVICE_ID 255

long seconds = 0;      // A running seconds counter
unsigned byteCount = 0;

BYTE UART_EVENT = 0;
BYTE PACKET_RECEIVED = 0;

// CAN
#define RESPOND_TO_ID_AD   0x201
#define RESPOND_TO_ID_LED  0x202
struct rx_stat rxstat;
int32 rx_id;
int canBuffer[8];
int rx_len;


#include "timers.h"
#include "tasks.c"
#include "ISRs.c"

void main() {

   seconds = 0;
    set_tris_e(0);

   // set the incoming packet message to all zeros
   memset(&msg,0,4);   
   
   // == 200 ints per second
   setup_timer_2 ( T2_DIV_BY_16, 250, 15);
   set_timer2(0);


   enable_interrupts(INT_TIMER2);
   enable_interrupts(int_rda);
      ext_int_edge(H_TO_L);      // init interrupt triggering for button press

   setup_adc_ports(AN0_TO_AN1);

   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   delay_ms(200);
   lcd_init();
  delay_ms(200);

   enable_interrupts(GLOBAL);
 
  can_init();


   lcd_putc("Ready...\n"); 
   printf("Running..\r\n");

   delay_ms(1000);
 
   while (TRUE) {

      taskPollInput();
      taskCanHandler();
      taskUART();
      taskProcessInPacket();
      taskRespondToCCU();
      taskUpdateHub();
      taskUpdateDisplay();
      taskSecondCount();     


   }
}


Here is the code for my taskCanHandler() task.

Code:

void taskCanHandler(){
   
   if ( can_kbhit() )   //if data is waiting in buffer...
      {
         if(can_getd(rx_id, &canBuffer[0], rx_len, rxstat)) { //...then get data from buffer
            if (rx_id == RESPOND_TO_ID_LED) {
           lcd_gotoxy(1,4);
         printf(lcd_putc,"CAN id:%lu", rx_id);
           }
     else{
      lcd_gotoxy(1,4);
      printf(lcd_putc,"CcAN id:%lu", rx_id);
      }
              
         }
      }
   

}



And just in case: here are my isr's
Code:



#INT_TIMER2                       
void clock_isr() {                 
   if(timerPollInput) timerPollInput--;
   if(timerUART) timerUART--;
   if(timerRespondToCCU) timerRespondToCCU--;
   if(timerUpdateHub) timerUpdateHub--;
   if(timerDisplay) timerDisplay--;
   if(timerSecCount) timerSecCount--;
}


#int_rda
void serial_isr() {
   
   static int t;
   
   byteCount++;
   UART_EVENT = 1;
   buffer[next_in]=getc();
   TEST = buffer[next_in];
   t=next_in;
   
   next_in=(next_in+1) % BUFFER_SIZE;
   
   if(next_in==next_out){
        next_in=t;           // Buffer full !!
   
   }

}


PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 1:55 pm     Reply with quote

Quote:

PIC MCP2510
C7/SDO -> pin15 SO
B1/SCK -> pin13 SCK
B0/SDI -> pin14 SI

The SDI and SDO connections are wrong. SDO on the PIC must go
to SDI on the MCP2510. Output goes to input. Then the SDO on
the MCP2510 must go to the SDI pin on the PIC.

Also, I wouldn't tie the \CS pin to ground. For all we know, the internal
hardware state machine inside the MCP2510 may be looking for the
level changes on the chip select pin. It may work. I've never tried it.
But it's an extra experiment and it doesn't need to be done at this time.
The can-mcp2510.c driver expects to use it and I would hook it up just
as the driver says.

There are a few other things. I wouldn't put in the USB bootloader code
at the same time that you're trying to bring up the MCP2510. I would
get rid of all the #org's and #build statements.

You've removed the UART receiver by commenting out "rcv=PIN_C7"
from the #use rs232 statement. But your main code still has a line
in it to enable INT_RDA interrupts. That line should be removed.

Also, be aware that when only the transmitter is defined in the #use rs232
statement, the compiler will create a software UART. It won't use the
hardware UART.
mat72



Joined: 19 Jan 2006
Posts: 11

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 3:07 pm     Reply with quote

Thanks, I'll give it a try. Is it necessary to get rid of the USB bootloader? I find it great.

Thanks,

Matt
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 3:22 pm     Reply with quote

If it works, use it. My concern was that while you're bringing up a
new peripheral chip, any extraneous code cound introduce confusion
with regard to finding the problems.
mat72



Joined: 19 Jan 2006
Posts: 11

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 6:46 pm     Reply with quote

right now thats how I'm uploading the code. My programmer is packed up somewhere. I made the other fixes and its still crashing on the init code
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 09, 2007 6:56 pm     Reply with quote

Quote:
It's still crashing on the init code

This thread shows how to find a problem within can_init(), by placing
putc() statements within the routine to show code execution progress:
http://www.ccsinfo.com/forum/viewtopic.php?t=21949

More detail on trouble-shooting can_init() lock-ups:
(Scroll down to the middle of the thread for an example)
http://www.ccsinfo.com/forum/viewtopic.php?t=29220

Code to test basic reading/writing to the CAN chip
(See the post near the end of the thread):
http://www.ccsinfo.com/forum/viewtopic.php?t=27648
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