//==============================================================================
// Project : MC96F8316 Demo Program
// Date : 2015.09.22
// Author : Tom Chan(陈春健)
// Device : MC96F8316(28SOP)
// Voltage : 5V
// Frequency : internal 8 MHz RC oscillator
// ADC range : 5V (Vdd)
// edit tool : Keil uVision4
//==============================================================================
//------------------------------------------------------------------------------
// Include file and variable
//------------------------------------------------------------------------------
#include <MC96F8316.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define nop _nop_()
#define s(x,y) (x |= (1 << y)) //位置1
#define c(x,y) (x &= ~ (1 << y)) //位置0
#define b(x,y) (x & (1 << y)) //位判断 if(b(x,y)) 判断1 if(b(x,y)==0) 判断0
#define b0(x,y) ((x & (1 << y)) == 0) //位0判断 if(b0(x,y)) 判断0
#define b1(x,y) (x & (1 << y)) //位1判断 if(b1(x,y)) 判断1
//------------------------------------------------------------------------------
// Real Time Clock variable define
//------------------------------------------------------------------------------
unsigned char WDT_13s;
void MCUInitialSetting()
{
//uchar i;
// System Clock Select & builtin reset level select
SCCR = 0x00; // INT RC OSC (fIRC) for system clock
OSCCR = 0x20; // internal RC OSC 8MHz
nop;
nop;
nop;
LVRCR = 0x01; //LVR 禁止
P0IO = 0xff; //all output
P0OD = 0x00; //推换输出
P0PU = 0x00; //无上拉电阻
P0 = 0xff; //输出低电平
P03DB = 0x00;
P0FSR = 0x00; //普通I/O
P1IO = 0xff; // all output
P1OD = 0x00; // 推换输出
P1PU = 0x00; // 无上拉电阻
P1 = 0xff; // 输出低电平
P12DB = 0x00; //
P1FSRH = 0x00; // 普通I/O
P1FSRL = 0x00; // 普通I/O
P2IO = 0xff; // OUTPUT
P2OD = 0x00; // 推换输出
P2PU = 0x00; // 无上拉电阻
P2 = 0xff; // 输出低电平
P2FSR = 0x00; // 普通I/O
P3IO = 0xff; // P30,31 INPUT ,OTHER OUTPUT
P3OD = 0x00; // 输出
P3PU = 0x00; // P30,31
P3 = 0xff; // 输出低电平
P3FSR = 0x00; // 普通I/O
EIPOL1 = 0x00; // Ext Int8 falling edge trigger
EIPOL2 = 0x00; // Ext 7,8,9不触发
EIPOL0L = 0x00; // Ext Int 3~0 Polarity Register Low
EIPOL0H = 0x00; // Ext Int 7~4 Polarity Register High
EIFLAG0 = 0x00; // Ext Int Flag Register0
EIFLAG1 = 0x00; // Ext Int Flag Register1
// b7 b6 b5 b4 b3 b2 b1 b0
IE = 0x80; //(EA) - ExG0 - - Ex12 Ex11 Ex10
IE1 = 0x00; // - - - (TX) (RX) I2C Ex6 Ex5
//IE2 = 0x02; // - - ExG1 - T2 (T1) T0 T0V
IE3 = 0x00; // - - - BIT WDT (WT) SPI ADC
}
//------------------------------------------------------------------------------
// task : Timer and Interrupt start
//------------------------------------------------------------------------------
void Timer_Start()
{
// internal RC OSC 8MHz
//T0CR = 0x85; // PCS=8MHz/8=1000000Hz( 1us ), Timer 0 enable
//T0DR = 0xF9; // 1us x 250 =250us
WDTDR = 0xFF; // period 51.2ms*256=13.1S
WDTCR = 0x80; // setting
WDTCR |= 0x02; // Use WDTRC 5KHz/256分频=51.2ms; 256分频请查看时钟发生器方框图;BIT定时器无效
WDTCR |= 0x20; // Clear Watch-dog timer
IE3 |= 0x08; // Enable WDT interrupt
}
void Process_Power()//进入省电模式之前进行I/O口设置
{
P0IO = 0xff; //all output
P0OD = 0x00; //推换输出
P0PU = 0x00; //无上拉电阻
P0 = 0xff; //输出
P03DB = 0x00;
P0FSR = 0x00; //普通I/O
P1IO = 0xff; // all output
P1OD = 0x00; // 推换输出
P1PU = 0x00; // 无上拉电阻
P1 = 0xff; // 输出
P12DB = 0x00; //
P1FSRH = 0x00; // 普通I/O
P1FSRL = 0x00; // 普通I/O
P2IO = 0xff; // OUTPUT
P2OD = 0x00; // 推换输出
P2PU = 0x00; // 无上拉电阻
P2 = 0xff; // 输出
P2FSR = 0x00; // 普通I/O
P3IO = 0xdf; // P35 input; other OUTPUT
P3OD = 0x00; // 输出
P3PU = 0x20; //
P3 |= 0x5f; //
P3FSR = 0x00; // 普通I/O
EIPOL1 = 0x02; // Ext10下降沿触发
// b7 b6 b5 b4 b3 b2 b1 b0
IE |= 0x81; //(EA) - ExG0 - - Ex12 Ex11 Ex10
PCON = 0x03; // WDT interrupt Enable, 1.8uA 功耗
nop;
nop;
nop;
nop;
nop;
nop;
}
//==============================================================================
// Main Program
//==============================================================================
void main()
{
MCUInitialSetting();
Timer_Start();
Process_Power();
//------------------------------------------------------------------------------
// main loop
//------------------------------------------------------------------------------
while(1)
{
nop;
nop;
nop;
nop;
nop;
nop;
Process_Power();
}
}
void Ext10_Int_Handler(void) interrupt 0 //Ext10
{
EIFLAG1=0; // 清集中断标志位
P0IO = 0xff; //all output
P0OD = 0x00; //推换输出
P0PU = 0x00; //无上拉电阻
P0 = 0x00; //输出低电平
P03DB = 0x00;
P0FSR = 0x00; //普通I/O
P1IO = 0xff; // all output
P1OD = 0x00; // 推换输出
P1PU = 0x00; // 无上拉电阻
P1 = 0x00; // 输出低电平
P12DB = 0x00; //
P1FSRH = 0x00; // 普通I/O
P1FSRL = 0x00; // 普通I/O
P2IO = 0xff; // OUTPUT
P2OD = 0x00; // 推换输出
P2PU = 0x00; // 无上拉电阻
P2 = 0x00; // 输出低电平
P2FSR = 0x00; // 普通I/O
P3IO = 0xff; // P30,31 INPUT ,OTHER OUTPUT
P3OD = 0x00; // 输出
P3PU = 0x00; // P30,31
P3 = 0x00; // 输出低电平
P3FSR = 0x00; // 普通I/O
EIPOL1 = 0x00; // Ext10不触发
IE &= 0xfe;
}
//==============================================================================
// task : 1 Sec interval Real Time Clock Interval by Watch Timer
// internal WDTRC OSC 5KHz/256分频=51.2ms; 51.2ms*256=13.1S
//==============================================================================
void INT_WDT() interrupt 21
{
// Watch-dog timer interrupt
// TODO: add your code here
WDT_13s++;
WDTCR |= 0x20; // Clear Watch-dog timer
nop;
nop;
nop;
P37 = ~P37;
}
//==============================================================================
// Appindix : interrupt vector
//==============================================================================
//void Ext10_Int_Handler(void) interrupt 0 using 1
//void Ext11_Int_Handl