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

📄 prog42.asm

📁 包括了各种常用的8051单片机的程序
💻 ASM
字号:
;  PROG42 - Add a PWM Output to PROG41
;
;  This Application Uses the 75.76 KHz Interrupt Signal for a Motor PWM
;   Output.
;
;  For PROG42, the LED will be used instead of a Motor PWM Control.
;
;  Timer 1 Interrupt Rate: 75.76 KHz (for a 18.94 KHz PWM Frequency) 
;
;  Serial Interface Speed: 2400 bps (Actually) 
;
;  Myke Predko
;  98.04.20
;
;  Hardware Notes:
;  AT89C2051 is used as the Microcontroller
;   - Oscillator Speed is 20 MHz
;  Serial Output is on P3.1
;  Serial Input is on P3.0
;  LED PWM Signal is on P1.5

;  Constant Declarations
TimerReload EQU 0EAh            ;  Value to Reload the Timer With

;  Variable Declarations
define OnCount=R3               ;  Use Registers for PWM Variables
define OffCount=R4

OffValue    EQU 021h
LastChar    EQU 028h            ;  Last Character Sent to The Program
Dlay1       EQU LastChar+1      ;  24 Bit Value to Delay 1 Second Between TX's
Dlay2       EQU LastChar+2
Dlay3       EQU LastChar+3

OnValue     EQU LastChar+4      ;  Reload Value for the PWM "On"


;  Macros


;  Mainline
 org 0                          ;  Execution Starts Here

  ajmp    MainLine              ;  Jump Over to the Mainline

 org 01Bh                       ;  Timer 1 Interrupt Vector

Tmr1Int:                        

  cjne    OffCount,#0,PWMOff

PWMOn:                          ;  The PWM is On

  setb    P1.5                  ;  Output a High Value

  djnz    OnCount,IntEnd        ;  If OnCount != 0, Then Loop Around

  mov     OnCount,OnValue
  mov     OffCount,OffValue

IntEnd:

  reti

PWMOff:                         ;  Turn off the PWM

  clr     P1.5                  ;  Output a Low Value

  jb      10,IntEnd             ;  Is the Value Always Low?

  dec     OffCount              ;  No, Decrement the Counter

  reti
 

Mainline:                       ;  Mainline Code, Setup Timer1 and Transmit
                                ;   "A" Once Every Second or Last Character
                                ;   Received.

  mov     SCON,#%01010000       ;  Run Serial Port in 8 Bit Mode
  mov     TMOD,#%00100001       ;  Run the Timers, Tmr1 in 8 Bit Reload Mode
  mov     TH1,#TimerReload      ;  Enable the Timer
  mov     TL1,#TimerReload
  orl     TCON,#040h            ;  Enable Tmr1 to Run
  anl     PCON,#07Fh            ;  Make Sure SMOD is Reset

  mov     LastChar,#'A'         ;  Setup the Last Character to Transmit

  mov     OffValue,#4           ;  Start with the PWM Off All the Time
  mov     OffCount,#4
  mov     OnValue,#0
  mov     OnCount,#0

  mov     IE,#%10001000         ;  Enable the Timer Overflow Interrupt


Loop:                           ;  Loop Here to Transmit Each Character

  acall   Dlay                  ;  Wait one Second

  dec     OffValue              ;  Change the LED Values
  inc     OnValue

  jnb     15,SendChar           ;  If Value Hasn't Rolled Over, Send it  

  mov     OffValue,#4
  mov     OnValue,#0

SendChar:                       ;  Read/Output Last Char Sent to the AT89C2051

  mov     OffCount,OffValue     ;  Make Sure the PWM Counters are Set Correctly
  mov     OnCount,OnValue

  clr     TI                    ;  Turn off the Transmit Completed Flag
  mov     A,LastChar
  mov     SBUF,A

  jnb     RI,Loop               ;  Was Anything Received?  

  clr     RI                    ;  Yes, Clear RI

  mov     A,SBUF                ;  Save the Character Received
  mov     LastChar,A
  
  ajmp    Loop                  ;  Wait to Transmit the Next Character


Dlay:                           ;  Delay 1 Second

  mov     Dlay1,#0              ;  Initialize Delay Values
  mov     Dlay2,#0
  mov     Dlay3,#00Ch

DlayLoop:                       ;  Loop Here for 1 Second
  djnz    Dlay1,DlayLoop
  djnz    Dlay2,DlayLoop
  djnz    Dlay3,DlayLoop

  ret                           ;  Return to the Caller

⌨️ 快捷键说明

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