/* ***************************************************************************** * * File Name - adc.c * Function - Reads the voltage on each of the uFlash ADC pins prints the * voltage out each second, * * You will need: * (1) uFlash & & CCS PIC C Compiler (PCM/PCW) * (2) PC Terminal emulator (such as Hyperterm) * * Instructions: * (1) Compile & downloading to the uFlash * (2) Setup Hyprterm for 57K6 Baud,8 bits, no parity, no echo, no handshaking * (3) Remove the Pgrm Jumper from the uFlash (this stops Hyperterm from * holding the uFlash in reset). * (4) Run Hyperterm * (5) Put voltages of between 0 and 5V on pins P4,P5,P6 & P7 & observe readout * ***************************************************************************** */ #include void init_hardware(void); float read_voltage(BYTE ch); /* ******************************** main *************************************** */ void main(void) { init_hardware(); // set up the adc port while(TRUE) // loop forever { printf("%1.2f,",read_voltage(0)); // print each of the voltages printf("%1.2f,",read_voltage(1)); printf("%1.2f,",read_voltage(2)); printf("%1.2f\r",read_voltage(3)); delay_ms(1000); // delay 1 sec } } /* ************************** init_hardware ************************************ * * Sets up the uFlash ADC port */ void init_hardware(void) { setup_adc(ADC_CLOCK_DIV_32); setup_adc_ports(ALL_ANALOG); } /* ************************** read_voltage ************************************ * * Reads the adc value on a specified port and returns it scaled * as a voltage. */ float read_voltage(BYTE ch) { float v; // the voltage on the channel set_adc_channel(ch); // select the required channel delay_us(500); // allow to settle v=read_adc(); // read the raw adc value v*=(5.0/1024.0); // scale to a voltage return v; // return the voltage }