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