#include <stdio.h>
#include <htc.h>
#include <pic16f877.h>
#include "usart.h"
__CONFIG(DEBUGEN & WDTDIS & LVPDIS); // Setup the configuration word for ise with ICD2
/* Sample code to set up the A2D module */
void init_a2d(void){
ADCON0=0; // select Fosc/2
ADCON1=0x80; // select left justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}
/* Return an 8 bit result */
unsigned ((ADRESH<<8)+(ADRESL))(unsigned char channel){
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}
void main(void){
unsigned char x;
init_a2d(); // initialise the A2D module
GIE=0; // we don't want interrupts
TRISB=0xF0; // the lower four bits of POTRB will be used in output mode
for(;
{
x=read_a2d(1); // sample the analog value on RA0
PORTB = (8>>(x>>6)); // Use the 2 MS Bits of the result to select the bit position of the LED on PORTB
}
INTCON=0; // purpose of disabling the interrupts.
init_comms(); // set up the USART - settings defined in usart.h
// Output a message to prompt the user for a keypress
printf("\rPress a key and I will echo it back:\n");
while(1){
x = getch(); // read a response from the user
printf("\rI detected [%c]",x); // echo it back
}
}