测试PWM5输出端口P0.3,LED闪烁端口是P1.5 ;以下是汇编代码
;功能寄存器定义 SFRS EQU 91H
TA EQU 0C7H
PWMCON0 EQU 0D8H
PWMCON1 EQU 0DFH
CKCON EQU 08EH
PWMPL EQU 0D9H
PWMPH EQU 0D1H
PWM0L EQU 0DAH
PWM0H EQU 0D2H
PWM1L EQU 0DBH
PWM1H EQU 0D3H
PWM2L EQU 0DCH
PWM2H EQU 0D4H
PWM3L EQU 0DDH
PWM3H EQU 0D5H
PWM4L EQU 0CCH;Page1
PWM4H EQU 0C4H;Page1
PWM5L EQU 0CDH;Page1
PWM5H EQU 0C5H;Page1
PIOCON0 EQU 0DEH
PIOCON1 EQU 0C6H
PDTEN EQU 0F9H
PDTCNT EQU 0FAH
PMEN EQU 0FBH
PMD EQU 0FCH
PBD EQU 0D7H
PNP EQU 0D6H
PWMINTC EQU 0B7H
P0M1 EQU 0B1H
P0M2 EQU 0B2H
P1M1 EQU 0B3H
P1M2 EQU 0B4H
P2S EQU 0B5H
P3M1 EQU 0ACH
P3M2 EQU 0ADH
CKDIV EQU 095H;系统时钟除频
;功能位定义
CLRPWM BIT PWMCON0.4
PWMF BIT PWMCON0.5
LOAD BIT PWMCON0.6
PWMRUN BIT PWMCON0.7
;程序入口
ORG 0000H
LJMP MAIN
ORG 0100H
MAIN:
ANL P1M1,#11011111B
ORL P1M2,#00100000B;P1.5推挽
ANL P0M1,#11110111B
ORL P0M2,#00001000B;P0.3推挽
MOV PIOCON0,#00100000B;设置P0.3用作PWM5输出
ORL PWMCON1,#00000011B;PWM时钟为系统时钟16M的1/8=2Mhz
CLR CLRPWM
ANL PWMCON1,#00111111B;群组模式禁止
MOV PWMPH,#07H
MOV PWMPL,#0CFH
;PWM frequency = Fpwm/((PWMPH,PWMPL) + 1) <Fpwm = Fsys/PWM_CLOCK_DIV>
; = (16MHz/8)/(0x7CF + 1)
; = 1KHz (1ms)
;载入PWM5的占空比值
MOV TA,#0AAH
MOV TA,#55H
ORL SFRS,#00000001B ;Page1
MOV PWM5H,#02H
MOV PWM5L,#0CFH
MOV TA,#0AAH
MOV TA,#55H
ANL SFRS,#11111110B ;Page0
SETB LOAD ;载入占空比相关数值
SETB PWMRUN ;运行PWM
;LJMP $
LED:
SETB P1.5
LCALL DL500MS
CLR P1.5
LCALL DL500MS
LJMP LED
DL500MS: ;误差 0us
MOV R7,#0F1H
DL1:
MOV R6,#48H
DL0:
MOV R5,#0E5H
DJNZ R5,$
DJNZ R6,DL0
DJNZ R7,DL1
NOP
NOP
RET
END
|