1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Oshonsoft with SPI device and Max7219/21

Discussion in 'Oshonsoft' started by Superdat, Jul 19, 2017.

  1. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    OK.
    I am happy to help.
    It is difficult to give meaningful advice if I don't have have a clear understanding of exactly what you are trying to achieve.

    Why are you using assembly code to work with PIC's user specified pins for the SPI I/O when the Oshonsoft Basic could be used with the PIC's internal SPI regs.?

    E

    Define SPI_CS_REG = PORTB
    Define SPI_CS_BIT = 5 'the PCB is marked CSN, Orange
    Define SPI_SCK_REG = PORTB
    Define SPI_SCK_BIT = 4 'White
    Define SPI_SDI_REG = PORTB
    Define SPI_SDI_BIT = 1 'Blue 10K pu.
    Define SPI_SDO_REG = PORTB
    Define SPI_SDO_BIT = 2 'Green
    SPIPrepare


    Example of a SPI Func:
    Function rd_fifo(arg1 As Byte) As Byte
    SPICSOn
    SPISend 0xa0
    WaitUs 10
    SPIReceive fifo
    WaitUs 10
    SPICSOff
    End Function
     
  2. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    114
    Likes:
    4
    OK
    I'm trying to use hardware SPI so I can use 2 SPI devices the PIC as the Master the BME280 as the slave + Max7219/21 as another slave.
    This means 2 x CS lines which Oshonsoft's software SPI doesn't support.
    I don't know how to use Assembly so I've been using Direct Addressing to enable hardware SPI.
    I have encountered another problem though (which is probably what killed the BME280). BME280 has clock idle high while Max7219 is clock idle low.
    The circuit was pulled down when I connected the 2 clocks. So my project is probably dead anyway. Looks like I'll have to use a lcd.

    But! having got this close, I'd like to see a completed hardware SPI.
    What I cannot figure out is how to detect the RX flag and buffer full (BF) using direct addressing as shown in the Datasheet.

    You say use an Interrupt, this is what I don't know how to do.

    This example from Pic Basic Pro uses SSPIF to detect, but it has a strange couple of lines in the get data section.
    I've changed the file extension so I can upload it, this site doesn't like pbp.
    SSPBUF = "?" ' send ? to start conversion
    GoSub letclear ' wait for buffer to clear
    IF SSPBUF<>"!" Then getdata ' wait for reply (!)

    Where the heck does "!" come from and why?
     

    Attached Files:

  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE

    Attached Files:

  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    114
    Likes:
    4

    Hi
    Yes but isn't that using C? The code in SPIMast.pbp was Pic Pro Basic and the ! was within inverts i.e. "!" so surely that makes it text.
    I've just read an article that explains why polling isn't so good and interrupts better.
    Would INTCONbits.GIE = TRUE do the trick?
    Then "ON Interrupt"
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi S,
    I do not see a direct way of using Oshon Basic for two SPI devices, but the following method is worth pursuing.
    Assign the SPI pins as normal, use two additional output pins on the PIC as CS1 and Cs2.
    Connect the CS1 line to one devices CS select and the CS2 line to the other devices CS select,, the 'normal' CS0 pin is not connected.

    Refer to the attached Basic demo and scope image for the method and result.

    E

    EDIT:
    I am assuming that you have the PIC/MCU as the SPI master and the two external SPI devices are slaves.??
    If that is the case, the PIC/MCU will control the data I/O transfer from the external devices, so why did you consider using interrupts in the master.?
    Do the two external devices have any sort of data Ready flag, that you could use for a MCU interrupt.?
     

    Attached Files:

    Last edited: Aug 26, 2017
  7. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    114
    Likes:
    4
    Hi E
    That's more or less what I'm doing with the direct addrssing. I didn't try the method you show in the .bas file, but that looks promising. Set CS but don't use it.
    The PIC is the master and the BME and Max7219 are both slaves.
    Before I try anything else though, I have a hardware problem. When I connected up SDI, SDO and SCK to the BME280 (Max 7219 was already connected) , the 5V went down.
    I tried a few variations, and found that SCK was causing the problem. I think that's what killed the BME280.
    The BME worked fine on its own, I was able to set up and receive data from the BME. I even got Temp, Pressure and Humidity readings using Oshonsoft AVR IDE.
    I switched to PIC16F877a after my Mega328P failed (bad connection on the Xtal).
    The max7219 also worked with 877a. it was only when the SCK was joined up to all 3 devices that the 5v failed. Joining SPI/SPO made no difference.

    I checked the 3.3v-5v level changer this morning and that is working as it should, so definitely a dead BME280.
    The only difference I can see between BME and Max is that the clock (SCK) is idle low for one and idle high for the other.
    I can't see why that should destroy the chip though.
    I think I'll buy a 5v version of the BME280, i.e. one with built in level changer, at least that takes one bit of the circuit out of the equation.

    Oh yes why did I consider using Interrupts .....because you said so!!! Although polling isn't as efficient, the PIC hasn't got much else to do in this program, so it probably doesn't make too much difference. I think I'll persue the Interrupt method because it's something I want to understand better.
    The Max7219 is pretty dumb, data in only, data out of its shift registers at SDO to another max7219 if needed.
    The BME is a slave too but it does have a lot of configuration registers, most of which can be read. The favouite is chip id, sending to register 0x0D (no data needed), will get a return byte of 0x60, its id. So even if nothing else is setup, the chip id will show a sign of life.

    Anyway I have plenty to work with now, the main problem was the hardware, I wasn't getting any response from the BME280 because it was dead! Why is another matter.
    I have a suspicion that the program will work fine with a functioning BME280.

    The best made plans of mice and men!!! :)
     
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi S,
    Is it possible that you have damaged the BME by powering up the MCU, BME and Max in the 'wrong' sequence.? ref image A03, A04.

    E
     

    Attached Files:

    • A03.gif
      A03.gif
      File size:
      32.6 KB
      Views:
      25
    • A04.gif
      A04.gif
      File size:
      28.6 KB
      Views:
      22
  9. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    114
    Likes:
    4
    Hi
    Hard to say, the BME came on a mini breakout board, it only has one 3.3v imput. So I guess the plan is that both VDD and VDDIO are powered simultaneously.
    None of the mini pcbs come with a separate VDD and VDDIO, just 3.3V or 5V for those with a built in 3.3V regulator.
    VDD and VDDIO on the chip both have a track to a capacitor (1 each) but I can't see any more tracks to follow. There's another 4 resistors and that's it so
    I definitely did not have power on when I made the connections. The 5V dropped slowly over about 2 seconds. When I was testing each wire, I powered off as soon as the power started to dip. VDDIO is used for I2C SDA and SCL!!!!!. Maybe because I was hooked up to the PICKit2 at the same time SCK got some voltage, but then the 3.3V regulator would ave needed to be powered to pass it on to the BME. I'm using 2N7000 as the voltage levelers.

    Anyway before I try another BME, I'll see if I can get SPI working between two MicroCs. Then I'll connect up very carefully.

    It's interesting to see that the much more expensive Adafruit breakout board is for 3.3v input but has 4 additional devices onboard, 2 could be transistors, 1 a diode and a 5 pin device that could be anything (OP Amp?). They may have isolated VDDIO and VDD. Unfortunately the price ratio between Ada and Ebay is about 5:1
     

Share This Page