原创 转21ic)51汇编等周期脉宽调制pwm子程序

2007-3-8 20:54 2584 7 7 分类: MCU/ 嵌入式
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        $


;**********************************************************************
;**********************************************************************
;定时器1中断服务程序
TIMER1_ISR:
            USING       1                       ; register bank 1
            PUSH        ACC
            PUSH        PSW


            JB          P_PWM1,PWM_OUT_LOW      ;如果输出为高则,输出低
            SETB        P_PWM1
            MOV         TL1,PWM_HIGH_L
            MOV         TH1,PWM_HIGH_H
            JMP         TIMER_ISR_RET


PWM_OUT_LOW:
            CLR         P_PWM1
            MOV         TL1,PWM_LOW_L
            MOV         TH1,PWM_LOW_H


TIMER_ISR_RET:
            POP         PSW
            POP         ACC
            RETI
;*********************************************************************
;
            END


PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条