Menu

SPI Digital Pins What are they?

Help
2017-10-05
2017-10-06
  • David Thompson

    David Thompson - 2017-10-05

    I'm doing some code converting but this is confusing me somewhat.

    What are the digital pins shown for hardware SPI e.g.
    define SPI_SCK digital_13
    define SPI_DO digital_11
    define SPI_DI digital_12

        Where does digital_XX come from? I can't see any reference to it in the 328P data sheet.
    
        Also most examples (328P) have:
        SPI_Reset digital_9
        SPI_DC digital_8
    
        What are they? I thought SPI was 4 wires + 2 for power. Where do these extra ones go to/from?
    
                If I don't use the last 2 will it still be using the SPI hardware?
         If I want to use a different chip e.g. ATtiny44 which pins are the digital ones?
    
     

    Last edit: David Thompson 2017-10-05
  • David Thompson

    David Thompson - 2017-10-06

    Looks like I need to answer my own question. Judging by the lack of replies and 91 views I'm not the only one mystified by these Digital_pins

    It would seem that whoever made up this example is an Arduino User. Pins 10-13 are the Arduino reference to pins PortB.2 to PortB.5

    However although digital pins 8 and 9 are indeed arduino pinouts there is no mention that I can see about using them as SPI_Reset and SPI_DC, so that question remains.

    What are SPI_Reset and SPI_DC used for and how are they connected?

    The answer may be in #include <UNO_mega328p.h

    I've just made some code using PortB instead of Digital_
    I've also removed all reference to Reset and SPI_DC
    The code compiles uning ATMEGA328P I'll see if it works later.

    If however I try using attiny44 an error occurs saying that "Symbol SPIF was not configured"
    Plus another that I can't remember.
    I know from my dabblings with 328P that SPIF is a register (bit I think) that is used to either check or clear the SPI buffer.

    I thought GCB programs were supposed to work with all supported chips.

    The Atmega328P isn't for the excluse use of Arduino users, so if code only works with 328P it would be helpful to know. If all the code examples Mega328P are for Arduino only, I for one won't be very happy.

     
  • Anobium

    Anobium - 2017-10-06

    :-)

    The pins constants, I believe, where created by Chuck to support the ChipIno. The constants are defined in UNO_mega328p.h
    You can remove the UNO_mega328p.h and directly access the port registers.

    Re ATTINY44. Not sure about this. What are you trying to do? What version of GCBASIC etc? What command? It could be the command you are using is not supported by the chip but I am totally guessing.

    Re. ' thought GCB programs were supposed to work with all supported chips.' - Yes. That is the design intent. There are clearly phyiscal limitation in terms of the chip capabilites but the design intent is supportabilty across all chips. BUT, a big but, this supportabilty across all chips means that someone has created, tested and release a specific capability. This means new chips and some obsure capability needs someone to invest time into the building the capability.

     
  • David Thompson

    David Thompson - 2017-10-06

    Hi
    I've already done that, looks like I should have looked at the PIC examples first, Most of my SPI code from a different compiler is for Attiny44/Meg328p so I looked at GCB AVR examples.
    I'm just trying to convert what I've got so that I know what I'm doing before I start trying to make the BME280 SPI work.
    I'll see if the Attiny44 code compiles using the PIC example as the source.

     
    • Anobium

      Anobium - 2017-10-06

      Rather than have me develop a BME/BMP280 library here that is different from yours - do you want the progress I have made so far?

       
  • David Thompson

    David Thompson - 2017-10-06

    Yes please. Once I get going, it would be best if the only difference between the 2 bits of code is the method of communicating i.e. I2C or SPI.

     
  • stan cartwright

    stan cartwright - 2017-10-06

    David-I thought SPI was 4 wires + 2 for power. Where do these extra ones go to/from?
    here's header for spi glcd co Anobium

    #chip mega328p, 16
    #option explicit
    #include <glcd.h>
    ;
    #define GLCD_TYPE GLCD_TYPE_ILI9341
    #define GLCD_DC   portb.2
    #define GLCD_CS   portd.7
    #define GLCD_RESET   portd.4 ; Reset line Tie high..not needed
    #define GLCD_DO   portb.3 ;  MOSI SDI
    #define GLCD_SCK   portb.5 ;    SCK
    #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
    

    same thing...diferent names
    Serial Clock:

    SCLK: SCK.
    Master Output {\displaystyle \rightarrow } \rightarrow Slave Input:

    MOSI: SIMO, SDO, DI, DIN, SI, MTSR.
    Master Input {\displaystyle \leftarrow } \leftarrow Slave Output:

    MISO: SOMI, SDI, DO, DOUT, SO, MRST.
    Serial Data I/O (bidirectional):

    SDIO: SIO
    Slave Select:

    SS: S̅S̅, SSEL, CS, C̅S̅, CE, nSS, /SS, SS#.

    ... and now port or pin for avr

     
  • David Thompson

    David Thompson - 2017-10-07

    Yes I figured that out thanks.
    I'm getting very desponent, I just cannot get SPI to work. If I remember correctly that's why I gave up on GCB before.
    It's not the hardware because I can use a different compiler with the sam settings and everything works.
    Here is my last attempt.

     
  • mmotte

    mmotte - 2017-10-07

    I recognized the module that you are attempting to control as the MAX7219. A year ago I was working with this module and it worked just fine with GCB. Attached is the code I was building into a driver module but never finished.

    I think you initialization of the module is strange.. Values may be right beacuse of "don't cares". Didn't see where you turned the display "on"

     MLED_write( 0x09 , 0xFF)
        'set intensity 3/4
        MLED_write( 0x0A , 0x06)
        'set scan limit
        MLED_write( 0x0B , 0x07)
        'set display ON
        MLED_write( 0x0C , 0x01)
    

    vs

    SPI_CS = 0
    FastHWSPITransfer  (0xfc)  'Shutdown Mode
    FastHWSPITransfer (0xff)  'Normal
    SPI_CS= 1
    Gosub wait
    SPI_CS= 0
    FastHWSPITransfer (0xff)  'Test= 1
    FastHWSPITransfer (0xfe)  'Test Off
    SPI_CS= 1
    Gosub wait
    SPI_CS= 0
    FastHWSPITransfer (0xfa)  'intensity
    FastHWSPITransfer (0xf7)  '13/32
    SPI_CS= 1
    Gosub wait
    SPI_CS= 0
    FastHWSPITransfer (0xff)  'scan limit, number of 7segs
    FastHWSPITransfer (0xfe)  '8
    SPI_CS= 1
    Gosub wait
    SPI_CS = 0
    FastHWSPITransfer (0xff)  'decode mode
    FastHWSPITransfer (0xfe)  'B
    SPI_CS = 1
    

    As you can see I did not use the "FastHWSPITransfer" method.

    You'll have to fix the processor and CS.

    Hope this helps.

    I will setup a display to test again today if I find time but it looks like a busy day ahead.

    GL
    Mike

     
  • stan cartwright

    stan cartwright - 2017-10-07

    16f887 data sheet. 28pin device, where is portd.3?

    define SPI_CS = portd.3

    oops,sorry,there's a 40pin device

     

    Last edit: stan cartwright 2017-10-07
  • mmotte

    mmotte - 2017-10-07

    David,
    I took time and wired it up to a 16F886 which just a smaller pin countwith the MAx7219.

    my chip select is on A.5

    and for the version 98 compiler I had to change the SPIMode to yours

    SPIMode MasterFast,0
    

    did not work but

    SPIMode ( MasterFast, SPI_CPOL_0 + SPI_CPHA_0 )
    

    did work

    Note the decimal point is turned on by adding "+0x80"

    MLED_write(NumDig-2 ,MLEDValueTemp +0x80)
    

    So the code I sent above works except for the SPIMode.

    GL
    BR
    Mike

     
  • David Thompson

    David Thompson - 2017-10-07

    Thanks
    I like the comment about attempting to address Max7219 and my "strange initialisation Mr. Mmotte. lol
    lol still! Oddly enough you solution looks stange too ;-) As beauty is in the eye of the beholder, One man's idea of obvious is another man's idea of total confusion.

    The code you submitted is pretty much the same in terms of data sent, just tighter.
    Where does MLED_write come from?

    My sample code is from a different compiler to GCB, I used it because I knew it worked. Then I tried to change it to suit GCB.
    I find turning test on and off usually kickstarts the 7segments. I can see in the sample I submitted there is no delay between Test on and off, I'll change that.
    It is indeed Max7219/21 but I'm not "trying" I'm well in charge of this chip, that's why I used it for this experiment. For most of my projects I use custom characters. I do a bit of astrophotography that's why I like Red 7segment LEDs. I made a 4 channel auto dew heater.
    It displays Amb temp & humidity + temperature of 4 heaters. It controls each heater using amb + hum to determine dew point and keep it a few degrees higher. 3 x Max7221 + Mega328P. I had to use Direct Addressing for the ADC (not supported) which was a bit of a slog but I got there eventually :-)

    I think I've tried SPIMode ( MasterFast, SPI_CPOL_0 + SPI_CPHA_0 ) but I'll check

    MLED_write( 0x0C , 0x01) Is the same as

    FastHWSPITransfer (0xfc) 'Shutdown Mode
    can also be 0x0C the most significant nibble can be anything. I prefer to use F rather than 0

    FastHWSPITransfer (0xff) 'Normal
    Because the 7 high bits can be anything, only bit 0 does anything
    The compiler I was using doesn't support add + data in one SPIsend (but it might I'll check)
    So:
    Hex C = Shutdown Register
    0 = Shutdown
    1 = Normal mode
    but you can't sent one nibble or bit only !!

    Just had a look at your code, I see you've had to use some direct addressing.
    Your code writing style is very different to mine but I can see well enought what you are doing.

    I'm a bit concerned about having to use direct addressing for what ought to be a simple (ish) basic routine.

    Anyway thanks for the suggestions and code, you've given me something else to try.
    I'll report back :-)

    The code you supplied works fine after I changed CS pin mapping.
    I can see now that you defined MLED_Write in the subs.

    Makes no difference if I use any of these (1 active 2 commented out)
    SPIMode MasterFast,0
    SPIMode ( MasterFast, SPI_CPOL_0 + SPI_CPHA_0 )
    SSPSTAT = 0x40 + SSPCON1 = 0x20
    They all work

    So we are back to why doesn't SPI work using CGB?

     

    Last edit: David Thompson 2017-10-07
  • mmotte

    mmotte - 2017-10-08

    David,
    I am confused.?
    You said "They all work" but then ask "So we are back to why doesn't SPI work using CGB?"
    Looks like SPI is working using GCB. Works for me!

    One other question.
    "having to use direct addressing" , What do you mean direct addressing? Please point to an example of a direct address so I may understand.

    I am not being critical, just trying to understand. Being an electrician, I was always amazed at different names for common items as you work in different industrial plants.

    BR
    Mike

     
  • David Thompson

    David Thompson - 2017-10-08

    SSPSTAT = 0x40
    SSPCON1 = 0x20

    These are registers in the 16F886/7 and they are being directly addressed by you.
    SPIMode MasterFast,0 presumably does the same but the coder is unaware of the registers being addressed. The BASIC assembler does that transparently or at least it should ;-)

    I thinks some of your code is too cleaver for me.
    I can't see where cmd or serBuf in the line below come from.
    SPITransfer cmd, serBuf
    Neither score a hit if I search the 16f886/7 DS

    What I'm saying is your code works regardless of which of 3 variants of "SPIMode MasterFast,0" I use.
    However the code I'm trying to use from the GCB examples (posted above) does not work using
    FastHWSPITransfer
    I think I tried without sucess, SPITransfer X, X but I'll try that one again later.

    Is that software SPI?

     
  • mmotte

    mmotte - 2017-10-09

    David,
    attached is your working file, remember that you need to change the CS.

    Your code had a few minor problems with scan limit and decode mode. You were not addressing the correct registers.

    My code is not too clever for you.
    "SPITransfer " is another subroutine in the hwspi.h driver file in the include lowlevel subdirectory . It is similar to FastHWSPITransfer but it takes two arguments. One that is sent and one that is received. cmd and serBuf are variables that I used when this subroutine was used.

    hwspi.h actually has four subroutine that you can use. Check out the Help file- the cow with a question mark in and you will find more examples.
    Sub SPIMode (In SPICurrentMode)
    Sub SPIMode (In SPICurrentMode, In SPIClockMode)
    Sub HWSPITransfer(In SPITxData, Out SPIRxData) aka SPITransfer
    Sub FastHWSPITransfer( In SPITxData ) 'master only

    A subroutine makes your code simpler.
    ex:
    Your code:

    SPI_CS= 0
    FastHWSPITransfer (0xfB)  'scan limit, number of 7segs
    FastHWSPITransfer (0xf7)  '8
    SPI_CS= 1
    

    could be replaced by

    MLED_write 0xfB, 0xf7  ' 0xfb goes in for 'cmd' and the 0xf7 goes in for 'dayta'
    

    You do need this subroutine in your code for it to work if you use the MLED_write command :

    sub  MLED_write( cmd , dayta)#NR
     SPI_CS= 0
    FastHWSPITransfer (cmd)  
    FastHWSPITransfer (dayta)  
    SPI_CS= 1
    end sub
    

    I agree the example in the demo file for SPI is a bit overwhelming because it brings together many of the capabilities of GCB all at one time. It is hard to pick out what applies and what is extra code. Also it has compiler directives which i even am just starting to understand. We need more simple code examples and they can be added by anyone now using the GitHub that we using for Help and examples.

    My wife says I give too much info. I hope this isn't overwhelming. But here is your code working!

    BR
    Mike

     
  • David Thompson

    David Thompson - 2017-10-09

    Give that man a Star ****
    That works Mike thank you very much.
    Only question I have is why PortA.5 and not PortD.3 for CS/Load or is that one of lifes little mysteries?

    Just Checked the 16f886/7 DS and SS is shown as RA5 but rather strangely RA4 will work too!!!

     

    Last edit: David Thompson 2017-10-09
  • mmotte

    mmotte - 2017-10-09

    David,
    Port A.5 is just the pin I wired CS to. The 16F886 does not have PortD.3 on any pins. nothing magic!

    Anobium,
    I do want to work on some demo files but GitHub seems another language to me. I will dive in when I get a chance.

     
    • Anobium

      Anobium - 2017-10-09

      @Mmotte. Do not struggle with GitHub. Take a copy of a file and email to me, or, or add new = email again.

       
  • stan cartwright

    stan cartwright - 2017-10-09

    Mmotte. "I do want to work on some demo files but GitHub seems another language to me."
    Ha. So I'm not alone. repository/suppository

     
  • David Thompson

    David Thompson - 2017-10-10

    Just had another look, works fine on PortD too.
    Looks like Test Off got copied into Scan Limit and Decode Mode. How I don't know, maybe when I did search and relace. Anyway alls good now, except Proton which still doesn't work but I'm not worried about that.

     

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.