NY8BE64A 采用双时钟机制,高速振荡或者低速振荡都可以分别选择内部RC振荡或外部Crystal输入。在双时钟机制
下,NY8BE64A 可选择多种工作模式如正常模式(Normal)、慢速模式(Slow mode)、待机模式(Standby mode) 与睡眠
模式(Halt mode)可节省电力消耗延长电池寿命。并且微控制器在使用内部RC高速振荡时,低速振荡可以同时使用外
部精准的晶振计时。可以维持高速处理同时又能精准计算真实时间。
在省电的模式下如待机模式(Standby mode) 与睡眠模式(Halt mode)中,有多种事件可以触发中断唤醒NY8BE64A 进
入正常操作模式(Normal) 或 慢速模式(Slow mode) 来处理突发事件。
NY8BE64A提供了四种工作模式来定制各种应用和节省电力消耗,四种模式分别是正常模式、慢速模式、待机模
式和睡眠模式。正常模式被指定为高速运行模式,慢速模式被指定为低速模式,以节省功耗。在待机模式下,
NY8BE64A将停止几乎所有的运作,除了定时器 0/定时器 1/定时器 4/定时器 5/ WDT,用来定期唤醒。在睡眠模
式下,NY8BE64A将睡眠直到外部事件或看门狗定时器来唤醒。
源文件 main.c
/* =========================================================================
* Project: Sleep Wakeup
* File: main.c
* Description: Into Halt mode & Standby mode, and while PortB input change then do wakeup.
* While PB1 input change, then wakeup from Halt(Standby) mode and set PB2 output low.
* Also switch between Normal Mode and Slow Mode.
* FINST = 4MHz/4T(I_HRC).
*
* Author: JasonLee
* Version: V2.0
* Date: 2019/10/30
=========================================================================*/
#include <ny8.h>
#include "ny8_constant.h"
#define HALT_MODE 0x04
#define FHOSC_SEL 0x01
#define STANDBY_MODE 0x08
#define UPDATE_REG(x) __asm__("MOVR _" #x ",F")
//! interrupt service routine
void isr(void) __interrupt(0)
{
if(INTFbits.PABIF)
{
INTFbits.PABIF = 0; // Clear PABIF(PortB input change interrupt flag bit)
}
}
void main(void)
{
DISI();
// Initial GPIO
IOSTC = C_PC_Output; // Set PC[1:0] to output mode
IOSTA = C_PA_Output; // Set PA[7:0] to output mode
BPHCON = (unsigned char)~C_PB1_PHB; // Enable PB1 Pull-High resistor
BWUCON = C_PB1_Wakeup; // Enable PB1 input change wakeup function
IOSTB = C_PB1_Input; // Set PB1 to input mode,others set to output mode
PORTB = 0; // PORTB data buffer=0x00
PORTC = 0; // PORTC data buffer=0x00
PORTA = 0; // PORTA data buffer=0x00
// Initial Interrupt Setting
INTE = C_INT_PABKey; // Enable PortB input change interrupt
INTF = 0; // Clear all interrupt flags
// Normal mode into Slow mode
OSCCR = C_FLOSC_Sel; // OSCCR[0]=0 , FOSC is Low-frequency oscillation (FLOSC)
// Slow mode into Normal mode
OSCCR = C_FHOSC_Sel; // OSCCR[0]=1 , FOSC is high-frequency oscillation (FHOSC)
// Chioce while wakeup from Halt mode or Standby mode,the procedure will enter
// interrupt service routine or not.
ENI(); // 1. Enable all unmasked interrupts.
// 2. After wakeup frorm Halt mode or
// Standby mode, the procedure will
// enter interrupt service routine
//DISI(); // 1. Disable all interrupts.
// 2. After wakeup frorm Halt mode or
// Standby mode. The procedure will
// not enter interrupt service routine
PORTBbits.PB2 = 1; // Set PB2 outputs high
while(1)
{
CLRWDT(); // Clear WatchDog
AWUCON = 0; // Disable PA input change wakeup function
// A. Normal mode into Halt mode. While PB1 input change then wakeup and set PB2 outputs low
UPDATE_REG(PORTB); // Read PORTB Data buffer
//choice one way to enter Halt mode
PCONbits.WDTEN = 0; // Disable WatchDog
SLEEP(); // 1. Execute instruction to enters Halt mode (from Normal mode)
//OSCCR = HALT_MODE | FHOSC_SEL // 2. Set OSCCR register to enters Halt mode (from Normal mode)
CLRWDT(); // Clear WatchDog
PCONbits.WDTEN = 1; // Enable WatchDog
PORTBbits.PB2 = 0; // while wakeup from Halt mode then set PB2 outputs low
INTFbits.PABIF = 0; // Clear PABIF(PortB input change interrupt flag bit)
// B. Normal mode into standby mode. While PB1 input change then wakeup and set PB2 outputs high
UPDATE_REG(PORTB); // Read PORTB Data buffer
OSCCR = C_Standby_Mode | C_FHOSC_Sel; // set OSCCR register to enters Standby mode (from Normal mode)
PORTBbits.PB2 = 1; // while wakeup from Standby mode then set PB2 outputs high
INTFbits.PABIF = 0; // Clear PBIF(PortB input change interrupt flag bit)
}
}
/* =========================================================================
* Project: Sleep Wakeup
* File: main.c
* Description: Into Halt mode & Standby mode, and while PortB input change then do wakeup.
* While PB1 input change, then wakeup from Halt(Standby) mode and set PB2 output low.
* Also switch between Normal Mode and Slow Mode.
* FINST = 4MHz/4T(I_HRC).
*
* Author: JasonLee
* Version: V2.0
* Date: 2019/10/30
=========================================================================*/
/* =========================================================================
* 项目: 睡眠唤醒
* 文件: main.c
* 描述: 进入 Halt 模式(睡眠模式)和 Standby 模式(待机模式),当 PortB 输入变化时进行唤醒。
* 当 PB1 输入变化时,从 Halt(Standby)模式唤醒,并将PB2输出设为低。
* 还可以在正常模式和慢模式之间切换。
* FINST = 4MHz/4T(I_HRC)。
*
* 作者: JasonLee
* 版本: V2.0
* 日期: 2019/10/30
*/
#include <ny8.h>
#include "ny8_constant.h"
#define HALT_MODE 0x04
#define FHOSC_SEL 0x01
#define STANDBY_MODE 0x08
#define UPDATE_REG(x) __asm__("MOVR _" #x ",F")
//! interrupt service routine
void isr(void) __interrupt(0) {
if(INTFbits.PABIF) {
INTFbits.PABIF = 0; // Clear PABIF(PortB input change interrupt flag bit)
}
}
void main(void) {
DISI();
// 初始化GPIO
IOSTC = C_PC_Output; // 将PC[1:0]设置为输出模式
IOSTA = C_PA_Output; // 将PA[7:0]设置为输出模式
BPHCON = (unsigned char)~C_PB1_PHB; // 启用PB1上拉电阻
// BWUCON、AWUCON
BWUCON = C_PB1_Wakeup; // 启用PB1输入变化唤醒功能
IOSTB = C_PB1_Input; // 将PB1设置为输入模式,其他设置为输出模式
PORTB = 0; // PORTB数据缓冲区=0x00
PORTC = 0; // PORTC数据缓冲区=0x00
PORTA = 0; // PORTA数据缓冲区=0x00
// 初始化中断设置
INTE = C_INT_PABKey; // 启用PortB输入变化中断
INTF = 0; // 清除所有中断标志
// 正常模式切换到慢模式
OSCCR = C_FLOSC_Sel; // OSCCR[0]=0,FOSC为低频振荡(FLOSC)
// 慢模式切换到正常模式
OSCCR = C_FHOSC_Sel; // OSCCR[0]=1,FOSC为高频振荡(FHOSC)
// 选择在从Halt模式或Standby模式唤醒时,程序是否进入
// 中断服务例程。
ENI(); // 1. 启用所有未屏蔽的中断。
// 2. 从Halt模式或Standby模式唤醒后,程序将
// 进入中断服务例程。
// DISI(); // 1. 禁用所有中断。
// 2. 从Halt模式或Standby模式唤醒后,程序将
// 不进入中断服务例程。
PORTBbits.PB2 = 1; // 设置PB2输出为高
while(1) {
CLRWDT(); // 清除看门狗
AWUCON = 0; // 禁用PA输入变化唤醒功能
A. 正常模式切换到Halt模式。当PB1输入变化时唤醒,并将PB2输出设为低
UPDATE_REG(PORTB); // 读取PORTB数据缓冲区
选择一种进入 Halt 模式的方式
PCONbits.WDTEN = 0; // 禁用看门狗
SLEEP(); // 1. 执行指令进入 Halt 模式(从正常模式)
//OSCCR = HALT_MODE | FHOSC_SEL // 2. 设置OSCCR寄存器进入Halt模式(从正常模式)
CLRWDT(); // 清除看门狗
PCONbits.WDTEN = 1; // 启用看门狗
PORTBbits.PB2 = 0; // 从Halt模式唤醒时将PB2输出设为低
INTFbits.PABIF = 0; // 清除PABIF(PortB输入变化中断标志位)
// B. 正常模式切换到 Standby 模式。当PB1输入变化时唤醒,并将PB2输出设为高
// UPDATE_REG(PORTB); // 读取PORTB数据缓冲区
// OSCCR = C_Standby_Mode | C_FHOSC_Sel; // 设置OSCCR寄存器进入Standby模式(从正常模式)
// PORTBbits.PB2 = 1; // 从Standby模式唤醒时将PB2输出设为高
// INTFbits.PABIF = 0; // 清除PBIF(PortB输入变化中断标志位)
}
}