#include "AD18F06A.h"
#include <stdio.h>
#include "common.h"

/*******************************************************************************
// ----Function: GPIO_Init
// --Parameters:
// -----Returns: None
//---------Note:所有IO初始化
*******************************************************************************/
void GPIO_Init(void)
{
	TRISA = 0X00;   //PA输出模式
	TRISB = 0X00;   //PB输出模式
	TRISC = 0X00;   //PC输出模式

	PORTA = 0X00;	//PORTA输出0
	PORTB = 0X00;	//PORTB输出0
	PORTC = 0X00;	//PORTC输出0

	PAPD = 0XFF;    //关闭PA下拉
	PAPU = 0XFF;    //关闭PA上拉
	PBPD = 0XFF;    //关闭PB下拉
	PBPU = 0XFF;    //关闭PB上拉
	PCPD = 0XFF;    //关闭PC下拉
	PCPU = 0XFF;    //关闭PC上拉

	PAOD = 0X00;    //关PA开漏输出
	PBOD = 0X00;    //关PB开漏输出
	PCOD = 0X00;    //关PC开漏输出

	ANASEL0 = 0X00; //关PA模拟量通道
	ANASEL1 = 0X00; //关PB模拟量通道
	ANASEL2 = 0X00; //关PC模拟量通道
}

void timer1_Init(void)
{
//	WDTCON = 0X01;
	T1CLKEN = 1;     	//开T1时钟
	TMR1H = 0X83;    	//t = 1000ms
	TMR1L = 0X00;
	T1CON1 = 0X03;   	//定时产生中断时，启动ADC采集
	T1CON0 = 0X43;   	//内部32KHZ

	// TMR1IF = 0;      //清中断标志
	// TMR1IP = 0;		 //高优先级中断
	// TMR1IE = 1;      //开TMR1中断
}

void usart0_init()
{
    UART0CLKEN = 1;
	RX0_PIN    = 1;
	TX0_PIN    = 0;

	PORTB|=0X06;
	PBPU&=~(1<<1);
	TX0_MAP=(1<<3)|0X02;	//PB2_TX
	RX0_MAP=(1<<3)|0X01;	//PB1_RX
	SPBRG     = DIVIDER0;
	RCSTA     = 0x90;
	TXSTA     = 0x26;//24
}


void putch( char byte)
{
	while(!TXIF)		continue;
	TXREG = byte;
}


unsigned char getch()
{
	/* retrieve one byte */
	while(!RCIF)	/* set when register is not empty */
		continue;
	return RCREG;
}



unsigned char getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}


void Config_ADC()
{
	ADCCLKEN=1;
	TRISB0 = 1;PB0PD = 1;PB0PU = 1;PB0OD = 0;PB0AEN = 1;	//PB0为模拟输入口
	ADRESH0=0x00;
	ADCON0=0x01;
	ADCON1=0xBE;
	ADCON3=0X00;
	ADIF   = 0;           //清AD中断标志
	ADIE   = 1;           //开AD中断
	ADIP   = 1;           //高优先级中断

}


/*******************************************************************************

*******************************************************************************/



