|
/**********************************************************************<br />* ?2005 Microchip Technology Inc.<br />*<br />* FileName: ADC.c<br />* Dependencies: Header (.h) files if applicable, see below<br />* Processor: dsPIC30Fxxxx<br />* Compiler: MPLAB?C30 v1.32.00 or higher<br />*<br />* SOFTWARE LICENSE AGREEMENT:<br />* Microchip Technology Inc. (揗icrochip? licenses this software to you<br />* solely for use with Microchip dsPIC?digital signal controller<br />* products. The software is owned by Microchip and is protected under<br />* applicable copyright laws. All rights reserved.<br />*<br />* SOFTWARE IS PROVIDED 揂S IS.? MICROCHIP EXPRESSLY DISCLAIMS ANY<br />* WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING BUT NOT<br />* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A<br />* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP<br />* BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL<br />* DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT, COST OF<br />* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS<br />* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),<br />* ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS.<br />*<br />* REVISION HISTORY:<br />*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />* Author Date Comments on this revision<br />*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />* Hrushikesh Vasuki 07/29/05 First release of source file<br />*<br />*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />*<br />* ADDITIONAL NOTES:<br />* This file contains two functions - ADC_Init() and _ADCInterrupt().<br />*<br />**********************************************************************/<br /><br />#include "p30fxxxx.h"<br /><br /><br />unsigned int ADResult1 = 0;<br />unsigned int ADResult2 = 0;<br /><br />//Functions and Variables with Global Scope:<br />void ADC_Init(void);<br />void __attribute__((__interrupt__)) _ADCInterrupt(void);<br /><br />//Functions:<br />//ADC_Init() is used to configure A/D to convert 16 samples of 1 input<br />//channel per interrupt. The A/D is set up for a sampling rate of 1MSPS<br />//Timer3 is used to provide sampling time delay.<br />//The input pin being acquired and converted is AN7.<br />void ADC_Init(void)<br />{<br /> //ADCON1 Register<br /> //Set up A/D for Automatic Sampling<br /> //Use internal counter (SAMC) to provide sampling time<br /> //Set up A/D conversrion results to be read in 1.15 fractional<br /> //number format.<br /> //Set up Sequential sampling for multiple S/H amplifiers<br /> //All other bits to their default state<br /> ADCON1bits.FORM = 3;<br /> ADCON1bits.SSRC = 7;<br /> ADCON1bits.ASAM = 1;<br /> ADCON1bits.SIMSAM = 0;<br /><br /> //ADCON2 Register<br /> //Set up A/D for interrupting after 2 samples get filled in the buffer<br /> //Set up to sample on 2 S/H amplifiers - CH0 and CH1<br /> //All other bits to their default state<br /> ADCON2bits.SMPI = 1;<br /> ADCON2bits.CHPS = 1;<br /> //ADCON2bits.VCFG = 3; //Ideally use external references<br /><br /> //ADCON3 Register<br /> //We would like to set up a sampling rate of 1 MSPS<br /> //Total Conversion Time= 1/Sampling Rate = 125 microseconds<br /> //At 29.4 MIPS, Tcy = 33.9 ns = Instruction Cycle Time<br /> //The A/D converter will take 12*Tad periods to convert each sample<br /> //So for ~1 MSPS we need to have Tad close to 83.3ns<br /> //Using equaion in the Family Reference Manual we have<br /> //ADCS = 2*Tad/Tcy - 1<br /> ADCON3bits.SAMC = 0;<br /> ADCON3bits.ADCS = 4;<br /><br /> //ADCHS Register<br /> //Set up A/D Channel Select Register to convert AN3 on Mux A input<br /> //of CH0 and CH1 S/H amplifiers<br /> ADCHS = 0x0023;<br /><br /> //ADCSSL Register<br /> //Channel Scanning is disabled. All bits left to their default state<br /> ADCSSL = 0x0000;<br /><br /> //ADPCFG Register<br /> //Set up channels AN7 as analog input and configure rest as digital<br /> //Recall that we configured all A/D pins as digital when code execution<br /> //entered main() out of reset<br /> ADPCFG = 0xFFFF;<br /> ADPCFGbits.PCFG3 = 0;<br /><br /> //Clear the A/D interrupt flag bit<br /> IFS0bits.ADIF = 0;<br /><br /> //Set the A/D interrupt enable bit<br /> IEC0bits.ADIE = 1;<br /><br /> //Turn on the A/D converter<br /> //This is typically done after configuring other registers<br /> ADCON1bits.ADON = 1;<br /><br />}<br /><br />//_ADCInterrupt() is the A/D interrupt service routine (ISR).<br />//The routine must have global scope in order to be an ISR.<br />//The ISR name is chosen from the device linker script.<br />void __attribute__((__interrupt__)) _ADCInterrupt(void)<br />{<br /> ADResult1 = ADCBUF0;<br /> ADResult2 = ADCBUF1;<br /> <br /> //Clear the A/D Interrupt flag bit or else the CPU will<br /> //keep vectoring back to the ISR<br /> IFS0bits.ADIF = 0;<br /> <br /> <br />}<br /><br />
|