SPI and 18F4550


Closed Thread
Results 1 to 11 of 11

Thread: SPI and 18F4550

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default SPI and 18F4550

    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

    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!

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: SPI and 18F4550

    Hi,
    There's an example on MELABS website showing how to use the MSSP module if that's the idea.

    /Henrik.

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: SPI and 18F4550

    I think that's the idea. Thanks for the link. I hope I can get this thing working.....

  4. #4
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: SPI and 18F4550

    There is also this article that i made;
    http://www.picbasic.co.uk/forum/cont...P-PBP-MSSP-ASM
    Thanks and Regards;
    Gadelhas

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: SPI and 18F4550

    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

    Code:
    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

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: SPI and 18F4550

    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.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts