#include <htc.h>
#include <stdio.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
unsigned char outbuff[12];
void HSerinit(void), HseroutString(unsigned char* str), Hserout(unsigned char ch);
int getADC(void);
void main(void)
{
int txdata; // data to transmit
TRISA = 0xff ;
HSerinit();
__delay_ms(150);
while(1)
{
txdata = getADC(); // get analogue value on RA0
sprintf(outbuff,"%d\r",txdata); // convert to ascii text
HseroutString(outbuff); // send data
}
}
void HseroutString(unsigned char* str)
{
while(*str != 0) // while all transmitted
Hserout(*str++);
}
void Hserout(unsigned char ch)
{
while(!TXIF);
TXREG = ch;
}
int getADC()
{
ADCON1=0b00000000;
ADCON0=0b10000001; //000 = channel 0, (RA0/AN0)
__delay_ms(10);
GO_DONE=1;
__delay_ms(10);
while(GO_DONE);
return ADRESH;
}
void HSerinit()
{
TRISC = 0x80; // TX was an input!
SPBRG = 129; // 20Mhz xtal 9600 BAUD
TXSTA = 0x24; // TXEN and BRGH
RCSTA = 0x90; // SPEN and CREN
}