⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prog29c.asm

📁 包括了各种常用的8051单片机的程序
💻 ASM
字号:
;  PROG29B - Working through the Emulator  
;
;  Start with the Serial Interface.
;
;  PROG29C - Start Doing the Data Interface
;
;  PROG29B - Start with the Command Line Interface.  Upon Return, R0 Points to the
;   First Parameter and R1 Points to the Second.  If there is a Third, Error
;
;   Input String Buffer is 10 Long Starting at 080h.
;
;   Commands are:
;    H        - Help, Display this List
;    R        - Reset
;    D        - Download a New Application
;    E [Addr] - Start Running at the Optional Address
;    S [Addr] - Single Step from the Optional Address
;             - If just a Carriage Return Received then Single Step 
;    U [Addr] - Unassemble at the PC or Optional Address
;    E Addr   - Modify the Control Store at the Address
;    G Addr   - Read from the Scratchpad RAM/SFR Address
;    W Addr   - Write to the Scratchpad RAM/SFR Address
;    R        - Display the Scratchpad RAM Registers
;    B Addr   - Toggle Set Breakpoint at the Specified Address
;    L        - List the Breakpoints
;  
;  PROG29A - Develop Serial Interface - Bounce Back Characters from the Host
;   Terminal.  
;
;  The Serial Interface consists of a Command and a Parameter Followed by a
;   Carriage Return, We Have to Interpret this as well as:
;
;   1.  Ignore Following "Line Feed" - Wait 10 msecs after CR.
;   
;   2.  Handle BackSpacing
;
;  Myke Predko
;  98.06.25
;
;  Hardware Notes:
;  DS87C520 is used as the Microcontroller
;   - Oscillator Speed is 20 MHz
;  P2.7 is Console Serial Output
;  P2.6 is Console Serial Input

;  Constant Declarations
define SerOut=P2.7              ;  Serial Output Bit
define SerIn=P2.6               ;  Serial Input Bit
;define SerOut=P1.0              ;  Serial Output Bit
;define SerIn=P1.6               ;  Serial Input Bit

CR     EQU 00Dh                 ;  ASCII Carriage Return
LF     EQU 00Ah                 ;  ASCII Line Feed
BS     EQU 008h                 ;  ASCII BackSpace

;  Variable Declarations
Buffer EQU 080h                 ;  Input/Breakpoint Buffer Area
                                ;   - 80 Characters Long


;  Macros


;  Mainline
 org 0                          ;  Execution Starts Here
MainLine:                       ;  Mainline of the Program

  acall   DisplayHelp1          ;  As the First Thing, Display Help
  acall   DisplayHelp2
  acall   DisplayHelp3

Loop:

  acall   Prompt                ;  Set the Prompt

  acall   GetCommand            ;  Wait for the Command to Come in

;  #### - Display Data at R0 and R1

  ajmp    Loop  


;  Subroutines
Prompt:                         ;  Display a Prompt for the Program

;  #### - Put more Information Here

  mov     A,#'>'
  acall   SendChar

  mov     A,#' '
  acall   SendChar

  ret


GetCommand:                     ;  Get the Command from the User

  mov     R2,#0                 ;  R3 Counts the Number of Characters Entered
  mov     R3,#' '               ;  R4 Has the Last Character Entered
                                ;   Use "Blank" incase a Character Entered
  mov     R4,#0FFh              ;  R5 Has the Position of the First Parameter
  mov     R5,#0FFh              ;  R6 Has the Position of the Second Parameter

GetCommand_Loop:                ;  Loop Around Here

  acall   GetChar               ;  Get Character/Wait 10 msecs

  jc      GetCommand_Loop       ;  If Carry Set, Nothing to Check

  cjne    A,#CR,GetCommand_Test ;  Look for a Carriage Return

  acall   GetChar               ;  Wait for/Skip the LF

  mov     0,R4                  ;  Pass Back the Parameters
  mov     1,R5

  ret

GetCommand_Test:                ;  Now, Process the Character

  mov     R1,A                  ;  Save the Character

  clr     C                     ;  Do we Have a Special Character?
  subb    A,#' '                ;  Is there anything less than ' '?

  jnc     GetCommand_Char       ;  No, Process the Character

  cjne    R1,#BS,GetCommand_Loop;  If it's not BackSpace, Display the Character
  
  cjne    R2,#0,GetCommand_BS   ;  If No Characters, Then Skip
  ajmp    GetCommand_Loop
GetCommand_BS:

  mov     A,R1                  ;  Send the BackSpace Character
  acall   SendChar

  dec     R2                    ;  Decrement the Displayed Characters

  mov     A,R2                  ;  Now, Do We Have to Clear the Parameter Pos'ns?
  xrl     A,R5                  ;  Check the Second Parameter
  jnz     GetCommand_BSFirst

  mov     R5,#0FFh              ;  Reset the Second Parameter

GetCommand_BSFirst:             ;  Check the position against the First Parameter

  mov     A,R2
  xrl     A,R4
  jnz     GetCommand_BSNew      

  mov     R4,#0FFh

GetCommand_BSNew:               ;  Now, Finish off with Resetting the Last Character

  mov     R3,#' '               ;  Get the Last Character
  
  mov     A,R2
  jz      GetCommand_Loop       ;  If At the Start of the Row, Return

  add     A,#Buffer             ;  Get the Actual Position from the Buffer
  dec     A                     ;   Of the Previous Character

  mov     R0,A                  ;  Get the Character and Save it in R3
  mov     A,@R0
  mov     R3,A

  ajmp    GetCommand_Loop

GetCommand_Char:                ;  Process the Character

  cjne    R2,#10,GetCommand_OK  ;  If 10 Characters In, 
  ajmp    GetCommand_Loop
GetCommand_OK:

  cjne    R3,#' ',GetCommand_Do ;  If Previous == ' ', Save the Parameter?
  mov     A,R1
  xrl     A,#' '
  jz      GetCommand_Do         ;  If Current is Also ' ', Don't Save Parameter

  cjne    R4,#0FFh,GetCommand_2nd

  mov     4,R2                  ;  Save the Position for the First Parameter

  ajmp    GetCommand_Do

GetCommand_2nd:                 ;  Save the Parameter as 2nd?

  cjne    R5,#0FFh,GetCommand_Do

  mov     5,R2                  ;   Yes

GetCommand_Do:                  ;  Save the Character and Wait for Next

  mov     A,R2                  ;  Save the Character
  add     A,#Buffer
  mov     R0,A
  mov     A,R1
  mov     @R0,A

  mov     A,R1
  mov     R3,A                  ;  Save the Character as Previous
  acall   SendChar              ;  Send the Character to the Display

  inc     R2                    ;  Point to the Next Position

  ajmp    GetCommand_Loop


DisplayHelp1:                   ;  Display the "Help" Menu

  mov     R0,#(HelpTable1 - GetHelp1)

DisplayHelp_Loop1:              ;  Loop Here to Display Every Character
  mov     A,R0                  ;   Get the Next Character to Display
  movc A,@A+PC
GetHelp1:

  jz      DisplayHelp_End1      ;  '\0' is the End Character

  acall   SendChar              ;  Send the Character to the Console

  inc     R0                    ;  Point to the Next Character to Display

  ajmp    DisplayHelp_Loop1     ;  Loop Around to Display the Next Character
  
DisplayHelp_End1:

  ret

HelpTable1:                     ;  Messages to be Displayed
  db      CR,LF                 ;  Start a New Line
  db      'H        - Help, Display this List',CR,LF
  db      'R        - Reset',CR,LF
  db      'D        - Download a New Application',CR,LF
  db      'E [Addr] - Execute at the Optional Address',CR,LF
  db      'S [Addr] - Single Step from the Optional Address',CR,LF,0
DisplayHelp2:                   ;  Display the "Help" Menu

  mov     R0,#(HelpTable2 - GetHelp2)

DisplayHelp_Loop2:              ;  Loop Here to Display Every Character
  mov     A,R0                  ;   Get the Next Character to Display
  movc A,@A+PC
GetHelp2:

  jz      DisplayHelp_End2      ;  '\0' is the End Character

  acall   SendChar              ;  Send the Character to the Console

  inc     R0                    ;  Point to the Next Character to Display

  ajmp    DisplayHelp_Loop2     ;  Loop Around to Display the Next Character
  
DisplayHelp_End2:

  ret

HelpTable2:                     ;  Messages to be Displayed
  db      '         - If just a Carriage Return Received then Single Step',CR,LF
  db      'U [Addr] - Unassemble at the PC or Optional Address',CR,LF
  db      'E Addr   - Modify the Control Store at the Address',CR,LF
  db      'G Addr   - Read from the Scratchpad RAM/SFR Address',CR,LF,0
DisplayHelp3:

  mov     R0,#(HelpTable2 - GetHelp2)

DisplayHelp_Loop3:              ;  Loop Here to Display Every Character
  mov     A,R0                  ;   Get the Next Character to Display
  movc A,@A+PC
GetHelp3:

  jz      DisplayHelp_End3      ;  '\0' is the End Character

  acall   SendChar              ;  Send the Character to the Console

  inc     R0                    ;  Point to the Next Character to Display

  ajmp    DisplayHelp_Loop3     ;  Loop Around to Display the Next Character
  
DisplayHelp_End3:

  ret

HelpTable3:                     ;  Messages to be Displayed
  db      'W Addr   - Write to the Scratchpad RAM/SFR Address',CR,LF
  db      'R        - Display the Scratchpad RAM Registers',CR,LF
  db      'B Addr   - Toggle Set Breakpoint at the Specified Address',CR,LF
  db      'L        - List the Breakpoints',CR,LF,0


GetChar:                        ;  Wait for a Character and Return it in Accumulator

  mov  R7,#28                   ;  Delay 10 msecs for Test
  mov  R6,#0
GCLoop1:			;  Wait 10 msecs for a Start Bit
  jnb  SerIn,GCSkip
  djnz R6,GCLoop1
  djnz R7,GCLoop1

  setb C                        ;  Nothing Received, Set Carry to Indicate

  ret

GCSkip:                         ;  Have Something to Receive

  mov  R6,#9	                ;  Want to Read 9 Bits

  mov  R7,#70		        ;  Delay Less than 1/2 Bit to Make Up for Calls, Etc.
GCLoop2:
  djnz R7,GCLoop2

  jb   SerIn,GCLoop1	        ;  If "Glitch", keep Waiting

GCLoop:			        ;  Now, Read 9 Bits

  mov  C,SerIn	                ;  Read the Input Bit
  rrc  A

  mov  R7,#170  	        ;  Delay Loop for 104 usecs per bit
GCLoop3:
  djnz R7,GCLoop3
  nop
  nop

  djnz R6,GCLoop

  clr  C                        ;  Make Sure Carry is Clear

  ret


SendChar:                       ;  Send the Serial Character
  mov	 R6,#10		        ;  We're Actually Sending 10 Bits

  clr	 C	       	        ;  First Bit is a "0"

SCLoop:			        ;  Loop Here for Each Bit

  mov	 SerOut,C               ;  Output the Bit in the Carry Flag

  mov	 R7,#170	        ;  Delay to 104 usecs (520 Cycles) per loop
SCLoop1:
  djnz	 R7,SCLoop1
  nop

  setb	 C		        ;  Shift the Next Bit to Carry
  rrc 	 A		        ;   And Load in a "1" for Stop Bit

  djnz	 R6,SCLoop

  ret

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -