我当前的项目需要让 dsPIC30F 在收到外部“低功耗警告”信号时进入低功耗模式。我已经验证了它EnterLowPowerMode()
确实执行了。但是,当我尝试OSCCON
使用内置__builtin_write_OSCCONL()
和asm()
嵌入 C 代码来调整后标量时, 的值OSCCON<7:6>
保持不变。
我不明白为什么程序集不起作用。看起来很简单,够了。
void EnterLowPowerMode()
{
uint8_t writeUnlock1 = 0x46;
uint8_t writeUnlock2 = 0x57;
uint8_t writeData = 0xC0;
// Set PMD1 and PMD2 equal to 0xFFFF
*PMD1 = 0xFFFF; // Disable peripherals in Peripheral Module Disable register 1
*PMD2 = 0xFFFF; // Disable peripherals in Peripheral Module Disable register 2
// Unlock OSCCON and set OSCCON<7:6> to b11
// OSCCON address is 0x0742
__builtin_write_OSCCONL(0xC0);
// asm("MOV.w #0x0742,w0");
// asm("MOV.w #0x46,w1");
// asm("MOV.w #0x57,w2");
// asm("mov.w #0xC0,w3");
// asm("disi #3");
// asm("mov.b w1,[w0]");
// asm("mov.b w2,[w0]");
// asm("mov.b w3,[w0]");
// Set bit 15 of T1CON equal to 0
*T1CON = *T1CON & 0x7FFF;
// Set T1MR equal to 0x0000
*T1MR = 0x0000;
// Set T1PR equal to 0x00FA
*T1PR = 0x00FA;
// Set T1CON equal to 0x8000
*T1CON = 0x8000;
// Set gInLowPowerMode to kcg_true
gInLowPowerMode = kcg_true;
while(gLowPowerTime < C_LOW_POWER_TIMEOUT)
{
// wait
}
// repeat OSCCON unlock process here
// Set bits 6-7 of OSCCON to b00
*OSCCON = *OSCCON & 0xFF3F;
// Set bit 15 of T1CON to 0
*T1CON = *T1CON & 0x7FFF;
// Set T1MR to 0x0000
*T1MR = 0x0000;
// Set T1PR equal to 0x07D0
*T1PR = 0x07D0;
// Set T1CON equal to 0x8010
*T1CON = 0x8010;
// Set PMD1 equal to 0x81FC
*PMD1 = 0x81FC;
// Set PMD2 equal to 0xFFF9
*PMD2 = 0xFFF9;
// set SHORT_POWER_INTERRUPT to kcg_true
SHORT_POWER_INTERRUPT = kcg_true;
// set gInLowPowerMode to kcg_false
gInLowPowerMode = kcg_false;
}// end EnterLowPowerMode()
gLowPowerTime 在中断中增加,该中断取决于应该使用 OSCCON 后标量设置的新的、较慢的时钟,但由于它运行得太快,因此中断不会中断。
我也试过把所有的装配线放在一个 asm(); 声明,这仍然不起作用。
如果我遗漏了什么,请告诉我,我可以很快更新。