Skip to content

Instantly share code, notes, and snippets.

@MrWooJ
Last active June 8, 2017 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrWooJ/da38fc004912bea87b9c695aa4782fd2 to your computer and use it in GitHub Desktop.
Save MrWooJ/da38fc004912bea87b9c695aa4782fd2 to your computer and use it in GitHub Desktop.
Microprocessor Project: Assembly 8051
; Note1: About DJNZ Operation (DJNZ R1, $)
; | Decrement the Contents of R1 and If the Result Is
; | Non-Zero Jump Back to This Line. In this Way, The Contents
; | of R1 Are Continuously Decremented until They Each Zero.
; | Remember That This Command Takes 2 Machine Cycles.
; Note2: About Registers Usage
; | Use of R0: Holding Delay x Counter
; | Use of R1: Holding First Sensor Input Value
; | Use of R2: Holding Secoond Sensor Input Value
; | Use of R3: Holding Third Sensor Input Value
; | Use of R4: Holding Threshold Input Value
; | Use of R5: Holding Delay x Counter
; | Use of R6: Holding Temporary Values for Load/Store Accumulator
; | Use of R7: Holding Timer/Counter 1 Prefered Input Value
; Note3: About Usage of ORL in Timer/Counters
; | Why Do We Use Logical OR in TMOD?
; | Because It Might be Both Timer/Counters Were Working Synchronously,
; | So We Should Control The Desire T/C Without Affecting on Another T/C.
; | So We Use OR to Make Hands on Only for Desired Bits, Not Others.
;------------------------------------------------------------------
;---------------------** PROGRAM OPERATIONS **---------------------
;------------------------------------------------------------------
; ** Memory Re-Position **
MEMLOCATIONS:
ORG 0000H ; Position 00H of Code Segment Stands for Reset Service Routine
SJMP INIT ; Jump to Init Label to Avoid the IVT
ORG 0050H ; At Position 0x50
; ** Start Program **
INIT:
CALL TURNOFFLED
CALL PREINITREGISTERS ; Call Pre-Init for Filling Constant Registers
CALL SETUPTIMERT1 ; Only Setup Predefined Setting on Timer/Counter 1
CALL SETUPUART ; Only Setup Predefined Setting on UART
CALL STARTTIMER1 ; Start Timer/Counter 1 in Timer mode
RESTART:
CALL RESETREGISTERS ; Restarting Registers Value for Starting Over
MOV R5, #20 ; Store Value for Delay Counter Times
MAIN:
MOV R0, #200 ; Store Value for Delay Counter Times
AGAIN:
CALL SETUPTIMERT0 ; Only Setup Predefined Setting on Timer/Counter 0
CALL STARTTIMER0 ; Start Timer/Counter 0 in Timer mode
BACK:
JNB TF0, BACK ; Wait for 5000 Cycles by T/C 0 Overflow Flag
CALL RESETTC0 ; Re-Start TImer/Counter 0 From Beginning
DJNZ R0, AGAIN ; Repeat This Process for 200 Times
DJNZ R5, MAIN ; Repeat Whole This Block for 20 Times
CALL TURNOFFLED ; Turn off Threshold LED Before Starting Program
CALL PROGRAM ; Now It is Time to Do Program Operation
CALL RESTART ; Repeat this Process Forever
;--------------------------------------------------------------------
;---------------------** ALGORITHM OPERATIONS **---------------------
;--------------------------------------------------------------------
; ** Doing Program Subroutine **
PROGRAM:
CALL READINPUT ; Read Inputs from Pins and Store them in Registers
CALL ALGORITHM ; Do Algorithm Process to Find Passed Threshold Inputs
CALL SENDDATA ; Prepare Input Data to UART
RET ; Return From Subroutine Label Call
; ** Operate Algorithm Subroutine **
ALGORITHM:
MOV A, R4 ; Load Value of Register #4 into A Register
MOV B, R1 ; Load Value of Register #1 into B Register
CALL CHECK ; Call Check Subroutine to Check Input and Threshold Values
MOV A, R4 ; Load Value of Register #4 into A Register
MOV B, R2 ; Load Value of Register #2 into B Register
CALL CHECK ; Call Check Subroutine to Check Input and Threshold Values
MOV A, R4 ; Load Value of Register #4 into A Register
MOV B, R3 ; Load Value of Register #3 into B Register
CALL CHECK ; Call Check Subroutine to Check Input and Threshold Values
MOV A, R6 ; Load Value of Register #6 into A Register
MOV B, #1 ; Load Immediate Value One into B Register
CALL FINALCHECK ; Call FINALCHECK Subroutine to Check Input and Threshold Values
RET ; Return From Subroutine Label Call
; ** Send to UART Algorithm Subroutine **
SENDDATA:
CLR TI ; Clear TI Flag for Sending New Character
MOV DPTR, #400H ; Head Point of Data Pointer to Position of 0x400
MOV A, R1 ; Load Value of Register #1 into A Register
MOVC A, @A+DPTR ; Look Up Based on Acc + Data Pointer Position
CALL TRANSMITDATA ; Transmit Prepare Character to UART
MOV A, #16 ; Load Value of Dash into A Register
MOVC A, @A+DPTR ; Look Up Based on Acc + Data Pointer Position
CALL TRANSMITDATA ; Transmit Prepare Character to UART
MOV A, R2 ; Load Value of Register #2 into A Register
MOVC A, @A+DPTR ; Look Up Based on Acc + Data Pointer Position
CALL TRANSMITDATA ; Transmit Prepare Character to UART
MOV A, #16 ; Load Value of Dash into A Register
MOVC A, @A+DPTR ; Look Up Based on Acc + Data Pointer Position
CALL TRANSMITDATA ; Transmit Prepare Character to UART
MOV A, R3 ; Load Value of Register #3 into A Register
MOVC A, @A+DPTR ; Look Up Based on Acc + Data Pointer Position
CALL TRANSMITDATA ; Transmit Prepare Character to UART
RET ; Return From Subroutine Label Call
; ** Check Subroutine **
CHECK:
CJNE A, B, NEXT ; Jump to NEXT Label if A and B Are Not Equal
RET ; Return From Subroutine Label Call
NEXT:
MOV A, #00 ; Load Immediate Value One into A Register
ADDC A, R6 ; Add Accumulator Value by Value of R6 and Carry then Store in Accumulator
MOV R6, A ; Store Accumulator Value into R6 Register
RET ; Return From Subroutine Label Call
; ** Final Check Subroutine **
FINALCHECK:
CJNE A, B, FINALNEXT ; Jump to FINALNEXT Label if A and B Are Not Equal
RET ; Return From Subroutine Label Call
FINALNEXT:
JNC TURNONLED ; Turn On LED by Jumping to TURNONLED if Carry is Zero
RET ; Return From Subroutine Label Call
; ** Read Data Inputs **
READINPUT:
MOV A, P0 ; Load P0 Pin Data Value Into Accumulator
ANL A, #0x0F ; Store Low Nibble of Accumulator in Accumulator (Pin P0.0 to P0.3 Data Input)
MOV R1, A ; Store Value of Accumulator in Register #1
MOV A, P0 ; Load P0 Pin Data Value Into Accumulator
ANL A, #0xF0 ; Store High Nibble of Accumulator in Accumulator (Pin P0.4 to P0.7 Data Input)
SWAP A ; Swap Nibbles of Accumulator in Order to Appropriate Store in Register
MOV R2, A ; Store Value of Accumulator in Register #2
MOV A, P1 ; Load P1 Pin Data Value Into Accumulator
ANL A, #0x0F ; Store Low Nibble of Accumulator in Accumulator (Pin P1.0 to P1.3 Data Input)
MOV R3, A ; Store Value of Accumulator in Register #3
MOV A, P1 ; Load P1 Pin Data Value Into Accumulator
ANL A, #0xF0 ; Store High Nibble of Accumulator in Accumulator (Pin P1.4 to P1.7 Data Input)
SWAP A ; Swap Nibbles of Accumulator in Order to Appropriate Store in Register
MOV R4, A ; Store Value of Accumulator in Register #4
RET ; Return From Subroutine Label Call
;-----------------------------------------------------------------
;---------------------** SERIAL OPERATIONS **---------------------
;-----------------------------------------------------------------
; ** Setup UART Subroutine **
SETUPUART:
MOV SCON, #50H ; Put Serial Controller in Mode 1 and Ren Enable
MOV A, PCON ; Load PCON Value into Accumulator
SETB ACC.7 ; Set 7th Bit of Accumulator to Set SMOD in PCON for Double Baud Rate
MOV PCON, A ; Store Accumulator Value in PCON
RET ; Return From Subroutine Label Call
; ** Transmit Data Preparation for UART Subroutine **
TRANSMITDATA:
MOV SBUF, A ; Load ACC Data into SBUF for Transmission
HERE:
JNB TI, HERE ; Loop to Here Until TI Become Set Meaning Ready for Transmitting
CLR TI ; Clear Transmit Interrupt Flag After Sending Data
RET ; Return From Subroutine Label Call
;--------------------------------------------------------------
;---------------------** LED OPERATIONS **---------------------
;--------------------------------------------------------------
; ** Turn On LED Subroutine **
TURNONLED:
SETB P2.0 ; Write High Signal on P2.0 in Order to Turn on Connected LED
RET ; Return From Subroutine Label Call
; ** Turn Off LED Subroutine **
TURNOFFLED:
CLR P2.0 ; Write Low Signal on P2.0 in Order to Turn off Connected LED
RET ; Return From Subroutine Label Call
;----------------------------------------------------------------
;---------------------** TIMER OPERATIONS **---------------------
;----------------------------------------------------------------
; ** Setup Timer/Counter 0 Subroutine **
SETUPTIMERT0:
ORL TMOD, #01 ; Put Timer/Counter 0 in 16-Bit Timer Mode by 00000001B
MOV TH0, #0xEC ; Load Value EC to to High Byte (TH) for Reload Action
MOV TL0, #78 ; Load Value EC to to Low Byte (TL) for Reload Action
RET ; Return From Subroutine Label Call
; ** Setup Timer/Counter 1 Subroutine **
SETUPTIMERT1:
ORL TMOD, #20H ; Put Timer/Counter 1 in Auto Reload Timer Mode by 00010000B
MOV TH1, R7 ; Load Register #7 Value microseconds Delay on TH for Reload Action
RET ; Return From Subroutine Label Call
; ** Reset Timer/Counter 0 Subroutine **
RESETTC0:
CLR TR0 ; Stop Timer/Counter 0 After Receiving Interrupt Flag
CLR TF0 ; Clear Timer/Counter 0 Overflow Flag After Interrupt Flag
RET ; Return From Subroutine Label Call
; ** Reset Timer/Counter 1 Subroutine **
RESETTC1:
CLR TR1 ; Stop Timer/Counter 1 After Receiving Interrupt Flag
CLR TF1 ; Clear Timer/Counter 1 Overflow Flag After Interrupt Flag
RET ; Return From Subroutine Label Call
; ** Start Timer/Counter 0 Subroutine **
STARTTIMER0:
SETB TR0 ; Starting Timer/Counter 0 Only by Setting TR0
RET ; Return From Subroutine Label Call
; ** Start Timer/Counter 1 Subroutine **
STARTTIMER1:
SETB TR1 ; Starting Timer/Counter 1 Only by Setting TR1
RET ; Return From Subroutine Label Call
; ** Start All Timer/Counters Subroutine **
STARTTIMERSATONCE:
ORL TCON, #01010000B ; Start Both Timers at Once
RET ; Return From Subroutine Label Call
;--------------------------------------------------------------------------
;---------------------** RAM & REGISTERS OPERATIONS **---------------------
;--------------------------------------------------------------------------
; ** Reset Program Values **
RESETREGISTERS:
MOV R1, #00H ; Reset Register #1 Which Holds First Sensor Data Input
MOV R2, #00H ; Reset Register #2 Which Holds Second Sensor Data Input
MOV R3, #00H ; Reset Register #3 Which Holds Third Sensor Data Input
MOV R4, #00H ; Reset Register #4 Which Holds Threshold Data Input
MOV R6, #00 ; Reset Temporary Register for Starting Over
RET ; Return From Subroutine Label Call
PREINITREGISTERS:
MOV R7, #-3 ; Set Register #7 Which Holds Timer/Counter 1 Data Input
RET ; Return From Subroutine Label Call
;----------------------------------------------------------------
;---------------------** FINISH OPERATION **---------------------
;----------------------------------------------------------------
; ** Finish Subroutine **
FINISH:
JMP $ ; Do Nothing Just Operate an Infinite Loop
ORG 400H
TABLE:
DB "0" ; Equal to Enable 0
DB "1" ; Equal to Enable 1
DB "2" ; Equal to Enable 2
DB "3" ; Equal to Enable 3
DB "4" ; Equal to Enable 4
DB "5" ; Equal to Enable 5
DB "6" ; Equal to Enable 6
DB "7" ; Equal to Enable 7
DB "8" ; Equal to Enable 8
DB "9" ; Equal to Enable 9
DB "A" ; Equal to Enable A
DB "B" ; Equal to Enable B
DB "C" ; Equal to Enable C
DB "D" ; Equal to Enable D
DB "E" ; Equal to Enable E
DB "F" ; Equal to Enable F
DB "-" ; Equal to Enable -
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment