Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Termostato con PIC e ds18s20

Raccolta di codici sorgenti

Moderatore: Foto UtentePaolino

0
voti

[1] Termostato con PIC e ds18s20

Messaggioda Foto UtenteAlien233 » 29 mar 2012, 14:47

Ciao a tutti,
eccomi a esporre il mio problema, volevo creare un termostato che al raggiungimento di alcune temperature faccia partire una ventola controllata in PWM, (es, a 30°C PWM al 40%, a 40°C PWM al 60% e così via).
La parte di controllo della ventola per ora non è un problema, con la libreria di Mikrobasic ed un transistor il problema non dovrebbe esserci.
Al contrario invece nel settare le temperature di intervento, essendo il ds18b20 un sensore digitale, non so come impostare la parte di programma per confrontare la temperatura rilevata con la soglia da me impostata.
finché c'è da visualizzare la temperatura sul display il problema non si pone, ma non riesco a fare il confronto con la soglia.
Qualcuno mi sa aiutare?
Grazie della disponibilità,
233
Avatar utente
Foto UtenteAlien233
0 2
 
Messaggi: 20
Iscritto il: 8 nov 2007, 19:04

0
voti

[2] Re: Termostato con PIC e ds18s20

Messaggioda Foto Utentesimo85 » 29 mar 2012, 14:54

A pagina 9 del datasheet c'è una rappresentazione per il collegamento con un dispositivo TX & RX. Direi quindi che puoi usare la EUSART del PIC, ma non ho letto molto, sono sveglio da poco. Ti invito quindi a leggere bene il datasheet. Non hai specificato quale PIC vuoi usare.
Avatar utente
Foto Utentesimo85
30,9k 7 12 13
Disattivato su sua richiesta
 
Messaggi: 9927
Iscritto il: 30 ago 2010, 4:59

0
voti

[3] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteAlien233 » 29 mar 2012, 15:34

Ok, gli darò un'occhiata.
Il PIC in questione è il 16f887
Avatar utente
Foto UtenteAlien233
0 2
 
Messaggi: 20
Iscritto il: 8 nov 2007, 19:04

0
voti

[4] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteRobert8 » 29 mar 2012, 16:03

Scusa ma non ho capito quale sia il tuo problema. Affermi:
Alien233 ha scritto:finché c'è da visualizzare la temperatura sul display il problema non si pone, ma non riesco a fare il confronto con la soglia.

Quindi, suppongo, disponi di un byte (o due, non ricordo se il valore del sensore è su 8 o 10 bit) col valore della temperatura, ora dove sta il problema nel confrontare tale valore con una tua soglia?
Avatar utente
Foto UtenteRobert8
3.449 2 8 13
Master EY
Master EY
 
Messaggi: 373
Iscritto il: 15 giu 2011, 16:30

0
voti

[5] Re: Termostato con PIC e ds18s20

Messaggioda Foto Utentesimo85 » 29 mar 2012, 16:22

È che a quanto sembra non esiste una tensione di soglia da confrontare. :-)
La comunicazione tra il termostato ed il microcontrollore è seriale..
Avatar utente
Foto Utentesimo85
30,9k 7 12 13
Disattivato su sua richiesta
 
Messaggi: 9927
Iscritto il: 30 ago 2010, 4:59

0
voti

[6] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteRobert8 » 29 mar 2012, 16:43

Sarà anche seriale, 1-wire in particolare, però se fatta la lettura riesci a farne il display vuol dire che il dato ce l'hai da qualche parte. A meno che per fare lettura e display si sia usufruito di qualche libreria. In questo caso: auguri! Devi sbatterti per sviscerare programmi fatti da altri (sempre che si disponga dei sorgenti).

E' per questo che ho sempre detestato le librerie: non appena devi fare qualcosa di alternativo, sei fregato.
Avatar utente
Foto UtenteRobert8
3.449 2 8 13
Master EY
Master EY
 
Messaggi: 373
Iscritto il: 15 giu 2011, 16:30

0
voti

[7] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteAlien233 » 29 mar 2012, 17:04

Esattamente, uso una libreria di Mikrobasic per leggere il valore della sonda e poi buttarlo nel display.
Il problema è che il dato visualizzato non è un valore numerico, ma testuale, infatti la variabile è dichiarata come string, a me servirebbe numerica (float, visto che c'è la virgola).
Sbaglio?
La soglia è un numero inserito da me nel programma.
Avatar utente
Foto UtenteAlien233
0 2
 
Messaggi: 20
Iscritto il: 8 nov 2007, 19:04

0
voti

[8] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteAlien233 » 29 mar 2012, 17:12

Il programma da me usato è simile a questo,

Codice: Seleziona tutto
program OneWire

' LCD module connections
dim LCD_RS as sbit at LATB4_bit
dim LCD_EN as sbit at LATB6_bit
dim LCD_D4 as sbit at LATD4_bit
dim LCD_D5 as sbit at LATD5_bit
dim LCD_D6 as sbit at LATD6_bit
dim LCD_D7 as sbit at LATD7_bit

dim LCD_RS_Direction as sbit at TRISB4_bit
dim LCD_EN_Direction as sbit at TRISB6_bit
dim LCD_D4_Direction as sbit at TRISD4_bit
dim LCD_D5_Direction as sbit at TRISD5_bit
dim LCD_D6_Direction as sbit at TRISD6_bit
dim LCD_D7_Direction as sbit at TRISD7_bit
' End LCD module connections

'  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
'  18S20: 9  (default settingcan be 9,10,11,or 12)
'  18B20: 12
const TEMP_RESOLUTION as byte = 9

dim text as char[9]
    temp as word

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:
  ADPCFG = 0                                     ' Configure AN pins as digital I/O

  text = "000.0000"
  Lcd_Init()                                     ' Initialize LCD
  Lcd_Cmd(_LCD_CLEAR)                            ' Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF)                       ' Turn cursor off
  Lcd_Out(1, 1, " Temperature:   ")

  Lcd_Chr(2,13,178)                              ' Print degree character, "C" for Centigrades
                                                 ' Different LCD displays have different char code for degree
  Lcd_Chr(2,14,"C")                              ' If you see greek alpha letter try typing 178 instead of 223

  '--- Main loop
  while TRUE
    '--- Perform temperature reading
    Ow_Reset(PORTF, 6)                           ' Onewire reset signal
    Ow_Write(PORTF, 6, 0xCC)                     ' Issue command SKIP_ROM
    Ow_Write(PORTF, 6, 0x44)                     ' Issue command CONVERT_T
    Delay_us(120)

    Ow_Reset(PORTF, 6)
    Ow_Write(PORTF, 6, 0xCC)                     ' Issue command SKIP_ROM
    Ow_Write(PORTF, 6, 0xBE)                     ' Issue command READ_SCRATCHPAD
   
    temp =  Ow_Read(PORTF, 6)
    temp = (Ow_Read(PORTF, 6) << 8) + temp

    '--- Format and display result on Lcd

    Display_Temperature(temp)

    Delay_ms(520)
  wend
end.
Avatar utente
Foto UtenteAlien233
0 2
 
Messaggi: 20
Iscritto il: 8 nov 2007, 19:04

0
voti

[9] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteRobert8 » 29 mar 2012, 17:26

Premesso che non ho mai usato il Basic, però:

Codice: Seleziona tutto
temp =  Ow_Read(PORTF, 6)
temp = (Ow_Read(PORTF, 6) << 8) + temp

temp mi pare binario su 16 bit, potresti usarlo per fare il confronto con la tua soglia.
Avatar utente
Foto UtenteRobert8
3.449 2 8 13
Master EY
Master EY
 
Messaggi: 373
Iscritto il: 15 giu 2011, 16:30

0
voti

[10] Re: Termostato con PIC e ds18s20

Messaggioda Foto UtenteAlien233 » 29 mar 2012, 17:31

Robert8 ha scritto:Premesso che non ho mai usato il Basic, però:

Codice: Seleziona tutto
temp =  Ow_Read(PORTF, 6)
temp = (Ow_Read(PORTF, 6) << 8) + temp

temp mi pare binario su 16 bit, potresti usarlo per fare il confronto con la tua soglia.


Ho provato, ma non ho ottenuto nessun risultato. Avrò sbagliato qualcosa forse?
Avatar utente
Foto UtenteAlien233
0 2
 
Messaggi: 20
Iscritto il: 8 nov 2007, 19:04

Prossimo

Torna a Firmware e programmazione

Chi c’è in linea

Visitano il forum: Google [Bot] e 5 ospiti