intel
MOV CLK_PCKENR1,#80H ;开TIM1时钟,关闭其他外设时钟
MOV CLK_PCKENR2,#00H
;初始化TIM_CH1
BSET PB_DDR,#0 ;将PB0设为低速推挽输出
BSET PB_CR1,#0
BRES PB_CR2,#0
BRES PB_ODR,#0
;初始化TIM1
MOV TIM1_PSCRH,#00H ;不分频
MOV TIM1_PSCRL,#00H
MOV TIM1_ARRH,#03H
MOV TIM1_ARRL,#0FFH ;自动重装1023
MOV TIM1_CNTRH,#00H
MOV TIM1_CNTRL,#00H ;计数器初始值为0
MOV TIM1_CCER1,#04H ;使能TIM1_CH1N,注意是互补输出
MOV TIM1_CCMR1,#68H ;PWM模式1,TIM1_CCR1预装载,输出
MOV TIM1_BKR , #80H ;主输出使能
MOV TIM1_EGR , #01H ;置UG,产生更新事件初始化所有寄存器
MOV TIM1_CR1,#81H ;预装载缓冲,向上计数,开启定时
infinite_loop.l
LD A,#0
PWMUP:
INC A ;A从0加到最大值0ffh
CP A,#0FFH
JRNE PWM1
LD A,#0FFH ;A从最大值减到0
PWMDOWN:
DEC A
JRNE PWM2
jra infinite_loop
PWM1:
LD R00,A
MOV TIM1_CCR1H,#03H ;将A送到比较值的低位,从而改变占
MOV TIM1_CCR1L,R00 ;空比,从而达到调节亮度的目的
CALL DELAY
JP PWMUP
PWM2:
LD R01,A
MOV TIM1_CCR1H,#03H
MOV TIM1_CCR1L,R01
CALL DELAY
JP PWMDOWN
DELAY:
MOV R03,#200 ; 延时子函数
D1:
MOV R04,#100
D2:
DEC R04
JRNE D2
DEC R03
JRNE D1
RET
motorola