CCS C and Microchip ADC – Analog Input

Last night I was messing around with some stuff just for experimentation’s sake. I might use this in my Datalogger though, so I thought I would explore. I’m using the Pickit 2 to do some experiments on and learn new code. Here’s what I’ve discovered.

Using a potentiometer to trigger events:

In digital inputs, a high (1) is close to 5 volts, and a low (0) is close to ground. That’s all you get. Analog inputs are capable of many intermediate values ranging from either 0 – 255, or more depending on what chip you are using. I’m using the PIC16F690, and it has an 8-bit ADC. In this case ground will return 0×00 (0) and vdd will return 0xff (255).

#include <16f690.h>
#fuses intrc_io, nowdt, nobrownout, put
#use delay(clock=4000000)

void main(void)
{
unsigned int adcByte;
setup_adc(adc_clock_internal); //The ADC uses a clock to work
setup_adc_ports(sAN0); //pin 19 ADC0
set_adc_channel(0); //This ADC0 is channel 0

while (1) //stay in this loop until reset
{
adcByte=read_adc(); //read_adc() return an 8-bit variable
/*I’m using the byte returned as the delay for an LED */
output_high(PIN_C5); delay_ms(adcByte);
output_low(PIN_C5); delay_ms(adcByte);
}
}

 



This entry was posted in Electronics and tagged , , , , , , , by robbie. Bookmark the permalink.

About robbie

I am an electronics enthusiest and a ham radio operator (W1RCP). I like to play with electronics. It's fun and educational. I looked forward to working in the engineering field in the future. I have a BS in Electronics Engineering Technology from DeVry University. I also have an Associate's degree in Marketing Management from Moultrie Tech, and a diploma in Electronics from MTC.

Comments are closed.