16 #ifdef __MSP430_HAS_TxB7__
21 void Timer_B_startCounter ( uint16_t baseAddress,
25 HWREG16(baseAddress + OFS_TBxCTL) |= timerMode;
28 void Timer_B_initContinuousMode(uint16_t baseAddress,
29 Timer_B_initContinuousModeParam *
param)
33 OFS_TBxCTL) &= ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
36 TIMER_B_TBIE_INTERRUPT_ENABLE +
40 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
42 HWREG16(baseAddress + OFS_TBxEX0) |=
param->clockSourceDivider&0x7;
44 HWREG16(baseAddress + OFS_TBxCTL) |= (
param->clockSource +
46 param->timerInterruptEnable_TBIE +
47 ((
param->clockSourceDivider>>3)<<6));
49 if(
param->startTimer) {
50 HWREG16(baseAddress + OFS_TBxCTL) |= TIMER_B_CONTINUOUS_MODE;
54 void Timer_B_initUpMode (uint16_t baseAddress,
55 Timer_B_initUpModeParam *
param)
58 HWREG16(baseAddress + OFS_TBxCTL) &=
59 ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
62 TIMER_B_TBIE_INTERRUPT_ENABLE +
65 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
67 HWREG16(baseAddress + OFS_TBxEX0) |=
param->clockSourceDivider&0x7;
69 HWREG16(baseAddress + OFS_TBxCTL) |= (
param->clockSource +
71 param->timerInterruptEnable_TBIE +
72 ((
param->clockSourceDivider>>3)<<6));
74 if (
param->startTimer) {
75 HWREG16(baseAddress + OFS_TBxCTL) |= TIMER_B_UP_MODE;
78 if (TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE ==
79 param->captureCompareInterruptEnable_CCR0_CCIE){
80 HWREG16(baseAddress + OFS_TBxCCTL0) |= TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
82 HWREG16(baseAddress + OFS_TBxCCTL0) &= ~TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
88 void Timer_B_initUpDownMode(uint16_t baseAddress,
89 Timer_B_initUpDownModeParam *
param)
91 HWREG16(baseAddress + OFS_TBxCTL) &=
92 ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
95 TIMER_B_TBIE_INTERRUPT_ENABLE +
98 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
100 HWREG16(baseAddress + OFS_TBxEX0) |=
param->clockSourceDivider&0x7;
102 HWREG16(baseAddress + OFS_TBxCTL) |= (
param->clockSource +
105 param->timerInterruptEnable_TBIE +
106 ((
param->clockSourceDivider>>3)<<6));
108 if (
param->startTimer) {
109 HWREG16(baseAddress + OFS_TBxCTL) |= TIMER_B_UPDOWN_MODE;
112 if (TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE ==
113 param->captureCompareInterruptEnable_CCR0_CCIE){
114 HWREG16(baseAddress + OFS_TBxCCTL0) |= TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
116 HWREG16(baseAddress + OFS_TBxCCTL0) &= ~TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
119 HWREG16(baseAddress + OFS_TBxCCR0) =
param->timerPeriod;
122 void Timer_B_initCaptureMode(uint16_t baseAddress,
123 Timer_B_initCaptureModeParam *
param)
129 ~(TIMER_B_CAPTUREMODE_RISING_AND_FALLING_EDGE +
130 TIMER_B_CAPTURE_INPUTSELECT_Vcc +
131 TIMER_B_CAPTURE_SYNCHRONOUS +
133 TIMER_B_TBIE_INTERRUPT_ENABLE +
138 param->captureInputSelect +
139 param->synchronizeCaptureSource +
140 param->captureInterruptEnable +
141 param->captureOutputMode
145 void Timer_B_initCompareMode(uint16_t baseAddress,
146 Timer_B_initCompareModeParam *
param)
152 ~(TIMER_B_CAPTURECOMPARE_INTERRUPT_ENABLE +
153 TIMER_B_OUTPUTMODE_RESET_SET
156 HWREG16(baseAddress +
param->compareRegister) |= (
param->compareInterruptEnable +
157 param->compareOutputMode
163 void Timer_B_enableInterrupt (uint16_t baseAddress)
165 HWREG16(baseAddress + OFS_TBxCTL) |= TBIE;
168 void Timer_B_disableInterrupt (uint16_t baseAddress)
170 HWREG16(baseAddress + OFS_TBxCTL) &= ~TBIE;
173 uint32_t Timer_B_getInterruptStatus (uint16_t baseAddress)
175 return (
HWREG16(baseAddress + OFS_TBxCTL) & TBIFG );
178 void Timer_B_enableCaptureCompareInterrupt (uint16_t baseAddress,
179 uint16_t captureCompareRegister
182 HWREG16(baseAddress + captureCompareRegister) |= CCIE;
185 void Timer_B_disableCaptureCompareInterrupt (uint16_t baseAddress,
186 uint16_t captureCompareRegister
189 HWREG16(baseAddress + captureCompareRegister) &= ~CCIE;
192 uint32_t Timer_B_getCaptureCompareInterruptStatus (uint16_t baseAddress,
193 uint16_t captureCompareRegister,
197 return (
HWREG16(baseAddress + captureCompareRegister) & mask );
200 void Timer_B_clear (uint16_t baseAddress)
202 HWREG16(baseAddress + OFS_TBxCTL) |= TBCLR;
205 uint8_t Timer_B_getSynchronizedCaptureCompareInput
206 (uint16_t baseAddress,
207 uint16_t captureCompareRegister,
208 uint16_t
synchronized
211 if (
HWREG16(baseAddress + captureCompareRegister) &
synchronized){
212 return ( TIMER_B_CAPTURECOMPARE_INPUT_HIGH) ;
214 return ( TIMER_B_CAPTURECOMPARE_INPUT_LOW) ;
218 uint8_t Timer_B_getOutputForOutputModeOutBitValue
219 (uint16_t baseAddress,
220 uint16_t captureCompareRegister
224 if (
HWREG16(baseAddress + captureCompareRegister) & OUT){
225 return ( TIMER_B_OUTPUTMODE_OUTBITVALUE_HIGH) ;
227 return ( TIMER_B_OUTPUTMODE_OUTBITVALUE_LOW) ;
231 uint16_t Timer_B_getCaptureCompareCount
232 (uint16_t baseAddress,
233 uint16_t captureCompareRegister
236 return (
HWREG16(baseAddress + OFS_TBxR + captureCompareRegister));
239 void Timer_B_setOutputForOutputModeOutBitValue
240 (uint16_t baseAddress,
241 uint16_t captureCompareRegister,
242 uint16_t outputModeOutBitValue
245 HWREG16(baseAddress + captureCompareRegister) &= ~OUT;
246 HWREG16(baseAddress + captureCompareRegister) |= outputModeOutBitValue;
249 void Timer_B_outputPWM(uint16_t baseAddress, Timer_B_outputPWMParam *
param)
252 HWREG16(baseAddress + OFS_TBxCTL) &=
253 ~( TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
254 TIMER_B_UPDOWN_MODE + TIMER_B_DO_CLEAR +
255 TIMER_B_TBIE_INTERRUPT_ENABLE
257 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
259 HWREG16(baseAddress + OFS_TBxEX0) |=
param->clockSourceDivider&0x7;
261 HWREG16(baseAddress + OFS_TBxCTL) |= (
param->clockSource +
264 ((
param->clockSourceDivider>>3)<<6));
266 HWREG16(baseAddress + OFS_TBxCCR0) =
param->timerPeriod;
268 HWREG16(baseAddress + OFS_TBxCCTL0) &=
269 ~(TIMER_B_CAPTURECOMPARE_INTERRUPT_ENABLE +
270 TIMER_B_OUTPUTMODE_RESET_SET
278 void Timer_B_stop ( uint16_t baseAddress )
280 HWREG16(baseAddress + OFS_TBxCTL) &= ~MC_3;
284 void Timer_B_setCompareValue ( uint16_t baseAddress,
285 uint16_t compareRegister,
286 uint16_t compareValue
289 HWREG16(baseAddress + compareRegister + OFS_TBxR) = compareValue;
292 void Timer_B_clearTimerInterrupt (uint16_t baseAddress)
294 HWREG16(baseAddress + OFS_TBxCTL) &= ~TBIFG;
297 void Timer_B_clearCaptureCompareInterrupt (uint16_t baseAddress,
298 uint16_t captureCompareRegister
301 HWREG16(baseAddress + captureCompareRegister) &= ~CCIFG;
304 void Timer_B_selectCounterLength (uint16_t baseAddress,
305 uint16_t counterLength
308 HWREG16(baseAddress + OFS_TBxCTL) &= ~CNTL_3;
309 HWREG16(baseAddress + OFS_TBxCTL) |= counterLength;
312 void Timer_B_selectLatchingGroup(uint16_t baseAddress,
315 HWREG16(baseAddress + OFS_TBxCTL) &= ~TBCLGRP_3;
316 HWREG16(baseAddress + OFS_TBxCTL) |= groupLatch;
319 void Timer_B_initCompareLatchLoadEvent(uint16_t baseAddress,
320 uint16_t compareRegister,
321 uint16_t compareLatchLoadEvent
324 HWREG16(baseAddress + compareRegister) &= ~CLLD_3;
325 HWREG16(baseAddress + compareRegister) |= compareLatchLoadEvent;
328 uint16_t Timer_B_getCounterValue (uint16_t baseAddress)
330 uint16_t voteOne, voteTwo, res;
332 voteTwo =
HWREG16(baseAddress + OFS_TBxR);
337 voteTwo =
HWREG16(baseAddress + OFS_TBxR);
339 if(voteTwo > voteOne) {
340 res = voteTwo - voteOne;
341 }
else if(voteOne > voteTwo) {
342 res = voteOne - voteTwo;
347 }
while ( res > TIMER_B_THRESHOLD);
352 void Timer_B_setOutputMode(uint16_t baseAddress,
353 uint16_t compareRegister,
354 uint16_t compareOutputMode)
356 uint16_t
temp =
HWREG16(baseAddress + compareRegister);
357 HWREG16(baseAddress + compareRegister) =
temp & ~(OUTMOD_7) | compareOutputMode;
MPU_initThreeSegmentsParam param