PIC12F508單片機是沒有中斷的,按鍵中斷只能是查詢方式。
編譯器用的XC8,編譯環境IDE用的是MPLAB X IDE。
下載器是PICKIT3.
//***************************************************
// __________________
// VDD-| 1 8 |-VSS
// GP5-| 2 27 |-GP0/DAT
// GP4-| 3 26 |-GP1/CLK
//GP3/RMCLR--| 4 25 |-GP2
// |________________|
// 12F508
//***************************************************
//看門狗 休眠喚醒
//按鍵喚醒
#include
#include
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT Enable )
#pragma config CP = OFF // Code Protection bit (Code protection off)
#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)
#define uchar unsigned char
#define uint unsigned int
#define LED1 GP5
#define LED2 GP4
#define KEY GP3
bit KEY_F;
uchar count;
//uchar GP3_F;
void Init()
{
TRIS=~0x3F; //GP3輸入,其它輸出
// OPTION=0x07; //這個寄存器上電復位均為1
// OPTION=nGPWU|nGPPU|PSA|PS1|PS2; //引腳中斷喚醒禁止 弱上拉禁止 定時器分配看門狗時間是18Ms*64=1.152S
OPTION=0x1E; // 引腳中斷喚醒使能 弱上拉使能 定時器分配看門狗時間是18Ms*64=1.152S
// TMR0=0x63; //10ms
}
void main()
{
Init();
while(1)
{
if((GPWUF==1)&&(KEY==0)) //引腳引起的中斷喚醒
{
LED2=~LED2;
}
if(nTO==0) //看門狗引起的復位
{
LED1=~LED1;
}
KEY_F=KEY; //讀出休眠前的按鍵狀態。
SLEEP();
}
}