本帖最后由 cl234583745 于 2015-5-29 08:07 编辑
这个功能不好用仿真器测试,要掉电重启才知道是否好使。但用仿真器时候不断点击重启和全速。就发现有个别时候程序刚开始就进低压检测中断了。估计就是这个时候重启了。结果就是读写都没有好使
单独测试eeprom读写程序都好使,并且是以前用过的程序。估计是低压检测中断初始化和中断函数不对,或者跟总线时钟的初始化冲突,导致重启(看门狗去掉也会重启)
void MCU_init(void)
{
//SOPT1 = 0xc0; //看门狗
//SOPT2 = 0x00;
if( (SPMSC1&0x80)==0x80)
{
SPMSC1=SPMSC1|0x40;//清除上电产生的置位
}
SPMSC1 = 0x3C;//系统电源管理状态和控制寄存器 0001 1100
SPMSC2 = 0x20; //系统电源管理状态和控制寄存器 0011 0000
if (*(unsigned char*)0xFFAF != 0xFF) { /* Test if the device trim value is stored on the specified address */
MCGTRM = *(unsigned char*)0xFFAF; /* Initialize MCGTRM register from a non volatile memory */
MCGSC = *(unsigned char*)0xFFAE; /* Initialize MCGSC register from a non volatile memory */
}
/* MCGC2: BDIV=0,RANGE=1,HGO=1,LP=0,EREFS=1,ERCLKEN=1,EREFSTEN=0 */
setReg8(MCGC2, 0x36); /* Set MCGC2 register */
/* MCGC1: CLKS=2,RDIV=7,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
setReg8(MCGC1, 0xBA); /* Set MCGC1 register */
while(!MCGSC_OSCINIT) { /* Wait until external reference is stable */
SRS = 0x55; /* Reset watchdog counter write 55, AA */
SRS = 0xAA;
}
while(MCGSC_IREFST) { /* Wait until external reference is selected */
SRS = 0x55; /* Reset watchdog counter write 55, AA */
SRS = 0xAA;
}
while((MCGSC & 0x0C) != 0x08) { /* Wait until external clock is selected as a bus clock reference */
SRS = 0x55; /* Reset watchdog counter write 55, AA */
SRS = 0xAA;
}
/* MCGC2: BDIV=0,RANGE=1,HGO=1,LP=1,EREFS=1,ERCLKEN=1,EREFSTEN=0 */
setReg8(MCGC2, 0x3E); /* Set MCGC2 register */
/* MCGC1: CLKS=2,RDIV=2,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
setReg8(MCGC1, 0x92); /* Set MCGC1 register */
/* MCGC3: LOLIE=0,PLLS=1,CME=0,??=0,VDIV=6 */
setReg8(MCGC3, 0x46); /* Set MCGC3 register */
/* MCGC2: LP=0 */
clrReg8Bits(MCGC2, 0x08);
while(!MCGSC_PLLST) { /* Wait until PLL is selected */
SRS = 0x55; /* Reset watchdog counter write 55, AA */
SRS = 0xAA;
}
while(!MCGSC_LOCK) { /* Wait until PLL is locked */
SRS = 0x55; /* Reset watchdog counter write 55, AA */
SRS = 0xAA;
}
/* MCGC1: CLKS=0,RDIV=2,IREFS=0,IRCLKEN=1,IREFSTEN=0 */
setReg8(MCGC1, 0x12); /* Set MCGC1 register */
while((MCGSC & 0x0C) != 0x0C) { /* Wait until PLL clock is selected as a bus clock reference */
SRS = 0x55; /* Reset watchdog counter write 55, AA */
SRS = 0xAA;
}
interrupt VectorNumber_Vlvd void LVD_ISR(void)
{
if((SPMSC1&0x80)==0x80)
{
SPMSC1=SPMSC1|0x40;
W_CS();
BM_E=0;
}
}
|