/****************************************************************************************
 * KF8A系列单片机	KF8A100EON_DEMO	开发板演示程序
 * 标    题: 定时器实验
 * 项目名: 04- IOC_Sample
 * 开发环境：ChipON IDE
 * 版          本     ：V1.0 (2019/08/10)
 * 作 者: 上海芯旺微电子有限公司
 * 功能简述：使用T0 ~ T10定时器每500ms翻转点亮LED_R
 * 硬件说明：无
 ****************************************************************************************/
#include<KF8A100GQT.h>
#include "includes.h"

volatile uint16 T0_count,T1_count,T2_count,T3_count,T4_count,T5_count,	\
				T6_count,T7_count,T8_count,T9_count,T10_count;

//__sfr  __at(0x23D)		PFTCTL=0x53;	//倍频到6倍需要添加此配置


void delay_ms(uint16 num)
{
	uint16 i,j;
	for(i=0;i<num;i++)
	{
		j=1000;
		while(j--);
	}
}

void SYS_Init(void)
{
	//使用外部晶振时，引脚配置输入，配置字选择外部时钟且允许切换
	SCLKCTL = SYSCLK_SCLK_SEL_INTHF|SYSCLK_FREQ_DIV1;	//INTHF作为主时钟
	_NOP();_NOP();
	PLLEN=0;
	PLLCTL = PLL_SEL_DIV1|PLL_SEL_INTHF|PLLBIAS_CUR_MIN|PLLPMUL_4;
	PLLEN=1;
	delay_ms(200);
	SCLKCTL|=SYSYCLK_SEL_SCKS|SYSCLK_SCLK_SEL_UPCK;		//UPCK作为主时钟	64M
	SCLKOE = 0;		//机器周期禁能输出,输出波形与IO翻转速率相关
	CLKOUTSEL = 0;
	HFCKCTL = 0xCE;	//外设时钟开启,选择INTHF为HFCLK时钟信号

	TR0 = 0;	TR1	= 0;	TR2	= 0;	TR3 = 0;
	TR4 = 0;	TR5 = 0;	TR6 = 0;	TR7 = 0;
	TR9 = 0;	TRA = 0;	TRB = 0;
	P0LR = 0;	P1LR = 0;	P2LR = 0;	P3LR = 0;
	P4LR = 0;	P5LR = 0;	P6LR = 0;	P7LR = 0;
	P9LR = 0;	PALR = 0;	PBLR = 0;
}


//主函数
void main()
{
	uchar i=0;
	SYS_Init();

	switch(Timer_CH)
	{
	case 0:		Timer0_Init();	break;
	case 1:		Timer1_Init();	break;
	case 2:		Timer2_Init();	break;
	case 3:		Timer3_Init();	break;
	case 5:		Timer5_Init();	break;
	case 8:		Timer8_Init();	break;
	case 10:	Timer10_Init();	break;
	default :	break;
	}


	//总中断开启
	AIE=1;
	PUIE=1;

	while(1)
	{
		switch(Timer_CH)
		{
		case 0:
			if(T0_count>=2000)
			{
				T0_count=0;
				LED_R=!LED_R;
			}
			break;
		case 1:
			if(T1_count>=2000)
			{
				T1_count=0;
				LED_R=!LED_R;
			}
			break;
		case 2:
			if(T2_count>=500)
			{
				T2_count=0;
				LED_R=!LED_R;
			}
			break;
		case 3:
			if(T3_count>=2000)
			{
				T3_count=0;
				LED_R=!LED_R;
			}
			break;
		case 5:
			if(T5_count>=500)
			{
				T5_count=0;
				LED_R=!LED_R;
			}
			break;
		case 8:
			if(T8_count>=500)
			{
				T8_count=0;
				LED_R=!LED_R;
			}
			break;
		case 10:
			if(T10_count>=500)
			{
				T10_count=0;
				LED_R=!LED_R;
			}
			break;

		default :	{LED_R=1;LED_B=1;LED_G=1;	break;}
		}
	}
}
//中断函数0:0X04入口地址
void int_fun0() __interrupt (0)
{
	if(T0IE & T0IF)
	{
		T0=5;
		T0IF=0;
		T0_count++;
	}
	if(T1IE & T1IF)
	{
		T1IF=0;
		T1_count++;
	}
	if(T2IE & T2IF)
	{
		T2IF=0;
		T2_count++;
	}
	if(T3IE & T3IF)
	{
		T3IF=0;
		T3_count++;
	}
	if(T5IE & T5IF)
	{
		T5IF=0;
		T5_count++;
	}
	if(T8IE & T8IF)
	{
		T8IF=0;
		T8_count++;
//		_NOP();		不添加此NOP，主函数中IO翻转频率出错
	}
	if(T10IE & T10IF)
	{
		T10IF=0;
		T10_count++;
	}
}


//中断函数1:0x14入口地址
void int_fun1() __interrupt (1)
{

}
