📄 funadc.c
字号:
/****************************************************************
* funadc.c See funlib.h for instuctions *
* Don Luebbe about using this file. *
* 10/12/02 *
* *
* This file defines three ADC functions: initADC(), getADC(), *
* and getallADC(). initADC() initializes the ADC and should *
* be called before attempting to use the ADC and should be *
* called only once. *
* *
* Example: main() *
* { *
* initADC(); *
* ... *
* while(1) *
* { *
* ... *
* } *
* } *
* *
* getADC() accepts one desired channel to sample and returns *
* the results. It should be used when sampling only one *
* channel. *
* *
* Example: adc4 = getADC(4); *
* This function call samples only the ADCIN4 pin *
* and place the value (between 0 - 1023) into the *
* variable adc4. *
* *
* getallADC() accepts the total number of samples desired. *
* It reads the global array ADCchannels[] for which channels *
* are to be sampled. It returns the results in the global *
* array ADCresults[]. ADCchannels can be changed at any point *
* in the program. This method should be used when many *
* near-simultaneous samples are desired. *
* *
* Example: ADCchannels = {5,6,2,2,0,0,...,0,0,}; *
* getallADC(3); *
* adc6 = ADCresults[1]; *
* This code will cause the ADC to sample, in *
* order, channels 5-6-2-2 since the total number *
* of conversions is 3 + 1 = 4. The value from *
* ADCIN6 is then placed in the variable adc6. *
****************************************************************/
#include "f2407_c.h"
#include "funlib.h"
#include <math.h>
int ADCchannels[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int ADCresults[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int initADC(void)
{
*ADCTRL1 = 0x4000; /*reset must be done individually*/
*ADCTRL1 = 0x1010;
/*
bit 15 0: reserved
bit 14 0: reset ADC module
bit 13-12 10: complete conversion before suspend
bit 11-8 0000: acquisition window prescale
bit 7 0: conversion clock prescale
bit 6 0: continuous run
bit 5 0: interrupt priority
bit 4 1: cascaded mode
bit 3 0: calibration enable
bit 2 0: bridge enable
bit 1 0: Vref select
bit 0 0: self test enable
*/
*ADCTRL2 = 0x0000;
/*
bit 15 0: EVB SOC
bit 14 0: reset sequencer1/start calibration
bit 13 0: SOC sequencer
bit 12 0: sequencer busy
bit 11-10 00: interrupt disabled
bit 9 0: interrupt flag
bit 8 0: EVA disable
bit 7 0: external SOC
bit 6 0: reset SEQ2
bit 5 0: SOC SEQ2
bit 4 0: SEQ2 busy
bit 3-2 00: interrupt disabled
bit 1 0: interrupt flag
bit 0 0: EVB disable
*/
return 0;
}
int getADC(int channel)
{
*MAX_CONV = 0; /* Only one conversion needed */
*CHSELSEQ1 = 0x0000 | channel;
*ADCTRL2 = 0x4000; /* reset */
*ADCTRL2 = 0x2000; /* SOC */
while ((*AUTO_SEQ_SR & 0xF00)!=0); /* wait while conversion finishes */
return (*RESULT0 >> 6);
}
int getallADC(int convmax)
{
int i = 0;
*MAX_CONV = convmax - 1; /* Total conversions = MAX_CONV + 1 (ie 3)*/
for (i = 0; i < 4; i++)
{
*CHSELSEQ1 = *CHSELSEQ1 | (ADCchannels[i] << (4*i));
}
for (i = 0; i < 4; i++)
{
*CHSELSEQ2 = *CHSELSEQ2 | (ADCchannels[(i+4)] << (4*i));
}
for (i = 0; i < 4; i++)
{
*CHSELSEQ3 = *CHSELSEQ3 | (ADCchannels[(i+8)] << (4*i));
}
for (i = 0; i < 4; i++)
{
*CHSELSEQ4 = *CHSELSEQ4 | (ADCchannels[(i+12)] << (4*i));
}
/*
CHSELSEQ1 receives ADCchannels 0,1,2,3
CHSELSEQ2 receives ADCchannels 4,5,6,7
CHSELSEQ3 receives ADCchannels 8,9,10,11
CHSELSEQ4 receives ADCchannels 12,13,14,15
*/
*ADCTRL2 = 0x4000; /* reset */
*ADCTRL2 = 0x2000; /* SOC */
while ((*AUTO_SEQ_SR & 0xF00)!=0); /* wait while conversion finishes */
ADCresults[0] = (*RESULT0 >> 6);
ADCresults[1] = (*RESULT1 >> 6);
ADCresults[2] = (*RESULT2 >> 6);
ADCresults[3] = (*RESULT3 >> 6);
ADCresults[4] = (*RESULT4 >> 6);
ADCresults[5] = (*RESULT5 >> 6);
ADCresults[6] = (*RESULT6 >> 6);
ADCresults[7] = (*RESULT7 >> 6);
ADCresults[8] = (*RESULT8 >> 6);
ADCresults[9] = (*RESULT9 >> 6);
ADCresults[10] = (*RESULT10 >> 6);
ADCresults[11] = (*RESULT11 >> 6);
ADCresults[12] = (*RESULT12 >> 6);
ADCresults[13] = (*RESULT13 >> 6);
ADCresults[14] = (*RESULT14 >> 6);
ADCresults[15] = (*RESULT15 >> 6);
/* Store samples into global array ADCresults */
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -