|
|
View previous topic :: View next topic |
Author |
Message |
iso9001
Joined: 02 Dec 2003 Posts: 262
|
Erratic SPI with 16F876A and MCP2515 |
Posted: Sun Feb 19, 2006 10:32 pm |
|
|
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
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
|
|
Posted: Sun Feb 19, 2006 11:20 pm |
|
|
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
|
|
Posted: Mon Feb 20, 2006 12:58 am |
|
|
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
|
|
Posted: Mon Feb 20, 2006 3:08 am |
|
|
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. |
|
|
|
|
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
|