Hüseyin Aslan
Hüseyin Aslan

Reputation: 31

8051 assembly button click

I can't seem to find an answer for this question.

One LED is connected to port P2.4, and one button connected port P1.7. We have COUNT variable, and we increment it each time the button is clicked. So if we press the button 5 times led is on, if we press button 10 times led is off and its continue 15 led is on, 20 is off. I think its might be necessary to use the CJNE instruction, but I'm not sure.

LED EQU P2.4
button EQU P1.7
COUNT EQU 30H
    ORG  00H 
    LJMP main
    main:
          CLR P2.4
          SETB P1.7
          MOV COUNT,#0
    again: 
           JB button,$
           JNB button,$
           INC COUNT 
          MOV A,COUNT
           ANL A,#01h
           MOV P2.4,A
           JMP again
    END

Upvotes: 3

Views: 2879

Answers (2)

Michael Petch
Michael Petch

Reputation: 47633

It appears you were using COUNT to check if a number was even odd and set the LED accordingly. I'm not sure you had to do that. A simplified version could be:

LED EQU P2.4
button EQU P1.7

COUNT EQU 30H
ORG  00H 
main:
      MOV COUNT, #0
      SETB button  ; Enabled button
again: 
      SETB LED     ; Turn off LED segment 
      JB button,$  ; Wait until button pressed
      CLR LED      ; Turn on the LED segment
      JNB button,$ ; Wait until button released
      INC COUNT    ; Increment count on release
      JMP again    ; Do again
END

I've kept the COUNT variable but I'm not sure when you intended to increment it. I assumed after you release the button given the code you presented.

This assumes that you have the ports correct. I'm not convinced the LED is on P2, and I'm not sure P1 is the switch. Is it possible you have them reversed? If they are you might want to alter the first two lines to be:

LED EQU P1.4
button EQU P2.7

Upvotes: 0

David Hoelzer
David Hoelzer

Reputation: 16379

You never tell us how you have COUNT and BUTTON defined. This leads me to two possible errors here.

Firstly, I'm not sure if this is a copy-paste error, but I do not believe that

 MOV COUNT

is legal. I'm fairly certain that you need to specify a source and destination. I would guess that this is supposed to be:

 MOV COUNT,0

Secondly, you have this:

 SETB P1.7

You never tell us, but I'm assuming that this is the pin that the button is connected to. Yet, later, you do this:

 JB BUTTON, $
 JNB BUTTON, $

Since I don't see you define BUTTON as an identifier for P1.7 anywhere, I'm going to assume that your code is infinitely stuck at JNB BUTTON,$. I believe this would correct this:

 JB P1.7, $
 JNB P1.7, $

Revision based on your update You have defined count as a value, but you have not reserved any memory for it. The result is that you are (likely) inadvertently incrementing a byte in memory rather than a byte that you control. You will want to reserve memory for that. I'd have to actually fire up what you have in an emulator to see what it will do in the real world.

I would expect this to look something like this:

DSEG
       ORG 30h
COUNT  DS  1

CSEG
       ORG 0h
    <Your code goes here>

Upvotes: 0

Related Questions