abstract: this application note shows how to use timer type 2 on a maxq2000 microcontroller to generate pulse width modulation (pwm) waveforms. it also discusses issues that should be considered when using timer type 2 for pwm generation.
introductionpulse width modulation (pwm) is a technique in which the duty cycle of the signal conveys meaning. specifically, the amount of time the clock signal is logic-high vs. logic-low tells the signal's receiver to do something. pwm is often used to control fan speed and dc and servo motors. with fan-speed control, the longer the high time of the pulse, the faster the fan will run. using pwm with some external circuitry, it is even possible to implement a simple digital-to-analog converter (dac) by charging an rc circuit of known time constant for a specific length of time. adding a comparator circuit could create a simple analog-to-digital converter (adc). you could even recharge a battery, which would require different external circuitry and more advanced changes.this application note explains how to use the timer type 2 on the maxq200 microcontroller to generate and control pwm.
maxq2000 and timer type 2the maxq2000 is a low-power, high-performance risc lcd microcontroller with a large variety of peripherals and features, including three 16-bit timers. these type 2 timers provide many functions: 8/16-bit timer/counter, autoreload, counting external pulses, capture, and compare. the type 2 timer also provides the maxq2000 with pwm capability. please see the maxq users guide (pdf) and maxq2000 users guide supplement (pdf) for more information about the maxq2000 and timer type 2.
pwm signal generationthe maxq2000's timer type 2 utilizes autoreload with compare to generate the pwm signal. every time the timer overflows or has a compare value match, it toggles the logic state of the output pins t2p and t2pb. figure 1 shows a basic pwm waveform in which the base period (1/fb) corresponds to the timer's overflow. the timer and reload register should be set to a value that will overflow and reload every 1/fb seconds. use equation 1 to determine this value:
where t2r16 is the initial value of timer type 2 as well as the reload value. system clk may be either the actual system clock, i.e., 16mhz, or optionally the 32khz clock. if the base frequency, fb, is significantly smaller than the system frequency, t2div can be used to divide the input clock down to a more reasonable level.
the high time of the pwm waveform, th, corresponds to a match of the timer compare register. the compare register should be set to a value that will match th after every reload. use equation 2 for this. it is important to remember that th must be less than the base period. note, also, that compare output toggle occurs on the next timer tick after a match, which explains the -1 in the following equation. if the high time and the base period were the same, the output waveform would only toggle one time on overflow, resulting in a half-speed wave that will cause problems.
in some applications th may be described by a percentage or duty cycle (dc). in this case, use the following equation 3, where a dc of 50% (half high, half low) is represented as 0.50.
figure 1. pwm waveform.
considerationssome pwm applications require changing either the base frequency of the waveform or modifing the high time. it is important to stop the timer using tr2 before making these changes. otherwise, the pwm waveform can invert because the timer toggles the output an additional time. stopping and restarting the timer guarantees a consistent waveform.
some hardware is sensitive to the pwm glitch resulting from stopping and restarting the pwm waveform as described above. if glitch-free pwm is required, more care must be taken when updating timer registers. one scheme uses the overflow and compare flags as signals to update. when the compare flag/interrupt flag is set, update the reload register. when the overflow flag is set, update the compare register. this scheme allows changes to occur in a predictable manner. note that problems can occur with this scheme near boundaries. specifically, if timer input is an undivided system clock, attempting to update the compare register to one count more than the reload can cause unexpected behavior.
because of the output's toggling behavior, it is important to consider the initial state of the pin. the initial state of the output is controlled by t2pol; if set to 0, it inverts the output signal. this behavior can be seen in the dual 8-bit timer example in the appendix below.
the maxq2000 and timer type 2 support many options required for specialized applications. it is possible to gate the pwm signal on the secondary output pin by using the primary output pin as a gating input. this effectively stops the timer tick and will stop the pwm signal. other specialized modes include single-shot and gated single-shot.
timer type 2 also supports pwm signal generation in three 8-bit modes. these modes allow the timer to be used for multiple tasks when the greater precision of the 16-bit timer is not required. the dual 8-bit mode allows two unique pwm signals to be generated by the timer, while the other 8-bit modes only support pwm output on the secondary pin, t2pb. to configure the timers in the 8-bit modes, use equations 1 and 2 for reload and compare values, except substitute 256 for 65536.
the system clock frequency affects the accuracy of the output waveform. a 16mhz clock, for example, divides by 8 much more evenly than a 14.7456mhhz clock. many fast timer ticks allow better accuracy than two large slow ones. the 16-bit timer is more accurate than the 8-bit timer for this reason.
example codeappendix a contains code samples for setting up pwm on timer 0 of a maxq2000 in both 16-bit and dual 8-bit modes.
appendix a. code samples; 16 bit pwm example code
; maxq2000 has 3 type 2 timer/counters with 2 output pins each
; system clock is 16mhz
; we will use timer 0
$include (maxq2000.inc)
; some basic initialization. set accumulator controls to normal.
move apc, #0 ; no accumulator-modulo behavior
move ap, #0 ; select accumulator 0
move t2cfg0, #00000000b ; configure the timer
; t2ci = 0 - use system clock
; tdiv = 000 - use divide by 1
; t2md = 0 - 16 bit
; ccf = 00 - compare/reload mode
; c/t2 = 0 - timer mode
move t2cna0, #01100000b ; set up timer control
; et2 = 0 - no interrupts
; t2oe0 = 1 - utilize primary output pin
; t2pol0 = 1 - initial output high
; tr2l = 0 - don't care in 16 bit mode
; tr2 = 0 - don't run yet
; cprl2 = 0 - don't care in compare mode
; ss2 = 0 - no single shot
; g2en = 0 - no clock gating
move t2v0, #63936 ; set initial timer count to generate
; 10khz base frequency pwm wave
move t2r0, #63936 ; set reload to same value
; move t2c0, #64735 ; 50% duty
; move t2c0, #64095 ; 10% duty
move t2c0, #65135 ; 75% duty
move acc, t2cna0
or #00001000b ; start the timer
move t2cna0, acc
jump $
end
; dual 8-bit pwm example code
; timers will output inverse wave forms
; maxq2000 has 3 type 2 timer/counters with 2 output pins each
; system clock is 16mhz
; we will use timer 0
$include (maxq2000.inc);
; some basic initialization. set accumulator controls to normal.
move apc, #0 ; no accumulator-modulo behavior
move ap, #0 ; select accumulator 0
move t2cfg0, #00111000b ; configure the timer
; t2ci = 0 - use system clock
; tdiv = 011 - use divide by 8
; t2md = 1 - 8 bit
; ccf = 00 - compare/reload mode
; c/t2 = 0 - timer mode
move t2cna0, #01000000b ; set up timer control
; et2 = 0 - no interrupts
; t2oe0 = 1 - utilize primary output pin
; t2pol0 = 0 - initial output low
; tr2l = 0 - don't run lower yet
; tr2 = 0 - don't run upper yet
; cprl2 = 0 - don't care in compare mode
; ss2 = 0 - no single shot
; g2en = 0 - no clock gating
move t2cnb0, #01100000b ; set up timer control b
; etl2 = 0 - no interrupts
; t2oe1 = 1 - utilize primary output pin
; t2pol1 = 1 - initial output high
; reserved
; 3:0 - interrupt flags
move t2v0, #56 ; set initial timers count to generate
move t2h0, #56 ; 10khz base frequency pwm wave
move t2r0, #56 ; set reload to same value
move t2rh0, #56
move t2c0, #95 ; set high time for 20us
move t2ch0, #95
move acc, t2cna0
or #00011000b ; start the timer
move t2cna0, acc
jump $
end
布局物联网 外媒传ARM同意收购美国数据分析公司
四维图新用户大会丨智能驾驶主题论坛,聚焦技术突破与体验升级
瑞萨电子推出RZ/G 系列HMI解决方案支持多图像处理和3D图形任务
可燃气体传感器在油轮有多重要
5G+云网融合驱动数字化转型
脉冲宽度调制(PWM)与MAXQ2000定时器-Pulse
中国联通表示面对5G业务会推出一客一策基于场景多元化的收费模式
涨幅55%只是开始!厂商放话:SSD等要持续涨价2年
将变频器改造成UPS的可行性
这些关键性能参数,让你正确选择PLL频率合成器
三星s8搭载如此多反人类的科技!国产手机还是要多多学习!
Linux环境下段错误的产生原因及调试方法小结
保时捷第三代Cayenne正式发布 新车将迎来亚洲首秀
关于全ADI有源器件解决方案的组成部分
ThinkPad L系列笔记本新品推出,搭载英特尔Comet Lake处理器
基于ST20-TP4和ST20TP4芯片实现数字电视机顶盒系统的设计
用低成本的措施来实现满足可靠性的交付
小米Note3评测:上市一周就降价?小米6究竟比小米Note3好在哪?
realme X7 Pro怎么样?搭载天玑1000+ 120Hz高刷柔性屏 IMX686后摄
运算放大器的简易测量--失调和偏置电流