晶振XT2=8MHZ,XT1=32768HZ,MCLK=XT2,ACLK=XT1
void Init_CLK()
{
BCSCTL1 &= ~XT2OFF; // XT2= HF XTAL
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--);
}
while ((IFG1 & OFIFG));
BCSCTL2 |= SELM_2;
}
}
//主函数调用:
//WriteFlag在timerA中定时中断扫描中置位,
//_BIC_SR_IRQ(LPM3_bits); 跳出省电模式
//在主函数中初始化后
for(;;)
{
if(WriteFlag)
{
FBufOut[0]=UpPack.ucTimeSecond;
FBufOut[1]=UpPack.ucTimeMinute;
FBufOut[2]=UpPack.ucTimeHour;
WDTCTL=WDTPW+WDTHOLD;
IE1&=~WDTIE;
ClrFlashSeg(0XFF00);
Write_Flash(FBufOut); //然后写入指定的字段
}
_BIS_SR(LPM3_bits); // CPU off,进入LPM0
}
//写flash
void Write_Flash(uchar *p)
{
_DINT();
char *Flash_ptr; // Flash pointer
unsigned int i;
Flash_ptr = (char *) 0xff00; // Initialize Flash pointer
FCTL2 = FWKEY + FSSEL_1 + FN4; // 设置为375hz
FCTL3 = FWKEY;
FCTL1 = FWKEY + WRT;
*Flash_ptr = 0; // Dummy write to erase Flash segment
//FCTL1 = FWKEY + WRT; // Set WRT bit for write operation
while((FCTL3&BUSY)==BUSY) ;//等待FLASH存储器完成操作
for (i=0; i<8; i++)
{
*Flash_ptr++ = p[i]; // Write value to flash
}
FCTL1 = FWKEY; // Clear WRT bit
FCTL3 = FWKEY + LOCK; // Reset LOCK bit
_EINT();
}
//读flash
void Read_Flash(uchar *p)
{
_DINT();
uchar *Flash_ptr;
uint i;
Flash_ptr=(uchar*)0xff00; //初始化
for(i=0;i<8;i++)
{
p[i]=*(Flash_ptr++);
}
_EINT();
}
********************************/
void ClrFlashSeg(unsigned int FlashAdd)
{
unsigned int* p_flash;
p_flash = (unsigned int*)FlashAdd;
FCTL2 = FWKEY + FSSEL_1 + FN4;
FCTL3 = FWKEY;
FCTL1 = FWKEY + ERASE;
*p_flash = 0x1234; //write a random data.
while((FCTL3 & BUSY) != BUSY)
{
break;
}
FCTL3 = FWKEY + LOCK;
}
//不加如写flash一切都事正常得,只要写flash就会出现程序跑飞或者复位
大家帮我看一下,多谢了!!!!!!!!!!!!