在线时间2208 小时
UID2073122
注册时间2016-1-12
NXP金币119
TA的每日心情 | 开心 2020-6-18 08:45 |
---|
签到天数: 812 天 连续签到: 1 天 [LV.10]以坛为家III
金牌会员
 
- 积分
- 6321
- 最后登录
- 2025-7-15
|
本节我们讲解LPC824的ADC功能,LPC824的ADC特性如下:
int main(void)
{
uint32_t rawSample;
int j;
SystemCoreClockUpdate();
Board_Init();
DEBUGSTR("ADC sequencer demo\r\n");
DEBUGOUT("System Clock: %uMHz\r\n", SystemCoreClock / 1000000);
DEBUGOUT("Device ID: 0x%x\r\n", Chip_SYSCTL_GetDeviceID());
/* Setup ADC for 12-bit mode and normal power */
Chip_ADC_Init(LPC_ADC, 0);//初始化ADC为12位,正常供电模式
/* Need to do a calibration after initialization and trim */
Chip_ADC_StartCalibration(LPC_ADC);//校准ADC
while (!(Chip_ADC_IsCalibrationDone(LPC_ADC))) {}//等待ADC校准完成
/* Setup sequencer A for ADC channel 3, EOS interrupt */
/* Setup a sequencer to do the following:
Perform ADC conversion of ADC channels 3 only */
Chip_ADC_SetupSequencer(LPC_ADC, ADC_SEQA_IDX,
(ADC_SEQ_CTRL_CHANSEL(BOARD_ADC_CH) | ADC_SEQ_CTRL_MODE_EOS));
/* Enable the clock to the Switch Matrix */
Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);//使能开关矩阵时钟
/* Configure the SWM for P0-23 as the input for the ADC1 */
Chip_SWM_EnableFixedPin(SWM_FIXED_ADC3);//配置P0-23为ADC1输入脚
/* Disable the clock to the Switch Matrix to save power */
Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);//关闭开关矩阵时钟节能
/* Setup threshold 0 low and high values to about 25% and 75% of max */
Chip_ADC_SetThrLowValue(LPC_ADC, 0, ((1 * 0xFFF) / 4));//设置ADC最低转换门限有25%
Chip_ADC_SetThrHighValue(LPC_ADC, 0, ((3 * 0xFFF) / 4));//设置ADC最高转换门限有75%
/* Clear all pending interrupts */
Chip_ADC_ClearFlags(LPC_ADC, Chip_ADC_GetFlags(LPC_ADC));//清除ADC中断标志
/* Enable ADC overrun and sequence A completion interrupts */
Chip_ADC_EnableInt(LPC_ADC, (ADC_INTEN_SEQA_ENABLE | ADC_INTEN_OVRRUN_ENABLE));//使能ADC超限和转换完成中断
/* Use threshold 0 for ADC channel and enable threshold interrupt mode for
channel as crossing */
Chip_ADC_SelectTH0Channels(LPC_ADC, ADC_THRSEL_CHAN_SEL_THR1(BOARD_ADC_CH));//选择门限通道
Chip_ADC_SetThresholdInt(LPC_ADC, BOARD_ADC_CH, ADC_INTEN_THCMP_CROSSING);//使能门限中断
/* Enable ADC NVIC interrupt */
NVIC_EnableIRQ(ADC_SEQA_IRQn);//使能ADC NVIC
/* Enable sequencer */
Chip_ADC_EnableSequencer(LPC_ADC, ADC_SEQA_IDX);//使能ADC转换
/* This example uses the periodic sysTick to manually trigger the ADC,
but a periodic timer can be used in a match configuration to start
an ADC sequence without software intervention. */
SysTick_Config(SystemCoreClock / TICKRATE_HZ);
/* Endless loop */
while (1) {
/* Sleep until something happens */
__WFI();
if (thresholdCrossed) {
thresholdCrossed = false;
DEBUGSTR("********ADC threshold event********\r\n");
}
/* Is a conversion sequence complete? */
if (sequenceComplete) {
sequenceComplete = false;
/* Get raw sample data for channels 0-11 */
for (j = 0; j < 12; j++) {
rawSample = Chip_ADC_GetDataReg(LPC_ADC, j);
/* Show some ADC data */
if (rawSample & (ADC_DR_OVERRUN | ADC_SEQ_GDAT_DATAVALID)) {
DEBUGOUT("Chan: %d Val: %d\r\n", j, ADC_DR_RESULT(rawSample));
DEBUGOUT("Threshold range: 0x%x ", ADC_DR_THCMPRANGE(rawSample));
DEBUGOUT("Threshold cross: 0x%x\r\n", ADC_DR_THCMPCROSS(rawSample));
DEBUGOUT("Overrun: %s \r\n", (rawSample & ADC_DR_OVERRUN) ? "true" : "false");
DEBUGOUT("Data Valid: %s\r\n\r\n", (rawSample & ADC_SEQ_GDAT_DATAVALID) ? "true" : "false");
}
}
}
}
}
|
|