#include <htc.h>
#include <stdio.h>
__CONFIG(0xFF32);
#define u8 unsigned char
#define u16 unsigned int
void delayus()
{
int i;
for(i=200;i--;);
}
/********延时程序********/
void Delay_ms(u16 xms)
{
int i,j;
for(i=0;i<xms;i++)
{ for(j=0;j<71;j++) ; }
}
void port_init(void)
{
PORTA=0xFF;
TRISA=0x01; //RA0输入
ADCON1=0x8e; //右对齐,RA0做模拟输入,其他做普通IO
ADCON0=0x41; //0:01 000 001 fosc/8,RA0,使能AD
delayus(); //配置完后需要延时一会
}
void putch(u8 c)
{
TXREG = c;
while(TRMT==0);
}
void usart_init(void)
{
TXSTA=0x24; //开启发射使能位、高波特率,TRMT初始值可0可1
RCSTA=0x80;
SPBRG=0x19; //4M晶振,波特率9600,则SPBRG初值为25
}
void main()
{
int result=0;
port_init();
usart_init();
printf("hello!\n");
while(1)
{
GO_DONE=1;
while(GO_DONE);
result=ADRESL+(ADRESH<<8);
printf("result=%d\n",result);
Delay_ms(500);
}
}