lab 2: timer
DESCRIPTION
Lab 2: Timer. When Setting Registers. GPIO registers P1OUT = 0x80; P1IN, P1SEL …… What are these P1IN, P1OUT …… Register and bit definitions. Registers are store here. msp430x16x.h. You see this #include Things that other done to make your life easier - PowerPoint PPT PresentationTRANSCRIPT
Network and Systems Laboratorynslab.ee.ntu.edu.tw
2010/10/14Wireless Sensor Network And Labs fall 2010 1
Network and Systems Laboratorynslab.ee.ntu.edu.tw
When Setting RegistersGPIO registers
P1OUT = 0x80;P1IN, P1SEL ……
What are these P1IN, P1OUT ……Register and bit definitions
Registers are store here
2010/10/14Wireless Sensor Network And Labs fall 2010 2
Network and Systems Laboratorynslab.ee.ntu.edu.tw
msp430x16x.hYou see this
#include <msp430x16x.h>Things that other done to make your life easier
Most embedded systems programs include a header file which describes the target processor.
Contains descriptions of interrupt vectorsROM and RAM sizes and locations register names and locations port names and locations register bit definitions macro definitions
2010/10/14Wireless Sensor Network And Labs fall 2010 3
Network and Systems Laboratorynslab.ee.ntu.edu.tw
What’s Inside msp430x16x.h
Define this name at this address• DEFC –> 8-bit• DEFW 16-bit
This is why compiler understand P1IN, P1OUT, ……
2010/10/14Wireless Sensor Network And Labs fall 2010 4
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Interrupt Vectors
This is why compiler understand PORT2_VECTOR
2010/10/14Wireless Sensor Network And Labs fall 2010 5
Network and Systems Laboratorynslab.ee.ntu.edu.tw
What’s Inside msp430x16x.hYou can do this
Set bit 0 and bit 7 P1SEL |= BIT0 + BIT7;
Clear bit 0 and bit 7 P1SEL &= ~(BIT0 + BIT7);
There are many othersYou will meet them soon
2010/10/14Wireless Sensor Network And Labs fall 2010 6
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Things You Can DoThe above are things that other done to
make your life easierYou can do something to make your life
easierHardware Abstraction Layer (HAL)Macros
2010/10/14Wireless Sensor Network And Labs fall 2010 7
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Hardware Abstraction Layer (HAL)An abstraction layer between software and
hardwareImplemented in softwareYou can see it in Windows, Linux, embedded
system, and etc.Provide application programming interfaces
(APIs)Easily portableIntuitive name
2010/10/14Wireless Sensor Network And Labs fall 2010 8
Network and Systems Laboratorynslab.ee.ntu.edu.tw
;;;
LEDs HALYou want to have a HAL for LEDsExample
Filename: hal_LEDs.h
Filename: hal_LEDs.c
Macros• Replace• For short expression
Functions• Branch• Need extra cycles
2010/10/14Wireless Sensor Network And Labs fall 2010 9
Network and Systems Laboratorynslab.ee.ntu.edu.tw
MSP430digitally controlled oscillator
Low-frequency/high-frequency oscillator
high-frequency oscillator (optional)
MSP430 Clock System
LFXT1CLK
XT2CLK
DCOCLK
Clock ModulesMCLK: Master Clock
SMCLK:Sub-main
clock
ACLK:Auxiliary
clock
Clock Signals
CPU
Peripherals:Timer, UART, …
32.768KHz fixed rate
2010/10/14Wireless Sensor Network And Labs fall 2010 10
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Schematic
Connected to a 32.768KHz watch crystal
No second oscillator (XT2CLK)
2010/10/14Wireless Sensor Network And Labs fall 2010 11
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Generate ClockOscillator module
include crystal and drive circuit
Internal drive circuit + crystalmost common case
Digitally Controlled Oscillatoran oscillator circuit constructed
by some electrical componentscan control its frequencyfrequency varies with
temperature, voltage, and from device to device
2010/10/14Wireless Sensor Network And Labs fall 2010 12
Network and Systems Laboratorynslab.ee.ntu.edu.tw
MSP430 Clock system
Flexible clock sourcesAlways-on low frequency crystal (eg. 32.768KHz)On-demand high speed DCO (up to 8MHz)
DCO on and stable in < 1μs
2010/10/14Wireless Sensor Network And Labs fall 2010 13
Network and Systems Laboratorynslab.ee.ntu.edu.tw
MSP430digitally controlled oscillator
Low-frequency/high-frequency oscillator
high-frequency oscillator (optional)
MSP430 Clock System
LFXT1CLK
XT2CLK
DCOCLK
Clock ModulesMCLK: Master Clock
SMCLK:Sub-main
clock
ACLK:Auxiliary
clock
Clock Signals
CPU
Peripherals:Timer, UART, …
32.768KHz fixed rate
2010/10/14Wireless Sensor Network And Labs fall 2010 14
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Adjusting DCO Frequencycurrent injected into the DCO
defines the fundamental frequencyinternal or external resistor
controls the current
three RSELx bits select one of eight nominal frequency ranges
three DCOx bits divide the DCO range selected by the RSELx bits
five MODx bits (modulation) further adjust the frequency
2010/10/14Wireless Sensor Network And Labs fall 2010 15
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Clock Module Registers
2010/10/14Wireless Sensor Network And Labs fall 2010 16
Network and Systems Laboratorynslab.ee.ntu.edu.tw
BCSCTL1
ACLK
Low Freq. (32.768K)??High Freq. (450K ~ 8M)??
Divider = 4
32.768K
8192
2010/10/14Wireless Sensor Network And Labs fall 2010 17
Network and Systems Laboratorynslab.ee.ntu.edu.tw
BCSCTL2
No external resistor on Taroko
2010/10/14Wireless Sensor Network And Labs fall 2010 18
Network and Systems Laboratorynslab.ee.ntu.edu.tw
TimerA counter that is incremented/decremented
when the clock pulses
Two timer on MSP430F1611Timer A3
3 sets of configurable capture/compare registersTimer B7
7 sets of configurable capture/compare registers16-bit timer
at most count to 65535
+/-1
+/-1
2010/10/14Wireless Sensor Network And Labs fall 2010 19
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Timer
Timer (counter)0, 1, 2,…….,65534,
65535
ACLK
SMCLK
External signalseg. sensors,
events
Clock Signals
Outputs
TriggerExternal/internal event trigger an
timer interrupt and record current counter value
Timer Interval•when counts to a certain value, generate an interrupt
PWM output•generate pulse width modulation (PWM)
2010/10/14Wireless Sensor Network And Labs fall 2010 20
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Its frequency changed by temperature and supply voltage• Temperature drift = -0.38 %/oC• Vcc Variation = 5 %/V(msp430f1611 datasheet)
Clock SignalsACLK (Watch Crystal 32.768KHz)
fixed rateMuch accurate timingSlow startup (mS)
SMCLK (DCO)Control its frequency in softwareless accurateFast startup (< 6 μS)
External signalsAny devices that can generate
2010/10/14Wireless Sensor Network And Labs fall 2010 21
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Counter16-bit counter register TAR
Increments/decrements with each rising edge of the clock signal
4 operating modesStop Up – counts to TACCR0Continuous – counts to
0xFFFF (65535)Up/down – counts to
TACCR0 and back to zero2010/10/14
Wireless Sensor Network And Labs fall 2010 22
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Timer_A Control RegisterExternal clock sources
If set, an interrupt is generated when
timer resets to 0x0000 from any
other value. (Overflow)
There are many other interrupts that can be generate
2010/10/14Wireless Sensor Network And Labs fall 2010 23
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Capture/CompareCapture
Catch an internal/external eventRecord the counter value to register (TACCRx)Generate an interrupt
CompareSet a value in TACCRxWhen counter value (TAR) = TACCRx
Generate an interrupt Set/reset/toggle an output signal
Capture/compare register
2010/10/14Wireless Sensor Network And Labs fall 2010 24
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Usage of Capture ModeRecord time event
Speed computationsTime measurements
Example: Timer source = 32.768KHz; Continuous Mode TAR increment every 1/32768 second
TAR (counter)
Events
TACCRx = 15000
TACCRx = 60000
t1 = (60000-15000) * (1/32768) seconds = 1.373 second
2010/10/14Wireless Sensor Network And Labs fall 2010 25
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Can We Use GPIO Interrupt
2010/10/14Wireless Sensor Network And Labs fall 2010 26
TAR (counter)
EventsTimer CaptureTACCRx =
15000Generate interrupt
Timer_A ISR {Read
TACCRx }TACCRx =
15000
TAR (counter)
Events
P1.1 Interrupt
ISR { Read TAR }
TAR = 15100
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Usage of Compare ModeUsage
Interrupts at specific time intervals.Generate PWM output signals
Example: flash a LED every secondTimer source = 32.768KHz; Up ModeSet TACCR0 to 32767flash LED in the Timer_A0 ISR
Interrupts2010/10/14
Wireless Sensor Network And Labs fall 2010 27
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Usage of Compare ModeExample: flash a LED every ½ second, flash
another every 1.25 secondsTimer source = 32.768KHz; Continuous
ModeSet TACCR1 = 16383; TACCR2 = 40959
TACCR1TACCR2
In ISR TACCR1 += 16384
In ISR TACCR2 += 40960
Overflow TACCR2 += 40960 > 65535 TACCR2 = 40959 + 40960; TACCR2 = 16383;2010/10/14
Wireless Sensor Network And Labs fall 2010 28
Network and Systems Laboratorynslab.ee.ntu.edu.tw
NotesContinuous Mode
Useful for generating multiple independent time intervals
Time intervals can be produced with other modesTACCR0 is used as the period registerOverflow handling is more complex
2010/10/14Wireless Sensor Network And Labs fall 2010 29
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Timer Output7 output modesControl by TACCR0 and TACCRx
(Action 1)/(Action 2)
Counts to TACCRx, perform
(Action 1)
Counts to TACCR0,
perform (Action 2)
2010/10/14Wireless Sensor Network And Labs fall 2010 30
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Where Are The Outputs
Check device datasheet
2010/10/14Wireless Sensor Network And Labs fall 2010 31
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Timer InterruptsInterrupt sources
Timer_A3 has 4 interrupt sourcesTimer_B7 has 8 interrupt sources
Interrupt vectorsThere are two interrupt vectors for each timer(TA/TB)CCR0 interrupt vector for (TA/TB)CCR0 CCIFGTAIV interrupt vector for all other CCIFG flags and
TAIFGInterrupt flags
TACCR0 CCIFG flag is automatically reset when the TACCR0 interrupt request is serviced
Any access, read or write, of the TAIV register automatically resets the highest pending interrupt flag2010/10/14
Wireless Sensor Network And Labs fall 2010 32
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Capture/Compare Control RegisterTACCTLx
We use synchronous capture
second capture was performed before the value from the first capture was read
Where is the capture value?
2010/10/14Wireless Sensor Network And Labs fall 2010 33
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Robot CarServo motors
Robot Power (Vcc)
RedRobot Ground
(GND) Black
Robot SignalWhite Battery
GroundBlack Battery
PowerRed
2010/10/14Wireless Sensor Network And Labs fall 2010 34
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Pulse Width Modulation
Pulse Width Modulation (PWM)varying the pulse width
Usage of PWMControl motor, telecommunication, voltage
regulation, and etc.
Pulse
Pulse width
Period
2010/10/14Wireless Sensor Network And Labs fall 2010 35
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Pulse
Pulse width
Period ≈ 20 ms
Servo MotorA PWM input controls it angular position
Pulse width = 1.5 ms; position = 90o (neutral)Example
pulse width = 1.25 ms; position = 0o
pulse width = 1.75 ms; position = 180o
Varies between brands and models
The servo motor we used is Continuous Rotation modelOther models will just move to the
programmed position and stop
2010/10/14Wireless Sensor Network And Labs fall 2010 36
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Control Servo Motor
The servo motors we used are 1.5 ms neutralIf pulse width = 1.5 ms stopIf pulse width > 1.5 ms rotate in one directionIf pulse width < 1.5 ms rotate in another
direction
Pulse
Pulse width
Period ≈ 20 ms
2010/10/14Wireless Sensor Network And Labs fall 2010 37
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Generate PWMTwo ways
Timer + GPIO Set/reset a GPIO pin inside timer ISR Controlled by software, need extra CPU cycles
Timer output Use one of the timer output mode Totally controlled by hardware
No interrupt required
2010/10/14Wireless Sensor Network And Labs fall 2010 38
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Timer + GPIO Use two timer interrupts to generate PWM
Choose a GPIO pin to generate PWM control signal When TACCR0 generate interrupt, set this pin (period) When TACCRx generate interrupt, reset this pin (pulse width)
Pulse
Pulse width
Period ≈ 20 ms
2010/10/14Wireless Sensor Network And Labs fall 2010 39
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Today’s Labs1. MSP430 Clock system
Use DCO as MCLK clock sourceUse Lab1_1 program file, changing DCO frequency
Max frequency Approximate 1 MHz
Observe the LED flash in different rate
MCLK is used for CPU, When the speed of MCLK increase, this while loop will end faster
2010/10/14Wireless Sensor Network And Labs fall 2010 40
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Today’s Labs2. Flash a LED every second (sample file on website)
1. You can use Timer_A3 or Timer_B72. Read user guide, find out related timer registers3. Generate an interrupt every second, flash a LED in
the ISR4. For registers setting
1. Hexadecimal 0x12342. Bit definitions
Check TI code examples to get some ideals http://www-s.ti.com/sc/techzip/slac015.zip
TASSEL_0TASSEL_1TASSEL_2TASSEL_3
2010/10/14Wireless Sensor Network And Labs fall 2010 41
Network and Systems Laboratorynslab.ee.ntu.edu.tw
Today’s Labs3. Generate multiple time interval
1. Flash a LED every second, flash second LED every 1.5 seconds, flash third LED every 2.5 second
2. If you need another interrupts1. add another ISR by yourself
3. What mode should timer operate? Up mode? Continuous mode?
4. Control the servo motor on the robot car1. Move forward, move backward
2010/10/14Wireless Sensor Network And Labs fall 2010 42