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,319
    Likes:
    649
    Location:
    Ex Yorks' Hants UK
    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:
    122
    Likes:
    5
    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,319
    Likes:
    649
    Location:
    Ex Yorks' Hants UK

    Attached Files:

  4. dave

    Dave New Member

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


     
  5. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5

    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,319
    Likes:
    649
    Location:
    Ex Yorks' Hants UK
    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:
    122
    Likes:
    5
    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,319
    Likes:
    649
    Location:
    Ex Yorks' Hants UK
    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:
      38
    • A04.gif
      A04.gif
      File size:
      28.6 KB
      Views:
      42
  9. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5
    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
     
    • Like Like x 1
    • Dislike Dislike x 1
  10. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5
    Hi
    My lack of input has not been due to inactivity, the BME280 is a real beast of a sensor to program. Certainly the most difficult one I've ever tried to code, the most difficult part is understanding the pretty horrendous equations. I've been doing my own research and also a bit of collaboration with ppl on the GCB forum.
    The good news is that I've managed to get Temperature and Humidity working, there isn't enough flash on a 16f887 for Pressure, the numbers are on the big side. It is coded for SPI
    I need to tidy up and comment the code, then I'll post it under a new thread. I'm not over bothered about Pressure, so it will have to wait a while, I'll try using a Mega328P because I have some.

    The code highlights a number of things in Oshonsoft that don't work or don't work as expected, but there were workarounds.
     
  11. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,511
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    I will be using BMP280 modules 0n 18LF4520 PICs here later: http://www.electro-tech-online.com/threads/lcd-spi-projects.152107/page-4
    If you concider your programs relevant, will you post a copy in the thread please.
    C.
     
  12. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5
    Hi C
    I've gone quiet again ;-)
    My sensor is playing up so I can't verify that the changes I've made work as expected. I think I'll post it as a "Works in Progress".
    I'm also suffering from "Burn out" on this project so I need to do something else for a while.
    Setting the configuration and reading calibration and Press,Temp, & Hum data is no mean feat in itself. BME & BMP have the same register locations.
    I know that I've got that part working, so it will help anyone starting from scratch.
    The next phase and IMO most difficult is getting the gigantic set of equations to work with the calibration and PTH data.
    Or more to the point produce the expected result.
    The calibration data has Signed Words which Oshonsoft doesn't so these need to be handled manually.
    Floats (Single) are also used but as far as I can see are not essential for Temp and Hum.
    I've been working with ppl on the GCB forum, GCB has 16 bit signed Words (Integer) but does not have Floats.
    Pressure is causing problems!
    Why you might ask? The are masses of parenthesis in the enormous equations and they need to be modified.
    The wrong position of a set of Parenthesis makes an incredible difference to the result.

    I don't have access to the code on this PC, I'll post it later today. I'll give it a new Thread Name i.e. BME280 SPI
     
    • Informative Informative x 1
  13. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,511
    Likes:
    11
    Location:
    Dorset UK.
    Hi D,
    Sounds like a good idea to have a rest if you've got 'burn out' I sometimes feel like a terrier with a rag, that won't let go.
    I didn't know it was so involved to get readings from these little modules.
    I'll look out for your thread. will you call it BME/BMP280 please?
    C.
     
  14. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5
    Yes you are correct, I had a niggling bad feeling about this project and sure enough after a quick check I found a wire adrift, now humidity works but it's too low 11%
    If I give it a "haw" the humidity goes up so the sensors working but not very accurately.
    I think it might be SPI related becuase I get different calibration data from the same BME280 when using Oshonsoft or GCB
    I'm talking big differences, but it only needs a slip of a few bits to completely change things.
    Unfortunatly the Stretch_Clock doesn't work so tweaking isn't possible. Maybe a different SPI FOSC
    Very strange though because the Data Analyser shows output data exactly as expected.
    Anyway more research needed and I'm still burnt out, I just don't want to do it , yet!
    Bottom line is I've not posted today as promised, I need to put in a few more comments, and today's discovery took the time away.
    I'll still post it, as it will still help if you haven't done anything with the BME/BMP before. I'll do the extra comments first.
     
  15. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,511
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    Good to see you have results.
    Best thing it to have a break, until you're ready.
    I'm struggling with other things also, but once I have my test PCB, I'll be able to help you with your tests.
    Cheers for now.
    C.
     
  16. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,319
    Likes:
    649
    Location:
    Ex Yorks' Hants UK
    Hi,
    Its easy to get stale on a niggling project, resulting in diminishing returns on your efforts.:banghead:
    I find the best cure is to switch to another project, that requires some physical effort for a couple of days.

    E
     
  17. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,511
    Likes:
    11
    Location:
    Dorset UK.
    Hi E,
    Instead of physical effort, I chose to push on with another difficult thing, making a PCB. I think I've drawn it at least. This afternoon, I should be able to check it and etch it.
    Here's a photo.
    EDIT: There are a few minor erros, but it should work as a test, then Ill improve it later.
    Cheers, C
     

    Attached Files:

    Last edited: Nov 28, 2017
  18. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5
    Hi E and C
    I agree, I've been fixing my vehicle access restriction Post (Bollard). It's been laying the garage for months awaiting a time slot. It's the sink into the ground type so corrosion & gunk from water ingress often cause problems.
    Well I've sort of fixed the code for both Basic compiler varients, it wasn't SPICLK FOSC just good old programmer error. As you say Eric, you can get stale mentally, so much so, I doubt if I could tell if an LED was ON or OFF. Oshonsoft is quite limited in its Bit Wise operations so it's not a simple cut and paste from GCB which is very good for Bitwise.
    "Sort of fixed" means I get the same result form the data analyser and both varients of Basic compiler & the spreadsheet. But....the result is 10%. My bought humidity monitor says 42%
    So my very unsatisfactory fix is 10% x 4 !! The sensor is quite reactive and a "haw" produces a similar responce on both humidity sensors. If I wanted to be really technical I could use x 4.2!!
    I must say that having an accurate humidity sensor (BME280) and then multiplying the result by a "suitable" number to get the desired result does not go down well.
    Next I have to check when the temperature goes below zero and put in some code to handle it. I keep getting distracted from that task.
    I'll attach the results then.

    Nice board C, one advantage of SMD, shame about the tiny traces, I can hardly see them. I've just bought a Hot Air Rework Stations so I'll be doing some SMD experiments soon. Once I've sorted this B****dy280 sorry BME280 ;-)
     
  19. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,511
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    Bollards! Check this site, it has helped me with a lot of ideas over the years: www.diynot.com They may suggest a flexible paint or similar for the submersed bit.

    PCB: I can give you tips on SM surface mount stuff for beginners, if you ask. e,g, USB microscope.
    C
     
  20. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    122
    Likes:
    5
    Well all this grief is not without benefit, I went back to Proton Basic and quickly solved my earlier problems. Just one biggy no sensor output, just a repeating pattern of 0,0,128 decimal.
    I've posted on the Proton Forum to see if there's a solution.
    If not I think I'll retire from this project, it's easier to learn how to make Arduino work with my peripherals.

    C, although I'm not a beginner with pcb making, I'm always interested in anything that might improve my results
     
  21. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,511
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    Could you post your code please? I may be able to try it later.
    Sorry, I presumed you were a beginner. I am testing a metal plate sandwich for transferring the PCB image from laser print, where I'm improving. Also finding the easiest way for me.
    C
     

Share This Page