Chip_IOCON_PinMuxSet(LPC_IOCON,0,8,IOCON_MODE_INACT); Chip_SWM_EnableFixedPin(SWM_FIXED_ADC0_0); Chip_IOCON_PinMuxSet(LPC_IOCON,0,7,IOCON_MODE_INACT); Chip_SWM_EnableFixedPin(SWM_FIXED_ADC0_1); Chip_IOCON_PinMuxSet(LPC_IOCON,0,6,IOCON_MODE_INACT); Chip_SWM_EnableFixedPin(SWM_FIXED_ADC0_2); Chip_IOCON_PinMuxSet(LPC_IOCON,0,5,IOCON_MODE_INACT); Chip_SWM_EnableFixedPin(SWM_FIXED_ADC0_3); Chip_IOCON_PinMuxSet(LPC_IOCON,0,4,IOCON_MODE_INACT); Chip_SWM_EnableFixedPin(SWM_FIXED_ADC0_4); Chip_IOCON_PinMuxSet(LPC_IOCON,0,3,IOCON_MODE_INACT); Chip_SWM_EnableFixedPin(SWM_FIXED_ADC0_5); |
Chip_ADC_Init(LPC_ADC0, 0); Chip_ADC_StartCalibration(LPC_ADC0); while (!(Chip_ADC_IsCalibrationDone(LPC_ADC0))); Chip_ADC_SetClockRate(LPC_ADC0, ADC_MAX_SAMPLE_RATE / 4); Chip_ADC_DisableSequencer(LPC_ADC0,ADC_SEQA_IDX); Chip_ADC_SetSequencerBits(LPC_ADC0,ADC_SEQA_IDX,(0x3F | ADC_SEQ_CTRL_MODE_EOS)); Chip_ADC_ClearFlags(LPC_ADC0, Chip_ADC_GetFlags(LPC_ADC0)); Chip_ADC_EnableSequencer(LPC_ADC0, ADC_SEQA_IDX); |
Chip_ADC_StartSequencer(LPC_ADC0, ADC_SEQA_IDX); uint32_t sample = 0; do { sample = Chip_ADC_GetDataReg(LPC_ADC0,channel); } while (!(sample & ADC_SEQ_GDAT_DATAVALID)); return ADC_DR_RESULT(sample); |
Chip_ADC_Init(LPC_ADC1, 0); Chip_ADC_SetClockRate(LPC_ADC1, ADC_MAX_SAMPLE_RATE); Chip_ADC_SetupSequencer(LPC_ADC1, ADC_SEQA_IDX, (ADC_SEQ_CTRL_CHANSEL(0) | // AD1[0] P1.1 = Batt 1 current ADC_SEQ_CTRL_CHANSEL(9) | // AD1[9] P0.16 = Batt 2 current ADC_SEQ_CTRL_CHANSEL(8) | // AD1[8] P0.15 = VBUS sense ADC_SEQ_CTRL_CHANSEL(4) | // AD1[4] P1.2 = Batt 1 voltage ADC_SEQ_CTRL_CHANSEL(5) | // AD1[5] P1.3 = Batt 2 voltage ADC_SEQ_CTRL_MODE_EOS)); // We want interrupt on end of sequence Chip_ADC_SetTrim(LPC_ADC1, ADC_TRIM_VRANGE_HIGHV); // Disables pullups/pulldowns and disable digital mode Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 1, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 16, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 15, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 2, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 3, IOCON_MODE_INACT); |
void Adc::Init(Hw::Pin p) { channel = p.adc; if (!setup) { Chip_ADC_Init(LPC_ADC0, 0); Chip_ADC_SetClockRate(LPC_ADC0, ADC_MAX_SAMPLE_RATE / 4); Chip_ADC_SetupSequencer(LPC_ADC0, ADC_SEQA_IDX,ADC_SEQ_CTRL_CHANSEL(channel)); Chip_ADC_SetTrim(LPC_ADC0,ADC_TRIM_VRANGE_LOWV); Chip_ADC_StartCalibration(LPC_ADC0); while (!(Chip_ADC_IsCalibrationDone(LPC_ADC0))); Chip_ADC_ClearFlags(LPC_ADC0, Chip_ADC_GetFlags(LPC_ADC0)); Chip_ADC_EnableSequencer(LPC_ADC0, ADC_SEQA_IDX); setup = true; } else { Chip_ADC_DisableSequencer(LPC_ADC0,ADC_SEQA_IDX); Chip_ADC_SetSequencerBits(LPC_ADC0,ADC_SEQA_IDX,ADC_SEQ_CTRL_CHANSEL(channel)); Chip_ADC_EnableSequencer(LPC_ADC0, ADC_SEQA_IDX); } } uint16_t Adc::Read(void) { Chip_ADC_StartSequencer(LPC_ADC0, ADC_SEQA_IDX); uint32_t sample = 0; uint32_t timeout = scheduler.Millis() + 10; do { sample = Chip_ADC_GetDataReg(LPC_ADC0,channel); } while (!(sample & ADC_DR_DATAVALID) && timeout > scheduler.Millis()); sample = (ADC_DR_RESULT(sample) * 3300) / 4095; return sample; } |
Chip_ADC_Init(LPC_ADC1, ADC_SEQA_IDX); Chip_ADC_SetClockRate(LPC_ADC1, ADC_MAX_SAMPLE_RATE); Chip_ADC_SetupSequencer(LPC_ADC1, ADC_SEQA_IDX, (ADC_SEQ_CTRL_CHANSEL(0) | // AD1[0] P1.1 = Batt 1 current ADC_SEQ_CTRL_CHANSEL(9) | // AD1[9] P0.16 = Batt 2 current ADC_SEQ_CTRL_CHANSEL(8) | // AD1[8] P0.15 = VBUS sense ADC_SEQ_CTRL_CHANSEL(4) | // AD1[4] P1.2 = Batt 1 voltage ADC_SEQ_CTRL_CHANSEL(5) | // AD1[5] P1.3 = Batt 2 voltage ADC_SEQ_CTRL_MODE_EOS)); // We want interrupt on end of sequence Chip_ADC_SetTrim(LPC_ADC1, ADC_TRIM_VRANGE_HIGHV); // Disables pullups/pulldowns and disable digital mode Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 1, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 16, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 15, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 2, IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 3, IOCON_MODE_INACT); // Assign AD channels to their pins via SWM (fixed pins) Chip_SWM_EnableFixedPin(SWM_FIXED_ADC1_0); // P1.1 Chip_SWM_EnableFixedPin(SWM_FIXED_ADC1_9); // P0.16 Chip_SWM_EnableFixedPin(SWM_FIXED_ADC1_8); // P0.15 Chip_SWM_EnableFixedPin(SWM_FIXED_ADC1_4); // P1.2 Chip_SWM_EnableFixedPin(SWM_FIXED_ADC1_5); // P1.3 // Start calibration Chip_ADC_StartCalibration(LPC_ADC1); while (!(Chip_ADC_IsCalibrationDone(LPC_ADC1))) OS_WAIT(10); // Clear any pending interrupts Chip_ADC_ClearFlags(LPC_ADC1, Chip_ADC_GetFlags(LPC_ADC1)); // Enable sequence A completion Chip_ADC_EnableInt(LPC_ADC1, ADC_INTEN_SEQA_ENABLE); NVIC_EnableIRQ(ADC1_SEQA_IRQn); // Enable sequence A, will be started by thAD thread Chip_ADC_EnableSequencer(LPC_ADC1, ADC_SEQA_IDX); |
//----------------------------------------------------------------------------- // IRQ handler for sequence A from AD converter // Called when the sequence is completed //----------------------------------------------------------------------------- void ADC1_SEQA_IRQHandler(void) { unsigned int pending; // Get pending interrupts pending = Chip_ADC_GetFlags(LPC_ADC1); // Sequence A completion interrupt if (pending & ADC_FLAGS_SEQA_INT_MASK) { sequence1Complete = TRUE; } // Clear Sequence A completion interrupt Chip_ADC_ClearFlags(LPC_ADC1, ADC_FLAGS_SEQA_INT_MASK); } |
// Start the process.... Chip_ADC_StartSequencer(LPC_ADC1, ADC_SEQA_IDX); while (1) { // If ADC is complete, go start another conversion if (sequence1Complete) { sequence1Complete=FALSE; // Read all channels (and filter) adc_value[0]=filter(0, ADC_DR_RESULT(Chip_ADC_GetDataReg(LPC_ADC1, 0))); adc_value[1]=filter(1, ADC_DR_RESULT(Chip_ADC_GetDataReg(LPC_ADC1, 9))); adc_value[2]=filter(2, ADC_DR_RESULT(Chip_ADC_GetDataReg(LPC_ADC1, 8))); adc_value[3]=filter(3, ADC_DR_RESULT(Chip_ADC_GetDataReg(LPC_ADC1, 4))); adc_value[4]=filter(4, ADC_DR_RESULT(Chip_ADC_GetDataReg(LPC_ADC1, 5))); // Start another conversion Chip_ADC_StartSequencer(LPC_ADC1, ADC_SEQA_IDX); } .. do other stuff etc .. } |