Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
;-------Delay Subroutine
;period of one MC = 0.001 ms
;75(50(255x2x0.001)) = 1912.5 ms = 2 s
org 0x300
Delay:
mov R0, #75 ;1 MC
L1: mov R1, #50
L2: mov R2, #255
L3: DJNZ R2, L3 ;2 MC
DJNZ R1, L2
DJNZ R0, L1
RET ;2 MC
end
;please notice that we have ignored extra delay corresponding to some instructions in the Delay
;subroutine and this extra delay is: {50(1+2) + 75(1+2) + 2} x 0.001 = 0.377 ms = 0.000377 s
;Assume that bit P2.3 is an input and represents the condition of an oven. If it goes HIGH, it means that
;the oven is hot. Monitor the bit continuously. Whenever it goes HIGH, send a low-to-high pulse to
;pin P1.5 to turn on the buzzer.
ORG 0H
OVEN BIT P2.3
SETB OVEN ;to make the pin an input
BUZZER BIT P1.5
REPEAT: JB OVEN, BUZ
SJMP REPEAT
BUZ: CLR BUZZER
SETB BUZZER
SJMP REPEAT
END
<LABEL> <INSTRUCTION> <OPERAND>
OVEN BIT P2.3
;equates table
OVEN BIT P2.3
BUZZER BIT P1.5
;start main code
org 0x0000 ;reset vector
setb OVEN ;P2.3 high
Repeat: jnb OVEN,Repeat ;continuously poll P2.3 until P2.3 is high
acall Delay ;run delay
cpl BUZZER ;switch P1.5 to opposite state
acall Delay ;run delay
ajmp Repeat ;loop forever
Delay: mov R0,#0xFF
mov R1,#0xFF
DelayCount: djnz R0,DelayCount
djnz R1,DelayCount
ret
end
Code:;equates table OVEN BIT P2.3 BUZZER BIT P1.5 ;start main code org 0x0000 ;reset vector setb OVEN ;P2.3 high Repeat: jnb OVEN,Repeat ;continuously poll P2.3 until P2.3 is high acall Delay ;run delay cpl BUZZER ;switch P1.5 to opposite state acall Delay ;run delay ajmp Repeat ;loop forever Delay: mov R0,#0xFF mov R1,#0xFF DelayCount: djnz R0,DelayCount djnz R1,DelayCount ret end
...
The buzzer will continuously sound until P2.3 is driven low again by the sensor.
Thanks a lot, Jon.
In my humble view, whether the buzzer will sound or not once P2.3 is driven low entirely depends what was the state of the buzzer just before P2.3 went low. The complement operation is actually turning the buzzer on and off. Do you get my point? Please let me know if I'm correct. Thank you.
Best wishes
PG
In order for the buzzer to sound, it must be cycled high and low continuously at a human audible frequency with a square wave. That is what the loop routine that continuously compliments P1.5 does with the delay calls and the "cpl" instruction. When P2.3 is set low again, pin P1.5 may remain in either the high or the low state, but it will not sound if it is left in either of those two states because pin P1.5 is outputting a continuous high or a continuous low (basically a DC signal) once P2.3 is set low.
;Observe the following, noting the role of the OV flag
ORG 0H
MOV A, #-120
MOV R4, #-2
ADD A, R4
SJMP $
END
Compiling file: EXAMP_14.asm
Initializing pre-processor ...
Notice at 5 in EXAMP_14.asm: Overflow `-120' -> `65416'
Syntax error at 5 in EXAMP_14.asm: Operand value out of range: `-120' (`65416')
Notice at 6 in EXAMP_14.asm: Overflow `-2' -> `65534'
Syntax error at 6 in EXAMP_14.asm: Operand value out of range: `-2' (`65534')
Pre-processing FAILED !
Creating code listing file ... -> "EXAMP_14.lst"
2 errors, 0 warnings
Thank you, Jon.
Yes, I understand that I should create a new thread and I will. But some time it's good to combine all the queries about a subject in one place. Besides in the last two months I have had good experience learning from you and Ian Rogers therefore making queries here would notify you that I need your help once again! Anyway, I will create a new thread.
Doesn't a buzzer work on a DC signal? In my last post I had in mind that a buzzer worked on DC and that made me confused.
What's wrong with the code below? It doesn't assemble. Both "-120" and "-2" aren't out of range because range for negative numbers is -1 to -128. Please help me with this.
Jon Wilder said:Assemblers don't deal with signed values (+/-). They only deal with unsigned values (+ only). The microcontroller does not have a floating point unit on it so the microcontroller itself can only deal with unsigned integers (i.e. positive only). In C language, you can use negative values via using signed variables because the compiler handles them via the use of the carry flag. But assemblers weren't really meant to deal with signed variables. Since the 8051 is an 8-bit processor and the accumulator is only 8 bits wide (along with all other RAM registers on an MCS-51), you can only load it with values between 0-255, or 0x00-0xFF in hex.
ORG 0H
MOV A, #-120
MOV R4, #-2
ADD A, R4
SJMP $
END
ORG 0H
MOV A, #87h ; is the same as -120
MOV R4, #0FEh ; is the same as -2
ADD A, R4
SJMP $
END
Code:ORG 0H MOV A, #87h ; is the same as -120 MOV R4, #0FEh ; is the same as -2 ADD A, R4 SJMP $ END
This is how you initiate negative numbers
When you work with negative numbers, you must take care of the sign... ie if you TeST a number by checking the MSBit to check for negativity (is that a word )
ORG 0H
MOV A, #-120
MOV R4, #-2
ADD A, R4
SJMP $
END
;Assume that P1 is an input port connected to a temperature sensor. Write a program to read
;the temperature and test it for the value 75. According to the test results, place the temperature
;value into the registers indicated by the following.
; if T=75 then A=75
; if T<75 then R1=T
; if T>75 then R2=T
ORG 0H
MOV P1, #0FFH
MOV A, P1
MOV R0, #75
CJNE A, R0, UN_EQUAL ;###############################################
SJMP EXIT
UN_EQUAL: JC GREATER
MOV R2, A
SJMP EXIT
GREATER: MOV R1, A
EXIT: NOP
SJMP $
END
Compiling file: EXAMP_25.asm
Initializing pre-processor ...
Syntax error at 14 in EXAMP_25.asm: Invalid set of operands: cjne 224,R0,UN_EQUAL
Pre-processing FAILED !
Creating code listing file ... -> "EXAMP_25.lst"
1 error, 0 warnings