📄 pwm16_2.asm
字号:
;------------------------------------------------------------------------------
; FILENAME: PWM16_2.asm
; VERSION: Rev B, 2002 Mar 30
;------------------------------------------------------------------------------
; DESCRIPTION:
; PWM16_2 PWM16 User Module API.
;
;------------------------------------------------------------------------------
; Copyright (c) Cypress MicroSystems 2000-2002. All Rights Reserved.
;------------------------------------------------------------------------------
;-----------------------------------------------
; include instance specific register definitions
;-----------------------------------------------
include "m8c.inc"
include "PWM16_2.inc"
area text (ROM, REL)
;-------------------------------------------------------------------
; Declare the functions global for both assembler and C compiler.
;
; Note that there are two names for each API. First name is
; assembler reference. Name with underscore is name refence for
; C compiler. Calling function in C source code does not require
; the underscore.
;-------------------------------------------------------------------
export PWM16_2_EnableInt
export _PWM16_2_EnableInt
export PWM16_2_DisableInt
export _PWM16_2_DisableInt
export PWM16_2_Start
export _PWM16_2_Start
export PWM16_2_Stop
export _PWM16_2_Stop
export PWM16_2_WritePeriod
export _PWM16_2_WritePeriod
export PWM16_2_WritePulseWidth
export _PWM16_2_WritePulseWidth
export wPWM16_2_ReadPulseWidth
export _wPWM16_2_ReadPulseWidth
export wPWM16_2_ReadCounter
export _wPWM16_2_ReadCounter
;-----------
; EQUATES
;-----------
bfCONTROL_REG_START_BIT: equ 1 ; Control register start bit
bfINPUT_REG_CLOCK_MASK: equ 0Fh ; input register clock mask
bfINPUT_REG_NULL: equ 00h ; clear the input register
bfINPUT_REG_PREV_MASK: equ 30h ; input register input-previous
;-----------------------------------------------------------------------------
; FUNCTION NAME: PWM16_2_EnableInt
;
; DESCRIPTION:
; Enables this PWM's interrupt by setting the interrupt enable mask bit
; associated with this User Module. Remember to call the global interrupt
; enable function by using the macro: M8C_EnableGInt.
;
; ARGUMENTS:
; none.
;
; RETURNS:
; none.
;
; SIDE EFFECTS:
; none.
;
; THEORY of OPERATION:
; Sets the specific user module interrupt enable mask bit.
;
;-----------------------------------------------------------------------------
PWM16_2_EnableInt:
_PWM16_2_EnableInt:
M8C_EnableIntMask PWM16_2_INT_REG, bPWM16_2_INT_MASK
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: PWM16_2_DisableInt
;
; DESCRIPTION:
; Disables this PWM's interrupt by clearing the interrupt enable mask bit
; associated with this User Module.
;
; ARGUMENTS:
; none.
;
; RETURNS:
; none.
;
; SIDE EFFECTS:
; none.
;
; THEORY of OPERATION:
; Clears the specific user module interrupt enable mask bit.
;
;-----------------------------------------------------------------------------
PWM16_2_DisableInt:
_PWM16_2_DisableInt:
M8C_DisableIntMask PWM16_2_INT_REG, bPWM16_2_INT_MASK
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: PWM16_2_Start
;
; DESCRIPTION:
; Sets the start bit in the Control register of this user module. The
; PWM will begin counting on the next input clock as soon as the
; enable input is asserted high.
;
; ARGUMENTS:
; none.
;
; RETURNS:
; none.
;
; SIDE EFFECTS:
; none.
;
; THEORY of OPERATION:
; Set the start bit in the Control register of the LSB block.
;
;-----------------------------------------------------------------------------
PWM16_2_Start:
_PWM16_2_Start:
or REG[PWM16_2_CONTROL_LSB_REG], bfCONTROL_REG_START_BIT
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: PWM16_2_Stop
;
; DESCRIPTION:
; Disables PWM operation.
;
; ARGUMENTS:
; none.
;
; RETURNS:
; none.
;
; SIDE EFFECTS:
; After this function completes, the Counter register will latch any data
; written to the Period register. Writing to the Period register is
; performed using the PWM16_2_WritePeriod function.
;
; THEORY of OPERATION:
; Clear the start bit in the Control register of the LSB block.
;
;-----------------------------------------------------------------------------
PWM16_2_Stop:
_PWM16_2_Stop:
and REG[PWM16_2_CONTROL_LSB_REG], ~bfCONTROL_REG_START_BIT
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: PWM16_2_WritePeriod
;
; DESCRIPTION:
; Write the period value into the Period register.
;
; ARGUMENTS:
; WORD wPeriodValue - period count -
; MSB passed in X register
; LSB passed in A register
;
; RETURNS:
; none.
;
; SIDE EFFECTS:
; If the PWM user module is stopped, then this value will also be
; latched into the Counter registers.
;
; THEORY of OPERATION:
; Write data into the Period registers.
;
;-----------------------------------------------------------------------------
PWM16_2_WritePeriod:
_PWM16_2_WritePeriod:
tst REG[PWM16_2_CONTROL_LSB_REG], bfCONTROL_REG_START_BIT
jnz .CounterRunning
; Counter is stopped. Due to chip errata, we have to set the clock low for
; the write to the period register to cause the data to be immediately transferred
; into the Counter.
.CounterStopped:
push X
mov X, A ; save the period argument
M8C_SetBank1
mov A, REG[PWM16_2_INPUT_LSB_REG] ; save the context of the clock - input register
push A
and REG[PWM16_2_INPUT_LSB_REG], F0h ; set the clock signal low
and REG[PWM16_2_INPUT_MSB_REG], F0h
M8C_SetBank0
mov A, X
mov REG[PWM16_2_PERIOD_LSB_REG], A ; set the period register with the new period
pop A
pop X
push A
mov A, X
mov REG[PWM16_2_PERIOD_MSB_REG], A
pop A
M8C_SetBank1
mov REG[PWM16_2_INPUT_LSB_REG], A ; restore the clock
and A, 0Fh ; High nibble is always 0x3 for higher order bits
or A, 30h
mov REG[PWM16_2_INPUT_MSB_REG], A
M8C_SetBank0
ret
; Counter is running - write the period into the period register.
; Upon Terminal Count this value will get loaded into the counter.
.CounterRunning:
mov REG[PWM16_2_PERIOD_LSB_REG], A
mov A, X
mov REG[PWM16_2_PERIOD_MSB_REG], A
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: PWM16_2_WritePulseWidth
;
; DESCRIPTION:
; Writes compare value into the PulseWidth register.
;
; ARGUMENTS:
; WORD wPulseWidth - compare value count -
; MSB passed in X register
; LSB passed in A register
;
; RETURNS:
; none.
;
; SIDE EFFECTS:
; none.
;
; THEORY of OPERATION:
; Write data into the PulseWidth registers.
;
;-----------------------------------------------------------------------------
PWM16_2_WritePulseWidth:
_PWM16_2_WritePulseWidth:
mov REG[PWM16_2_PWDITH_LSB_REG], A
mov A, X
mov REG[PWM16_2_PWDITH_MSG_REG], A
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: wPWM16_2_ReadPulseWidth
;
; DESCRIPTION:
; Reads the PulseWidth registers.
;
; ARGUMENTS:
; none.
;
; RETURNS:
; WORD wPulseWidth - value read from PulseWidth register -
; LSB returned in A register
; MSB returned in X register
;
; SIDE EFFECTS:
; none.
;
; THEORY of OPERATION:
; Read the PulseWidth register and return value in A.
;
;-----------------------------------------------------------------------------
wPWM16_2_ReadPulseWidth:
_wPWM16_2_ReadPulseWidth:
mov A, REG[PWM16_2_PWDITH_MSG_REG]
mov X, A
mov A, REG[PWM16_2_PWDITH_LSB_REG]
ret
;-----------------------------------------------------------------------------
; FUNCTION NAME: wPWM16_2_ReadCounter
;
; DESCRIPTION:
; Reads the count in the Counter register.
;
; ARGUMENTS:
; none.
;
; RETURNS:
; WORD wCount - current count value in Count register.
; LSB - returned in A register
; MSB - returned in X register
;
; SIDE EFFECTS:
; Reading the Counter register may cause the Counter register to miss
; one or more counts due to the fact that the clock is stopped while
; the Counter register is read. The preferred method is to use the
; interrupt feature to determine when the counter has arrived at a
; specified value.
;
; THEORY of OPERATION:
; Reading the Counter register causes its value to be latched into the
; CompareValue register. Care must be taken to stop the clock and save
; the CompareValue register's contents before reading the counter.
;
;-----------------------------------------------------------------------------
wPWM16_2_ReadCounter:
_wPWM16_2_ReadCounter:
bOrigClockSetting: EQU 0
bOrigCompareValue: EQU 1
wCounter: EQU 2 ; must be at top of stack frame
STACK_FRAME_SIZE: EQU 4 ; stack frame is 4 bytes
; create a stack frame
mov X, SP
add SP, STACK_FRAME_SIZE
; save the input register clock setting
M8C_SetBank1
mov A, REG[PWM16_2_INPUT_LSB_REG]
mov [X+bOrigClockSetting], A
; disable the clock
mov REG[PWM16_2_INPUT_LSB_REG], bfINPUT_REG_NULL
mov REG[PWM16_2_INPUT_MSB_REG], bfINPUT_REG_PREV_MASK
M8C_SetBank0
;--------------------------
; Process the MSB byte
;--------------------------
; save the PulseWidth register value
mov A, REG[PWM16_2_PWDITH_MSG_REG]
mov [X+bOrigCompareValue], A
; Read the counter. This latches the counter data into
; the PulseWidth register. This may cause an interrupt.
mov A, REG[PWM16_2_COUNTER_MSB_REG]
; Read the PulseWidth register, which contains the counter value
mov A, REG[PWM16_2_PWDITH_MSG_REG]
mov [X+wCounter], A
; Restore the PulseWidth register
mov A, [X+bOrigCompareValue]
mov REG[PWM16_2_PWDITH_MSG_REG], A
;--------------------------
; Process the LSB byte
;--------------------------
; save the PulseWidth register value
mov A, REG[PWM16_2_PWDITH_LSB_REG]
mov [X+bOrigCompareValue], A
; Read the counter. This latches the counter data into
; the PulseWidth register. This may cause an interrupt.
mov A, REG[PWM16_2_COUNTER_LSB_REG]
; Read the PulseWidth register, which contains the counter value
mov A, REG[PWM16_2_PWDITH_LSB_REG]
mov [X+wCounter+1], A
; Restore the PulseWidth register
mov A, [X+bOrigCompareValue]
mov REG[PWM16_2_PWDITH_LSB_REG], A
; restore the input register clock setting
M8C_SetBank1
mov A, [X+bOrigClockSetting]
and A, bfINPUT_REG_CLOCK_MASK
or A, bfINPUT_REG_PREV_MASK
mov REG[PWM16_2_INPUT_MSB_REG], A
mov A, [X+bOrigClockSetting]
mov REG[PWM16_2_INPUT_LSB_REG], A
M8C_SetBank0
; Setup the return value in X and A
mov A, [X+wCounter] ; get the MSB
push A
mov A, [X+wCounter+1] ; LSB in A
pop X
; Delete the stack frame
ADD SP, -(STACK_FRAME_SIZE)
ret
; end of PWM16 API code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -