#include "C8051F020.h"
#include "stdlib.h"
#include "datatype.h"
#include "DAC.h"
#include "LCD.h"
INT8U UART0_BUSY = 0;
#define SYSCLK       22118400          // External crystal oscillator frequency 
#define SAR_CLK      2500000   
INT8U  ADC_END_FLAG = 0; 
INT16U ADC0_Value;
 
#define	W_CMD    0xf8  //写命令
#define	W_DAT    0xfa  //写数据  
//**********************************************************

//系统时钟初始化	

//**********************************************************
void OSCILLATOR_Init (void)
{
   int i;                              // delay counter

   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184MHz crystal

   for (i=0; i < 256; i++) ;           // wait for oscillator to start

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector

}
//**********************************************************

//-----------------------------------------------------------------------------
//ADC0_Init 
//-----------------------------------------------------------------------------
void ADC_EINT ()
{
    EIE2 |= BIT1; //允许ADC转换结束中断
	EA = 1;
}
void ADC0_Init (void)
{
    AMX0CF &= ~(BIT2 + BIT1 + BIT0); //配置AIN0 AIN1 AIN2 AIN3位单通道输入
	ADC0CF |=  BIT4 + BIT5;  //
	ADC0CF &= ~(BIT7 + BIT6);      //设置AD转换时钟为2MHz (系统时钟为16MHz时)
 //   ADC0CF = (SYSCLK/SAR_CLK) << 3;     // ADC conversion clock = 2.5MHz
	ADC0CF &= ~(BIT2 + BIT1 + BIT0);  //PGA增益为1

	ADC0CN &= ~(BIT6 + BIT3 + BIT2 + BIT0); //向BUSY写1启动AD转换  数据右对齐
	ADC0CN |= BIT7;	//使能AD

	REF0CN &= ~BIT4; //参考电压取自VREF0
	REF0CN |= BIT1 + BIT0; //

	ADC_EINT ();
}


//选择AIN0通道
void SC_ADC_Port0 ()
{
	AMX0SL &= ~(BIT3 + BIT2 + BIT1 + BIT0);    
}

//选择AIN1通道
void SC_ADC_Port1 ()
{
    AMX0SL &= ~(BIT3 + BIT2 + BIT1);
	AMX0SL |= BIT0;
}

//选择AIN2通道
void SC_ADC_Port2 ()
{
    AMX0SL &= ~(BIT3 + BIT2 + BIT0);
	AMX0SL |= BIT1;
}

//选择AIN3通道
void SC_ADC_Port3 ()
{
    AMX0SL &= ~(BIT3 + BIT2 );
	AMX0SL |= BIT1+BIT0;
}

//-----------------------------------------------------------------------------
//ADC转换 
//-----------------------------------------------------------------------------
void Start_ADC (INT8S AIN_Index)
{
    ADC0CN &= ~BIT5; //清除转换结束标志
    ADC_END_FLAG = 0;

	switch (AIN_Index)
	{
	case 0 : SC_ADC_Port0 (); break;
	case 1 : SC_ADC_Port1 (); break;
	case 2 : SC_ADC_Port2 (); break;
	case 3 : SC_ADC_Port3 (); break;
	}
	

    ADC0CN |= BIT4;  //启动ADC转换
}
  

//-----------------------------------------------------------------------------
//ADC0 转换结束中断服务程序
//-----------------------------------------------------------------------------
void ADC0_INT_ISR () interrupt 15
{
    INT16U temp1,temp2;
    if (AD0INT ==1)
    {
        AD0INT = 0;
        ADC_END_FLAG = 1;
        temp1 = ADC0L;
        temp2 = ADC0H;
        ADC0_Value = (temp2 << 8) + temp1; 
        Start_ADC (0);
    }
}

void main (void)
{
	int i, j;
   	unsigned char buf[4];

   	WDTCN = 0xde;                       // Disable watchdog timer
   	WDTCN = 0xad;

  	OSCILLATOR_Init ();                 // Initialize oscillator
   	Init_DAC1 ();                       // Initialize crossbar and GPIO
  
   	LCD_Init ();
   	LCD_ClearScreen();
   	LCD_DispStrAt ("128*64液晶显示屏", 1, 1);
   	ADC0_Init ();                       // Init ADC
	
	Start_ADC (0);
	while(1)
	{ 
		for(i = 4095; i >= 0; i = i - 10)
	   	{				 
			Update_DAC1_DATA(i);
			for (j = 0; j < 1000; j++);
	   		LCD_DispStrAt ("128*64液晶显示屏", 1, 1);
	   		buf[3] = ADC0_Value/1000;
     		buf[2] = ADC0_Value%1000/100;
		 	buf[1] = ADC0_Value%100/10;
		 	buf[0] = ADC0_Value%10;
			LCD_SetDisAdd (3, 1);
			for(j = 4; j > 0; j--)
			{
    	  		LCD_WriteData (W_DAT, buf[j-1] + 0x30);
			}

   	   	}
	   
  	}  
}