0% found this document useful (0 votes)
85 views20 pages

PWM and Periodic Interrupts Using Timers: Texas Instruments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views20 pages

PWM and Periodic Interrupts Using Timers: Texas Instruments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

模块 13

讲解:定时器

| PWM and Periodic interrupts using Timers Texas Instruments


使用定时器的周期性中断

在本模块中您将学到:
 TI MSP432上的Timer A
• 时钟输入,预分频
• 计数器

 周期性中断
• 额外的线程
• 优先级

2 | PWM and Periodic interrupts using Timers Texas Instruments


Timer - A用于输入和输出
TAxCCR0
Timer A特性
 引脚
• 输入捕获
• 输出比较 TAxR

 精度
• 16位

 分辨率
• 时钟周期
• 预分频

SMCLK = 48MHz/4 = 12 MHz, 83.33ns

3 | PWM and Periodic interrupts using Timers Texas Instruments


Timer - A 寄存器
15-10 9-8 7-6 5-4 3 2 1 0 Name
$4000.0000 TASSEL ID MC TACLR TAIE TAIFG TA0CTL

15-14 13-12 11 10 9 8 7-5 4 3 2 1 0


$4000.0002 CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL0
$4000.0004 CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL1
$4000.0006 CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL2
$4000.0008 CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL3
$4000.000A CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL4
$4000.000C CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL5
$4000.000E CM CCIS SCS SCCI CAP OUTMOD CCIE CCI OUT COV CCIFG TA0CCTL6

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

4 | PWM and Periodic interrupts using Timers Texas Instruments


时钟和预分频

TASSEL Selected Clock ID Prescale


00 TAxCLK 00 /1
01 ACLK 01 /2
10 SMCLK 10 /4
11 INCLK 11 /8

分辨率 = T * 2ID * (TAIDEX+1)


范围=精度*分辨率

5 | PWM and Periodic interrupts using Timers Texas Instruments


Timer - A 模式

MC Mode control
00 Stop
01 Up mode: Timer counts up to TAxCCR0
10 Continuous mode: Timer counts up to 0xFFFF

11 Up/down mode: Timer counts up to TAxCCR0 then down to 0x0000

OUTMOD On match to TAxCCRy On match to TAxCCR0


000 OUT bit value
001 Set
010 Toggle Reset
011 Set Reset
100 Toggle
101 Reset
110 Toggle Set
111 Reset Set

6 | PWM and Periodic interrupts using Timers Texas Instruments


时钟和预分频

0)停止定时器(MC = 00)
1)设置定时器时钟和预分频
2)设置子模块0进行比较,arm中断
3)将TAxCCR0设置为中断周期减1
4)在正确的NVIC优先级寄存器中设置优先级
5)使能NVIC中断使能寄存器中的中断
6)复位定时器并以向上模式启动
7)启用中断(在所有设备初始化后的主程序中)

7 | PWM and Periodic interrupts using Timers Texas Instruments


中断向量,数字,名称和优先级
Vector Number IRQ ISR name NVIC priority Priority
0x0000002C 11 -5 SVC_Handler SCB_SHPR2 31 – 29
0x00000038 14 -2 PendSV_Handler SCB_SHPR3 23 – 21
0x0000003C 15 -1 SysTick_Handler SCB_SHPR3 31 – 29
0x00000060 24 8 TA0_0_IRQHandler NVIC_IPR2 7–5
0x00000064 25 9 TA0_N_IRQHandler NVIC_IPR2 15 – 13
0x00000068 26 10 TA1_0_IRQHandler NVIC_IPR2 23 – 21
0x0000006C 27 11 TA1_N_IRQHandler NVIC_IPR2 31 – 29
0x00000070 28 12 TA2_0_IRQHandler NVIC_IPR3 7–5
0x00000074 29 13 TA2_N_IRQHandler NVIC_IPR3 15 – 13
0x00000078 30 14 TA3_0_IRQHandler NVIC_IPR3 23 –TA2_0_IRQHandler(void){
21
TA3_N_IRQHandler NVIC_IPR3
void
0x0000007C 31 15 31 – 29
0x00000080 32 16 EUSCIA0_IRQHandler NVIC_IPR4 TIMER_A2->CCTL[0]
7–5 &= ~0x0001; // ack
0x00000084 33 17 EUSCIA1_IRQHandler NVIC_IPR4 15 –
// body 13
0x00000088 34 18 EUSCIA2_IRQHandler NVIC_IPR4 23 – 21
0x0000008C 35 19 EUSCIA3_IRQHandler NVIC_IPR4 } 31 – 29
0x00000090 36 20 EUSCIB0_IRQHandler NVIC_IPR5 7–5
0x00000094 37 21 EUSCIB1_IRQHandler NVIC_IPR5 15 – 13
0x00000098 38 22 EUSCIB2_IRQHandler NVIC_IPR5 23 – 21
0x0000009C 39 23 EUSCIB3_IRQHandler NVIC_IPR5 31 – 29
0x000000CC 51 35 PORT1_IRQHandler NVIC_IPR8 31 – 29
0x000000D0 52 36 PORT2_IRQHandler NVIC_IPR9 7–5
0x000000D4 53 37 PORT3_IRQHandler NVIC_IPR9 15 – 13
0x000000D8 54 38 PORT4_IRQHandler NVIC_IPR9 23 – 21
0x000000DC 55 39 PORT5_IRQHandler NVIC_IPR9 31 – 29
0x000000E0 56 40 PORT6_IRQHandler NVIC_IPR10 7–5

寻找interruptVectors []
在文件startup_msp432p401r_ccs.c中

8 | PWM and Periodic interrupts using Timers Texas Instruments


周期性的中断
void (*TimerA2Task)(void); // user function
void TimerA2_Init(void(*task)(void), uint16_t period){ 在所有设备初始化后,在主程序
TimerA2Task = task; // user function 中启用中断
// bits9-8=10, clock source to SMCLK
// bits7-6=10, input clock divider /4
// bits5-4=00, stop mode
// bit2=0, set this bit to clear
// bit1=0, no interrupt on timer T * 2ID * (TAIDEX+1)(TAxCCR0+1)
TIMER_A2->CTL = 0x0280; = 2µs*period
// bits15-14=00, no capture mode
// bit8=0, compare mode
// bit4=1, enable capture/compare interrupt on CCIFG
// bit2=0, output this value in output mode 0 void TA2_0_IRQHandler(void){
// bit0=0, clear capture/compare interrupt pending TIMER_A2->CCTL[0] &= ~0x0001;
TIMER_A2->CCTL[0] = 0x0010; *TimerA2Task();
TIMER_A2->CCR[0] = (period - 1); // compare match value }
TIMER_A2->EX0 = 0x0005; // configure for input clock divider /6
NVIC->IP[3] = (NVIC->IP[3]&0xFFFFFF00)|0x00000040; // priority 2
NVIC->ISER[0] = 0x00001000; // enable interrupt 12 in NVIC
TIMER_A2->CTL |= 0x0014; // reset and start Timer A in up mode
}

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 概述
 时钟输入
 预分频
 计数器

void TA0_0_IRQHandler(void){ TimerA0.c


周期性中断 TIMER_A0->CCTL[0] &= ~0x0001;
 额外线程 // ack
// body
 优先级
} TimerA1.c
void TA1_0_IRQHandler(void){
TIMER_A1->CCTL[0] &= ~0x0001;
// ack
// body
}
void TA2_0_IRQHandler(void){ TimerA2.c
TIMER_A2->CCTL[0] &= ~0x0001;
// ack
// body
}

10 | PWM and Periodic interrupts using Timers Texas Instruments


模块 13
讲解:定时器

| PWM and Periodic interrupts using Timers Texas Instruments


PWM使用定时器

在本模块中您将学到:
 TI MSP432上的Timer A
• 时钟输入,预分频
• 计数器

 PWM 输出
• 调整电机功率
• 两个独立的输出

Period Period

High Low High Low

12 | PWM and Periodic interrupts using Timers Texas Instruments


使用电机驱动配电板的接口电路(概述)

PWM on
P2.6, P2.7

13 | PWM and Periodic interrupts using Timers Texas Instruments


MSP432 输入/输出

Lab code
Starter code

14 | PWM and Periodic interrupts using Timers Texas Instruments


使用Timer-A实现两个PWM输出 你将它转换为
TA0.4/P2.7
TA0.3/P2.6

TA0CCR2 TA0CCR0
Driver Duty cycle =
MSP432
Motor TA0CCR2
TA0CCR0
TA0.2/P2.5
TA0CCR1 TA0CCR0
Duty cycle =
Motor TA0CCR1
TA0.1/P2.4 TA0CCR0

15 | PWM and Periodic interrupts using Timers Texas Instruments


High High
使用Timer-A实现两个PWM输出 Dutycycle  
High  Low Period

PWM模式 Period Period


 上/下计数
• 最高到CCR0(10) High Low High Low
• 最低降到0
 当定时器等于TA0CCR1(7)时,P2.4 = 1
 当定时器等于TA0CCR1(7)时,P2.4 = 0

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%

18 | PWM and Periodic interrupts using Timers Texas Instruments


总结

PWM 输出
 调整电机功率
 两个独立的输出

Period Period

High Low High Low

19 | PWM and Periodic interrupts using Timers Texas Instruments


IMPORTANT NOTICE FOR TI DESIGN INFORMATION AND RESOURCES

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

You might also like