pic 16f628 ds18b20 2x16 lcd dht11

Upload: radoslav-gavranovic

Post on 12-Oct-2015

94 views

Category:

Documents


2 download

DESCRIPTION

pic16f628

TRANSCRIPT

'****Inkubator PIC 16F628 DS18B20 2X16 LCD DHT11****

'****Inkubator PIC 16F628 DS18B20 2X16 LCD DHT11****

Define SIMULATION_WAITMS_VALUE = 1

TRISA = %00111111 'port a je izlaz

PORTA = 0

TRISB = %00000000 'port b je izlaz

PORTB = 0

AllDigital

Define LCD_LINES = 2

Define LCD_CHARS = 16

Define LCD_BITS = 4

Define LCD_DREG = PORTB

Define LCD_DBIT = 0

Define LCD_RSREG = PORTB

Define LCD_RSBIT = 4

Define LCD_EREG = PORTB

Define LCD_EBIT = 5

Define LCD_COMMANDUS = 5000

Define LCD_DATAUS = 100

Define LCD_INITMS = 500

Define 1WIRE_REG = PORTB

Define 1WIRE_BIT = 6

Symbol okretac = PORTA.7

Symbol mikrolijevo = PORTA.0

Symbol mikrodesno = PORTA.1

Symbol grijac = PORTA.6

Symbol piezzo = PORTB.7

Symbol plus = PORTA.4

Symbol minus = PORTA.5

Symbol meni = PORTA.3

Dim podatak As Byte

Dim templbit As Byte

Dim temphbit As Byte

Dim temperatura As Word

Dim set As Word

Dim temp1 As Word

Dim temp2 As Word

Dim temp3 As Word

Dim temp4 As Word

Dim v As Bit

Dim a As Byte

Dim b As Byte

Dim hum As Byte

Dim humd As Byte

Dim sum As Byte

Dim okreni As Word

okreni = 0

Read 12, a 'INTERNAL EEPROM READING ADRESS 12 HIGH BYTE

Read 13, b 'INTERNAL EEPROM READING ADRESS 13 LOW BYTE

set.HB = a 'newsetvalue high byte

set.LB = b 'newsetvalue low byte

Lcdinit

Lcddefchar 0, %01110, %01010, %01110, %00000, %00000, %00000, %00000, %00000 'degree symbol

Lcdcmdout LcdClear

Lcdout " INKUBATOR JAJA "

Lcdcmdout LcdLine2Home

Lcdout " SOFRONIJE "

Gosub okrenime

Gosub showset

start1:

Gosub showtemp

Gosub showrh

Lcdcmdout LcdClear

Lcdout "T:", #temp1, ",", #temp2, 0, "C (", #temp3, ",", #temp4, ")"

Lcdcmdout LcdLine2Home

Lcdout "RH:", #hum, "%"

If temperatura < set Then grijac = 1 'relay out On

If temperatura >= set Then grijac = 0 'relay out off

If temperatura < 36 And v = 0 Then piezzo = 1

If temperatura > 40 And v = 0 Then piezzo = 1

If plus = 0 And piezzo = 1 Then v = 1

If temperatura > 36 And temperatura < 40 Then v = 0

If meni = 0 Then Gosub settmp 'setting temp set value

okreni = okreni + 1

If okreni = 20000 Then Gosub okrenime

Goto start1

End

showtemp: '--------------------SHOW TEMP----------------------

DS18S20Start

WaitMs 1000

DS18S20ReadT templbit, temphbit

temperatura = temphbit

temperatura = ShiftLeft(temperatura, 8)

temperatura = temperatura Or templbit

temp2 = temperatura

temp2 = ShiftLeft(temp2, 12)

temp2 = ShiftRight(temp2, 12)

If temp2 = 15 Then temp2 = 93

If temp2 = 14 Then temp2 = 87

If temp2 = 13 Then temp2 = 81

If temp2 = 12 Then temp2 = 75

If temp2 = 11 Then temp2 = 68

If temp2 = 10 Then temp2 = 62

If temp2 = 9 Then temp2 = 56

If temp2 = 8 Then temp2 = 50

If temp2 = 7 Then temp2 = 43

If temp2 = 6 Then temp2 = 37

If temp2 = 5 Then temp2 = 31

If temp2 = 4 Then temp2 = 25

If temp2 = 3 Then temp2 = 18

If temp2 = 2 Then temp2 = 12

If temp2 = 1 Then temp2 = 0

If temp2 = 0 Then temp2 = 0

temp1 = temperatura

temp1 = ShiftLeft(temp1, 5)

temp1 = ShiftRight(temp1, 9)

Return

showset: '------------------SHOW TEMP SET----------------

temp4 = set

temp4 = ShiftLeft(temp4, 12)

temp4 = ShiftRight(temp4, 12)

If temp4 = 15 Then temp4 = 93

If temp4 = 14 Then temp4 = 87

If temp4 = 13 Then temp4 = 81

If temp4 = 12 Then temp4 = 75

If temp4 = 11 Then temp4 = 68

If temp4 = 10 Then temp4 = 62

If temp4 = 9 Then temp4 = 56

If temp4 = 8 Then temp4 = 50

If temp4 = 7 Then temp4 = 43

If temp4 = 6 Then temp4 = 37

If temp4 = 5 Then temp4 = 31

If temp4 = 4 Then temp4 = 25

If temp4 = 3 Then temp4 = 18

If temp4 = 2 Then temp4 = 12

If temp4 = 1 Then temp4 = 0

If temp4 = 0 Then temp4 = 0

temp3 = set

temp3 = ShiftLeft(temp3, 5)

temp3 = ShiftRight(temp3, 9)

Lcdcmdout LcdClear

Lcdout "Podesavanje"

Lcdcmdout LcdLine2Home

Lcdout #temp3, ".", #temp4, 0, "C "

WaitMs 400

Return

settmp:

piezzo = 0

grijac = 0

Lcdcmdout LcdClear

Lcdout "Podesavanje"

While meni = 0

Wend

loop:

If plus = 0 Then set = set + 1 'temp set increment

If set > 672 Then set = 560

Gosub showset

If meni = 0 Then Goto store

Goto loop

store:

a = set.HB 'High Byte

b = set.LB 'Low Byte

Write 12, a 'INTERNAL EEPROM WRITING ADRESS 12 HIGH BYTE

Write 13, b 'INTERNAL EEPROM WRITING ADRESS 13 LOW BYTE

Lcdcmdout LcdClear

Lcdcmdout LcdLine1Home

Lcdout " PODESENO "

While meni = 0

Wend

Return

showrh:

init:

a = 0

b = 0

TRISB.6 = 0

PORTB.6 = 1

WaitMs 2000

PORTB.6 = 0

WaitMs 20

TRISB.6 = 1

WaitUs 5

dht11response:

While PORTB.6 'cekam nulu

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

WaitUs 5

e:

a = a + 1

If a = 255 Then Goto err 'cekam 1

If PORTB.6 = 0 Then Goto e

WaitUs 3

a = 0

While PORTB.6

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

WaitUs 5

For b = 0 To 7

humh:

If a = 255 Then Goto err

If PORTB.6 = 0 Then Goto humh

hum = ShiftLeft(hum, 1)

WaitUs 8

If PORTB.6 = 1 Then podatak = 1 Else podatak = 0

hum = hum + podatak

a = 0

While PORTB.6

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

Next b

a = 0

For b = 0 To 7

huml:

If a = 255 Then Goto err

If PORTB.6 = 0 Then Goto huml

humd = ShiftLeft(humd, 1)

WaitUs 8

If PORTB.6 = 1 Then podatak = 1 Else podatak = 0

humd = humd + podatak

a = 0

While PORTB.6

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

Next b

a = 0

For b = 0 To 7

temph:

If a = 255 Then Goto err

If PORTB.6 = 0 Then Goto temph

temp1 = ShiftLeft(temp1, 1)

WaitUs 8

If PORTB.6 = 1 Then podatak = 1 Else podatak = 0

temp1 = temp1 + podatak

a = 0

While PORTB.6

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

Next b

a = 0

For b = 0 To 7

templ:

If a = 255 Then Goto err

If PORTB.6 = 0 Then Goto templ

temp2 = ShiftLeft(temp2, 1)

WaitUs 8

If PORTB.6 = 1 Then podatak = 1 Else podatak = 0

temp2 = temp2 + podatak

a = 0

While PORTB.6

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

Next b

a = 0

For b = 0 To 7

kontrolnibit:

If a = 255 Then Goto err

If PORTB.6 = 0 Then Goto kontrolnibit

sum = ShiftLeft(sum, 1)

WaitUs 8

If PORTB.6 = 1 Then podatak = 1 Else podatak = 0

sum = sum + podatak

a = 0

While PORTB.6

a = a + 1

If a = 255 Then Goto err

Wend

a = 0

Next b

a = hum + humd + temp1 + temp2

err:

If a sum Then hum = 0

Return

okrenime:

grijac = 0

a = 0

If mikrolijevo = 0 Then Goto okrenidesno

If mikrodesno = 0 Then Goto okrenilijevo

okrenidesno:

okretac = 1

a = a + 1

WaitMs 1000

If mikrodesno = 0 Then Goto krajokretanja

If a = 120 Then Goto krajokretanja

Goto okrenidesno

okrenilijevo:

okretac = 1

a = a + 1

WaitMs 1000

If mikrolijevo = 0 Then Goto krajokretanja

If a = 120 Then Goto krajokretanja

Goto okrenilijevo

krajokretanja:

okretac = 0

Return