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

MSP-430 플래쉬 메모리 읽기 쓰기

로망와니 2010. 11. 27. 16:24

#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

void Beep( unsigned int Pitch );
void DelayLoop( unsigned int awDelay );
void WordWriteDataToFlash( unsigned int* awpAddress, unsigned int awData );
void EraseDataFromFlash( unsigned int* awpAddress );
void CheckBusyFlash( void );

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.

 P2SEL = 0x30;       // P2.4, P2.5는 UART로 P2.0 - P2.3까지 키스캔으로 사용.
 P2DIR = 0x10;       // P2.4 TX로 output사용 나머지는 input으로 사용.

 P3SEL = 0x00;       // P3 All port I/O.+
 P3DIR = 0xFF;       // P3 All port output.

 P4SEL = 0xFC;                                                   //1111 1100
 P4DIR = 0x03;                                                   //0000 0011

 P6SEL = 0x1F;          
       
 P3OUT = 0xFF;
 P1OUT = 0x00;


WordWriteDataToFlash( (unsigned int*) 0xB200, 0x01 );

if( 0x01 == *((unsigned int*) 0xB200))
{
    Beep(Pitch_do);
}
DelayLoop(10000);
}

if( 0x02 == *((unsigned int*) 0xB200))
{
    Beep(Pitch_sol);
}
DelayLoop(10000);
}

EraseDataFromFlash( (unsigned int*) 0xB200 );
if( 0x01 == *((unsigned int*) 0xB200))
{
    Beep(Pitch_si);
}

DelayLoop(10000);
}


void WordWriteDataToFlash( unsigned int* awpAddress, unsigned int awData )
{
// AllIntDisable();
  CheckBusyFlash();// while( FCTL3 == BUSY );   // Check Busy.
// BUSY                (0x0001)   Flash busy: 1  0000 0000 0000 0001
// FWKEY               (0xA500)   Flash key for write 1010 0101 0000 0000
// FSSEL1              (0x0080)   Flash clock select 1 0000 0000 1000 0000
// FN1                 (0x0002)    32*FN5 + 16*FN4 + 8*FN3 + 4*FN2 + 2*FN1 + FN0 + 1  0000 0000 0000 0010
// WRT                 (0x0040)   Enable bit for Flash write 0000 0000 0100 0000
  FCTL2 = FWKEY | FSSEL1 | FN1; // FN0일때SMCLK / 2.   FN1이면 SMCLK /3
  FCTL3 = FWKEY;     // Clear LOCK.
  FCTL1 = FWKEY | WRT;   // Enable Write.
 
  *awpAddress = awData;

// LOCK                (0x0010)   Lock bit: 1 - Flash is locked (read only) 0000 0000 0001 0000
// ERASE               (0x0002)   Enable bit for Flash segment erase 0000 0000 0000 0010

  CheckBusyFlash();
  FCTL1 = FWKEY;     // Clear WRT;
  FCTL3 = FWKEY | LOCK;   // Set LOCK.

// AllIntEnable();
}


void EraseDataFromFlash( unsigned int* awpAddress )
{
// AllIntDisable();
 CheckBusyFlash();
  FCTL2 = FWKEY | FSSEL1 | FN1; // FN0일때SMCLK / 2.   FN1이면 SMCLK /3
  FCTL3 = FWKEY;     // Clear LOCK.
  FCTL1 = FWKEY | ERASE;   // Enable Erase.
 
  *awpAddress = 0;
 
  CheckBusyFlash();
  FCTL3 = FWKEY | LOCK;   // Set LOCK.

// AllIntEnable();
}

void CheckBusyFlash( void )
{
 while( FCTL3 == BUSY );   // Check Busy.
}


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++ )// 부저를 울리고 OpAmp Enable 비트를 같이 사용한다.//500은 음계 길이
 {
  P1OUT = 0x01;//BUZZER_OUT P1OUT #define BUZZER_PLAY 0x01, BUZZER_STOP 0x00, OPAMP_ENABLE 0x02
  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;
 }
}