📄 a2d.c
字号:
//
// Copyright 2003 Texas Instruments
// author: Russell Anderson
//
// A/D Conversion
// Integer test of A/D Converter
//
#include <REG1210.H>
#include <stdio.h>
#define SET 1
#define RESET 0
#define CLEAR 0
extern void autobaud(void);
extern long bipolar(void);
sbit Sync = P1^4;
void dacout (unsigned int val)
{
Sync = SET; // Sync pulse
Sync = CLEAR;
// Note: To avoid SPI overflow, sufficient delay between
// every 3 SPIDATA write is needed.
SPIDATA = 0; // Normal DAC Mode
SPIDATA = (char)(val>>8 & 0x00ff);
SPIDATA = (char)(val & 0x00ff);;
}
void main(void)
{
signed long int data result, dummy, average, max, min;
int k, samples, decimation, pga, gain, j;
CKCON = 0; // 0 MOVX cycle stretch
PDCON = 0x14; // turn on ADC-Vref, SPI and Systimers
/* SPI setup */
SPICON = 0x06; // SCLK=tclk/2, Double-Buf Mode, Order=MSB, Master, CPHA=1, CPOL=0
SPITCON = 0x28; // Enable SCLK, Enable MOSI
autobaud();
printf("\nMSC1210 ADC Conversion Test\n\n");
printf("Gain Dec. rate Ave Value Max-Min Max Min\n");
/* Setup ADC */
ADMUX = 0x08; //(AIN+ = AIN0), (AIN- = AINCOM) Voltage from DAC
ACLK = 9; // ACLK = 11,0592,000/10 = 1,105,920 Hz
// modclock = 1,105,920/64 = 17,280 Hz
samples=15;
gain = 0;
for (pga=0; pga<8 ; pga++) // PGA Loop
{
dacout(0x4000>>pga); // Set DAC to 1/4 full-scale
gain = gain*2;
gain = gain ? gain : 1;
ADCON0 = 0x30 | pga; // Vref On, Vref Hi, Buff off, BOD off, pga
for (decimation=90; decimation<=2070; decimation+=90) // decimation loop
{
ADCON2 = decimation & 0xFF; // LSB of decimation
ADCON3 =(decimation>>8) & 0x07; // MSB of decimation
ADCON1 = 0x01; // bipolar, auto, self calibration (offset, gain)
for (k=0; k<4; k++) { // Wait for Four conversions for filter to settle after calibration
while (!(AIE & 0x20))
; // Wait for data ready
dummy = bipolar(); // Dummy read to clear ADCIRQ
}
max = 0; min = 0x7FFFFFFF;
average = 0;
for (j = 0; j < samples; j++){
while (!(AIE & 0x20))
{} // Wait for next result
result = bipolar();
average += result;
max = (max > result) ? max : result;
min = (min < result) ? min : result;
}
average = average/samples; // Divide by 8
printf ("%3d,%7d,%6dHz,", gain,decimation,17280/decimation); // 17280 = ACLK/64
printf ("%12ld, %10ld, %8ld,", average, max-min, max);
printf (" %8ld\n",min); // One printf() can't handle all of these variables.
}// for decimation loop
} // PGA loop
printf("\n FINISHED \n-----------\n");
while(1)
;
} //main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -