MSP430 uses key interrupt to control the timer to generate a periodic signal lasting 1s[Copy link]
/// ... //================================================================================================================= // The delay generation system used in the competition is composed of a 555 timer and a monostable circuit and a delay circuit. The effect is OK. Due to device errors, // it is difficult to adjust the frequency to 800Hz and stabilize it. (Refer to Kang Huaguang Page 428, Exercise 8.4.4) // After the competition, I used TI's MSP430 Launchpad to redo it, and added a simple amplifier circuit. The program is as follows. // Originally, I wanted to use the PWM function for output, but I am not familiar with it yet, so I have to explore it slowly. #include
#include
// Define alias typedef unsigned char INT8U; typedef unsigned int INT16U; volatile INT16U i = 0; // Variables referenced in interrupts are best defined as volatile type. /// ... Select SMCLK as the clock source of timer A, and increase the counting mode P1DIR |= (BIT0 | BIT6); // Set P1 to output P1OUT = (BIT0 | BIT3); // At the beginning, LED1 is on, LED2 is off, and the pull-up mode is selected P2DIR |= BIT2; // Set the direction of P2.2 port to output P2OUT &= ~BIT2; // P2.2 outputs low level, so that the initial state buzzer does not sound P1REN |= BIT3; // Enable the pull-up resistor of P13 port P1IE |= BIT3; // P1.3 interrupt enable //P1IES &= ~BIT3; // Rising edge triggers interrupt P1IES |= BIT3; // Falling edge triggers interrupt, you can compare the difference with the rising edge P1IFG = 0x00; // Clear interrupt flag _EINT(); // Enable global interrupt LPM1; // CPU enters LPM1 mode, LPM0 Also for (;;) { _NOP(); // Enter an infinite loop, waiting for an interrupt to occur} } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Interrupt service function of timer A, where the pin status is changed regularly #pragma vector = TIMER0_A0_VECTOR __interrupt void Timer_A_Handler (void) { P2OUT ^= BIT2; if (++i > 1600) { // 1s time is up, stop and wait for the next key press to start CCTL0 &= ~CCIE; // Disable CCR0 interrupt, wait for the next key press to re-enable P2OUT &= ~BIT2; // After 1s, P2.2 will output a low level to stop the buzzer i = 0; // i is cleared, waiting for the next counting } } /// ... ^= (BIT0 | BIT6); // Indicates that the key change inverts P1.0/6, alternately turns on and off P1IFG &= ~BIT3; // Clear P1.3 interrupt flag } /// ... SMCLK remain active, MCLK is disabled – DCO's dc generator is disabled if DCO not used in active mode ************************************************************************************************