Menu

MEGA328P SPI - Issue - please help me understand

Help
Anobium
2017-04-04
2017-04-04
  • Anobium

    Anobium - 2017-04-04

    I justed posted the code below.

    My question Why must I set portb.2 as an output? This took me hours to figure out. The demos use portb.2 as the CS line but the code below did not - I used a shield and the shield exposes portd.4.

    I had to set portb.2/Digital 10 as an output - if I did not hardware SPI failed - why?

    Ideas? Comments?

    Sample Code Commentry

    This shows how the MEGA328p can support hardware and software SPI. To use hardware make sure the #define SPI_HardwareSPI is not commented out.
    To use software comment out #define SPI_HardwareSPI.

    To use the hardware SPI the Data Out, Data In and Clock (DO/DI and SCK) cannot be moved but the optional Data Command, Chip Select and Reset are all moveable.

    To use the hardware SPI the Data Out, Data In and Clock (DO/DI and SCK), Data Command, Chip Select and Reset are all moveable.

    I have called InitSPIMode to call SPIMode if needed, and set the lines.

    I have also define a sub called SendByteviaSPI to handle whether to call the Hardware or use Software (bit-banging) SPI.

    'Chip Settings.
        #chip mega328p, 16
        #option explicit
        #include <UNO_mega328p.h >
    
        #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       DIGITAL_8         ' Data command line
        #define SPI_CS       DIGITAL_4          ' Chip select line
        #define SPI_RESET    DIGITAL_9          ' Reset line
    
        'mandated for hardware
        #define SPI_DI       DIGITAL_12          ' Data in | MISO 
        #define SPI_DO       DIGITAL_11          ' Data out | MOSI
        #define SPI_SCK      DIGITAL_13          ' 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
    
        dir DIGITAL_10 Out    'needed and I am not sure why!
    
        DIM byte1 As byte
        DIM byte2 As byte
        DIM byte3 As byte
        DIM tt as byte
        DIM SPISendByte as byte
    
        byte1 = 100 ' temp values (will come from potentiometer later)
        byte2 = 150
        byte3 = 200
    
        InitSPIMode
    
    
    
        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 100 ms
        loop
    
    
    
    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 )
    
      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
    

    ~~~

     
  • Anobium

    Anobium - 2017-04-04

    Read the datasheet!!

    *Master Mode
    When the SPI is configured as a Master the user can determine the direction of the SS pin (this is DIGITAL10). If SS is configured as an output, the pin is a general output pin which does not affect the SPI system.
    Typically, the pin will be driving the SS pin of the SPI Slave.
    If SS is configured as an input, it must be held high to ensure Master SPI operation. If the SS pin is driven low by peripheral circuitry when the SPI is configured as a Master with the SS pin defined as an input, the SPI system interprets this as another master selecting the SPI as a slave and starting to send data to it.
    *

    So, when we initialise the Mega328p it is an input. The input left floating will be interpreted as another master selecting the SPI and all communications cease. Therefore, if I do not use Digital 10 as SS/Chip Select either set as an output or pull high.

    Case Closed - RTFM

     

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.