PWM and Periodic Interrupts Using Timers: Texas Instruments
PWM and Periodic Interrupts Using Timers: Texas Instruments
讲解:定时器
在本模块中您将学到:
TI MSP432上的Timer A
• 时钟输入,预分频
• 计数器
周期性中断
• 额外的线程
• 优先级
精度
• 16位
分辨率
• 时钟周期
• 预分频
15–0
$4000.0010 16-bit counter TA0R
$4000.0012 16-bit Capture/Compare 0 Register TA0CCR0
$4000.0014 16-bit Capture/Compare 1 Register TA0CCR1
$4000.0016 16-bit Capture/Compare 2 Register TA0CCR2
$4000.0018 16-bit Capture/Compare 3 Register TA0CCR3
$4000.001A 16-bit Capture/Compare 4 Register TA0CCR4
$4000.001C 16-bit Capture/Compare 5 Register TA0CCR5
$4000.001E 16-bit Capture/Compare 6 Register TA0CCR6
15-3 2-0
$4000.0020 TAIDEX TA0EX0
15-0
$4000.002E TAIV TA0IV
MC Mode control
00 Stop
01 Up mode: Timer counts up to TAxCCR0
10 Continuous mode: Timer counts up to 0xFFFF
0)停止定时器(MC = 00)
1)设置定时器时钟和预分频
2)设置子模块0进行比较,arm中断
3)将TAxCCR0设置为中断周期减1
4)在正确的NVIC优先级寄存器中设置优先级
5)使能NVIC中断使能寄存器中的中断
6)复位定时器并以向上模式启动
7)启用中断(在所有设备初始化后的主程序中)
寻找interruptVectors []
在文件startup_msp432p401r_ccs.c中
NVIC->IP[2]= (NVIC->IP[2]&0xFF00FFFF)|0x00400000;
NVIC->ISER[0] = 0x00000400; // enable interrupt 10 in NVIC
9 | PWM and Periodic interrupts using Timers Texas Instruments
总结
Timer 概述
时钟输入
预分频
计数器
在本模块中您将学到:
TI MSP432上的Timer A
• 时钟输入,预分频
• 计数器
PWM 输出
• 调整电机功率
• 两个独立的输出
Period Period
PWM on
P2.6, P2.7
Lab code
Starter code
TA0CCR2 TA0CCR0
Driver Duty cycle =
MSP432
Motor TA0CCR2
TA0CCR0
TA0.2/P2.5
TA0CCR1 TA0CCR0
Duty cycle =
Motor TA0CCR1
TA0.1/P2.4 TA0CCR0
T = 预分频/12MHz
周期 = 2*T*CCR0
16 | PWM and Periodic interrupts using Timers 占空比 = CCR1/CCR0 Texas Instruments
P2.4和P2.5上的PWM输出
// SMCLK = 48MHz/4 = 12 MHz, 83.33ns
// Counter counts up to TA0CCR0 and back down
// Let Timerclock period T = 8/12MHz = 666.7ns 你将它转换为:
// Period of P2.4 is period*1.333us, duty cycle is duty1/period TA0.4/P2.7
// Period of P2.5 is period*1.333us, duty cycle is duty2/period TA0.3/P2.6
void PWM_Init12(uint16_t period, uint16_t duty1, uint16_t duty2){
P2->DIR |= 0x30; // P2.4, P2.5 output
P2->SEL0 |= 0x30; // P2.4, P2.5 Timer0A functions
P2->SEL1 &= ~0x30; // P2.4, P2.5 Timer0A functions
TIMER_A0->CCTL[0] = 0x0080; // CCI0 toggle
TIMER_A0->CCR[0] = period; // Period is 2*period*8*83.33ns is 1.333*period
TIMER_A0->EX0 = 0x0000; // divide by 1
TIMER_A0->CCTL[1] = 0x0040; // CCR1 toggle/reset
TIMER_A0->CCR[1] = duty1; // CCR1 duty cycle is duty1/period
TIMER_A0->CCTL[2] = 0x0040; // CCR2 toggle/reset
TIMER_A0->CCR[2] = duty2; // CCR2 duty cycle is duty2/period
TIMER_A0->CTL = 0x02F0; // SMCLK=12MHz, divide by 8, up-down mode
// bit mode
// 9-8 10 TASSEL, SMCLK=12MHz
// 7-6 11 ID, divide by 8 T = 8/12MHz
// 5-4 11 MC, up-down mode
周期 = 2*T*15000 = 10ms
// 2 0 TACLR, no clear
// 1 0 TAIE, no interrupt
占空比 = CCR1/15000
// 0 TAIFG
}
17 | PWM and Periodic interrupts using Timers Texas Instruments
P2.4和P2.5上的PWM输出
你将它转换为:
TA0.4/P2.7
TA0.3/P2.6
//***************************PWM_Duty1*******************************
// change duty cycle of PWM output on P2.4
// Inputs: duty1
// Outputs: none
// period of P2.4 is 2*period*666.7ns, duty cycle is duty1/period
void PWM_Duty1(uint16_t duty1){
TIMER_A0->CCR[1] = duty1; // CCR1 duty cycle is duty1/period
}
//***************************PWM_Duty2*******************************
// change duty cycle of PWM output on P2.5
// Inputs: duty2
// Outputs: none// period of P2.5 is 2*period*666.7ns, duty cycle is duty2/period
void PWM_Duty2(uint16_t duty2){
TIMER_A0->CCR[2] = duty2; // CCR2 duty cycle is duty2/period
}
占空比
• 精确的14999种选择(0到14998)
• 范围0至99.99%
• 分辨率 0.0067%
PWM 输出
调整电机功率
两个独立的输出
Period Period
Texas Instruments Incorporated (‘TI”) technical, application or other design advice, services or information, including, but not limited to,
reference designs and materials relating to evaluation modules, (collectively, “TI Resources”) are intended to assist designers who are
developing applications that incorporate TI products; by downloading, accessing or using any particular TI Resource in any way, you
(individually or, if you are acting on behalf of a company, your company) agree to use it solely for this purpose and subject to the terms of
this Notice.
TI’s provision of TI Resources does not expand or otherwise alter TI’s applicable published warranties or warranty disclaimers for TI
products, and no additional obligations or liabilities arise from TI providing such TI Resources. TI reserves the right to make corrections,
enhancements, improvements and other changes to its TI Resources.
You understand and agree that you remain responsible for using your independent analysis, evaluation and judgment in designing your
applications and that you have full and exclusive responsibility to assure the safety of your applications and compliance of your applications
(and of all TI products used in or for your applications) with all applicable regulations, laws and other applicable requirements. You
represent that, with respect to your applications, you have all the necessary expertise to create and implement safeguards that (1)
anticipate dangerous consequences of failures, (2) monitor failures and their consequences, and (3) lessen the likelihood of failures that
might cause harm and take appropriate actions. You agree that prior to using or distributing any applications that include TI products, you
will thoroughly test such applications and the functionality of such TI products as used in such applications. TI has not conducted any
testing other than that specifically described in the published documentation for a particular TI Resource.
You are authorized to use, copy and modify any individual TI Resource only in connection with the development of applications that include
the TI product(s) identified in such TI Resource. NO OTHER LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE TO
ANY OTHER TI INTELLECTUAL PROPERTY RIGHT, AND NO LICENSE TO ANY TECHNOLOGY OR INTELLECTUAL PROPERTY
RIGHT OF TI OR ANY THIRD PARTY IS GRANTED HEREIN, including but not limited to any patent right, copyright, mask work right, or
other intellectual property right relating to any combination, machine, or process in which TI products or services are used. Information
regarding or referencing third-party products or services does not constitute a license to use such products or services, or a warranty or
endorsement thereof. Use of TI Resources may require a license from a third party under the patents or other intellectual property of the
third party, or a license from TI under the patents or other intellectual property of TI.
TI RESOURCES ARE PROVIDED “AS IS” AND WITH ALL FAULTS. TI DISCLAIMS ALL OTHER WARRANTIES OR
REPRESENTATIONS, EXPRESS OR IMPLIED, REGARDING TI RESOURCES OR USE THEREOF, INCLUDING BUT NOT LIMITED TO
ACCURACY OR COMPLETENESS, TITLE, ANY EPIDEMIC FAILURE WARRANTY AND ANY IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL
PROPERTY RIGHTS.
TI SHALL NOT BE LIABLE FOR AND SHALL NOT DEFEND OR INDEMNIFY YOU AGAINST ANY CLAIM, INCLUDING BUT NOT
LIMITED TO ANY INFRINGEMENT CLAIM THAT RELATES TO OR IS BASED ON ANY COMBINATION OF PRODUCTS EVEN IF
DESCRIBED IN TI RESOURCES OR OTHERWISE. IN NO EVENT SHALL TI BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL,
COLLATERAL, INDIRECT, PUNITIVE, INCIDENTAL, CONSEQUENTIAL OR EXEMPLARY DAMAGES IN CONNECTION WITH OR
ARISING OUT OF TI RESOURCES OR USE THEREOF, AND REGARDLESS OF WHETHER TI HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
You agree to fully indemnify TI and its representatives against any damages, costs, losses, and/or liabilities arising out of your non-
compliance with the terms and provisions of this Notice.
This Notice applies to TI Resources. Additional terms apply to the use and purchase of certain types of materials, TI products and services.
These include; without limitation, TI’s standard terms for semiconductor products http://www.ti.com/sc/docs/stdterms.htm), evaluation
modules, and samples (http://www.ti.com/sc/docs/sampterms.htm).
Mailing Address: Texas Instruments, Post Office Box 655303, Dallas, Texas 75265
Copyright © 2018, Texas Instruments Incorporated