0% found this document useful (0 votes)
155 views

Buzzer Song

This document contains C code that defines constants for musical notes, initializes variables to store note values and an index, and implements interrupt service routines to play notes on a timer and change the volume when buttons are pressed. The main function configures ports and timers to generate tones on a buzzer based on the note values, toggling LEDs on a timer interrupt and changing the volume or note on a button press interrupt.

Uploaded by

elad kotlovski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Buzzer Song

This document contains C code that defines constants for musical notes, initializes variables to store note values and an index, and implements interrupt service routines to play notes on a timer and change the volume when buttons are pressed. The main function configures ports and timers to generate tones on a buzzer based on the note values, toggling LEDs on a timer interrupt and changing the volume or note on a button press interrupt.

Uploaded by

elad kotlovski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <msp430xG46x.

h>
#include <msp430.h>

#define notas 25
#define SI0 15895
#define DO 15258
#define RE 13620
#define MI 12077
#define FA 11405
#define SOL 10159
#define LA 9106
#define SI 7963
#define DO2 7629
#define MUTE 0

//******************************************************************
// Global data
//******************************************************************
//unsigned int scale[notas] = {SI0, DO, RE, MI, FA, SOL, LA, SI, DO2, MUTE};
unsigned int space[notas] =
{DO,RE,MI,DO,MI,DO,RE,MI,FA,MI,RE,FA,MI,FA,SOL,MI,SOL,MI,SOL,FA,SOL,LA,SOL,FA,LA};
unsigned int index_notas = 0;
char volume = 2;
//******************************************************************
// Port1 Interrupt Service Routine
//******************************************************************
#pragma vector=PORT1_VECTOR
__interrupt void Port_1 (void)
{
if (P1IFG & 0x01) volume = volume - 1;
if (P1IFG & 0x02) volume = volume + 1;

if (volume == 6) volume = 5;
if (volume == 0) volume = 1;

TBCCR4 = space[index_notas]/volume;

P1IFG &= ~0x01; //Clean P1.0 Interrupt Flag


}
//*****************************************************************
// Basic Timer Interrupt Service Routine. Run with 1 sec period
//*****************************************************************
#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer_ISR(void)
{
// Toogle LED1 and LED2
P2OUT ^=0x06;

// get next note


TBCCR0 = space[index_notas];
// set duty-cycle
TBCCR4 = space[index_notas]/volume;

// manage note point


index_notas++;
if (index_notas == notas)
index_notas = 0;
}
//******************************************************************
// Main routine
//******************************************************************
void main (void)
{

WDTCTL = WDTPW | WDTHOLD; //Stop Watchdog Timer

//FLL+ configuration, ACLK = 32.768 kHz


FLL_CTL0 |= DCOPLUS + XCAP18PF; // DCO+ set
// fDCOCLK = 3.2-25Mhz
SCFI0 |= FN_4; // x2 DCO freq, 8MHz nominal DCO
// freq = xtal x D x N+1
SCFQCTL = 80; // (121+1) x 32768 x 2 = 7.99 MHz

// TimerB configuration
TBCCR0 = space[0]; // load first tone from
sequency
TBR = 0; // reset TBR
// SMCLK
// 16-bit, TBR(max) = 0FFFFh
// Each TBCLx latch loads independently
// Up mode: the timer counts up to TBCCR0
// Input divider. These bits select the divider for the input clock.(/1)
TBCTL = 0x210;
// output mode set/reset
TBCCTL4 = 0x60;
TBCCR4 = space[0]/2;

// Basic Timer 1 Configuration (1 sec)


// Basic Timer1 clock divide
// Basic Timer1 interrupt interval, fCLK2/128
BTCTL = 0x3E;
// Enable BT interrupt
IE2 |= 0x80;

// SW1 and SW2 configuration (Port1)


P1SEL = 0x31; // P1.0 and P1.2 digital I/O
P1DIR = 0x31; // P1.0 and P1.2 inputs*/
P1IFG = 0x00; // clear P1 flags
P1IES = 0x00; // high-to-low
transition interrupts
P1IE = 0x00; // Enable port interrupts*/

// LED1 and LED2 configuration (Port2)


P2DIR |= 0x06; // P2.2 and P2.1 as digital output
P2OUT = 0x06; // LED1 on and LED2 off

// Buzzer port configuration (Port3)


P3SEL |= 0x20; // P3.5 as special function
P3DIR |= 0x20; // P3.5 as digital output

// Interrupts enabled
__enable_interrupt();
while(1);
}

You might also like