could describe what each line of code only understand a few parties.
//code
include <msp430g2231.h>
#define LED1 BIT1
#define LED2 BIT2
#define LED3 BIT3
#define LED4 BIT4
#define LED5 BIT5
#define SLOWERBUTTON BIT6
#define FASTERBUTTON BIT7
#define CALIB_TIME 56 // Calibration constant for exact beat frequency
#define NUM_BEATS 37 // Number of speeds
#define BEEPER BIT0 // Port to connect speaker or LED
#define BEEPERTONE 180 // Beep tone/frequency
#define BEEPERDURATION 100 // Duration of beep
const unsigned int timings[NUM_BEATS] = {1500, 1429, 1364, 1304, 1154, 1111,
1071, 1034, 1000, 952, 909, 870, 833, 789, 750, 714, 682, 652, 625, 600, 577,
556, 536, 517, 500, 476, 455, 435, 417, 395, 375, 357, 341, 326, 313, 300, 288};
static unsigned int index = 13; // Start at middle speed
void delay_ms(unsigned int ms);
void delay_us(unsigned int us);
void delay_cal(unsigned int cycles);
void beep(unsigned int note, long duration);
void ticktack(int index);
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; // Disable Watchdog Timer
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
P1DIR |= (BIT0+BIT1+BIT2+BIT3+BIT4+BIT5); // P1.0-P1.5 output
P1OUT = FASTERBUTTON + SLOWERBUTTON; // P1.6, P1.7 set, else reset
P1REN |= FASTERBUTTON + SLOWERBUTTON; // P1.6, P1.7 internal pullup resistor
P1IE |= FASTERBUTTON + SLOWERBUTTON; // P1.6, P1.7 interrupt enabled
P1IES |= FASTERBUTTON + SLOWERBUTTON; // P1.6, P1.7 Hi/Lo edge
P1IFG &= ~FASTERBUTTON + SLOWERBUTTON; // P1.6, P1.7 IFG cleared
__enable_interrupt(); // enable all interrupts
while(1)
{
ticktack(index); // Do LED0-LED4 tick-tack routine using timing index
}
}
#pragma vector = PORT1_VECTOR
__interrupt void Port_1(void)
{
if(P1IFG & FASTERBUTTON && index < NUM_BEATS-1)
{
index++; // Increases beat freq.
P1OUT &= ~BIT0; // Turn off LED @ P1.0 if max/min freq. reached
}
else if(P1IFG & SLOWERBUTTON && index > 0)
{
index--; // Decreases beat freq.
P1OUT &= ~BIT0; // Turn off LED @ P1.0 if max/min freq. reached
}
if(index != 0 && index != NUM_BEATS-1)
{
delay_cal(1500); //
P1OUT |= BIT0; // Turn on LED @ P1.0 if max/min freq. reached
}
else
{
P1OUT |= BIT0;
}
P1IFG &= ~(FASTERBUTTON+SLOWERBUTTON); // P1.6, P1.7 IFG cleared
}
void delay_ms(unsigned int ms)
{
unsigned int i;
for (i = 0; i<= ms; i++)
__delay_cycles(1000);
}
void delay_us(unsigned int us)
{
unsigned int i;
for (i = 0; i<= us; i++)
__delay_cycles(1);
}
void delay_cal(unsigned int cycles)
{
unsigned int i;
for(i = 0; i <= cycles; i++)
{
__delay_cycles(CALIB_TIME); //Delay with built-in function
}
}
void beep(unsigned int note, long duration) {
long delay = (long)(62500/note); //This is the semiperiod of each note.
long time = (long)((duration*100)/delay); //This is how much time we need to spend on the note.
long i;
for (i = 0; i < time; i++) {
P1OUT |= BEEPER; //Set buzzer on...
delay_us(delay); //...for a semiperiod...
P1OUT &= ~BEEPER; //...then reset it...
delay_us(delay); //...for the other semiperiod.
}
}
void ticktack(int index)
{
unsigned int beat = timings[index]; // Take next/previous timing
P1OUT ^= LED2;
beep(BEEPERTONE, BEEPERDURATION);
P1OUT ^= LED1;
delay_cal(beat*2);
P1OUT ^= LED2;
delay_cal(beat*5);
P1OUT ^= LED2;
delay_cal(beat*2);
P1OUT ^= LED1;
delay_cal(beat*2);
P1OUT ^= LED3;
delay_cal(beat*1);
P1OUT ^= LED2;
delay_cal(beat*1);
P1OUT ^= LED4;
delay_cal(beat*1);
P1OUT ^= LED3;
delay_cal(beat*2);
beep(BEEPERTONE, BEEPERDURATION);
P1OUT ^= LED5;
delay_cal(beat*2);
P1OUT ^= LED4;
delay_cal(beat*5);
P1OUT ^= LED4;
delay_cal(beat*2);
P1OUT ^= LED5;
delay_cal(beat*2);
P1OUT ^= LED3;
delay_cal(beat*1);
P1OUT ^= LED4;
delay_cal(beat*1);
P1OUT ^= LED2;
delay_cal(beat*1);
P1OUT ^= LED3;
delay_cal(beat*2);
P1OUT ^= LED2;
}