//============================================================================
//文件名称：UartINT      
//功能概要：SD-FSL-KL25-EVB测试工程
//版权所有：苏州大学飞思卡尔嵌入式中心(sumcu.suda.edu.cn)
//版本更新：2012-11-12  V2.0
//         2013-01-27  V3.0（WYH)
//============================================================================
#include "includes.h"



int main(void)
{
	//1.声明主函数使用的局部变量
	uint_32 mRuncount;
	uint_8 buff[] = "HELLO WORLD\n";
	uint_8 flag;
	uint_8 * g_DispalyInit;
	//2.关总中断
	DISABLE_INTERRUPTS;
	//3.初始化底层模块
	LEDInit();                                  //LED初始化
	LCDInit();                                  //LCD初始化
	KBInit();                                   //键盘初始化
	tpm_init(TPM0,TPM_CLKSRC_PLL,1000);         //初始化TPM模块，1ms中断一次
	light_init(RUN_LIGHT_RED, LIGHT_ON);      //三色灯初始化
	light_init(RUN_LIGHT_GREEN, LIGHT_ON);
	light_init(RUN_LIGHT_BLUE, LIGHT_ON);
	beep_init(BEEP_PORT_PIN, BEEP_OFF);

    uart_init (UART_0, MCGIRCLK, 9600);     //串口0初始化, 时钟48000Khz,波特率115200  	
    uart_init (UART_1, BUSCLK, 9600);          //串口1初始化, 总线时钟24000Khz,波特率9600
    uart_init (UART_2, BUSCLK, 9600);          //串口2初始化, 总线时钟24000Khz,波特率9600
    
    usb_init();   
    
    uart_send_string(UART_0, "This is UART0 INT Test!\r\n");
    uart_send_string(UART_1, "This is UART1 INT Test!\r\n");
    uart_send_string(UART_2, "This is UART2 INT Test!\r\n");
    
    uart_enable_re_int(UART_0);
    uart_enable_re_int(UART_1);
    uart_enable_re_int(UART_2);
    g_DispalyInit = (uint_8 *)"Wait Receiving..Soochow 2013.01.";
    LCDShow(g_DispalyInit);
    //4. 给有关变量赋初值
    fg=1;
    flag = 1;
    LEDDataBuffer[0]='8';
    LEDDataBuffer[1]='8';
    LEDDataBuffer[2]='8';
    LEDDataBuffer[3]='8';

    //5. 使能模块中断
    tpm_enable_int(0);
    //6. 开总中断
    ENABLE_INTERRUPTS;
    
	for(;;)
	{
		if(fg==1){
			mRuncount++;
			if (mRuncount > RUN_COUNTER_MAX)
			{
				mRuncount = 0;
				switch (flag)
				{
				case 1:
					light_control(RUN_LIGHT_RED, LIGHT_ON);
					light_control(RUN_LIGHT_GREEN, LIGHT_OFF);
					light_control(RUN_LIGHT_BLUE, LIGHT_OFF);
					flag = 2;
					break;
				case 2:
					light_control(RUN_LIGHT_RED, LIGHT_OFF);
					light_control(RUN_LIGHT_GREEN, LIGHT_ON);
					light_control(RUN_LIGHT_BLUE, LIGHT_OFF);
					flag = 3;
					break;
				case 3:
					light_control(RUN_LIGHT_RED, LIGHT_OFF);
					light_control(RUN_LIGHT_GREEN, LIGHT_OFF);
					light_control(RUN_LIGHT_BLUE, LIGHT_ON);
					flag = 4;
					break;
				case 4:
					light_control(RUN_LIGHT_RED, LIGHT_ON);
					light_control(RUN_LIGHT_GREEN, LIGHT_ON);
					light_control(RUN_LIGHT_BLUE, LIGHT_ON);
					flag = 1;
					uart_sendN(UART_0, 13 ,buff);
					uart_sendN(UART_1, 13 ,buff);
					uart_sendN(UART_2, 13 ,buff);
					break;
				default:
					flag = 1;
					break;
				}// end_switch
			} // end_if
			}
			else
			{
				mRuncount++;
						if (mRuncount > RUN_COUNTER_MAX)
						{
							mRuncount = 0;
							 beep_control(BEEP_PORT_PIN, BEEP_OFF);
						}
			}
	} // end_while
	
	return 0;
}
