Using PWM Counter
ADwin
26
ADwin-Gold II
, Manual February 2013
7.4
Using PWM Counter
Reference clock generator
In this operating mode an internal reference clock generator clocks the counter with a
signal frequency of 100 MHz. The frequency and duty cycle can be read as well as high
time and low time.
Example
#Include
ADwinGoldII.inc
#Define
frequency
FPAR_1
#Define
dutycycle
FPAR_2
#Define
hightime
PAR_1
#Define
lowtime
PAR_2
Init:
…
Cnt_Enable
(
0
)
'stop all counters
Cnt_Clear
(
0001b
)
'clear counter 1
Rem set operation mode of counter 1:
Rem Bits 0..5: no importance
Rem Bit 6: detect rising edge as PWM signal
Rem Bit 7: input B/DIR as PWM input
Cnt_Mode
(
1
,
01000000b
)
Cnt_SE_Diff
(
1111b
)
'all inputs differential
Cnt_Enable
(
0001b
)
'start counter 1
…
Event:
…
Rem latch counter 1
Cnt_PW_Latch
(
0001b
)
Rem read frequency and duty cycle
Cnt_Get_PW
(
1
,
frequency
,
dutycycle
)
Rem read high time and low time
Cnt_Get_PW_HL
(
1
,
hightime
,
lowtime
)
Exception:
evaluate PWM registers on
your own
There are several registers assigned to each PWM counter being described below. If,
like in the example above, PWM counters are evaluated with standard instructions
Cnt_Get_PW
and
Cnt_Get_PW_HL
, no further knowledge is required about PWM reg-
isters. Use the evaluation with PWM registers for special solutions only.
In order to evaluate PWM signals, the counter values of the current and the 2 preceding
counter values are stored in latch registers, both for rising and falling edges. In addition,
there is a "shadow register" for each of these 6 registers.
The register values are changed with any edge like this:
–
Rising edge:
•
Copy counter value to L1+
•
If rising edge is set as reference edge:
Copy register L2+ to L3+
Copy register L1+ to L2+
Copy register L2– to L3–
Copy register L1– to L2–
–
Falling edge:
•
Copy counter value to L1–
•
If falling edge is set as reference edge:
Copy register L2– to L3–
Copy register L1– to L2–
Register
Latch
Shadow
register
Latch 1 for positive edges (current)
L1+
SL1+
Latch 2 for positive edges
L2+
SL2+
Latch 3 for positive edges
L3+
SL3+
Latch 1 for negative edges (current)
L1–
SL1–
Latch 2 for negative edges
L2–
SL2–
Latch 3 for negative edges
L3–
SL3–