abstract: this application note describes how to set up and use the timer b in the maxq® family of microcontrollers to generate desired pwm waveforms. the action will require that the compare functionality be enabled. the note includes source code for reference.
introductionthe maxq microcontrollers have three types of timers: timer 0, timer 1, and timer 2. timer b is an enhanced version of timer 1 with modifications to support different input-clock prescaling and set/reset/compare output functionality. this application note details how to set up and use timer b to generate desired waveforms. source code examples are included for reference.
timer b registers and their usethe maxq core configures timer b through a set of special function registers (sfr):
tbcn
— timer b control register
tbv
— timer b value register
tbr
— timer b reload/capture register
tbc
— timer b compare register
timer b control register: tbcnthe timer b control register is 16-bit wide and unrestricted read and write. it is cleared to 0000h on all forms of reset. below are brief descriptions of the register bit configuration.
bit 0: the cp/rld bit is used to set either capture or reload mode for timer b. when this bit is set to 1, timer b is in capture mode. when this bit is clear to 0, timer b is in reload mode.
bit 1: the etb bit is used to enable a timer b interrupt. setting this bit to 1 enables the interrupt from the timer b tfb and exfb flags.
bit 2: the trb bit is used to enable timer b operation.
bit 3: the exenb bit is used to enable the capture/reload function on the tbb pin for a negative transaction. when operating in autoreload mode (cp/rld = 0) with the output-compare functionality enabled (this mode will be important later), enabling the tbb input function (exenb = 1) will allow compare output negative transitions to set the exfb flag. no reload will occur, however, as a result of the external negative edge detection.
bit 4: the dcen bit is used to enable a count-down option for timer b. in conjunction with the tbb pin, this bit controls the direction that timer b counts. setting this bit to 1 causes timer b to count up if pin tbb is 1, and count down if pin tbb is 0. timer b always counts up when this bit is 0. when the compare-mode functionality of timer b is enabled, the up/down count control of timer b is controlled internally, based on the compare-mode settings. in the compare modes, the dcen bit controls whether the timer ramps up and resets (dcen = 0), or ramps up and down (dcen = 1).
bit 5: the tboe bit is used to enable the clock-output function on the tba pin.
bit 6: the exfb bit is used to flag when a negative transaction occurs on the tbb pin. this bit also toggles whenever timer b overflows or underflows if cp/rld = 0, dcen = 0, and tbcs:tbcr = 00b.
bit 7: the tfb bit is used to set a flag when timer b overflows or underflows.
bit 8-10: the tbps2:0 are used as a prescaler applied to the system clock input to timer b clock:
timer b clock = system clock/(2**(2*tbps2:0))
the values of tbps2:0 = 11xb are reserved.
bit 11-12: the tbcs:tbcr bits are used to configure timer b in compare mode if tbcs:tbcr00b. these compare-mode bits define several conditions: whether the pwm/compare-mode output function is enabled on the tbb pin; what is the initial output starting state; and what compare-mode output function is in effect.
bit 14-13: reserved bits
bit 15: the c/tb bit is used to determine whether timer b functions as a timer or a counter.
timer b value register: tbvthe timer b value register is a 16-bit register and is cleared to 0000h on all forms of reset. this register is unrestricted read/write and is used to load and read the 16-bit timer b value.
timer b capture/reload register: tbrtimer b capture/reload register is a 16-bit register and is cleared to 0000h on all forms of reset. this register is unrestricted read/write and is used to capture the tbv value when the timer b is configured in capture mode. this register is also used as the 16-bit reload value when timer b is configured in autoreload mode.
timer b compare register: tbcthe timer b compare register is a 16-bit register and is cleared to 0000h on all forms of reset. this register is unrestricted read/write and is for comparing tbc to the tbv value when timer b operates in compare mode.
operational modes of timer btimer b can be configured in capture or reload mode by setting the cp/rld bit in the timer b control register. values of tbcs:tbcr00b will set timer b in compare mode. it is not necessary to prevent configuration of the timer b capture mode (cp/rld=1) and the pwm compare output mode at the same time, however, this is not an intended timer b mode of operation. timer b can also be configured in compare mode, in which there are three different compare functions: reset, set, and toggle.
this following section reviews methods for configuring timer b in these different operational modes.
capture modebit 0 cp/rld of the timer b control register determines whether the capture or reload mode is used for timer b. setting this bit to 1 causes a timer b capture to occur when a falling edge is detected on the tbb timer pin if bit 3 exenb of control register is 1. setting bit 3 exenb to 0 causes timer b to ignore all external events on the tbb pin. the captured value is loaded into the reload register tbr.
reload modeclearing the cp/rld bit to 0 causes an autoreload to occur when timer b overflows or a falling edge is detected on tbb if exenb is 1. if compare functionality is enabled, the frequency and duty cycle of the output waveforms can be modified by changing the values in the reload (tbr) and compare (tbc) registers. in this way, the maxq processor can generate a pulse-width modulated (pwm) waveform.
compare modethe compare mode is only enabled when the values of the compare mode bits are different from 0, and when timer b is recommended to be in reload mode. the pwm outputs can be selectively enabled and the starting polarity can be inverted, depending on the value of these bits. the limits on frequency and duty cycle are determined by the frequency of the clock selected as source (i.e., the system or alternate clock) and the clock divisor selected. as noted above, there are three compare modes (set, reset, and toggle) with a predefined initial state of the tbb pin.
the compare reset mode is selected when cp/rld = 0 (reload mode) and values of tbcs:tbcr are 01, respectively. in this mode, the tbb will reset on a tbc match and set on 0000h. depending on the relative values between the tbr and tbc registers and on the up/down count, the timer will generate different pwm waveforms. if timer b does not implement tbc, then it will only set on 0000h. in this mode the initial value of the tbb pin is low.
the compare set mode is selected when cp/rld = 0 (reload mode) and the values of tbcs:tbcr are 10, respectively. in this mode, the tbb will set on a tbc match and set on tbr. depending on the relative values between the tbr and tbc registers and on the up/down count, the timer will generate different pwm waveforms. if timer b does not implement tbc, then it will only reset on tbr. in this mode the initial value of the tbb pin is high.
the compare toggle mode is selected when cp/rld = 0 (reload mode) and values of tbcs:tbcr are 11, respectively. in this mode, the tbb will toggle on a tbc match (except tbr or 0000h). depending on the relative values between the tbr and tbc registers and on the up/down count, the timer will generate different pwm waveforms. if timer b does not implement tbc, then it will only toggle on a tbr match. in this mode the initial value of tbb pin is unchanged. the initial state for the toggle mode depends on the previous set or reset mode. this means that to establish a specific starting state, the tbcs:tbcr bits should be configured to set or reset momentarily when changing from compare disable to compare toggle mode.
as mentioned above, we are particularly interested in this compare mode that is used to generate pwm on the tbb pin of timer b. the pwm waveform greatly depends on the relative values between tbr and tbc, with or without the tbc register, and the value of dcen bit (table 1).
pwm waveform of timer b in compare mode
timer b without tbc registerthe pin set/reset that can result from the tbc compare match can never occur for the set and reset modes. the user can only define a starting pin state that will take effect when the timer is enabled. pin set or clear operations for the respective reset and set modes will still occur. for the toggle mode, the tbr will be used to generate a 50% duty-cycle pwm. see table 1 and figure 1.
table 1. timer b output function without a tbc register
tbcs:tbcr
function
initial state (if tbr = 0)
00
none (compare disable)
no change
01 (reset)
set on 0000h
low
10 (set)
reset on tbr match
high
11 (toggle)
toggle on tbr match
no change
figure 1.
set or clear the tbb pin timed at tbr+1 and tbr, respectively for reset and set operations.
generate a 50% duty cycle for toggle mode at period 2*tbr + 2. example 1: set the tbb pin after 1 second since the timer was on (use reset compare mode).
frequency of the timer b: calculate timer b frequency based on a 12mhz system clock.
use reset compare mode with dcen = 0 to set the tbb pin.
calculate the values of tbr to delay 1 second by using below formula:
(tbr+1) × timer cycle = 1s where 1 timer cycle = 1/system clock
(tbr+1)/system clock = 1 => tbr = system clock -1. value of tbr tbr = 12000000/256 – 1 = 46874 sample code: move tb2cn,#000h ; reset timer b
move tb2cn,#0c00h ; timer b clock = system clock/256
; and in compare reset mode
move tb2r, #46874 ; set reload value of timer 2 tbr = 46874
move tb2v, #0001h ; reset tbv=0x01 just to avoid set
; operation on 0000h
move tb2cn.2,#1 ; start timer b to generate pwm
example 2: generate a 1mhz waveform with a 50% duty cycle staring with low (use toggle compare mode). (see figure 2.)
frequency of the timer b: calculate the timer b frequency based on a 12mhz system clock.
use the toggle compare mode with dcen = 0 to set the tbb pin.
calculate the values of tbr with a desired pwm frequency of 1mhz by using the formula:
period = (2 × tbr + 2) × timer cycle = 1/1000000 where 1 timer cycle = 1/12mhz => 2 × tbr + 2 = 12000000/1000000 = 12 or tbr = 5 sample code: move tb3cn,#000h ; reset timer b
move tb3cn,#0800h ; configure timer b clock in reset
; compare mode to start low
move tb3cn,#1800h ; timer b clock = system clock
; and in compare toggle mode with dcen =0
move tb3r, #5 ; set reload value of timer 3 tb3r = 5
move tb3v, #000h ; reset tbv=0x00
move tb3cn.2,#1 ; start timer b to generate pwm
figure 2.
set or clear the tbb pin timed at 2 × tbr and tbr, respectively, for reset and set operations.
generate a 50% duty cycle for toggle mode at period 4 × tbr. example 3: reset the tbb pin after a 1µs delay (use set compare mode).
frequency of the timer b: calculate timer b frequency based on a 12mhz system clock.
use set compare mode with dcen = 1 to set the tbb pin.
calculate the values of tbr to delay 1µs by using the formula:
tbr × timer cycle = 1µs where 1 timer cycle = 1/system clock
tbr/system clock = 1µs => tbr = 12000000/1000000. the value of tbr = 12. sample code: move tb3cn,#000h ; reset timer b
move tb3cn,#1010h ; timer b clock = system clock
; and in compare set mode with dcen = 1
move tb3r, #12 ; set reload value of timer 3 tb3r = 12
move tb3v, #000h ; reset tbv = 0x00
move tb3cn.2,#1 ; start timer b to generate pwm
example 4: generate a 1mhz waveform with a 50% duty cycle staring with high (use toggle compare mode).
frequency of the timer b: calculate timer b frequency based on a 12mhz system clock.
use toggle compare mode with dcen = 1 to set the tbb pin.
calculate the values of tbr with a desired pwm frequency of 1mhz by using the formula:
period = (4 × tbr) × timer cycle = 1/1000000 where 1 timer cycle = 1/12mhz => 4 × tbr = 12000000/1000000 = 12 or tbr = 3 sample code: move tb2cn,#000h ; reset timer b
move tb2cn,#01000h ; configure timer b clock in set
; compare mode to start high
move tb2cn,#1810h ; timer b clock = system clock
; and in compare toggle mode with dcen=1
move tb2r, #3 ; set reload value of timer 2 tb2r = 3
move tb2v, #000h ; reset tbv = 0x00
move tb2cn.2,#1 ; start timer b to generate pwm
timer b with tbc registerwhen the tbc register is within the counting range (tbc tbr) or in the special cases of tbc = tbr and tbc = 0, the set and reset functions allow a timer clear or set of the tbb pin. see table 2 and figure 3.
table 2. timer b output function with tbc register
tbcs:tbcr
function
initial state (if tbr = 0)
00
none (compare disable)
no change
01 (reset)
reset on tbc match, set on 0000h
low
10 (set)
set on tbc match, reset on tbr match
high
11 (toggle)
toggle on tbc match (except tbr or 0)
no change
figure 3.
tbc > tbr: set or clear the tbb pin for the respective reset and set modes. toggle mode is disabled.
tbc = tbr or tbc = 0: see notes 1, 2, and 3 after example 8 below.
tbc tbr = (12000000/32000) - 1 = 372
duty cycle = (tbr - tbc)/(tbr + 1) = 50% => tbc = 186 sample code: move tb0cn,#1000h ; configure timer b clock as same as
; system clock and in compare set mode
move tb0c, #00186 ; set compare value of timer 0 tbc = 186
move tb0r, #00372 ; set reload value of timer 0 tbr=372
move tb0v, #0000h ; reset tbv=0x00
move tb0cn.2,#1 ; start timer b to generate pwm
example 6: generate a 512hz waveform with a 50% duty cycle starting low (use toggle mode). (see figure 4.)
frequency of the timer b: this is the same as a 12mhz system clock.
use toggle compare mode with dcen = 0 to generate this waveform.
calculate the values of tbr and tbc with a pwm frequency of 512hz using the formula:
period = timer cycle /(2 × tbr+2) = 1/512 where 1 timer cycle = 1/12mhz
512 = 12000000/(2 × tbr + 2) => tbr = ((12000000/512) - 2)/2 = 23434/2 = 11717
duty cycle = 50% regardless tbc tbr: set or clear the tbb pin for the respective reset and set modes. toggle mode is disabled.
tbc = tbr or tbc = 0: see notes 1, 2, and 3 after example 8.
tbc tbc = 29 sample code: move tb0cn,#0910h ; configure timer b clock in system
; clock/4, in compare reset mode, and
; dcen = 1
move tb0c, #00029 ; set compare value of timer 0 tbc = 29
move tb0r, #00049 ; set reload value of timer 0 tbr = 49
move tb0v, #0000h ; reset tbv = 0x00
move tb0cn.2,#1 ; start timer b to generate pwm
example 8: generate a 512hz waveform with a 40% duty cycle starting high (use toggle mode).
frequency of the timer b: this is the same as a 12mhz system clock
use toggle compare mode with dcen = 1 to generate this waveform.
calculate the values of tbr and tbc with a pwm frequency of 512hz using the formula:
period = timer cycle/(2 × tbr) = 512 where 1 timer cycle = 1/12mhz
512 = 12000000/(2 × tbr) => tbr = (12000000/512)/2 = 23437/2 = 11718
duty cycle = 40% or tbc/tbr = 0.4 => tbc = 4687sample code: move tb1cn,#01000h ; configure timer b clock in set
; compare mode to start high
move tb1cn,#1810h ; configure timer b clock as same as
; system clock in compare toggle mode
; and dcen = 1
move tb1c, #004687 ; set compare value of timer 1 tbc = 4687
move tb1r, #011718 ; set reload value of timer 1 tbr=11718
move tb1v, #0000h ; reset tbv = 0x00
move tb1cn.2,#1 ; start timer b to generate pwm
notes:
1when the output compare function is configured to the set mode, configuring tbc = tbr disables the tbc compare match operation. the timer will not set when tbc = tbr.
2when the output compare function is configured to the reset mode, configuring tbc = 0 disables the tbc compare reset operation. the timer will not reset when tbc = 0.
3when the output compare function is configured to the toggle mode, configuring tbc = 0 or tbc = tbr disables the toggle function. the output pwm will be either low or high, depending on the initial starting state of the tbb pin.
4the initial state for the toggle mode depends on the previous set or reset mode. this means that the tbcs:tbcr bits should be configured to set (start high) or reset (start low) momentarily when changing from 'compare disable' to 'compare toggle mode' to establish a specific starting state.
examplesall the above examples are executed on a maxq microcontroller with timer b. the device has four timer bs, of which timers 0 and 1 have tbc registers and timers 2 and 3 do not. the clock is running at approximately 12mhz.
additional notes
when in compare mode with dcen = 1, the timer is always counting up regardless of the current value of the tbb pin. the timer functions like this because the counting direction is controlled internally.
the normal counting range will be between 0000h to tbr for counting up and from tbr to 0000h for counting down. note: this counting differs from tbr to 0ffffh and from 0ffffh to tbr, as in other timers.
three source-clock prescaler bits (tbps2:0) set a prescale value of 2**(n × 2) where n = 0, 1, 2, 3, 4, and 5, which is applied to the timer b source clock.
conclusionthe timer b in maxq microcontrollers can provide different pwm waveforms, depending on the relative values of the tbc and tbr registers and on the state of dcen bit. timer b will work with or without compare register tbc. tables 1 and 2 summarize the timer b output function without and with the tbc compare register, respectively. figures 1 through 4 show the typical pwm waveforms on the pin tbb of timer b for each particular configuration. for all cases, the user can determine the pwm period and duty cycle with the defined formulas.
maxq is a registered trademark of maxim integrated products, inc.
模拟量是什么?
光伏电站环境监测仪器的功能特点说明
医疗AI走出边缘的切入点在哪里
生物识别技术的应用与监管亟待加强跨领域、跨部门的顶层设计
印度降低关税以促进智能手机生产
How to Generate PWM Waveforms
小米6发布在即,1999元价格不在,涨价成定局,还支持吗?
芯片制造为什么要使用超纯水?
科思创获“十佳协同创新外资投资企业”荣誉
华为年度旗舰智能手表WATCH 3系列正式亮相
智慧加油站三维可视化监控系统解决方案
农药残留快速测定仪的应用及性能
浇注系统的浇口形式及尺寸设计
5G网络切片的兴起与未来
详解:气体传感器分类原理及应用
企业“挑大梁”,破除产业“卡脖子”痛点
有实力,你就来!第四届感知领航优秀项目征集令
生物识别行业发展渐入佳境 市场规模日益壮大
数字开关电源控制系统测试过程
以ARM7为核心的嵌入式工控网络设计