📄 f35x_adc0_buffered.c
字号:
stdev = sqrt (variance);
owr = SYSCLK /(ADC0CLK + 1);
owr = owr / (ADC0DEC + 1);
owr = owr / 128;
#endif // USE_FLOAT
// print statistics
#if (PRINT_STATISTICS == 1)
LED1 = 1;
printf ("SYSCLK = %lu\n", (unsigned long) SYSCLK);
printf ("ADC0CLK = 0x%02x\n", (unsigned) ADC0CLK);
printf ("ADC0DEC = 0x%02x%02x\n", (unsigned) ADC0DECH,
(unsigned) ADC0DECL);
printf ("min = %ld\n", min);
printf ("max = %ld\n", max);
#if (USE_FLOAT == 1)
printf ("average = %.2f\n", average);
printf ("stdev = %.2f\n", stdev);
printf ("variance = %.2f\n", variance);
printf ("OWR = %.2f Hz\n", owr);
#else
printf ("average = %ld\n", l_average);
printf ("variance = %ld\n", l_variance);
printf ("OWR = %ld Hz\n", l_owr);
#endif // USE_FLOAT
printf ("\n");
LED1 = 0;
#endif // PRINT_STATISTICS
// print samples
#if (PRINT_SAMPLES == 1)
for (i = 0; i < 128; i++)
{
ADC_OutputVal = sample_array[i];
printf ("%6ld\n", ADC_OutputVal);
// printf ("0x%06lx\n", ADC_OutputVal);
}
#endif // PRINT_SAMPLES
// print voltages
#if (PRINT_VOLTAGES == 1)
for (i = 0; i < 128; i++)
{
long Calculated_uV; // Measured voltage in uV
ADC_OutputVal = sample_array[i];
// Caculate measured voltage in uV:
// V (in uV) = ADCcode * VREF * 10 / 2^24
// Note1: Multiplying by 10 because VREF is in 10^-2 V
// Note2: Shifting by 4 before multiplying 10 to prevent overflow
// of unsigned long variable (32 bits)
Calculated_uV = ((((((ADC_OutputVal*2*VREF)/16)*10)/1024)*1000)/1024);
// Output result:
printf("ADC Output Code = %6ld [Calculated voltage = %+07ld uV]\n",
ADC_OutputVal, Calculated_uV);
}
#endif // PRINT_VOLTAGES
}// end while(1)
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal 24.5MHz
// oscillator as its clock source, with x 2 multiply for
// 49 MHz operation. Also enables missing clock detector reset.
//
void SYSCLK_Init (void)
{
unsigned i;
OSCICN = 0x80; // enable intosc
CLKSEL = 0x00; // select intosc as sysclk source
// INTOSC configure
OSCICN = 0x83;
// PLL configure
CLKMUL = 0x00; // Reset Clock Multiplier
CLKMUL &= ~0x03; // select INTOSC / 2 as PLL source
CLKMUL |= 0x80; // Enable 4x Multipler (MULEN = 1)
for (i = 0; i < 125; i++); // Delay for at least 5us
CLKMUL |= 0xC0; // Initialize Multiplier
while (!(CLKMUL & 0x20)); // Poll for Multiply Ready
// SYSCLK configure
VDM0CN = 0x80; // enable VDD monitor
RSTSRC = 0x06; // enable missing clock detector
// and VDD monitor reset sources
CLKSEL = 0x02; // select PLL as clock source
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.4 - TX0 (push-pull)
// P0.5 - RX0
// P0.6 - LED1 (push-pull)
// P0.7 - LED2 (push-pull)
//
void PORT_Init (void)
{
XBR0 = 0x01; // UART0 Selected
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0xD0; // TX, LEDs = Push-pull
}
//-----------------------------------------------------------------------------
// ADC0_Init extVREF Bipolar AIN0.1-AIN0.0
//-----------------------------------------------------------------------------
//
// This function initializes the ADC to measure across AIN0.1 and AIN0.0
// on the Target Board (Differential measurements, Bipolar codes)
//
void ADC0_Init (void)
{
unsigned ADC0_decimation;
REF0CN &= ~0x01; // disable internal vref
/*
REF0CN |= 0x01; // (enable if using internal vref)
*/
ADC0CN = 0x10; // Bipolar output codes, GAIN=1
/*
ADC0CF = 0x00; // interrupts upon SINC3 filter output
// and uses internal VREF
*/
ADC0CF = 0x04; // interrupts upon SINC3 filter output
// and uses external VREF
// Generate MDCLK for modulator.
// Ideally MDCLK = 2.4576
ADC0CLK = (SYSCLK/MDCLK)-1;
// Ideally, MDCLK = 2.4576 MHz
// ADC0DEC = 0x7FF; // set slowest OWR
// program decimation rate for desired OWR
ADC0_decimation = (unsigned long) SYSCLK/ (unsigned long) OWR /
(unsigned long) (ADC0CLK+1)/(unsigned long)128;
ADC0_decimation--;
ADC0DEC = ADC0_decimation;
ADC0BUF = 0x00; // Turn off Input Buffers
// Select Mux inputs
// ADC0MUX = 0x08; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.0
// AIN- => AGND
// ADC0MUX = 0x00; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.0
// AIN- => AIN0.0
ADC0MUX = 0x01; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.0
// AIN- => AIN0.1
// ADC0MUX = 0x10; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.1
// AIN- => AIN0.0
// ADC0MUX = 0x32; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.3
// AIN- => AIN0.2
// ADC0MUX = 0x54; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.5
// AIN- => AIN0.4
// ADC0MUX = 0x76; // Input pin selection:
// Setup for differential measurements
// AIN+ => AIN0.7
// AIN- => AIN0.6
// ADC0MUX = 0xff; // Input pin selection:
// Setup for differential measurements
// AIN+ => Temp+
// AIN- => Temp-
// ADC0MUX = 0x88; // Input pin selection:
// Setup for differential measurements
// AIN+ => AGND
// AIN- => AGND
ADC0MD = 0x80; // Enable the ADC0 (IDLE Mode)
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void UART0_Init (void)
{
SCON0 = 0x10; // 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -