1518 views|0 replies

1140

Posts

0

Resources
The OP
 

MSP430 implements 800Hz buzzer and stopwatch [Copy link]

It’s just a simple course assignment, and I think the code I wrote is okay!

#include <msp430.h>
#include <stdint.h>
#include "dr_lcdseg.h" //Call the segment LCD driver header file

int second = 0, secSum = 0, minSum = 0;

int count = 0;

void GPIO_init();

//Button S7 is reset
void P40_Onclick()
{
second = 0;
secSum = 0;
minSum = 0;
}
//Button S5 is start
void P42_Onclick()
{
TA0CTL |= MC_1;
}

//Button S3 is for pause
void P44_Onclick()
{
TA0CTL &= ~MC_1;
}

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Turn off the watchdog
P1DIR |= BIT5; //Control the buzzer output
P4DIR |= BIT5; //Control the LED output

initLcdSeg(); //Initialize segment LCD
TA0CTL |= MC_1 + TASSEL_2 + TACLR;
GPIO_init(); //IO port initialization
//Clock is SMCLK, comparison mode, clear counter at the beginning
TA0CCTL0 = CCIE; //Comparator interrupt enable
TA0CCR0 = 1250; //Comparison value is set to 1250, equivalent to 1/800s time interval
__bis_SR_register(GIE); //Enter low power consumption and enable total interrupt
while (1)
{
LCDSEG_SetDigit(1, secSum % 10);
LCDSEG_SetDigit(2, secSum / 10);
//The fourth digit displays a horizontal line
LCDSEG_SetDigit(3, 16);
LCDSEG_SetDigit(4, minSum % 10);
LCDSEG_SetDigit(5, minSum / 10);
}
}

//Interrupt function of TimerA
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
second++;
//After 20 interruptions, it is 1s
if (second == 800)
{
//Seconds plus 1
secSum++;
//When the seconds is 60, minutes plus 1
if (secSum == 60)
{
secSum = 0;
minSum++;
}
second = 0;
}

count++;
if (count == 1600)
{
P1OUT ^= BIT5; //Beep effect
P4OUT ^= BIT5; //Flash light effect
}
if (count > 3200)
{
P1OUT ^= BIT5; //Turn off the buzzer
P4OUT ^= BIT5; //Light off
count = 0;
}
}

// P4 interrupt function
#pragma vector=PORT4_VECTOR
__interrupt void Port_4(void)
{
_DINT();
unsigned int Push_Key = 0;
//----After eliminating the interference of output IO, lock the only interrupt flag bit that is triggered----
Push_Key = P4IFG & (~P4DIR);
//----Delay for a period of time to avoid the mechanical jitter area----
__delay_cycles(327); //Delay in ms

if (P4IFG != 0)
{
//----Judge which IO is pressed and call the event processing function of the IO
switch (Push_Key)
//Code 4: Do not use P1IN to judge to ensure that there is only 1 key response
{
case BIT0: //Key S7
P40_Onclick();
break;
case BIT2: //Key S5
P42_Onclick();
break;
case BIT4: //Key S3
P44_Onclick();
break;
default:
break; //In any case
}
}
P4IFG = 0; //The IO port interrupt flag must be manually cleared before exiting the interrupt
}

void GPIO_init()
{
//----Enable internal pull-up resistors with mechanical buttons----
//Enable internal pull-up and pull-down resistors on P4.0, P4.2, and P4.4
P4REN |= BIT0 + BIT2 + BIT4;
P4OUT |= BIT0 + BIT2 + BIT4; //Set the resistor to pull-up

//----Configure P4.0, P4.2, P4.4 interrupt parameters----
P4DIR &= ~(BIT0 + BIT2 + BIT4); //Set the button as input
P1IES |= BIT0 + BIT2 + BIT4; //Set P4.0, P4.2, P4.4 as falling edge interrupt
P4IE |= BIT0 + BIT2 + BIT4; //Enable P4.0, P4.2, P4.4 interrupts
}

This post is from Microcontroller MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list