초보의 아웅다웅 설계하기/MSP 430

MSP430 - 부져음 내기

로망와니 2010. 11. 6. 22:31

#include "MSP430X44X.H"      // Chip definitions for msp430F449.
#include <STRING.H>       // for some string functions.
#include <STDLIB.H>       // srand(), rand().


#define Pitch_do 125
#define Pitch_re 111
#define Pitch_mi 99
#define Pitch_fa 93
#define Pitch_sol 83
#define Pitch_ra 74
#define Pitch_si 66
#define Pitch_h_do 63
//A3 옥타브 기준
/*
#define Pitch_do 63
#define Pitch_re 55
#define Pitch_mi 49
#define Pitch_fa 46
#define Pitch_sol 41
#define Pitch_ra 37
#define Pitch_si 33
#define Pitch_h_do 31
*/ //A4 옥타브 기준

//음높이 [pitch, 音高]  도 125, 레 111, 미 99, 파 93, 솔 83, 라 74, 시 66, 도 63

void Beep( unsigned int Pitch );
void DelayLoop( unsigned int awDelay );

void main( void )
{
 _DINT();                                         // Diseable INT
 WDTCTL = WDTPW + WDTHOLD;   // Stop watchdog timer(better stop this fellow!)  
       
 P1SEL = 0x7C;       // Set I/O P1.0, P1.1, P1.7. 0111 1100
 P1DIR = 0x03;       // Set P1.0, P1.1 output and P1.7 input.

 P1OUT = 0x00;
       
        for( ; ; )
        {
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_ra);
            DelayLoop(600);
            Beep(Pitch_ra);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_mi);
            DelayLoop(1000);
           
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_mi);
            DelayLoop(600);
            Beep(Pitch_re);
            DelayLoop(600);
            Beep(Pitch_mi);
            DelayLoop(600);
            Beep(Pitch_do);
            DelayLoop(1000);

            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_ra);
            DelayLoop(600);
            Beep(Pitch_ra);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_mi);
            DelayLoop(1000);
           
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_sol);
            DelayLoop(600);
            Beep(Pitch_mi);
            DelayLoop(600);
            Beep(Pitch_mi);
            DelayLoop(600);
            Beep(Pitch_re);
            DelayLoop(1000);
          

            DelayLoop(10000);
        }

       
}


void Beep( unsigned int Pitch )//P1.0 Buzzer, P1.1 OPAMP_EN
{
 volatile int viDummyCnt = 0;
 int iIndexIn, iIndexOut;

//        음높이 [pitch, 音高]  도 125, 레 111, 미 99, 파 93, 솔 83, 라 74, 시 66, 도 63
        for( iIndexOut = 0; iIndexOut < 500; iIndexOut++ )//500은 음계 길이
 {
  P1OUT = 0x01;
  for( iIndexIn = 0; iIndexIn < Pitch; iIndexIn++ )// BEEP_DELAY_NORMAL 22, BEEP_DELAY_SETUP 15
  {
   viDummyCnt++;
  }
  P1OUT = 0x00;
  for( iIndexIn = 0; iIndexIn < Pitch; iIndexIn++ )
  {
   viDummyCnt++;
  }
 }
}

void DelayLoop( unsigned int awDelay )
{
 volatile int viDelay;
 volatile int viDummy;
 
 for( viDelay = 0; viDelay < awDelay; viDelay++ )
 {
  viDummy = viDelay;
 }
}

//X - tal32.768kHz
최적화나 프로그램 용량과 상관없이 대충의 시간만 계산하여 학교종이 땡땡땡을 만들었습니다.
음높이만 대충 맞추고 음 길이를 맞추지 않았기때문에 낮은음으로 갈수록 늘어지게 됩니다.
단음 중 부져가 낼 수 있는 최적화 주파수를 찾으려고 만들다 보니 하게 된 거라 실제 연주음으로 사용하려면 조금 더 다듬어야 합니다.