[SOLVED] detecting positive edge

Status
Not open for further replies.

IGIF16

Junior Member level 2
Joined
Dec 19, 2010
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Frince
Activity points
1,442
hi every one
i have wrote this simple code for two photo sensors to give logic 1 (5V) to Pic16f877 or Pic16f84A every thing is good but my problem if any one of the sensors still given 1 the X or Y will increase by 1 each cycle as the code please help me to overcome on this problem.
the advantage of the two sensor to detect and count the human enter and out a room to switch on or off the lighting.


dim x as short
dim y as short
dim a as byte
dim b as byte
sub procedure interrupt
portb.5 = 1
delay_ms (200)
portb.5 = 0
delay_ms (200)
TMr0 = 0
intcon = $20
end sub
main:
OPTION_REG = $80
trisb = $00
trisa = $FF
x = 0
y = 0
portb.1 = 1
portb.2 = 1
igi:
if porta.0 = 1 then
x = x + 1
y = y - 1
portb.2 = 0
delay_ms (100)
portb.2 = 1
end if
if y < 0 then
y = 0
end if
if porta.3 = 1 then
y = y + 1
x = x - 1
portb.1 = 0
delay_ms (100)
portb.1 = 1
end if
if x < 0 then
x = 0
end if
if x > 0 then
portb.4 = 1
else
portb.4 = 0
end if
if y > 0 then
portb.6 = 1
else
portb.6 = 0
end if
if porta.1 = 1 then
TMR0 = 0
INTCON = $A0
while true
nop
wend
end if
goto igi
end.


the code was written for Pic16f84A8)
 

Edge detection involves comparing the present state with a stored previous state, which I can't identify in your code. Nevertheless, you should clarify the involved sensor waveforms. I also see three inputs connected, so what is the third used for?
 

the third input for the interrupt , and my problem with the counting
if there is logic 1 (5v) on porta.3 or porta.0 the X and Y will increase by 1 each cycle
as i think maybe the detection of positive edge solve this problem if it is then please explain how to detect it
 

I already mentioned the method: compare present and previous state.
Code:
' (Please translate to correct BASIC)
if porta.0 = 1 and a0_prev = 0 then
...
end if
a0_prev = porta.0
 

It is very good but how i write it in mikroBasic because my code was written in mikroBasic

---------- Post added at 22:15 ---------- Previous post was at 22:07 ----------

or you mean by a0_prev a variable

---------- Post added at 22:31 ---------- Previous post was at 22:15 ----------

thanks a lot Mr.FvM i do it like this thanks again for idea


if porta.0 = 1 then
if a0_perv = 0 then
x = x + 1
y = y - 1
portb.2 = 0
delay_ms (100)
portb.2 = 1
end if
end if
a0_perv = porta.0


---------- Post added at 22:34 ---------- Previous post was at 22:31 ----------

thanks a lot Mr.FvM i do it in this fashion, thanks again for the idea

dim a0_perv as byte
if porta.0 = 1 then
if a0_perv = 0 then
x = x + 1
y = y - 1
portb.2 = 0
delay_ms (100)
portb.2 = 1
end if
end if
a0_perv = porta.0
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…