Menu

Using software SPI

Help
MBB
2017-12-15
2017-12-17
  • MBB

    MBB - 2017-12-15

    I'm using an 18F4550 and version 0.98.01.

    On the 18F4550, SDO is PORTC.7, SDI is PORTB.0, and SCK is PORTB.1

    In the GCB Help file, under the SPI Overview section, it says:
    "#define SPI_HardwareSPI 'comment this out to make into Software SPI but, you may have to change clock lines"

    Does this means if I comment out this line, I can change SDI from PORTB.0 to PORTD.7?

    Is there any other code I must use to change Ports?

     
    • Anobium

      Anobium - 2017-12-15

      What library are you using? The Help is referring to GCB library devices.

       
  • MBB

    MBB - 2017-12-15

    By library do you mean hwspi.h which I'm using?

     
    • Anobium

      Anobium - 2017-12-16

      I understand. A number of the SPI device libraries, most of them, support hardware and software implementation for SPI.

      If you looking at the Help: SPI Overview then I understand.

      The "#define SPI_HardwareSPI' refers to the example code shown.

      This example demonstrates the SPI capabilities for the mega328p. The process is similar of any microcontroller..

      This example show using the hardware SPI option and a sofware SPI option.

      Using hardware SPI mode - make sure the #define SPI_HardwareSPI is not commented out. Using software SPI mode - comment out #define SPI_HardwareSPI. The example code will then use software SPI.

      So, for your 18F4550 use the same method shown in the SPI Help example.
      define your ports, InitSPIMode (as shown in below and in the Help) and call SendByteviaSPI - SendByteviaSPI will handle the Hardware and Software SPI support for you.

      Evan

          #define SPI_HardwareSPI  'comment this out to make into Software SPI but, you may have to change clock lines
      
          'Pin mappings for SPI - this SPI driver supports Hardware SPI
          #define SPI_DC       portx.n          ' Data command line
          #define SPI_CS       portx.n          ' Chip select line
          #define SPI_RESET    portx.n          ' Reset line
      
          #define SPI_DI       portx.n          ' Data in | MISO
          #define SPI_DO       portx.n          ' Data out | MOSI
          #define SPI_SCK      portx.n          ' Clock Line
      
          dir SPI_DC    out
          dir SPI_CS    out
          dir SPI_RESET out
          dir SPI_DO    Out
          dir SPI_DI    In
          dir SPI_SCK   Out
      
          InitSPIMode
      
          'Send SPI bytes independent of hardware and software SPI config
          do forever
              set SPI_CS OFF;
              set SPI_DC OFF;
              SendByteviaSPI (byte1)
              set SPI_CS ON;
              set SPI_DC ON
      
              set SPI_CS OFF;
              set SPI_DC OFF;
              SendByteviaSPI (byte2)
              set SPI_CS ON;
              set SPI_DC ON
      
              set SPI_CS OFF;
              set SPI_DC OFF;
              SendByteviaSPI (byte3)
              set SPI_CS ON;
              set SPI_DC ON
      
              wait 10 ms
          loop
      
      '************** MEthods
      
      sub InitSPIMode
      
            #ifdef SPI_HardwareSPI
                SPIMode ( MasterFast, SPI_CPOL_0 + SPI_CPHA_0 )
            #endif
      
            set SPI_DO OFF;
            set SPI_CS ON;   therefore CPOL=0
            set SPI_DC ON;   deselect
      
      End sub
      
      sub  SendByteviaSPI( in SPISendByte as byte )
      
        set SPI_CS OFF
        set SPI_DC OFF;
      
        #ifdef SPI_HardwareSPI
           FastHWSPITransfer  SPISendByte
           set SPI_CS ON;
           exit sub
        #endif
      
        #ifndef SPI_HardwareSPI
        repeat 8
      
          if SPISendByte.7 = ON  then
            set SPI_DO ON;
          else
            set SPI_DO OFF;
          end if
          SET SPI_SCK On;           ; therefore CPOL=0 ==ON, and, where CPOL=1==ON
          rotate SPISendByte left
          set SPI_SCK Off;          ; therefore CPOL=0  =OFF, and, where CPOL=1==OFF
      
        end repeat
        set SPI_CS ON;
        set SPI_DO OFF;
        #endif
      
      end Sub
      
       
  • MBB

    MBB - 2017-12-17

    Thanks!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.