S215  08 1008

Lab HJ08-08 Counter Using Two Switches
15 points
OEES 215

Back to Main Page
 
Add some code to the program below so that pressing switch 1 will also increment the counter, but will do so when the switch is first released, rather than when it is first pressed. In other words, switch 0 will increment the counter when it goes from off to on (leading edge), and switch 1 will increment the counter when it goes from on to off (trailing edge).

Hint: You'll need a previn1 variable to store the previous state of switch 1.

Right click here to download the program that you see below. (The Ckedge function is also included in the download.)
 
proc main()

ubyte in0, previn0, ibyt, edgecondition, cnt 
Init()
previn0 = 0
cnt = 0
while 1==1 do
//****************
  ibyt = GetInBts()
  in0 = SelBit(ibyt,0)

  edgecondition = Ckedge(in0, previn0)
  previn0 = in0
  if edgecondition==1 then    //Leading edge
    cnt = cnt + 1
  endif
 
  Outall(cnt)
//****************
done

endproc
The program on the left is for a counter. Each time switch 0 is pressed, the number displayed in the LEDs is incremented by one. (Switch 0 is being used as a pushbutton.) The incrementing occurs when the switch is first pressed. The next increment will only occur when the switch has been released and then pressed again.

The Ckedge function is used to determine when the leading edge of in0 occurs. If Ckedge returns a 1, then a leading edge has occurred, and we add one to the counter (cnt). 

Back to Main Page