51汇编 等周期脉宽调制pwm 子程序
扫描二维码
随时随地手机看文章
;等周期脉宽调制PWM
;晶振:12MHz
;89C51
P_PWM1 EQU P1.0
cycle EQU 1000 ;PWM的周期(单位微秒),则频率为1000赫兹
cycle_high EQU 500 ;一个周期内,PWM端口为高电平的时间(初始值)
DSEG AT 20H
ORG 40H
PWM_HIGH_L DATA 40H
PWM_HIGH_H DATA 41H ;PWM端口为高电平时,定时器寄存器的值
PWM_LOW_L DATA 42H
PWM_LOW_H DATA 43H ;PWM端口为高电平时,定时器寄存器的值
ORG 60H ; stack origin
stack: DS 20H ; stack depth
CSEG
ORG 0000H ; power on/reset vector
jmp on_reset
ORG 0003H ; external interrupt 0 vector
reti ; undefined
ORG 000BH ; timer 0 overflow vector
reti ; undefined
ORG 0013H ; external interrupt 1 vector
reti ; undefined
ORG 001BH ; timer 1 overflow vector
JMP TIMER1_ISR
ORG 0023H ; serial I/O interrupt vector
reti ; undefined
;***********************************************************************************
;系统初始化等
ORG 0033H ; begin code sPACe
USING 0 ; register bank zero
on_reset:
MOV R7,#00H
DJNZ R7,$ ;延时
mov sp, #(stack-1) ; initialize stack pointer
;禁止各中断
MOV IE,#00H
MOV IP,#00H
SYS_INIT:
MOV R0,#00H
SYS_INIT_LOOP:
MOV R1,#00H
DJNZ R1,$
DJNZ R0,SYS_INIT_LOOP ;延时0.13S
;内部RAM(30H-5fH)清零
MOV R1,#30H
MOV A,#0
ZERO_LOOP: MOV @R1,A
INC R1
CJNE R1,#60H,ZERO_LOOP
;定时器1初始化
MOV TMOD,#00010000B ;方式1,16位定时器模式
MOV PWM_HIGH_L,#LOW(65536 - cycle_high)
MOV PWM_HIGH_H,#HIGH(65536 - cycle_high)
MOV PWM_LOW_L,#LOW(65536 - (cycle - cycle_high))
MOV PWM_LOW_H,#HIGH(65536 - (cycle - cycle_high))
SETB P_PWM1 ;先输出高
MOV TL1,PWM_HIGH_L
MOV TH1,PWM_HIGH_H
SETB ET1 ;T1开中断
SETB EA ;开总中断
SETB TR1 ;开定时器1
MAIN_LOOP:
;主循环
SJMP $
;********************