avatar_Peter

Auslesen des Sensors DS1820

Begonnen von Peter, 15.01.2021, 19:48:59 CET

Vorheriges Thema - Nächstes Thema

Peter

Hallo
Mit der Software lässt sich die ID des DS1820 auslesen und speichern.
Geschrieben wurde sie in MikroBasic.

program ds1820

INCLUDE sensor

'typ_sensor ------> welcher sensor angeschlossen ist
'adr_ausgabe ---------> adresse des sensors ausgeben LCD
'adr_laden (1..9) --------> läd die adresse des sensors aus dem EEPROM
'adr_speichern (1...9) ------> speichert die Adresse des sensors ins EEPROM
'sensor_lesen [8] ------> nach adr_laden ist in dieser variable die adresse gespeichert

const TEMP_RESOLUTION as byte = 9

dim text as char[9]
    temp, aa as word
    
dim LCD_RS as sbit at LATB4_bit
    LCD_EN as sbit at LATB5_bit
    LCD_D4 as sbit at LATB0_bit
    LCD_D5 as sbit at LATB1_bit
    LCD_D6 as sbit at LATB2_bit
    LCD_D7 as sbit at LATB3_bit

dim LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit

sub procedure Display_Temperature( dim temp2write as word )
const RES_SHIFT = TEMP_RESOLUTION - 8

dim temp_whole as byte
    temp_fraction as word

    text = "000.0000"
    ' Check if temperature is negative
    if (temp2write and 0x8000) then
      text[0] = "-"
      temp2write = not temp2write + 1
    end if

    ' Extract temp_whole
    temp_whole = word(temp2write >> RES_SHIFT)

    ' Convert temp_whole to characters
    if ( temp_whole div 100 ) then
      text[0] = temp_whole div 100  + 48
    else
      text[0] = "0"
    end if

    text[1] = (temp_whole div 10)mod 10 + 48               ' Extract tens digit
    text[2] =  temp_whole mod 10        + 48               ' Extract ones digit

    ' Extract temp_fraction and convert it to unsigned int
    temp_fraction  =  word(temp2write << (4-RES_SHIFT))
    temp_fraction  = temp_fraction and 0x000F
    temp_fraction  = temp_fraction * 625

    ' Convert temp_fraction to characters
    text[4] = word(temp_fraction div 1000)    + 48         ' Extract thousands digit
    text[5] = word((temp_fraction div 100)mod 10 + 48)     ' Extract hundreds digit
    text[6] = word((temp_fraction div 10)mod 10  + 48)     ' Extract tens digit
    text[7] = word(temp_fraction mod 10)      + 48         ' Extract ones digit

    ' Print temperature on Lcd
    Lcd_Out(2, 5, text)
end sub


main:

CMCON = 7
ADCON0 = 7
ADCON1 = 15

TRISA = 0
TRISC.0 = 1
TRISC.1 = 0

LATC = 0
aa = 1
LCD_Init ()

'adr_speichern (2)
'adr_laden (1)

While true
'typ_sensor
'adr_ausgabe
'------------------
adr_laden (2)

temp =  Ow_Read(PORTA, 4)
    temp = (Ow_Read(PORTA, 4) << 8) + temp
    Display_Temperature(temp)

if PORTC.0 = 1 then
LATC.1 = 1
adr_speichern (aa)
aa = aa + 1
end if
delay_ms(500)
LATC.1 = 0
Wend
end.

module sensor
DIM low_1, low_2 as Byte
DIM gg_2 as Byte [8]
DIM sensor_lesen as Byte [8]

Sub Procedure typ_sensor
Sub Procedure ADR_Ausgabe
Sub Procedure adr_speichern (DIM adr_r as Byte)
Sub Procedure adr_laden (DIM lad_en as Byte)

implements

Sub Procedure typ_sensor
DIM low_aw as Byte
DIM gg_g as Byte [8]

Lcd_Cmd(_LCD_CURSOR_OFF)
Lcd_Cmd(_LCD_CLEAR)

Ow_Reset(PORTA,4)
delay_ms(10)
Ow_Write(PORTA, 4, 0x33)

 While low_aw <=7
  gg_g [low_aw] = Ow_Read (PORTA,4)
  inc (low_aw)
  delay_ms(30)
 wend

low_aw = 0
low_1 = 0

 For low_2 = 0 to 7
  if gg_g [low_1] = 255 then
     low_1 = low_1 + 1

       if low_1 = 8 then
          LCD_Out (1,1,"kein Sensor")
       end if
  end if
 Next low_2
 
 Select case gg_g[0]
   Case 0x10
        LCD_Out (1,1,"DS1820,DS18S20")
   Case 0x28
        LCD_Out (1,1,"DS18B20")
   Case 0x22
        LCD_Out (1,1,"DS1822")

 End Select
End sub

Sub Procedure ADR_Ausgabe
DIM hi_hi as Byte
DIM ds18 as String [3]

hi_hi = 1
low_1 = 0

Lcd_Cmd(_LCD_CURSOR_OFF)
Lcd_Cmd(_LCD_CLEAR)

Ow_Reset(PORTA, 4)
delay_ms(10)

Ow_Write(PORTA, 4, 0x33)
delay_ms(10)

 While low_1 <=7
  gg_2 [low_1] = Ow_Read (PORTA,4)
  inc (low_1)
  delay_ms(30)
 wend
 
 low_1 = 0
 
 For low_2 = 0 to 7
  if gg_2 [low_1] = 255 then
     low_1 = low_1 + 1
     
       if low_1 = 8 then
          LCD_Out (1,1,"kein Sensor")
       end if
  end if
 Next low_2

 For Low_1 = 0 to 7
    ByteTohex (gg_2 [low_1],ds18)
    LCD_Out (2,hi_hi,ds18)
    hi_hi = hi_hi + 2
    delay_ms(50)
 Next low_1

low_1 = 0
hi_hi = 1
End sub

Sub Procedure adr_speichern (DIM adr_r as Byte)
DIM rr_dd, pos_iti as Byte

     Select case adr_r
        case 1
             rr_dd = 0x00
             EEPROM_Write (rr_dd,0x01)
             
        case 2
             rr_dd = 0x09
             EEPROM_Write (rr_dd,0x02)

        case 3
             rr_dd = 0x12
             EEPROM_Write (rr_dd,0x03)
             
        case 4
             rr_dd = 0x1b
             EEPROM_Write (rr_dd,0x04)
             
        case 5
             rr_dd = 0x24
             EEPROM_Write (rr_dd,0x05)
             
        case 6
             rr_dd = 0x2d
             EEPROM_Write (rr_dd,0x06)
             
        case 7
             rr_dd = 0x36
             EEPROM_Write (rr_dd,0x07)
             
        case 8
             rr_dd = 0x3f
             EEPROM_Write (rr_dd,0x08)
             
        case 9
             rr_dd = 0x48
             EEPROM_Write (rr_dd,0x09)
             
     end select
delay_ms(30)
Ow_Reset(PORTA, 4)
delay_ms(10)

Ow_Write(PORTA, 4, 0x33)
delay_ms(10)
low_1 = 0

 While low_1 <=7
  gg_2 [low_1] = Ow_Read (PORTA,4)
  inc (low_1)
  delay_ms(30)
 wend

 low_1 = 0

  For pos_iti = 0 to 7
      
      rr_dd = rr_dd + 1
      EEPROM_Write (rr_dd, gg_2[pos_iti])
      delay_ms(30)
      
  Next pos_iti
  
rr_dd = 0

End sub

Sub Procedure adr_laden (DIM lad_en as Byte)
DIM kl_sa, ert_d as Byte

   Select case lad_en
          case 1
               kl_sa = 0x01
               
          case 2
               kl_sa = 0x0a
               
          case 3
               kl_sa = 0x13
               
          case 4
               kl_sa = 0x1c
               
          case 5
               kl_sa = 0x25
               
          case 6
               kl_sa = 0x2e
               
          case 7
               kl_sa = 0x37
               
          case 8
               kl_sa = 0x40
               
          case 9
          kl_sa = 0x49
          
   end select
   
 For ert_d = 0 to 7
     sensor_lesen [ert_d] = EEPROM_Read (kl_sa)
     kl_sa = kl_sa + 1
     delay_ms(10)
 Next ert_d
'-----
Ow_Reset(PORTA, 4)
delay_ms(10)
Ow_Write(PORTA, 4, 0x55)

 For ert_d = 0 to 7
     Ow_Write(PORTA,4,sensor_lesen [ert_d])
 Next ert_d

delay_ms(10)
Ow_Write(PORTA, 4, 0x44)
delay_ms(800)
Ow_Reset(PORTA, 4)
delay_ms(10)
Ow_Write(PORTA, 4, 0x55)

 For ert_d = 0 to 7
     Ow_Write(PORTA,4,sensor_lesen [ert_d])
 Next ert_d
 
delay_ms(10)
Ow_Write(PORTA, 4, 0xbe)

End sub

end.

mfg Peter.
Like Like x 1 View List

Schnellantwort

Achtung: In diesem Thema wurde seit 120 Tagen nichts mehr geschrieben.
Wenn Sie nicht absolut sicher sind, dass Sie hier antworten möchten, starten Sie ein neues Thema.

Name:
Verifizierung:
Bitte lassen Sie dieses Feld leer:
Geben Sie die Buchstaben aus dem Bild ein
Buchstaben anhören / Neues Bild laden

Geben Sie die Buchstaben aus dem Bild ein:

Tastenkürzel: Alt+S Beitrag schreiben oder Alt+P für Vorschau