News:

;) This forum is the property of Proton software developers

Main Menu

DS18B20 6‑sensors

Started by tolyan249, Today at 05:14 PM

Previous topic - Next topic

tolyan249


'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Read a DS18B20 temperature sensor with a 12-bit resolution
' And display the temperatures in Celsius and Fahrenheit on a serial terminal
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F26K22                                       ' Tell the compiler what device to compile for
    Declare Xtal = 64                                       ' Tell the compiler what frequency the device is operating at
    Declare Float_Display_Type = Fast                       ' Use the faster, and more accurate, Float to ASCII library routine
'
' Setup USART1
'   
    Declare Hserial1_Baud = 9600                            ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTC.6                           ' Setup the pin used for USART1 TX

    Symbol DQ_Pin = PORTC.0                                 ' Setup the data pin (DQ) for the DS18B20 device
'
' Create some constants and variables for the demo
'
    Symbol cCelsius = 0                                     ' Tell the DS18B20_Read procedure to return the temperature in Celcius
    Symbol cFahrenheit = 1                                  ' Tell the DS18B20_Read procedure to return the temperature in Fahrenheit
   
    Dim fTemperature As Float                               ' Holds the temperature returned from the DS18B20_Read procedure
 
'-------------------------------------------------------------------------------
' The main program starts here
' Read a DS18B20 device and transmit the temperature in Fahrenheit and Celsius to a serial terminal
'
Main:
    Internal_Osc_64MHz()                                    ' Set the internal oscillator to operate at 64MHz

    Do                                                      ' Create a loop
        fTemperature = DS18B20_Read(cCelsius)               ' Read the temperature (in Celsius) from the DS18B20 device into fTemperature
        HRSOutLn "Temperature = ", Dec1 fTemperature, " C"  ' Transmit the temperature value to a serial terminal
       
        fTemperature = DS18B20_Read(cFahrenheit)            ' Read the temperature (in Fahrenheit) from the DS18B20 device into fTemperature
        HRSOutLn "Temperature = ", Dec1 fTemperature, " F"  ' Transmit the temperature value to a serial terminal
    Loop                                                    ' Do it forever
 
'-------------------------------------------------------------------------------
' Read a DS18B20 device with its default 12-bit resolution
' Input     : pCorF is 1 if the return is to be Fahrenheit, 0 for Celsius
' Output    : Returns the floating point temperature value
'           : If the device cannot be read, the value returned is $FFFFFFFF
' Notes     : None
'
Proc DS18B20_Read(pCorF As Bit), Float   
    Dim bData[9]  As Byte                                   ' Holds the data read from the DS18B20 device
    Dim bTimeOut  As Byte At bData#0 = $FF                  ' A timeout for the conversion wait loop
    Dim bRawTemp  As bData#0                                ' \ Array elements 0 and 1 hold the raw temperature value
    Dim bRawTempH As bData#1                                ' /
    Dim wRawTemp  As bRawTemp.SWord                         ' Combine them into a signed word variable
 
    OWrite DQ_Pin, 1, [$CC, $44]                            ' Start a temperature conversion
'
' Wait until the conversion is complete
'
    Repeat                                                  ' Create a loop
        If ORead DQ_Pin, 4 = 1 Then ReadTemp                ' Exit the loop if the conversion is complete
        DelayMS 10                                          ' A small delay within the loop
        Dec bTimeOut                                        ' Decrement the timeout counter
    Until STATUSbits_Z = 1                                  ' Finish the loop when it reaches zero
    Result = $FFFFFFFF                                      ' Return $FFFFFFFF because the loop completed
    ExitProc                                                ' Exit the procedure prematurely because it timed out
ReadTemp:
    OWrite DQ_Pin, 1, [$CC, $BE]                            ' Set to read the temperature
    ORead DQ_Pin, 0, [Str bData\9]                          ' Read 9 bytes into array bData
'
' Convert the data held in wRawTemp to a temperature value
' 
    Result = wRawTemp / 16.0                                ' Convert to Celsius
    If pCorF = 1 Then                                       ' Is the temperature returned to be Fahrenheit?
        Result = (Result * 1.8) + 32.0                      ' Yes. So do the conversion from Celsius to Fahrenheit
    EndIf
EndProc
 
'-------------------------------------------------------------------------------
' Set the PIC18F26K22's internal oscillator to operate at 64MHz
' Input     : None
' Output    : None
' Notes     : Waits for the internal oscillator to become stable
'
Proc Internal_Osc_64MHz()
    OSCCON  = %01110000
    OSCCON2 = %10000100
    Repeat : Until OSCCONbits_HFIOFS = 1                    ' Keep looping until the oscillator is stable
    Set OSCTUNEbits_PLLEN                                   ' Enable the 4xPLL
EndProc
 
'-------------------------------------------------------------------------------
' Setup the configuration fuses for 64MHz operation using the internal oscillator on a PIC18F26K22 device
'
Config_Start
    FOSC = INTIO67      ' Internal oscillator block. Port function on RA6 and RA7
    PLLCFG = On         ' Oscillator not multiplied by 4
    PRICLKEN = Off      ' Primary clock disabled
    IESO = Off          ' Oscillator Switchover mode disabled
    Debug = Off         ' Background debugger disabled. RB6 and RB7 Configured as general purpose I/O pins
    WDTEN = Off         ' WDT disabled (Control is placed on SWDTEN bit)
    XINST = Off         ' Standard mnemonic mode enabled
    FCMEN = On          ' Fail-Safe Clock Monitor enabled
    PWRTEN = On         ' PWRT enabled
    BOREN = On          ' Brown-out Reset enabled and Controlled by software (SBOREN is enabled)
    BORV = 190          ' VBOR set to 1.9 V nominal
    WDTPS = 128         ' Watchdog prescaler set to 1:128
    MCLRE = EXTMCLR     ' MCLR pin enabled, RE3 input pin disabled
    HFOFST = Off        ' The system clock is held Off until the HF-INTOSC is stable.
    PBADEN = Off        ' PORTB<4:0> pins are Configured as digital I/O on Reset
    CCP2MX = PORTC1     ' CCP2 input/output is multiplexed with RC1
    CCP3MX = PORTB5     ' P3A/CCP3 input/output is multiplexed with RB5
    T3CMX = PORTC0      ' T3CKI is on RC0
    P2BMX = PORTB5      ' P2B is on RB5
    STVREN = On         ' Stack full/underflow will cause Reset
    LVP = Off           ' Single-Supply ICSP disabled
    Cp0 = Off           ' Block 0 ($000800 - $001FFF) not code-protected
    CP1 = Off           ' Block 1 ($002000 - $003FFF) not code-protected
    CPB = Off           ' Boot block ($000000 - $0007FF) not code-protected
    CPD = Off           ' Data EEPROM not code-protected
    WRT0 = Off          ' Block 0 ($000800 - $001FFF) not write-protected
    WRT1 = Off          ' Block 1 ($002000 - $003FFF) not write-protected
    WRTB = Off          ' Boot block ($000000 - $0007FF) not write-protected
    WRTC = Off          ' Configuration registers ($300000 - $3000FF) not write-protected
    WRTD = Off          ' Data EEPROM not write-protected
    EBTR0 = Off         ' Block 0 ($000800 - $001FFF) not protected from table reads executed in other blocks
    EBTR1 = Off         ' Block 1 ($002000 - $003FFF) not protected from table reads executed in other blocks
    EBTRB = Off         ' Boot block ($000000 - $0007FF) not protected from table reads executed in other blocks
Config_End



I found it on the forum here,
I have a question:
how to create an array of sensors so as not to write 6 procedures
Proc DS18B20_Read(pCorF As Bit), Float

I tried to create an array
Symbol dq[0] PORTB.0
Symbol dq[1] PORTB.1
Symbol dq[2] PORTB.2
Symbol dq[3] PORTB.3
Symbol dq[4] PORTB.4
Symbol dq[5] PORTB.5

This doesn't work like this,
 

 OWrite dq[0], 1, [$CC, $44] doesn't work.

Is it possible to insert sensors into an array?


Dim DQ[6] As Byte
 
DQ[0]=PORTB.0
DQ[1]=PORTB.1
DQ[2]=PORTB.2
DQ[3]=PORTB.3
DQ[4]=PORTB.4
DQ[5]=PORTB.5

this seems to work

 OWrite dq[0], 1, [$CC, $44] doesn't work

Thank you.

RGV250

Hi,
I wonder if you could specify the port.pin inside the procedure and then just pass the value 1..6 as a parameter to the proc and use select case or similar to determine the pin to use?

Regards,
Bob

trastikata

#2
Quotehow to create an array of sensors so as not to write 6 procedures

Unless you dedicate an entire port to toggle as a single pin would have, you won't gain anything - the code execution would be no faster whether you use 6 procedures or somehow a pin array because each pin has to be toggled individually.

If the purpose is to avoid calling all 6 separately, then warp them 6 instances in a single procedure and call it once.

▲