Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Burglar alarm - checking inputs

Status
Not open for further replies.

adamscybot

New Member
Hi,

I'm new to PICs and have just begun programming a little alarm. I am using the PIC16F88.

I have got everything except for one part running nice and smoothly. The alarm features a code set feature (write to eeprom) and on every boot it fetches the 4-digit code from the eeprom and stores in registers code1,code2,code3,code4.

So if the code was say, buttons 0,3,1,2 you'd have:

code1 = 00000001
code2 = 00001000
code3 = 00000010
code4 = 00000100

So the bit that is on represents the button that needs to be pressed.

The problem is that I cant decode the registers into my code checking section. Here is the code check section for the first bit.

Code1 MOVF PORTA,W
BTFSC STATUS,2
GOTO Code1
BTFSS PORTA,code1
BSF flag,0
CALL Release
RETURN

So basically what I want to happen is that when a button on porta is pressed it checks that it is the same as the one represented in the code1 register.

The problem lies with :

BTFSS PORTA,code1

If I do a linear comparison like,

BTFSS PORTA,0

Everything works fine. But using code1 as the second parameter causes the code to fail (recognized as incorrect). I don't know how to do this part properly. I have tried calling up something to convert the "relative" bit positions from the "code" registers and convert them to pure binary then use that as a second parameter but that doesnt work either. E.g. changing 00001000 to 00000011
hence third button --> binary to number 3.

Just for notes, the BSF flag,0 is an error flag called upon later to see if the code was wrong. Release checks to see all buttons are released.

I don't know how to go about this.

Adam
 
Last edited:
Why are you testing it against PORTA? Check it like :
Code:
BTFSS PORTA,0
RETRUN    ;BAD NUMBER
CALL  DELAY10ms

BTFSS PORTA,3
RETURN    ;BAD NUMBER 
CALL  DELAY10ms

BTFSS PORTA,1
RETRUN    ;BAD NUMBER
CALL  DELAY10ms

BTFSS PORTA,2
RETURN     ;BAD NUMBER
GOTO SomeWhere    ;GOTO anywhere since the code is correct

be sure to delay/debounce or you will get some trouble.
 
Last edited:
I think you misunderstood me. Im "trying" to check the inputs on PORTA against the code in the code1 register.

Hence BTFSS PORTA,code1.

Which of course doesnt work, which is my problem.

My code is dyamic, it can be changed...

Just doing BTFSS PORTA,0

or whatever, will work but im trying to compare against another register. The code isnt the same all the time.
 
oh ok 1 minute i got the fix:

Is PORTA only for buttons? How are the buttons coming in?

like if you press number 2 is porta = 0000 0010 ?

Oh i think i see you are starting from :
0= 0x01 - 00000001
1 = 0x02 - 00000010


and so on am i right?
 
Last edited:
oh ok 1 minute i got the fix:

Is PORTA only for buttons? How are the buttons coming in?

like if you press number 2 is porta = 0000 0010 ?

Oh i think i see you are starting from :
0= 0x01 - 00000001
1 = 0x02 - 00000010


and so on am i right?

Well there is a code setting procedure ealier on that writes to eeprom. The code1,2,3 and 4 registers are read from the eeprom after that.

PORTA is also, yes, only 4 buttons. 0,1,2,3.

Addresses in eeprom start at 0x00 to 0x03. Ive then read them into the code registers.

Your are right :)
 
I think you misunderstood me. Im "trying" to check the inputs on PORTA against the code in the code1 register.

Hence BTFSS PORTA,code1.

Which of course doesnt work, which is my problem.

My code is dyamic, it can be changed...

Just doing BTFSS PORTA,0

or whatever, will work but im trying to compare against another register. The code isnt the same all the time.

Remember BIT TEST in File tests only one bit! BTFSS PORTA, code1 checks the bit code1 in port A

I don't think this is what your want, is it? You want to compare EACH bit read from Port A with EACH bit in code1, don't you?

You could start by comparing the first bit, and if that is correct, then the second and so on. If the first bit fails, then you don't have to compare any more.
 
I already compare 1 bit then the next etc.

I have 4 registers. code1,code2,code3,code4.

The snippet of code I posted is supposed to compare the first digit of the code only.

I do want to compare the input on portA to the code1. Then in the next code block I compare to code2 register. Etc. I simply posted the code1 section only as the rest are just slightly changed dupes.

E.g.

code1 = 00000001
code2 = 00001000
code3 = 00000010
code4 = 00000100

They are the registers. So that would be buttons 0,3,1,2 on porta. The contents change though, as I have a code setting routine to write to eeprom ealier on.
 
Hi,

I'm new to PICs and have just begun programming a little alarm. I am using the PIC16F88.


The problem lies with :

BTFSS PORTA,code1

If I do a linear comparison like,

BTFSS PORTA,0

Everything works fine. But using code1 as the second parameter causes the code to fail (recognized as incorrect). I don't know how to do this part properly. I have tried calling up something to convert the "relative" bit positions from the "code" registers and convert them to pure binary then use that as a second parameter but that doesnt work either.
I don't know how to go about this.

Adam


I already compare 1 bit then the next etc.

I have 4 registers. code1,code2,code3,code4.

The snippet of code I posted is supposed to compare the first digit of the code only.

I do want to compare the input on portA to the code1. Then in the next code block I compare to code2 register. Etc. I simply posted the code1 section only as the rest are just slightly changed dupes.

E.g.

code1 = 00000001
code2 = 00001000
code3 = 00000010
code4 = 00000100

They are the registers. So that would be buttons 0,3,1,2 on porta. The contents change though, as I have a code setting routine to write to eeprom ealier on.


You are NOT comparing a bit, if you are using a register! Look at your code1 - is it a bit? or is it 8 bits?


You can compare a register by anding it with another register, and checking the status bit, but BIT test is only one bit, so when you pass a file to the test when it expects a bit, of course you will get an error.

BTFSS PORTA,0
This works because 0 is one BIT of PortA. Code1 is a register you have defined, and that register has 8 bits

PS, a digit is not one bit...
 
Last edited:
something like this:

Code:
MOVLW PORTA
ANDLW 0x0F
INCF W
ANDLW CODE1

Then you have to check if W is a 0 or something like this . I forgot how to do it in ASM. Also im cleaning house since early and have not lot of time. Sorry for the delay also :D

The above should get you started
 
Hi. Sorry for slow reply.

BeeBop, I totally understand what you are saying and I know that is exactly why my code doesn't work. What Im asking is what can I do to make working code which has the same affect (well, has the same affect if it did work).

Maybe you could detail more for me on comparing using "AND" and the status bit?

AtomSoft: I see what your trying to do but I can't see how I can use the result to compare...(unless your pointing towards using the status register like BeeBop said). Surely if the person got the correect answer you would end up with an 8-bit register again.

EDIT: I have an idea. Just and the code1 with porta like you did then use decsz or something to that affect to see if it is >0. If it ism clearly the code is correct.
 
Last edited:
you might have to shift each bit and check if zero if you do it that way.. i think.. not sure ... if it was a 18F pic it would be way simpler.
 
you might have to shift each bit and check if zero if you do it that way.. i think.. not sure ... if it was a 18F pic it would be way simpler.

Why would it be any simpler on an 18F? - it's absolutely trival on any PIC - they include specific bit tests (BTFSS and BTFSC), a huge advantage of a PIC.
 
Lol Nigel your in Derby like me :D. What a coincidence :).

Anyway, Ive just tried this:

MOVF PORTA,W
BTFSC STATUS,2
GOTO Code1
MOVF PORTA,W
ANDLW code1
BTFSC STATUS,2
BSF flag,0
CALL Release
RETURN

So using the zero flag. Supposedly this should make it so that if the and result is 0, the z flag is set. So in this code if it isn't set (indiciating correct answer).

However, it doesnt seem to work properly. Ive got the code2,3 and 4 routines hard coded at the minute to button 0,2,2 while I mess with the code 1 procedure (dont want to break it all at once).

With this for some reason If I set the code to say, button 3 then do 0,0,2,2 or 1,0,2,2 it is saying the code is correct...
 
Lol Nigel your in Derby like me :D. What a coincidence :).

And I've also got two Cybots, and a website about them!.

That try was wrong -easy way to do it:

Read the entire port to W.

Transfer W to a GPR.

Check each of the bits in the GPR using either BTFSS or BTFSC, depending which way you're testing.

Also, use the include file names, I've no idea what 'Status, 2' means?, the correct way would be 'Status, Z' (assuming 2 is Z?).
 
And I've also got two Cybots, and a website about them!.

That try was wrong -easy way to do it:

Read the entire port to W.

Transfer W to a GPR.

Check each of the bits in the GPR using either BTFSS or BTFSC, depending which way you're testing.

Also, use the include file names, I've no idea what 'Status, 2' means?, the correct way would be 'Status, Z' (assuming 2 is Z?).


Hi, ive done that, it still doesnt work. did some debuging and saw the problem.

I tried this:

test MOVF cPORTA,W
ANDLW code1
MOVWF PORTB
GOTO test

and for some reason what is in code1 is totally ignored. No matter what If i press button 0 light 0 on B lights. Same for button 1. However 2 and 3 never work no matter what. Whatever you set the codeto, 1 and 2 pass the "and" check and nothing else does...confusing.

Can you remember the RealCybot site?

BTW, yes 2 is zero flag.
 
Last edited:
My bad, used ANDLW code1 which obviously wont work.

Working code:

Code1 MOVF PORTA,W
BTFSC STATUS,2
MOVF PORTA,W
ANDWF code1,0
MOVWF temp
CALL Flag
CALL Release
RETURN

It works!!!!!!!! Flag checks every bit in temp.

My problems are solved. Thanks to everyone.
 
My bad, used ANDLW code1 which obviously wont work.

Working code:
Code:
Code1 
    MOVF    PORTA, W
    BTFSC    STATUS, 2
    MOVF    PORTA, W
    ANDWF    code1, 0
    MOVWF    temp
    CALL    Flag
    CALL    Release
    RETURN

It works!!!!!!!! Flag checks every bit in temp.

My problems are solved. Thanks to everyone.


Sorry, I got here so late this morning. I was just working out how to explain (don't do enough assembler these days...) but I see you got it, on your own. Great job! :)

Now I can get some more coffee! :p

PS. Here is a tip for when you post code on here:
Surround it with
Code:
 and
tags
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top