PDA

View Full Version : SPI and 18F4550



Christopher4187
- 8th July 2012, 18:56
I'm a SPI virgin. At the same time, I haven't had the fun experience of working with CAN yet. I have a project that requires both :confused:

http://www.picbasic.co.uk/forum/showthread.php?t=16758

I've searched the forum for SPI but nothing useful comes up that I can use. Right now I'm taking it slow and I'm not worrying about the CAN portion yet. I only want to understand SPI right now and get the 18F4550 and MCP2515 to talk to each other. Can someone help me get the ball rolling and point me in the right direction. If there are some sample codes that would be great!

HenrikOlsson
- 9th July 2012, 06:08
Hi,
There's an example (http://melabs.com/samples/PBP-mixed/spimast.htm) on MELABS website showing how to use the MSSP module if that's the idea.

/Henrik.

Christopher4187
- 9th July 2012, 13:49
I think that's the idea. Thanks for the link. I hope I can get this thing working.....

gadelhas
- 9th July 2012, 16:07
There is also this article that i made;
http://www.picbasic.co.uk/forum/content.php?r=471-MCP23S17-Three-different-approaches-ShiftIn-ShiftOut-MSSP-PBP-MSSP-ASM

Christopher4187
- 10th July 2012, 05:11
Thanks for the info. It helped a little but I'm nowhere closer now than I was on Friday. Not one CAN frame received, nor was one sent. Part of my problem is that I don't completely understand the register settings listed in the datasheet. I keep reading it but it's not 100% clear so I'm sure my settings in the code aren't going to work. Can you (or anyone else) take a look at this and let me know where my mistakes are? I'm sure there are plenty:eek:


DEFINE OSC 20

' REGISTERS AND PINOUT ( 1 = IN; 0 = OUT )
' ================================================== ==============
'76543210 '76543210
TRISA = %11011111: PORTA = %00000000
TRISB = %00000000: PORTB = %00000001
TRISC = %00010000: PORTC = %00000000
TRISD = %00000000: PORTD = %00000000
TRISE = %00000000: PORTE = %00000000
ADCON0 = %00000000
ADCON1 = %00000000
ADRESH = %00000000
ADRESL = %00000000
CMCON = %00000000
SSPSTAT = %00000000 'I'M A LITTLE LOST WITH THE SSPSTAT AND SSPCON1
SSPCON1 = %00110000

' VARIABLES AND CONSTANTS
'================================================= =============================
TXBNCTRL var byte
TXBNSIDH var byte
TXBNSIDL VAR BYTE
TXBNDLC VAR BYTE
TXBNDM7 VAR BYTE
TXBNDM6 VAR BYTE
TXBNDM5 VAR BYTE
TXBNDM4 VAR BYTE
TXBNDM3 VAR BYTE
TXBNDM2 VAR BYTE
TXBNDM1 VAR BYTE
TXBNDM0 VAR BYTE
B0 VAR BYTE
B1 VAR BYTE
B2 VAR BYTE
B3 VAR BYTE
B4 VAR BYTE
B5 VAR BYTE
B6 VAR BYTE
B7 VAR BYTE




B7 = TXBNDM7 ' 8TH BYTE OF DATA FRAME
B6 = TXBNDM6 ' 7TH BYTE OF DATA FRAME
B5 = TXBNDM5 ' 6TH BYTE OF DATA FRAME
B4 = TXBNDM4 ' 5TH BYTE OF DATA FRAME
B3 = TXBNDM3 ' 4TH BYTE OF DATA FRAME
B2 = TXBNDM2 ' 3RD BYTE OF DATA FRAME
B1 = TXBNDM1 ' 2ND BYTE OF DATA FRAME
B0 = TXBNDM0 ' 1ST BYTE OF DATA FRAME

'THE PIC MUST ENABLE THE CANINTE.TXINE BIT TO CHANGE THE INTERRUPT FLAG
'THE PIC MUST CLEAR THE TXBNCTRL.TXREQ BIT BEFORE WRITING TO THE TRANSMIT BUFFER


' ALIAS & MODIFIERS
' ================================================== ==============
CS VAR PORTB.4
SCLK VAR PORTC.3
SDI VAR PORTB.0
SDO VAR PORTC.7
SENDLED VAR PORTA.5 'LED TO INDICATE THE PROGRAM IS IN THE SENDING ROUTINE
MAINLED VAR PORTE.2 'LED TO INDICATE THE PROGRAM IS IN THE MAIN ROUTINE




' START OF PROGRAM
'================================================= ===============================
START:
TOGGLE MAINLED 'LED INDICATING THE 18F4550 IS IN THE MAIN ROUTINE
PAUSE 100
GOSUB SENDCANDATA 'CAN DATA IS ABOUT TO BE SENT......OR WE COULD ONLY WISH......
GOTO START

' SEND CAN DATA SECTION
'================================================= ==============================
SENDCANDATA:
PAUSE 50
TOGGLE SENDLED ' LED FOR DEBUGGING
LOW CS ' CS PIN IS HELD LOW IN ORDER TO TRANSMIT DATA
TXBNDLC = %00000111 ' DATA LENGTH. THIS IS FOR 8 BYTES.
TXBNCTRL = %00001011 ' THE TXBNCTRL IS A REGISTER THAT DETERMINES THE CONDITIONS UNDER WHICH THE MESSAGE WILL BE TRANSMITTED. SEE DATA SHEET FOR DESCRIPTION.
TXBNSIDL = %11100000 ' STANDARD IDENTIFIER BITS
TXBNSIDH = %11111111 ' NOT REALLY SURE WHAT MICROCHIP WANTS HERE. IT JUST STANDARD IDENTIFIER BITS?

B0 = $0 'DATA IN BYTE 1
B1 = $01 'DATA IN BYTE 2
B2 = $03 'DATA IN BYTE 3
B3 = $06 'DATA IN BYTE 4
B4 = $08 'DATA IN BYTE 5
B5 = $0A 'DATA IN BYTE 6
B6 = $0C 'DATA IN BYTE 7
B7 = $0E 'DATA IN BYTE 8


IF TXBNCTRL.3=1 OR TXBNCTRL.5=1 OR TXBNCTRL.4=1 THEN GOTO SENDCANDATA 'THERE WAS AN ERROR, TRY AGAIN.
HIGH CS ' DATA TRANSMISSION IS COMPLETE. PUT CS LINE BACK TO 1.
PAUSE 1000

RETURN

HenrikOlsson
- 10th July 2012, 06:20
Hi,
As far as I can see there's nothing in your code that actually sends any data anywhere. In the SENDCANDATA you pull the CS line low, then you assign some values to a couple of variables, then you pull CS line high again. There's nothing to actually shift any data out of the PIC using either SHIFTOUT/SHIFTIN or the MSSP module.

Is there any chance you've looked at some code for a PIC with a built in CAN tranceiver? Is that perhaps where the TXBNDLC etc variables are coming from? If so, then those are probably registers for that particular PICs internal CAN tranceiver and simply creating variables with the same name will obviously not get anything out of your 2550 and into an external CAN tranceiver.

/Henrik.

Christopher4187
- 10th July 2012, 10:47
I was thinking the same thing last night about sending data but I really don't know how the SPI function works. Here is an excerpt from the MCP2515 data sheet (stand alone CAN IC) where I got the TXBNDLC variable from:

6574

HenrikOlsson
- 10th July 2012, 11:31
Yeah, but that register is in the MCP2515 - you're only assigning values to variables in the PIC, there's currently nothing that transfers the data TO the register in the MCP2515.

Look at the SPI section of the datasheet for the MCP2515, under Write instruction it says

The Write instruction is started by lowering the CS pin. The Write instruction is then sent to the MCP2515 followed by the address and at least one byte of data.
Then, looking at table 12-1 we can see that the Write instruction is (in binary) 00000010 or 2 in decimal. As you can see in your screenshot from the datasheet the adress of the TXBnDLC register(s) in the MCP2515 is $35, $45 and $55 respectively (apparently there's three of them, I haven't read enough yet to understand exactly what they do).

So to set the first of the TXBnDLC registers you need to shift out the WriteCommand (2), the adress ($35) and the actual data (7). The SPI section of the datasheet also tells us the device supports mode 0,0 and 1,1 and the timing diagram shows that it expects (and sends) MSB first. So, using SHIFTOUT the above would look something like

INCLUDE "modedefs.bas"

Write_Cmd CON %00000010 ' Value for the Write Command
TXB1DLC CON $35 ' Adress of the 1st TXBnDLC register in the MCP2515

LOW CS
SHIFTOUT DataPin, ClkPin, MSBFIRST, [Write_Cmd, TXB1DLC, 7]
HIGH CS
The example I linked to earlier and the article gadelhas wrote shows you how to set up and use the MSSP module.

/Henrik.

Christopher4187
- 10th July 2012, 12:53
Henrik,

You've made it a little clearer for me, thanks. I'm going to work with this today to see if I can get it to work. Something is strange with the demo boards I've received from Microchip. I can't erase the chip and even blinking an LED can be a task. I'm not even sure if the program I'm compiling is even getting to the 18F4550 at certain times. When I make the same circuit on a breadboard it doesn't give me any problems so I may have two issues at the same time, which makes things even more difficult.

Chris

Christopher4187
- 10th July 2012, 17:36
I'm getting closer. I couldn't figure out what was going on so I pulled out my scope and it seems pretty clear......at least for this step. This is a picture of the scope with a known good program (I'm using the same board for the working program and my program so I know the hardware is good). Yellow is the data and blue is the CS line.

6576

Now look at the scope with my program:

6577

So my signal looks like it's inverted. They speak about this in the BAUDCON, TXSTA and RXSTA settings but I tried adjusting them and it didn't help. Any ideas?

EDIT - It was under the SSPSTAT.6. The signal is correct now but still no CAN data. At least I'm getting closer......

HenrikOlsson
- 10th July 2012, 17:50
Ok, what exactly are we looking at?
My guess is the MOSI pin on the PIC (Ch1) and the CS line (Ch2)?

Without seeing your code it's a bit hard to guess what you're actually doing but what I can say is that BAUDCON, TXSTA and RXSTA (if we're talking about the PIC) are control registers for the USART and has nothing to do with the either the MSSP module or the SHIFTOUT/SHIFTIN - whatever you're actually using. Post the code ;-)

/Henrik.