Hello! I am working on a project where I have to enable a timer that counts up every tenth of a second and outputs my times in binary. Independently, the timer itself works perfectly. However, I want to add a function to use my internal board switches (P1.4 and P1.1) to stop and start my timer, respectively. In any case, I am working on a MSP432P401R board. Thank you beforehand! My code:
#include "msp.h"
int sec = 0, tensec = 0, min = 0; //time stuff
int A = 1;
void Initiate(){
//internal switches -- negative logic, lazy to do outside stuff
//1.1 -- start related stuff
//1.4 -- stop relted stuff
P1->DIR &= ~0x12;
P1->REN |= 0x12;
P1->OUT |= 0x12;
//asking for start LED
P1->DIR |= BIT0;
P1->OUT &= ~BIT0;
//lights for status -- onboard
P2->DIR |= 0x07;
P2->OUT &= ~0x07;
//minute lights
P5->DIR |= 0x03;
P5->OUT &= ~0x03;
//seconds lights -- 6 LEDs
P4->DIR |= 0x3F;
P4->OUT &= ~0x3F;
//tens of sec
P6->DIR |= BIT0;
P6->OUT &= ~BIT0;
}
int main(void) {
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // Stop WDT
// Configure GPIO
Initiate();
// Enable & configure the SysTick Timer Module
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
// SysTick->LOAD = 0x60000 - 1; // Period = 0x60000
SysTick->LOAD = 300000; // Period = tenth of a second
SysTick->VAL = 0x01; // Clear value by writing any value
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable interrupt
// Enable global interrupt
__enable_irq();
SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk; // Sleep on exit from ISR
__DSB(); // Ensure SLEEPONEXIT takes effect immediately
while (1)
__sleep();
} // End of main
// Interrupt service routine (ISR) invoked when SysTick down counter reaches 0.
void SysTick_Handler(void)
{
A = P1->IN & BIT1;
if((!A)){ //pressing the switch will make it pause
}else{
P6->OUT ^= BIT0; // Toggle tens of sec light
tensec++;
if(tensec == 10){
sec++;
P4->OUT &=~0x3F; //refresh seconds
P4->OUT |= sec;
tensec = 0; //restart it
if(sec == 60){
sec = 0; //restart secs
min++;
P5->OUT &= ~0x3F; //refresh
P5->OUT |= min;
}
}
}
}