Thursday, April 29, 2010

Automatic Temperature Indicator-Program

;Cetpa-Roorkee
; matirx keyboard with connections on p2 port
;rows are connected to p2.0 to p2.2
;columns are connected to p2.4 to p2.7
;Lcd connected at p1 with Controls pins
;at rs-p3.5,rw-p3.4,en-p3.3

sda equ p0.1
scl equ p0.0
rs equ p3.5
rw equ p3.4
en equ p3.3
lcd equ p1
lcd_busy equ p1.7
key_brd equ p2
th equ 50h
tl equ 51h
ascii_store equ 40h
delimiter equ 20h
delay_count equ 23h
temperature_store equ 30h
temp_result equ 35h
LCD_str equ 2ah

;Flags
th_flag equ 7fh
tl_flag equ 7eh
temp_flag equ 7dh

org 0000h
mov sp,#60h
lcall ini

clr th_flag
clr tl_flag

mov th,#40
mov tl,#37
mov dptr,#Msg0
acall msg_read
mov delay_count,#40 ;for 2-sec time
acall time ; $$$$$$$$$$$$$$$

value_enter:
mov r7,#01h
acall command
mov dptr,#Msg1 ;show th
acall read

mov a,th
mov r1,#LCD_str
acall bin2ascii
inc r1
acall tim_display

mov r7,#87H
acall command
mov dptr,#Msg2 ;show th
acall read

mov a,tl
mov r1,#LCD_str
acall bin2ascii
inc r1
acall tim_display
;till here default values are shown
;into nezt line show following
mov r7,#0c0h
acall command
mov dptr,#Msg5
acall msg_read

scan0:
mov key_brd,#0f0h ;bcoz upper 4
mov a,key_brd ;are connected to colu
cjne a,#0f0h,mode_check0
sjmp scan0
;**************** detecting rows
mode_check0:
acall delay1
mov a,key_brd
cjne a,#0f0h,mode_check1
sjmp scan0

mode_check1:
acall delay
acall delay1
mov key_brd,#0ffh
mov key_brd,#0fbh
mov a,key_brd
cjne a,#0fbh,enter_check
ajmp scan0

enter_check: ;This is enter button
jb acc.6,mod43
ajmp temp_conversion_start
ljmp scan0

mod43: ;This is clear button
jnb acc.7,clear_press
ljmp scan0

clear_press:
mov r7,#01h
acall command
mov dptr,#Msg1 ;show th
acall read
mov r7,#38h
acall command
mov r7,#0eh
acall command
mov r7,#06h
acall command
mov r7,#0c0h
acall command
setb th_flag
; mov r6,#3
ajmp forward
tl_edit:
mov r7,#01h
acall command

mov dptr,#Msg2 ;show tl
acall read
mov r7,#0c0h
acall command

setb tl_flag

forward:
mov lcd,#00h
scan:
mov key_brd,#0f0h ;bcoz upper 4 are
mov a,key_brd ;connected to column
cjne a,#0f0h,row
sjmp scan
;*************** detecting rows
row:
acall delay1
mov a,key_brd
cjne a,#0f0h,row111
sjmp scan

row111:
acall delay
acall delay1
mov key_brd,#0ffh
mov key_brd,#11111110b
mov a,key_brd
orl a,#0fh
cjne a,#0ffh,row1
mov key_brd,#0ffh
mov key_brd,#0fdh
mov a,key_brd
cjne a,#0fdh,row2

mov key_brd,#0ffh
mov key_brd,#0fbh
mov a,key_brd
cjne a,#0fbh,row3
ajmp scan

;**********
row1:
jb acc.4,c2
mov r7,#'0'
acall display
sjmp scan
c2:
jb acc.5,c3
mov r7,#'1'
acall display
sjmp scan
c3:
jb acc.6,c4
mov r7,#'2'
acall display
sjmp scan

c4:
mov r7,#'3'
acall display
sjmp scan
;****************
row2:
jb acc.4,c22
mov r7,#'4'
acall display
ljmp scan
c22:
jb acc.5,c32
mov r7,#'5'
acall display
ljmp scan
c32:
jb acc.6,c42
mov r7,#'6'
acall display
ljmp scan
c42:
mov r7,#'7'
acall display
ljmp scan
;****************
row3:
jb acc.4,c23
mov r7,#'8'
acall display
ljmp scan
c23:
jb acc.5,c33
mov r7,#'9'
acall display
ljmp scan
c33:
jb acc.6,c43
ajmp test ;This is enter button
ljmp scan
c43:
;This is clear button
;not used here
ljmp scan

*******************
test:
mov r7,#04h
acall command
acall ready
acall read_add1
clr c
mov b,#0c0h
subb a,b
mov r0,a
mov r1,#ascii_store
acall ready
acall read_data
repeat:
inc r1
acall ready
acall read_data
mov @r1,a
djnz r0,repeat
mov delimiter,r1
inc delimiter

************
;conversion routine ;bank 1 used
setb psw.3
mov r5,#00h ;used for msb
mov r6,#00h ;used for storing the lsb result
mov r0,#0ah
mov r2,#1
mov r3,#10
mov r4,#100
mov r1,#(ascii_store+1)
here: clr a
mov a,r1
cjne a,delimiter,convert
ajmp break ; read till there
convert:
mov a,@r1
anl a,#0fh
mov b,@r0
mul ab
mov r7,a
xch a,b
cjne a,#00h,hiii
looo:
mov a,r6 ;result of add
addc a,r7
jc for
con: mov r6,a
inc r1
inc r0
ajmp here
break:
clr psw.3

**********************
jb tl_flag,tl_save
clr c
mov a,0eh ;0eh is the add of r6 of bank1
cjne a,#7dh,th_check

th_check:
jnc error_msg ;a> or = 125 show error
mov th,0eh ;if c then save
clr th_flag
ajmp tl_edit

error_msg:
mov r7,#01h
acall command
jb tl_flag,tl_error
mov dptr,#Msg3
acall msg_read
acall time
ajmp clear_press ;again enter th

tl_error:
mov dptr,#Msg4
acall msg_read
acall time
ajmp tl_edit ;again enter tl

***************portion of the conversion routine
hiii:
add a,r5
mov r5,a
ajmp looo
for:
inc r5
ajmp con

**********************
tl_save:
clr c
mov a,0eh ;0eh is the add of r6 of bank2
cjne a,th,tl_check

tl_check:
jnc error_msg ;a> or = th show error
mov tl,0eh ;0eh is the add of r6 of bank2
clr tl_flag
;now we have to show the saved values on
;LCD & wait for enter to be pressed
ajmp value_enter
;___________________________
temp_conversion_start:
mov r0,#th
acall delay10
acall i2cstart
mov a,#90h
acall i2cwrite
mov a,#0ach
acall i2cwrite
acall delay10
mov a,#02h
acall i2cwrite
acall delay10
acall i2cstart
mov a,#90h
acall i2cwrite
mov a,#0a1h
acall i2cwrite
acall delay10
mov a,th ;#28h
acall i2cwrite
mov a,#00h
acall i2cwrite
acall delay10
acall i2cstart
mov a,#90h
acall i2cwrite
mov a,#0a2h
acall i2cwrite
acall delay10
mov a,tl ;#24h
acall i2cwrite
mov a,#00h
acall i2cwrite
acall delay10
acall i2cstop
clr temp_flag ;temp flag
mov r7,#01h
acall command
mov dptr,#Msg
acall read
mov temperature_store,#00h
here1:
acall temp ;temp read by this routine
jb temp_flag,EEE
setb temp_flag
mov temperature_store,a
ajmp here1
EEE:
cjne a,temperature_store,make_equal
ajmp here1

make_equal:
mov temperature_store,a
acall conversion
mov r7,#8bh
acall command
acall string
sjmp here1
; ************************
temp:
acall i2cstart
mov a,#90h
acall i2cwrite
;*****************************
mov a,#0eeh
acall i2cwrite
; start temp conversion
acall i2cstop
acall i2cstart
mov a,#90h
acall i2cwrite
mov a,#0aah
acall i2cwrite
; read last read temp
acall i2cstart
mov a,#91h
acall i2cwrite
acall i2cread
acall i2cstop
ret

i2cstart:
setb sda
acall delayone
setb scl
acall delayone
clr sda
clr scl
ret

i2cstop:
clr scl
clr sda
setb scl
acall delayone
setb sda
mov c,sda
ret

;=======================
i2cwrite:
mov r0,#08h
i2cwrite_loop:
rlc a
mov sda,c
setb scl
acall delayone
clr scl
djnz r0,i2cwrite_loop
mov c,sda
setb scl
acall delayone
clr scl
ret

;=======================
i2cread:
mov c,sda
mov r0,#8
rlc a
i2cread_loop:
setb scl
mov c,sda
rlc a
acall delayone
clr scl
djnz r0,i2cread_loop
ret
delayone:
mov r1,#100
loopa:
djnz r1,loopa
ret

;**********************
read_data: ;data is stored in the r0 register
mov lcd,#0ffh
setb rs ;to select data register
setb rw
clr en
setb en
mov a,lcd
clr en
ret

read_add1:
mov lcd,#0ffh
clr rs
setb rw
clr en
setb en
mov a,lcd
clr en
orl a,#80h
ret

read_add:
push acc
mov lcd,#0ffh
clr rs
setb rw
clr en
setb en
mov a,lcd
clr en
orl a,#80h
cjne a,#8fh,out
mov r7,#0c0h
acall command
out:
pop acc
ret

;******************
conversion:
push acc
mov a,temperature_store
mov r0,#(temperature_store+1)
conloop:
mov b,#10
div ab
orl 0f0h,#30h
mov @r0,0f0h
inc r0
cjne a,#00h,conloop
dec r0
mov r1,#temp_result;35h
conloop1:
mov a,@r0
mov @r1,a
dec r0
inc r1
cjne r0,#temperature_store,conloop1
mov @r1,#'$'
pop acc
ret

;====================
string:
push acc
mov r0,#temp_result;30h
strloop:
mov a,@r0
mov r7,a
cjne a,#'$',continue
mov r7,#' '
acall display
pop acc
ret
continue:
acall display
inc r0
sjmp strloop

;*******************
bin2ascii:
mov @r1,#'$'
dec r1
rep:
mov b,#10
div ab
orl 0f0h,#30h
mov @r1,0f0h
dec r1
cjne a,#00h,rep
ret

tim_display:
brij: mov 07h,@r1
cjne r7,#'$',shoe
ret
shoe:
acall display
inc r1
ajmp brij

command:
acall ready
mov lcd,r7
clr rs
clr rw
setb en
clr en
ret
display:
acall ready
mov lcd,r7
setb rs
clr rw
setb en
clr en
ret
ready:
clr en
mov lcd,#0ffh
clr rs
setb rw
wait:
clr en
setb en
jb lcd_busy,wait
clr en
ret

ini:
mov r7,#38h
acall delay1
acall delay1
acall command
mov r7,#38h
acall delay1
acall command
mov r7,#38h
acall delay1
acall command
mov r7,#0eh
acall command
mov r7,#01h
acall command
mov r7,#06h
acall command
mov r7,#80h
acall command
ret

delay10:
push acc
mov tmod,#01h
mov tl0,#10h
mov th0,#27h ;1050=2710h
mov tcon,#00h
setb tr0
wait12: jnb tf0,wait12
clr tr0
pop acc
ret

delay1:
push 07h
push 06h
mov r6,#100
loop24: mov r7,#100
loop23: djnz r7,loop23
djnz r6,loop24
pop 06h
pop 07h
ret
;**********

delay:
push acc
push 00h
mov r0,#02h
loopr:
mov b,#0a0h
loopb:
mov a,#0fah
loopa12:
dec a
jnz loopa12
djnz 0f0h,loopb
djnz r0,loopr
pop 00h
pop acc
ret

time:
push 00h
mov r0,30h ;30h contain counter
mov tmod,#01h
repeat1: mov tcon,#00h
mov th0,#3ch
mov tl0,#0aah
setb tr0
wait1: jnb tf0,wait1
clr tr0
djnz r0,repeat1
pop 00h
ret

msg_read:
push acc
push 07h

nex: clr a
movc a,@a+dptr
mov r7,a ;r7 is used for passing parameter to display
cjne a,#'0',aga
sjmp down
aga: acall read_add
acall display
inc dptr
sjmp nex
down:
pop 07h
pop acc
ret

read:
nex1:
clr a
movc a,@a+dptr
cjne a,#'0',aga1
sjmp down1
aga1:
mov r7,a
acall display
acall delay1
inc dptr
sjmp nex1
down1:
ret

Msg: db 'Temprature ','0'
Msg0: db 'Welcome To Auto Temp Controller','0'
Msg1: db 'TH ','0'
Msg2: db 'Tl ','0'
Msg3: db 'Error: TH > or = 125','0'
Msg4: db 'Error: Tl > or = TH','0'
Msg5: db 'Enter for cont.','0'
end








No comments:

Post a Comment