#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 timer3_PWM2(void)
{
	T1CLKEN = 1;
	T3CLKEN = 1;
	CCPCLKEN = 1;

	PWM10_MAP = 0X00;	//映射到PA0
    PWM11_MAP = 0X01;	//映射到PA1
	GIEH = 1;        	//所有外设中断
	CCPIE=1;
	CCPIF=0;
	CCPIP=1;

	TMR3IE=1;
	TMR3IF=0;
	TMR3IP=1;

	PR3H = 0X0F;		//4msFA0
	PR3L = 0X00;
	TMR3H = 0X00;
	TMR3L = 0X00;

	PWM2H = 0X00;		//50%占空比
	PWM2L = 0XD0;

	PXC = 0XC0;   		//使能TIMER3 PWM2,PWM3,PWM4控制CCP的HBRIDGE单元,   选择PWM2同时映射到3个HBRIDGE单元
	PMS = 0X80;   		//BYPASS二级防呆,互补式控制
	POLS = 0X00;   		//同相输出
	PME = 0X0C;    		//使能映射  PWM11、PWM10使能输出

	PWM2CON0 = 0X01;	//使能PWM2 使能PWM2引脚映射
	PWM2CON1 = 0X00;	//正向输出
	PWM1CON0=0x00;		//PWM——ADC使能
	T3CON = 0X07;		//Timer3配置：使能TImer3 预分频16
}


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;            //使能ADC
	ADCON1=0xBA;            //Verf = VDD;
	ADCON3=0X00;
    GO = 1;
	ADIF   = 0;           //清AD中断标志
	ADIE   = 1;           //开AD中断
	ADIP   = 1;           //高优先级中断

}


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

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



