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

Erratic SPI with 16F876A and MCP2515

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



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

Erratic SPI with 16F876A and MCP2515
PostPosted: Sun Feb 19, 2006 10:32 pm     Reply with quote

Hi all,

I'm having some pretty big problems getting the SPI interface working. It seems I reset the 2515, read in CANCTRL and get 00, do another read and get 0x87, one more and I get 00 again. Somtimes that first 00 is an 0xFF.

I'de say maybe I'm having clock issues, but I'm using a 10MHz ceramic res with built in caps on the 2515 and a 20MHz version on the 16F876A pic. The pic seems stable. I don't have a scope of course Confused

Its REALLY frustrating.


Wondering if anyone had a similar problem or have any ideas,


Here some of the code I'm using:
Code:

void ResetMCP2515(void)
{
   output_high(MCP2515_CS);
   output_low(MCP2515_RESET_PIN);
   delay_us(10);
   output_high(MCP2515_RESET_PIN);
   delay_ms(0);
}

int ReadRegister(int regaddr)
{
   output_low(MCP2515_CS);
   spi_write(READCMD);
   spi_write(regaddr);
   rreg = spi_read(0);
   output_high(MCP2515_CS);
   return(rreg);
}


int data=0;
int data2=0;
int data3=0;

void main()
{
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H);

   ResetMCP2515();

   data3 = ReadRegister(CANCTRL);
   data2= ReadRegister(CANCTRL);
   data = ReadRegister(CANCTRL);


I've written it a few different ways. This code is borrowed from another post I saw.


Thanks in advance! [/code]
iso9001



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

PostPosted: Sun Feb 19, 2006 11:20 pm     Reply with quote

Here is my entire (crappy) code. Maybe there is somthing I'm missing.

Output: Which now seems to be pretty stable
Code:

Resetting CAN
Reading STAT : 80
Reading STAT : 0b
Reading STAT : 0f
Reading STAT : 03
Reading STAT : 03
Setting CTRL to 0x0F
Reading STAT : 00
Reading CTRL : 00


Program:
Code:

#include <16F876A.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT,NOPROTECT, PUT, NOBROWNOUT
#use delay(clock=20000000)
#use rs232(DEBUGGER)

#define CS        PIN_A4
#define RESET     PIN_A3
#define READCMD   0x03
#define WRITECMD  0x02

#define CANCTRL   0x0F
#define CANSTAT   0x0E


int data=99;
int tempdata,temp;


void CANReset(void)
{
   output_low(RESET);
   delay_us(10);
   output_high(RESET);
}


void CANWrite(int addr, int value)
{
   output_low(CS);
   spi_write(WRITECMD);
   spi_write(addr);
   spi_write(value);
   output_high(CS);
}


int CANRead(int addr)
{
   output_low(CS);
   spi_write(READCMD);
   spi_write(addr);
   tempdata = spi_read(0);
   output_high(CS);
   return(tempdata);
}


void main() {
output_high(CS); //Start CS high

setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H );


printf("Resetting CAN\n");
CANReset();


printf("Reading STAT ");
data = CANRead(CANSTAT);
printf(": %2x\n", data);

printf("Reading STAT ");
data = CANRead(CANSTAT);
printf(": %2x\n", data);

printf("Reading STAT ");
data = CANRead(CANSTAT);
printf(": %2x\n", data);

printf("Reading STAT ");
data = CANRead(CANSTAT);
printf(": %2x\n", data);

printf("Reading STAT ");
data = CANRead(CANSTAT);
printf(": %2x\n", data);


printf("Setting CTRL to 0x0F\n");
CANWrite(CANCTRL, 0x80);

delay_ms(10);

printf("Reading STAT ");
data = CANRead(CANSTAT);
printf(": %2x\n", data);

printf("Reading CTRL ");
data = CANRead(CANCTRL);
printf(": %2x\n", data);

 while (1) { }
}


I'm very stuck.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 20, 2006 12:58 am     Reply with quote

Code:
#define CS        PIN_A4

One question to ask before looking at anything else, is do you have
a pull-up resistor on Pin A4 ?
iso9001



Joined: 02 Dec 2003
Posts: 262

View user's profile Send private message

PostPosted: Mon Feb 20, 2006 3:08 am     Reply with quote

No,

I'm pulling it high then low then high again from the pic................

Oh sh!t ! IT GOT ME AGAIN!!!!!!!! ARRRRRRRR I AM SOOO FREAKING STUPID!!!

I H A T E THE A4 PIN!!!!

Seriously.... I'm a retarded desk rabbit !




I had it on A5 but when that was going on my code was crappy and it didn't anyway. So I moved it up one and didn't even think about it.

Thank You PCM, good catch. I'll try that tomorrow.
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