Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 26th January 2009, 06:12 PM   #1
Default Burglar alarm - checking inputs

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 by adamscybot; 26th January 2009 at 06:14 PM.
adamscybot is offline  
Old 26th January 2009, 07:54 PM   #2
Default

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 by AtomSoft; 26th January 2009 at 07:57 PM.
AtomSoft is offline  
Old 26th January 2009, 08:03 PM   #3
Default

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.
adamscybot is offline  
Old 26th January 2009, 08:08 PM   #4
Default

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 by AtomSoft; 26th January 2009 at 08:11 PM.
AtomSoft is offline  
Old 26th January 2009, 08:13 PM   #5
Default

Quote:
Originally Posted by AtomSoft View Post
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
adamscybot is offline  
Old 26th January 2009, 08:51 PM   #6
Default

Quote:
Originally Posted by adamscybot View Post
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.
BeeBop is offline  
Old 26th January 2009, 10:00 PM   #7
Default

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.
adamscybot is offline  
Old 26th January 2009, 10:16 PM   #8
Default

Quote:
Originally Posted by adamscybot View Post
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

Quote:
Originally Posted by adamscybot View Post
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.

Quote:
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 by BeeBop; 26th January 2009 at 10:20 PM.
BeeBop is offline  
Old 26th January 2009, 11:24 PM   #9
Default

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

The above should get you started
AtomSoft is offline  
Old 28th January 2009, 03:46 PM   #10
Default

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 by adamscybot; 28th January 2009 at 03:49 PM.
adamscybot is offline  
Old 28th January 2009, 04:06 PM   #11
Default

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.
AtomSoft is offline  
Old 28th January 2009, 04:09 PM   #12
Default

Quote:
Originally Posted by AtomSoft View Post
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 28th January 2009, 04:16 PM   #13
Default

Lol Nigel your in Derby like me . 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...
adamscybot is offline  
Old 28th January 2009, 04:32 PM   #14
Default

Edit: Hang on just testing

Last edited by adamscybot; 28th January 2009 at 04:33 PM.
adamscybot is offline  
Old 28th January 2009, 04:32 PM   #15
Default

Quote:
Originally Posted by adamscybot View Post
Lol Nigel your in Derby like me . 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?).
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply

Tags
alarm, burglar, checking, inputs

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Burglar alarm help. compute_a_nerd Electronic Projects Design/Ideas/Reviews 10 13th January 2009 03:14 PM
burglar alarm yoursoninoz Electronic Projects Design/Ideas/Reviews 3 23rd March 2007 04:53 PM
Burglar Alarm tauraim Electronic Projects Design/Ideas/Reviews 6 1st March 2007 11:05 AM
Burglar alarm madmikejt12 Chit-Chat 6 21st October 2006 12:40 PM
burglar alarm chriz Electronic Projects Design/Ideas/Reviews 1 21st June 2005 10:17 PM



All times are GMT. The time now is 03:13 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker