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

SPI Code for BME280

Discussion in 'Oshonsoft' started by Superdat, Nov 29, 2017.

  1. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    128
    Likes:
    5
    Hi
    This a partial solution for anyone who is trying to use the BMe280. The Pressure/ Temperature part is very similar if not identical for BMP180/280.

    With reference to the Datahseet there are two problems within the code:
    1:
    Most of the data uses signed 32 and 16 bit variables which Oshonsoft does not have.
    It also uses Floats(Single) whivh Oshonsoft does have but I didn't find this very helpful.
    I've also been trying GCB which has 16 bit signed but no Floats, again with limited success.
    2: The equations used to calculated Pressure/Temp and Hum are horrendously complex, especially Pressure.
    This makes it extremely difficult to figure out a work around for the variables issue.

    The attacked code achieves the following:
    Writes Configuration data
    Can Read all Registers
    Chip id, Calibration and P,T & H data etc.
    Temperature works for + numbers (thanks to MMotte on the GCB forum for the Temp equation).
    Humidity works but is wildly inaccurate, it shows 10% when it is about 42%
    I've not done anything with Pressure (MMotte is working on it) because the calculations won't fit on a 16f887.
    This was done initally with GCB and converted to Oshonsoft.

    Proton Basic as all the required variables, has working I2C code which I've modified for SPI, it does everything but won't read PT & H data so another brick wall.

    Good Luck
     

    Attached Files:

  2. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,823
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    I haven't read your program properly, but try turning the BME280 'Chip select' ON for each SEND or SEND/RECEIVE then OFF [You may not need all of them]
    C.

    SPICSOn
    CS ON ##################################################
    SPISend chipidreg 'Request Chip ID
    CS OFF ##################################################
    CS ON ##################################################
    SPIReceive chipid 'Read Chip ID
    CS OFF ##################################################
    SPICSOff
    Hserout "chipid ", #chipid, CrLf
    'SPICSOn
    CS ON ##################################################
    'SPISend reset 'reset
    CS OFF ##################################################
    'SPICSOff
    'WaitMs 3
    'Write Configuration
    SPICSOn
    CS ON ##################################################
    SPISend ctrlhumreg 'configReg
    CS OFF ######################################
    CS ON ##################################################
    SPISend ctrlhumdata 'Write ctrlhum
    CS OFF ######################################
    SPICSOff
    SPICSOn
    CS ON #########################################
    SPISend ctrlmeasreg 'CtrlmeasReg
    CS OFF ######################################
    CS ON ##################################################
    SPISend ctrlmeasdata 'Write Ctrlmeas
    CS OFF ##################################################
    SPICSOff
    SPICSOn
    CS ON ##################################################
    SPISend configreg 'configReg
    CS OFF ##################################################
    CS ON ##################################################
    SPISend configdata 'Write config
    CS OFF ##################################################
    SPICSOff
     
  3. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    128
    Likes:
    5
    Hi C
    The code works as supplied, the big mountain is converting the horrendous"C" based data conversion to work with Basic.
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,823
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    Re your line: it does everything but won't read PT & H data so another brick wall.
    Are you saying that the program on #1 works now?

    Is 'the big mountain' a member? I couldn't find him. Could you give me a link please?
    C
     
  6. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,805
    Likes:
    958
    Location:
    Rochdale UK
    Aw Bless....

    He means he had a hill to climb..
     
  7. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,823
    Likes:
    11
    Location:
    Dorset UK.
    Hi I,
    If it can be read in more ways than one, count me in:)
    C
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    128
    Likes:
    5
    Hi C & I, are you taking the proverbial?
    The Code will as it says in the 1st post read all of the data for P,T & H
    It/I cannot convert the Math conversion code in the datasheet into a BASIC format, it is needed to convert the data into P,T & H
    Otherwise it's just a meaningless set of numbers.
    It can be done with Arduino, but that uses C++ so the C code conversion in the data sheet works as is.
    This is the brick wall a.k.a. the big mountain, last seen in the Rocky Mountains not far from Snowdon.

    In the even of wanting to use a BME280, I've decided that it's easier to cobble some Uno code together than convert this C Math into Basic.
    I'll most likely not use a BME280, cobbling shoes or arduino code is not my idea of fun.
    I'm back to stepper motors right now, much more relaxing, round and round they go......round and round
     
  10. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,805
    Likes:
    958
    Location:
    Rochdale UK
    No!! Not at all.. I was just laughing at C comments... I was preventing C from running out and looking for "big mountain"
     
  11. the alchimiste

    the alchimiste New Member

    Joined:
    Mar 14, 2018
    Messages:
    3
    Likes:
    0
    Hello Superdat,

    I've been going through this post as I'm actually strugling with getting a 4 * 7 segment display drived by the Max7219 connected to a pic 16F15344.

    I'm actually programing in asm as I feel it more detailed and I understand what I'm doing.
    After reading datasheet of pic16F15344 and Max7219, I've created an asm code and made my hardware connection.
    The code is buiding without any issue and getting loaded into the pic throught pickit3 but after scratching my head for hours and checking my connections, I've not been able to find out where is the issue and the 7segment display is still blank and don't show the value 1991 as I would like to display.
    I've seen throught this post that you've been suceding to make your 7segment display working with Max7219.
    Can you help me to understand where is the issue in my code or share your code?

    My code:

    ; TODO INSERT INCLUDE CODE HERE
    list p=16f15344 ; list directive to define processor
    #include <p16f15344.inc> ; processor specific variable definitions

    ; TODO INSERT CONFIG HERE

    __CONFIG _CONFIG1, _RSTOSC_HFINT1
    __CONFIG _CONFIG3, _WDTE_OFF
    __CONFIG _CONFIG4, _LVP_OFF
    __CONFIG _CONFIG5, _CP_OFF

    ; _RSTOSC_HFINT1 -> permet l'activation de l'oscillateur interne a une frequence de 1Mhz soit 1 microS/osccillation
    ; _LVP_OFF -> permet la programmation a faible tenssion
    ; _CP_OFF -> Ne cache pas le code
    ; _WDT_OFF -> Desactive le Watch Dog


    ; TODO PLACE VARIABLE DEFINITIONS GO HERE

    ;***** VARIABLE DEFINITIONS at the bank 0 begining at adress 0x20
    cblock 0X20
    registre_Démarrage : 1
    Donnée_Demmarrage : 1
    Donnée_Fin : 1

    registre_Parametrage : 1
    Donnée_ParametrageBCD : 1

    registre_Intensite : 1
    Donnée_Intensite : 1

    registre_nbrDigit : 1
    Donnée_nbrDigit : 1

    registre_Test : 1
    Donnée_Test : 1

    registre_Digit : 1
    Donnée_Digit : 1

    registre : 1
    donnee : 1
    endc
    ;*******************************************************************************
    ; Reset Vector
    ;*******************************************************************************

    org 00H ; processor reset vector
    goto START ; go to beginning of program

    ; TODO INSERT ISR HERE

    ;*******************************************************************************
    ; MAIN PROGRAM
    ;*******************************************************************************


    START

    ; Initialisaton des variables


    banksel registre_Démarrage

    movlw 0x0c
    movwf registre_Démarrage
    movlw 0x03
    movwf Donnée_Demmarrage

    movlw 0x09
    movwf registre_Parametrage
    movlw 0x0F
    movwf Donnée_ParametrageBCD

    movlw 0x0A
    movwf registre_Intensite
    movlw 0x07
    movwf Donnée_Intensite

    movlw 0x0B
    movwf registre_nbrDigit
    movlw 0x03
    movwf Donnée_nbrDigit

    movlw 0x0F
    movwf registre_Test
    movlw 0x01
    movwf Donnée_Test

    movlw 0x01
    movwf registre_Digit
    movlw 0x01
    movwf Donnée_Digit



    ; Initialisation des paramettres SPI

    banksel RB4PPS
    movlw 0x16 ;On associe SDO a RB4 ( les données transmises)
    movwf RB4PPS

    banksel RB5PPS
    movlw 0x15 ;On associe SCK a RB5 (l'horloge)
    movwf RB5PPS

    banksel TRISB
    movlw 0x8F ;RB4 et RB5 et RB6 en sortie tout le rest en entrée
    movwf TRISB

    banksel SSP1CON1
    movlw 0x20 ;On active le SPI
    movwf SSP1CON1

    Banksel SSP1STAT ; On parametre l'horloge et l'echantillonage des données
    clrf SSP1STAT

    banksel PORTB
    clrf PORTB ;on efface les valeurs du port B


    ; Debut du parametrage Driver
    ;1
    banksel registre_Démarrage
    movf registre_Démarrage,w
    movwf registre

    banksel Donnée_Demmarrage
    movf Donnée_Demmarrage,w
    movwf donnee
    call RoutineveriffinSDI


    ;2
    banksel registre_Parametrage
    movf registre_Parametrage,w
    movwf registre

    banksel Donnée_ParametrageBCD
    movf Donnée_ParametrageBCD,w
    movwf donnee
    call RoutineveriffinSDI


    ;3
    banksel registre_Intensite
    movf registre_Intensite,w
    movwf registre

    banksel Donnée_Intensite
    movf Donnée_Intensite,w
    movwf donnee
    call RoutineveriffinSDI



    ;4
    banksel registre_nbrDigit
    movf registre_nbrDigit,w
    movwf registre

    banksel Donnée_nbrDigit
    movf Donnée_nbrDigit,w
    movwf donnee
    call RoutineveriffinSDI



    ;5
    banksel registre_Test
    movf registre_Test,w
    movwf registre

    banksel Donnée_Test
    movf Donnée_Test,w
    movwf donnee
    call RoutineveriffinSDI

    banksel registre_Test
    movf registre_Test,w
    movwf registre


    movlw 0x00 ;fin du test
    movwf donnee
    call RoutineveriffinSDI

    ; Debut du programme envoie des chiffres

    ; Chiffre 1

    movlw 0x01
    banksel registre
    movwf registre

    movlw 0x01
    movwf donnee
    call RoutineveriffinSDI

    ; Chiffre 2

    movlw 0x02
    banksel registre
    movwf registre

    movlw 0x09
    movwf donnee
    call RoutineveriffinSDI



    ; Chiffre 3

    movlw 0x03
    banksel registre
    movwf registre

    movlw 0x09
    movwf donnee
    call RoutineveriffinSDI


    ; Chiffre 4

    movlw 0x04
    banksel registre
    movwf registre

    movlw 0x01
    movwf donnee
    call RoutineveriffinSDI
    sleep

    RoutineveriffinSDI
    banksel PORTB
    bcf PORTB,6
    banksel registre
    movf registre,w
    banksel SSP1BUF
    movwf SSP1BUF
    banksel SSP1STAT
    btfss SSP1STAT,0
    goto $-1
    banksel SSP1BUF
    movf SSP1BUF,w

    banksel donnee
    movf donnee,w
    banksel SSP1BUF
    movwf SSP1BUF
    banksel SSP1STAT
    btfss SSP1STAT,0
    goto $-1
    banksel PORTB
    bsf PORTB,6

    return
    end

    Thanks a lot
     
  12. the alchimiste

    the alchimiste New Member

    Joined:
    Mar 14, 2018
    Messages:
    3
    Likes:
    0
     
  13. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    128
    Likes:
    5
    Hi Alchi
    Is it just the 7 segments you want help with? I never did get the BME280 to work properly.
    I have some simple examples for driving 4 x 7 segement displays with Max7219/21, but they are in BASIC
    ASM is a foreign language to me.
    If you think it will help I can find them easily enough and post here.
     
  14. the alchimiste

    the alchimiste New Member

    Joined:
    Mar 14, 2018
    Messages:
    3
    Likes:
    0
    Hi Superdat and thanks for your quick reply!

    Yes for the moment I just want to make some SPI tests of communication between my pic and MAx7219,
    next steps will be to ask the pic 16F15344 throught a pic 16f887 to ask the Max7219 to display a number on a 7 segment display.
    Thats a pity for asm as I want to avoid using librairies. But thanks for your help anyway.
    If anyone else have some sugestions they are more than welcome :)

    Thanks
     
  15. Superdat

    Superdat Member

    Joined:
    Mar 3, 2015
    Messages:
    128
    Likes:
    5
    Hi Alchi
    Attached is an example using Max7219/21 and 7 segment LEDs and nothing else so it should be easy enough to figure out.
    The biggest problem I found with Max7221 was how to set up the registers. You still need to do this somehow with ASM.
    Hope it helps
     

    Attached Files:

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice