Flipklok
 
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1/*
2 * timer.c
3 *
4 * Created on: 20 mrt. 2025
5 */
10
11#include "led.h"
12#include "timer.h"
13#include "adc.h"
14#include "timeKeeping.h"
15#include "pinInterrupts.h"
16
17
18//------------------------------------------Timer0---------------------------------------------------
19
27{
28 //Timer setup
29 TA0CTL = TASSEL_1 | MC_2| TACLR | TAIE | ID_0; // Set to counter to ACLK, Continuous clock, clear counter of clock, Enable TAIFG interrupt, Divider set to 1
30 TA0CCTL1 = CCIE; // TACCR1 interrupt enabled
31 TA0CCTL2 &= ~CCIE; // TACCR2 interrupt disable
32 TA0CCR2 = stepperTimeOffsetSlow; // compare at stepperOffset
33 TA0CCR1 = pwmPeriod_cycles;
34}
35
37{
38 TA0CTL = TASSEL_1 | MC_2 | TAIE | ID_3; // Set to counter to ACLK, Continuous clock, Enable TAIFG interrupt, Divider set to 8
39 TA0CCTL1 &= ~CCIE; // TACCR1 interrupt disable
40 TA0CCTL2 &= ~CCIE; // TACCR2 interrupt disable
41}
42
44{
45 TA0CTL = TASSEL_1 | MC_2 | TAIE | ID_0; // Set to counter to ACLK, Continuous clock, Enable TAIFG interrupt, Divider set to 1
46 TA0CCTL1 |= CCIE; // TACCR1 interrupt enable
47}
48
50// CCR0 uses TIMER0_A0_VECTOR rest of capture registers uses TIMER0_A1_VECTOR
51#pragma vector=TIMER0_A0_VECTOR
52__interrupt void ISR_TA0_CCR0(void)
53{
54 TA0CCTL0 &= ~CCIFG; // Clear CCR0 interrupt flag
55 //Not used yet
56}
57
59#pragma vector=TIMER0_A1_VECTOR
60__interrupt void ISR_TA0(void)
61{
62 static unsigned int indicatorCount;
63 static unsigned int togglePwm;
64 switch(__even_in_range(TA0IV, 0x14 ))
65 {
66 case 0x00: break; // None
67 case 0x02: // CCR1 IFG used for PWM LED
68 if (0 == togglePwm)
69 {
70 TA0CCR1 = TA0R + pwmOnCycles;
71 P1OUT |= BIT6; //turn on
72// P1OUT |= BIT7;
73 togglePwm = 1;
74 }
75 else
76 {
77 TA0CCR1 = TA0R + pwmPeriod_cycles;
78 P1OUT &= ~BIT6; //turn off
79// P1OUT &= ~BIT7;
80 togglePwm = 0;
81
82 }
83 break;
84 case 0x04: // CCR2 IFG stepper
85 if (timerCompareStepperSpeedToggle == 0) //calculate next normal timer capture
86 {
87 TA0CCR2 = TA0R + stepperTimeOffsetSlow;
88 }
89 else //calculate next fast timer capture
90 {
91 TA0CCR2 = TA0R + stepperTimeOffsetFast;
92 }
93 stepperAdvance(); // Call stepper function
94 break;
95 case 0x0E:
97 if (toggleCalculateTimeDifference == 1 && toggleFwdInterrupt == 0) // When the fwdButton is not pressed and there are no more steps to take to get to the previous calculated time calculate new time difference.
98 {
100 }
101 break; // TA0IFG
102 default: _never_executed();
103 }
104}
105
106//------------------------------------------Timer1---------------------------------------------------
107
115{
116 //Timer setup
117 TA1CTL |= MC_0; // stop timer
118 TA1CTL |= TAIE; // setup end of cycle interrupt (up/down mode, so it get triggered when reaching 0)
119 TA1CTL |= TASSEL_1; // Set to counter to ACLK
120 TA1CTL |= TACLR; // Clear counter of clock
121 #warning "When using PCB divide by 1. When using MSP-EXP430G2ET devboard pins on devboard are not connected to LFXTAL internal clock should be used"
122 TA1CTL |= ID_0; // Divider set to 1
123
124 TA1CCR0 |= (capacityTimer - 1); // Every 4 seconds
125 TA1CCTL0 |= CCIE; // TACCR0 interrupt enabled
126// TA1CCTL1 = CCIE; // TACCR1 interrupt enabled
127// TA1CCTL2 = CCIE; // TACCR2 interrupt enabled
129 toggleTimer1Direction = 1; // Counting up
130 TA1CTL |= MC_3; // up/down
131
132}
133
134
135
136
138// CCR0 uses TIMER1_A0_VECTOR rest of capture registers uses TIMER1_A1_VECTOR
139#pragma vector=TIMER1_A0_VECTOR
140__interrupt void ISR_TA1_CCR0(void)
141{
142 TA1CCTL0 &= ~CCIFG; // Clear own CCR0 interrupt flag
143 toggleTimer1Direction = 0; // counting down
144 timeSinceLastCompleteDcfMessage += 4; //Increment counter by 4 seconds
145
147 {
149 }
150}
151
153#pragma vector=TIMER1_A1_VECTOR
154__interrupt void ISR_TA1(void)
155{
156 switch(__even_in_range(TA1IV, 0x0E ))
157 {
158 case 0x00: break; // None
159 case 0x02: break; // CCR1 IFG
160 case 0x04: break; // CCR2 IFG
161 case 0x0E:
162 toggleTimer1Direction = 1; // counting up
163 break; // TA0IFG will be reached when counting down and reaching 0 in up/down mode
164 default: _never_executed();
165 }
166}
void startAdcConv()
Start the ADC conversion'(s), gets called when end interrupt of ISR_TA0(void) is reached.
Definition adc.c:119
unsigned int pwmOnCycles
Definition led.c:15
bool toggleFwdInterrupt
bool timerCompareStepperSpeedToggle
#define stepperTimeOffsetSlow
void stepperAdvance()
#define stepperTimeOffsetFast
uint32_t timeSinceLastCompleteDcfMessage
Definition timeKeeping.h:22
bool toggleCalculateTimeDifference
Definition timeKeeping.c:16
void calculateTimeDifference()
calculate the difference between the mechanical time and the current time to have the time difference...
Definition timeKeeping.c:26
volatile bool toggleTimer1Direction
Definition timeKeeping.h:46
#define timerCountsInDay
Definition timeKeeping.h:23
__interrupt void ISR_TA0(void)
Interrupt vector of timer0 CCR1 till CCR2 and TA0IFG.
Definition timer.c:60
__interrupt void ISR_TA0_CCR0(void)
Interrupt vector of timer0 CCR0.
Definition timer.c:52
void setupTimer1()
Definition timer.c:114
void setupTimer0()
Definition timer.c:26
__interrupt void ISR_TA1_CCR0(void)
Interrupt vector of timer1 CCR0.
Definition timer.c:140
__interrupt void ISR_TA1(void)
Interrupt vector of timer1 CCR1 till CCR2 and TA0IFG.
Definition timer.c:154
void sleepTimer0()
Definition timer.c:36
void wakeUpTimer0()
Definition timer.c:43
#define pwmPeriod_cycles
Definition timer.h:17
#define capacityTimer
Definition timer.h:14