You are on page 1of 6

//This program uses microBasic application.

This
//program is a supplement to a burglar alarm project which
//uses PIC as the core processor.
program BurglarAlarm

symbol sensor = Adc_read(2)
symbol buzzer = PORTC.3

'Global Variables
dim mykpval,x,n,d1,d2,d3,d4,dataNum as byte
dim analogIn, b as string[6]
dim a as string[4]
dim thrshld,i as integer

'Procedures
sub procedure defaultpw
dim i as byte
for i = $01 to $06
if EEProm_Read(i) = $FF or $00 then
delay_ms(30)
EEProm_Write(i,$00)
delay_ms(30)
end if
next i
end sub

sub function getkey (dim state, display as byte) as string
dim x as string[1]
mykpval = 0
select case state
case 1
mykpval = keypad_read
if mykpval = 0 then
goto theEnd
end if
case 0
while mykpval = 0
mykpval = Keypad_Released
wend
end select
select case mykpval
case 5,6,7
mykpval = mykpval - 1

case 9,10,11
mykpval = mykpval - 2

case 14
mykpval = 0
end select
if display = 1 then
Lcd_Cmd(LCD_BLINK_CURSOR_ON)
IntToStr(mykpval,x)
Lcd_Out_Cp(x)
Lcd_Cmd(LCD_CURSOR_OFF)
end if
result = mykpval
theEnd:
end sub

sub procedure passwordentry
dim i,a,b as byte
dim appendtxt,z as string[6]
goback:
appendtxt = ""
z = ""
Lcd_Out(1,1,"6-Digit PIN:")
for i = $01 to $06
Lcd_Out(2,1,"")
a = getkey(0,0)
select case a
case 1
strAppendSuf(appendtxt,"1")
case 2
strAppendSuf(appendtxt,"2")
case 3
strAppendSuf(appendtxt,"3")
case 4
strAppendSuf(appendtxt,"4")
case 5
strAppendSuf(appendtxt,"5")
case 6
strAppendSuf(appendtxt,"6")
case 7
strAppendSuf(appendtxt,"7")
case 8
strAppendSuf(appendtxt,"8")
case 9
strAppendSuf(appendtxt,"9")
case 0
strAppendSuf(appendtxt,"0")
end select
Lcd_Out_Cp(appendtxt)
delay_ms(100)
next i
b = 0
for i = $01 to $06
b = EEProm_Read(i)
select case b
case 1
strAppendSuf(z,"1")
case 2
strAppendSuf(z,"2")
case 3
strAppendSuf(z,"3")
case 4
strAppendSuf(z,"4")
case 5
strAppendSuf(z,"5")
case 6
strAppendSuf(z,"6")
case 7
strAppendSuf(z,"7")
case 8
strAppendSuf(z,"8")
case 9
strAppendSuf(z,"9")
case 0
strAppendSuf(z,"0")
end select
delay_ms(100)
next i
for i = $01 to $06
if appendtxt[i] <> z[i] then
Lcd_Cmd(LCD_CLEAR)
Lcd_Out(1,1,"PIN Invalid!")
delay_ms(1000)
Lcd_Cmd(LCD_CLEAR)
goto goback
end if
next i
Lcd_Cmd(LCD_CLEAR)
Lcd_Out(1,1,"PIN Accepted!")
delay_ms(2000)
Lcd_Cmd(LCD_CLEAR)
end sub

sub procedure setpassword
dim x, i as byte
dim a as string[6]
a = ""
passwordentry
Lcd_Out(1,1,"New 6-Digit PIN:")
for i = $01 to $06
x = getkey(0,0)
select case x
case 1
strAppendSuf(a,"1")
case 2
strAppendSuf(a,"2")
case 3
strAppendSuf(a,"3")
case 4
strAppendSuf(a,"4")
case 5
strAppendSuf(a,"5")
case 6
strAppendSuf(a,"6")
case 7
strAppendSuf(a,"7")
case 8
strAppendSuf(a,"8")
case 9
strAppendSuf(a,"9")
case 0
strAppendSuf(a,"0")
end select
delay_ms(30)
EEProm_Write(i,x)
delay_ms(30)
lcd_Out(2,1,a)
next i
Lcd_Cmd(LCD_CLEAR)
Lcd_Out(1,1,"Password is Set!")
delay_ms(1000)
Lcd_Cmd(LCD_CLEAR)
end sub

sub function power(dim x, n as byte) as longint
dim i as byte
i = 0
result = 1
if n > 0 then
for i = 1 to n
result = result*x
next i
end if
end sub

main:
'Setting Up Peripherals
ADCON1 = $80 'Configure Analog Inputs and Vref
EECON1 = $80
TRISA = $FF 'PORTA is input
TRISC = $00 'PORTC is output
TRISD = %00000111 'Configuring Keypad
TRISB = %00000000 'Configuring LCD
lcd_init(PORTB)
keypad_init(PORTC)

Lcd_Cmd(LCD_CURSOR_OFF)

'Other Procedures
defaultpw
passwordentry

'Main Function
startLoop:
if thrshld = 0 then
thrshld = 500
Lcd_Out(1,1,"Def._Threshold")
Lcd_Out(2,1,"Value Set")
delay_ms(1000)
Lcd_Cmd(LCD_CLEAR)
end if


IntToStr(sensor,analogIn)
lcd_out(1,8,analogIn)
if sensor <= thrshld then
if sensor < 30 then
Lcd_Out(1,1,"Object: ")
Lcd_Out(2,1,"None ")
end if
if sensor >= 30 then
Lcd_Out(1,1,"Object: ")
Lcd_Out(2,1,"Negligible")
end if
end if
if sensor > thrshld then
Lcd_Out(1,1,"Alert!")
Lcd_Out(2,1,"----------")
end if

x = getkey(1,0)
select case x
case 13 '* Set Threshold
repeat:
Lcd_Cmd(LCD_CLEAR)
thrshld = 0
a = ""
i = 3
Lcd_Out(1,1,"Val[30-1023]:")
for n = 0 to 4
x = getkey(0,0)
select case x
case 1
strAppendSuf(a,"1")
case 2
strAppendSuf(a,"2")
case 3
strAppendSuf(a,"3")
case 4
strAppendSuf(a,"4")
case 5
strAppendSuf(a,"5")
case 6
strAppendSuf(a,"6")
case 7
strAppendSuf(a,"7")
case 8
strAppendSuf(a,"8")
case 9
strAppendSuf(a,"9")
case 0
strAppendSuf(a,"0")
end select

lcd_Out(2,1,a)
delay_ms(30)
i = i - 1
next n
i = 3
for n = 0 to 3
select case a[i]
case "1"
dataNum = 1
case "2"
dataNum = 2
case "3"
dataNum = 3
case "4"
dataNum = 4
case "5"
dataNum = 5
case "6"
dataNum = 6
case "7"
dataNum = 7
case "8"
dataNum = 8
case "9"
dataNum = 9
case "0"
dataNum = 0
end select
if n = 0 then
d1 = dataNum
end if
if n = 1 then
d2 = dataNum
end if
if n = 2 then
d3 = dataNum
end if
if n = 3 then
d4 = dataNum
end if
i = i - 1
next n
thrshld = d1*power(10,0)+d2*power(10,1)+d3*power(10,2)+d4*power(10,3)
if thrshld > 1023 then
Lcd_Cmd(LCD_CLEAR)
Lcd_Out(1,1,"Value > 1023!")
delay_ms(1500)
goto repeat
end if
if thrshld < 30 then
Lcd_Cmd(LCD_CLEAR)
Lcd_Out(1,1,"Value < 30!")
delay_ms(1500)
goto repeat
end if
Lcd_Out(1,1,"Threshold Set to:")
intToStr(thrshld,b)
delay_ms(50)
Lcd_Out(2,1,b)
delay_ms(1000)
Lcd_Cmd(LCD_CLEAR)
goto startLoop
case 15 '# Set Password
Lcd_Cmd(LCD_CLEAR)
setpassword
end select
goto startLoop

ending:

end.

You might also like