Hardware Reference
In-Depth Information
Since the brightness of the light is proportional to the average current that flows through
the bulb, we can dim the light bulb by reducing the duty cycle of the PWM output from 100
percent down to 10 percent in 5 s. We will reduce the duty cycle in steps. In 1 s, we reduce the
duty value 10 times. Therefore, we reduce the duty value by 1 every 100 ms in the first second
and reduce the duty value by 2 every 100 ms in the following 4 s.
The assembly program that implements this idea is as follows:
#include “c:\miniide\hcs12.inc”
org $1000
dim_cnt rmb 1
org $1500
movb #0,PWMCLK ; select clock A as the clock source
movb #1,PWMPOL ; make waveform to start with high level
movb #$0C,PWMCTL ; select 8-bit mode
movb #2,PWMPRCLK ; set clock prescaler to 4
movb #0,PWMCAE ; select left-aligned mode
movb #100,PWMPER0 ; set period value (PWM frequency doesn't matter)
movb #100,PWMDTY0 ; set duty value
bset PWME,PWME0 ; enable PWM channel 0
; The following instruction segment reduces duty count by 1 per 100 ms
movb
#10,dim_cnt
loop1
ldy
#1
jsr
delayby100ms
dec
PWMDTY0
; decrement duty by 1
dec dim_cnt
bne loop1
; The following instruction segment reduces duty count by 2 per 100 ms in 4 s
movb
#40,dim_cnt
loop2
ldy
#1
; wait for 100 ms
jsr
delayby100ms
;
dec
PWMDTY0
; decrement duty cycle by 2%
dec
PWMDTY0
; per 100 ms
dec
dim_cnt
bne
loop2
swi
#include
“c:\miniide\delay.asm” ; include delayby100ms here
end
The C language version of this program is as follows:
#include
“c:\cwHCS12\include\hcs12.h”
#include
“c:\cwHCS12\include\delay.h”
void main ()
{
int dim_cnt;
PWMCLK 5 0;
// select clock A as the clock source
PWMPOL 5 1;
// make waveform to start with high level
PWMCTL 5 0x0C;
// select 8-bit mode
PWMPRCLK 5 2;
// set clock prescaler to 4
PWMCAE 5 0;
// select left aligned mode
PWMPER0 5 100;
// set period of PWM0 to 0.1 ms
PWMDTY0 5 100;
// set duty cycle to 100%
Search WWH ::




Custom Search