PIC 16F877 i DS18B20 brljavi prilikom prikaza negativne temp

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Post Reply
User avatar
zoomtronic
Posts: 2
Joined: 07-11-2010, 21:41

PIC 16F877 i DS18B20 brljavi prilikom prikaza negativne temp

Post by zoomtronic »

Napravio sam termometar sa PIC-om 16F877 i sensorom DS18B20,
sve lepo radi na pozitivnoj temperaturi, međutim kad stavim sensor u zamrzivač,
počne da opada temperatura, kad dođe do 0 stepeni ne izbacuje znak - ispred temperature, a odbrojava
sasvim normalno. Kao da raste temperatura u +

evo koda

program OneWire


dim LCD_RS as sbit at RD2_bit
LCD_EN as sbit at RD3_bit
LCD_D4 as sbit at RD4_bit
LCD_D5 as sbit at RD5_bit
LCD_D6 as sbit at RD6_bit
LCD_D7 as sbit at RD7_bit
LCD_RS_Direction as sbit at TRISD2_bit
LCD_EN_Direction as sbit at TRISD3_bit
LCD_D4_Direction as sbit at TRISD4_bit
LCD_D5_Direction as sbit at TRISD5_bit
LCD_D6_Direction as sbit at TRISD6_bit
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 setting can be 9,10,11,or 12)
' 18B20: 12
const TEMP_RESOLUTION as byte = 12

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] = " "
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


Lcd_Out(2, 2, text)
end sub

main:
ANSEL = 0 ' Configure AN pins as digital I/O
ANSELH = 0

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, " Outside temp: ")
Lcd_Chr(2,13,223) ' u drugom redu na 13. mestu ' Print degree character, "C" for Centigrades
' Different Lcd displays have different char code for degree
Lcd_Chr(2,14,"C") ' u drugom redu na 14. mestu ' If you see greek alpha letter try typing 178 instead of 223

'--- Main loop
while (TRUE)
'--- Perform temperature reading DQ linija
Ow_Reset(PORTC, 2) ' Onewire reset signal
Ow_Write(PORTC, 2, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTC, 2, 0x44) ' Issue command CONVERT_T
Delay_us(170)

Ow_Reset(PORTC, 2)
Ow_Write(PORTC, 2, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTC, 2, 0xBE) ' Issue command READ_SCRATCHPAD

temp = Ow_Read(PORTC, 2)
temp = (Ow_Read(PORTC, 2) << 8 ) + temp

'--- Format and display result on Lcd

Display_Temperature(temp)

Delay_ms(300)
wend
end.
User avatar
pedja089
Administrator sajta
Administrator sajta
Posts: 7877
Joined: 20-02-2007, 14:50
Location: Beočin -Srbija

Re: PIC 16F877 i DS18B20 brljavi prilikom prikaza negativne temp

Post by pedja089 »

Mrsko milupati glavu sta si pisao, ali negativna temp je ako je najvisi bit postavljen na 1.
Oko toga malo pogledaj sta si zeznuo.
User avatar
Elektronicar
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 467
Joined: 19-01-2008, 17:05
Location: Balkan

Re: PIC 16F877 i DS18B20 brljavi prilikom prikaza negativne temp

Post by Elektronicar »

Koliko vidim u ovom delu se vrsi provera negativne temperature.

Code: Select all

' Check if temperature is negative
if (temp2write and 0x8000) then
text[0] = "-"
temp2write = not temp2write + 1
end if
Testira se rezultat 0x8000 odnosno AND odredjure negativnu temperaturu, u ondosu na rezultat koji je u obliku 1xxx xxxx xxxx xxxx

Ovo je mikro basic ako negresim mislim da nisi pravilno inicijalizovao DS18B20.
Pokusaj prvo sa rezolucinom od 9 ako sve radi onda 12 bit-a pa trazi gresku.
User avatar
zoomtronic
Posts: 2
Joined: 07-11-2010, 21:41

Re: PIC 16F877 i DS18B20 brljavi prilikom prikaza negativne temp

Post by zoomtronic »

Uspeo sam uz pomoć prijatelja

evo koda

program OneWire


dim
LCD_RS as sbit at RD2_bit ' RS linija portD2
LCD_EN as sbit at RD3_bit ' E linija portD3
LCD_D4 as sbit at RD4_bit ' Data 4 portD4
LCD_D5 as sbit at RD5_bit ' Data 5 portD5
LCD_D6 as sbit at RD6_bit ' Data 6 portD6
LCD_D7 as sbit at RD7_bit ' Data 7 portD7
LCD_RS_Direction as sbit at TRISD2_bit ' RS linija portD2
LCD_EN_Direction as sbit at TRISD3_bit ' E linija portD3
LCD_D4_Direction as sbit at TRISD4_bit ' Data 4 portD4
LCD_D5_Direction as sbit at TRISD5_bit ' Data 5 portD5
LCD_D6_Direction as sbit at TRISD6_bit ' Data 6 portD6
LCD_D7_Direction as sbit at TRISD7_bit ' Data 7 portD7
' End Lcd module connections

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

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 $8000) = 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] = " " ' " " izostavlja nulu na pocetku "0" prikazuje nulu
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
if text[0] = "0" then ' Eliminise nulu ispred temperature
text[0] = " "
end if
' Print temperature on Lcd (second figure determines position of xxx.xxxx st C)
Lcd_Out(2, 2, text) ' (first figure determines ROW, second figure determines space from begining of LCD)
end sub

main:



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, " Inside temp: ") ' u prvom redu na 1. mestu

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


'--- Main loop
while (TRUE)
'--- Perform temperature reading DQ linija
Ow_Reset(PORTC, 2) ' Onewire reset signal
Ow_Write(PORTC, 2, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTC, 2, 0x44) ' Issue command CONVERT_T
Delay_us(170)

Ow_Reset(PORTC, 2)
Ow_Write(PORTC, 2, 0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTC, 2, 0xBE) ' Issue command READ_SCRATCHPAD

temp = Ow_Read(PORTC, 2)
temp = (Ow_Read(PORTC, 2) << 8) + temp

'--- Format and display result on Lcd

Display_Temperature(temp)

Delay_ms(300)
wend
end.
User avatar
Elektronicar
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 467
Joined: 19-01-2008, 17:05
Location: Balkan

Re: PIC 16F877 i DS18B20 brljavi prilikom prikaza negativne temp

Post by Elektronicar »

Ako sam dobro primetio samo je ovaj deo koda promenjen neracunajuci ispis nule.

Code: Select all

temp2write = (not temp2write) + 1
Post Reply