int Get_ADC_Val(char* pin) { int channel, intChannel; if(pin == "P15") { channel = ADC_CHANNEL_0; intChannel = ADC_ADINTEN0; } else if(pin == "P16") { channel = ADC_CHANNEL_1; intChannel = ADC_ADINTEN1; } else if(pin == "P17") { channel = ADC_CHANNEL_2; intChannel = ADC_ADINTEN2; } else if(pin == "P18") { channel = ADC_CHANNEL_3; intChannel = ADC_ADINTEN3; } ADC_IntConfig(LPC_ADC, intChannel, DISABLE); ADC_ChannelCmd(LPC_ADC, channel, ENABLE); ADC_StartCmd(LPC_ADC, ADC_START_NOW); while(!(ADC_ChannelGetStatus(LPC_ADC, channel, ADC_DATA_DONE))); uint32_t adcVal= ADC_ChannelGetData(LPC_ADC, channel); ADC_ChannelCmd(LPC_ADC, channel, DISABLE); return (int) adcVal; }
uint16_t ADC_read() { ADC_StartCmd(LPC_ADC, ADC_START_NOW); while(!ADC_ChannelGetStatus(LPC_ADC, 4, 1)); return(ADC_ChannelGetData(LPC_ADC, 4)); }
/*********************************************************************//** * @brief c_entry: Main ADC program body * @param[in] None * @return None **********************************************************************/ void c_entry(void) { volatile uint32_t adc_value, tmp; uint8_t quit; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* Initialize ADC ----------------------------------------------------*/ /* * Init ADC pin that currently is being used on the board */ PINSEL_ConfigPin (BRD_ADC_PREPARED_CH_PORT, BRD_ADC_PREPARED_CH_PIN, BRD_ADC_PREPARED_CH_FUNC_NO); PINSEL_SetAnalogPinMode(BRD_ADC_PREPARED_CH_PORT,BRD_ADC_PREPARED_CH_PIN,ENABLE); /* Configuration for ADC : * ADC conversion rate = 400Khz */ ADC_Init(LPC_ADC, 400000); ADC_IntConfig(LPC_ADC, BRD_ADC_PREPARED_INTR, DISABLE); ADC_ChannelCmd(LPC_ADC, BRD_ADC_PREPARED_CHANNEL, ENABLE); while(1) { // Start conversion ADC_StartCmd(LPC_ADC, ADC_START_NOW); //Wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC, BRD_ADC_PREPARED_CHANNEL, ADC_DATA_DONE))); adc_value = ADC_ChannelGetData(LPC_ADC, BRD_ADC_PREPARED_CHANNEL); //Display the result of conversion on the UART _DBG("ADC value on channel "); _DBD(BRD_ADC_PREPARED_CHANNEL); _DBG(" is: "); _DBD32(adc_value); _DBG_(""); //delay for(tmp = 0; tmp < 1000000; tmp++); if(_DG_NONBLOCK(&quit) && (quit == 'Q' || quit == 'q')) break; } _DBG_("Demo termination!!!"); ADC_DeInit(LPC_ADC); }
/*********************************************************************//** * @brief ADC interrupt handler sub-routine * @param[in] None * @return None **********************************************************************/ void ADC_IRQHandler(void) { adc_value = 0; if (ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_2,ADC_DATA_DONE)) { adc_value = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_2); NVIC_DisableIRQ(ADC_IRQn); } }
uint32_t airSpeedValGet(void) { // start conversion ADC_StartCmd(LPC_ADC,ADC_START_NOW); // wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC, AIR_CHAN, ADC_DATA_DONE))); // return the value return ADC_ChannelGetData(LPC_ADC, AIR_CHAN); }
uint16_t AnalogIn::readBits() { ADC_ChannelCmd(LPC_ADC, channel, ENABLE); ADC_StartCmd(LPC_ADC, ADC_START_NOW); while(!(ADC_ChannelGetStatus(LPC_ADC, channel, ADC_DATA_DONE))); uint16_t bits = ADC_ChannelGetData(LPC_ADC, channel); ADC_ChannelCmd(LPC_ADC, channel, DISABLE); return bits; }
uint16_t getADC(int channel) { uint16_t adc_value; uint16_t adc_value1; uint16_t adc_value2; // Start conversion ADC_StartCmd(LPC_ADC,ADC_START_NOW); //Wait conversion complete ADC_IntConfig(LPC_ADC,ADC_ADINTEN2,RESET); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_2,ENABLE); while (!(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_2,ADC_DATA_DONE))); adc_value = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_2); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_2,DISABLE); ADC_IntConfig(LPC_ADC,ADC_ADINTEN1,RESET); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,ENABLE); while (!(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_1,ADC_DATA_DONE))); adc_value1 = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_1); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,DISABLE); ADC_IntConfig(LPC_ADC,ADC_ADINTEN0,RESET); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,ENABLE); while (!(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_0,ADC_DATA_DONE))); adc_value2 = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_0); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_0,DISABLE); if (channel == 0) { return adc_value; } if (channel == 1) { return adc_value1; } if (channel == 2) { return adc_value2; } else { return 0; } }
// Handle ADC interrupts // NOTE: This could probably be less complicated... void ADC_IRQHandler(void) { elua_adc_dev_state *d = adc_get_dev_state( 0 ); elua_adc_ch_state *s = d->ch_state[ d->seq_ctr ]; //int i; // Disable sampling & current sequence channel ADC_StartCmd( LPC_ADC, 0 ); ADC_ChannelCmd( LPC_ADC, s->id, DISABLE ); ADC_IntConfig( LPC_ADC, s->id, DISABLE ); if ( ADC_ChannelGetStatus( LPC_ADC, s->id, ADC_DATA_DONE ) ) { d->sample_buf[ d->seq_ctr ] = ( u16 )ADC_ChannelGetData( LPC_ADC, s->id ); s->value_fresh = 1; if ( s->logsmoothlen > 0 && s->smooth_ready == 0) adc_smooth_data( s->id ); #if defined( BUF_ENABLE_ADC ) else if ( s->reqsamples > 1 ) { buf_write( BUF_ID_ADC, s->id, ( t_buf_data* )s->value_ptr ); s->value_fresh = 0; } #endif if ( adc_samples_available( s->id ) >= s->reqsamples && s->freerunning == 0 ) platform_adc_stop( s->id ); } // Set up for next channel acquisition if we're still running if( d->running == 1 ) { // Prep next channel in sequence, if applicable if( d->seq_ctr < ( d->seq_len - 1 ) ) d->seq_ctr++; else if( d->seq_ctr == ( d->seq_len - 1 ) ) { adc_update_dev_sequence( 0 ); d->seq_ctr = 0; // reset sequence counter if on last sequence entry } ADC_ChannelCmd( LPC_ADC, d->ch_state[ d->seq_ctr ]->id, ENABLE ); ADC_IntConfig( LPC_ADC, d->ch_state[ d->seq_ctr ]->id, ENABLE ); if( d->clocked == 1 && d->seq_ctr == 0 ) // always use clock for first in clocked sequence ADC_StartCmd( LPC_ADC, adc_trig[ d->timer_id ] ); // Start next conversion if unclocked or if clocked and sequence index > 0 if( ( d->clocked == 1 && d->seq_ctr > 0 ) || d->clocked == 0 ) ADC_StartCmd( LPC_ADC, ADC_START_NOW ); } }
uint32_t adc_get(uint32_t channel) { uint32_t res; ADC_ChannelCmd(LPC_ADC0, channel, ENABLE); delayus(500); ADC_StartCmd(LPC_ADC0, ADC_START_NOW); while (!(ADC_ChannelGetStatus(LPC_ADC0, channel, ADC_DATA_DONE))); res = ADC_ChannelGetData(LPC_ADC0, channel); ADC_ChannelCmd(LPC_ADC0, channel, DISABLE); return res; }
int adc_get_channel_data(int channel) { /* Retrieve the 12 bit output from the ADC as an `int` given channel 0 .. 7 */ ADC_StartCmd(LPC_ADC, ADC_START_NOW); while (!ADC_ChannelGetStatus(LPC_ADC, channel, ADC_DATA_DONE)); int data = ADC_ChannelGetData(LPC_ADC, channel); /* Send `data' to PC */ record(ADC_HEADER, &data, sizeof(int), NULL); return data; }
/** * @brief Main Program body */ int c_entry(void) { PINSEL_CFG_Type PinCfg; uint32_t adc_value; /* * Initialize debug via UART */ debug_frmwrk_init(); // print welcome screen print_menu(); /* * Init ADC pin connect * AD0.2 on P0.25 */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 25; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); /* Configuration for ADC : * Frequency at 1Mhz * ADC channel 2, no Interrupt */ ADC_Init(LPC_ADC, 1000000); ADC_IntConfig(LPC_ADC,ADC_ADINTEN2,DISABLE); ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_2,ENABLE); while(1) { // Start conversion ADC_StartCmd(LPC_ADC,ADC_START_NOW); //Wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_2,ADC_DATA_DONE))); adc_value = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_2); //Display the result of conversion on the UART0 _DBG("ADC value on channel 2: "); _DBD32(adc_value); _DBG_(""); //delay 1s Timer_Wait(1000); } ADC_DeInit(LPC_ADC); return 1; }
// FIXME: de qAnalogInit is not really necessary, but without it consecutive readings are faulty (the second one is corrupted) // Maybe a BURST mode is preferible in this case but only 2 analogs are beign used right now. uint16_t qAnalog_Read(qAnalogInput * q){ uint16_t ret; qAnalog_Init(); ADC_ChannelCmd(LPC_ADC,q->adcChannel,ENABLE); ADC_StartCmd(LPC_ADC,ADC_START_NOW); //Wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC,q->adcChannel,ADC_DATA_DONE))); ret = ADC_ChannelGetData(LPC_ADC,q->adcChannel); ADC_ChannelCmd(LPC_ADC,q->adcChannel,DISABLE); return ret; }
int main(void) { serial_init(); init_adc(); init_dac(); //SysTick_Config(SystemCoreClock / 6); uint16_t adc_value; while(1) { // Read analogue value ADC_StartCmd(LPC_ADC,ADC_START_NOW); // Wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC,ADC_CHANNEL_1,ADC_DATA_DONE))); adc_value = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_1); DAC_UpdateValue(LPC_DAC, adc_value / 4); } return 0; }
int _analogReadAll(uint8_t * args) { uint8_t start_mode = 1; //Start now uint8_t adc_data_done = 1; //Data_done int pin[]={0,2,3}; int n,nb_pin=3; unsigned int adc_values[3]; for(n=0;n<nb_pin;n++) { ADC_Init(LPC_ADC, 200000); ADC_ChannelCmd(LPC_ADC, pin[n], ENABLE); ADC_StartCmd(LPC_ADC, start_mode); while( ! ADC_ChannelGetStatus(LPC_ADC, pin[n], adc_data_done)) {;} adc_values[n]= ADC_ChannelGetData(LPC_ADC, pin[n]); } sprintf( (char*) str, "%x %x %x\r\n", adc_values[0], adc_values[1], adc_values[2]); writeUSBOutString(str); return 0; }
int ADC_GetChannelData(int chan){ ADC_StartCmd(LPC_ADC, ADC_START_NOW); while(!(ADC_ChannelGetStatus(LPC_ADC, chan, ADC_DATA_DONE))); return ADC_ChannelGetData(LPC_ADC, chan); }
/*********************************************************************//** * @brief c_entry: Main ADC program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { PINSEL_CFG_Type PinCfg; uint32_t adc_value, tmp; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* Initialize ADC ----------------------------------------------------*/ /* Because the potentiometer on different boards (MCB & IAR) connect * with different ADC channel, so we have to configure correct ADC channel * on each board respectively. * If you want to check other ADC channels, you have to wire this ADC pin directly * to potentiometer pin (please see schematic doc for more reference) */ #ifdef MCB_LPC_1768 /* * Init ADC pin connect * AD0.2 on P0.25 */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 25; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); #elif defined (IAR_LPC_1768) /* * Init ADC pin connect * AD0.5 on P1.31 */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 31; PinCfg.Portnum = 1; PINSEL_ConfigPin(&PinCfg); #endif /* Configuration for ADC : * Select: ADC channel 2 (if using MCB1700 board) * ADC channel 5 (if using IAR-LPC1768 board) * ADC conversion rate = 200Khz */ ADC_Init(LPC_ADC, 200000); ADC_IntConfig(LPC_ADC,_ADC_INT,DISABLE); ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL,ENABLE); while(1) { // Start conversion ADC_StartCmd(LPC_ADC,ADC_START_NOW); //Wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC,_ADC_CHANNEL,ADC_DATA_DONE))); adc_value = ADC_ChannelGetData(LPC_ADC,_ADC_CHANNEL); //Display the result of conversion on the UART0 #ifdef MCB_LPC_1768 _DBG("ADC value on channel 2: "); #elif defined (IAR_LPC_1768) _DBG("ADC value on channel 5: "); #endif _DBD32(adc_value); _DBG_(""); //delay for(tmp = 0; tmp < 1000000; tmp++); } ADC_DeInit(LPC_ADC); return 1; }
/* ****************** * * * AllInOne * * * **********************8*/ int _AllInOne(uint8_t * args) { /*------------UART----------------*/ int n; uint8_t * arg_ptr; uint8_t txbuf[13], rxbuf[18]; uint32_t txbuflen=13, rxbuflen=18, length; LPC_UART_TypeDef* UART1=(LPC_UART_TypeDef*) LPC_UART1; static unsigned int message_number=0; for(n=0;n<txbuflen;n++)//reading of the arguments { if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1; txbuf[n] = (uint8_t) strtoul((char *) arg_ptr, NULL, 16); } UART_Send((LPC_UART_TypeDef*)(LPC_UART1), txbuf, txbuflen, BLOCKING); length = UART_Receive(UART1, rxbuf, rxbuflen, NONE_BLOCKING); /*------------ADC----------------*/ uint8_t start_mode = 1; //Start now uint8_t adc_data_done = 1; //Data_done int pin[]={0,2,3};//pin of the analog inputs int nb_pin=3; unsigned int adc_values[3]; for(n=0;n<nb_pin;n++) { ADC_Init(LPC_ADC, 200000); ADC_ChannelCmd(LPC_ADC, pin[n], ENABLE); ADC_StartCmd(LPC_ADC, start_mode); while( ! ADC_ChannelGetStatus(LPC_ADC, pin[n], adc_data_done)) {;} adc_values[n]= ADC_ChannelGetData(LPC_ADC, pin[n]); } /*------------Gyro----------------*/ uint8_t receive_buffer[6]; I2C_M_SETUP_Type setup; int16_t* axis_data; Status result; uint8_t transmit_buffer; uint8_t axis_enable_bit; setup.sl_addr7bit = GYRO_I2C_SLAVE_ADDRESS; setup.retransmissions_max = MAX_ST_I2C_RETRANSMISSIONS; setup.tx_data = &transmit_buffer; setup.tx_length = 1; setup.rx_data = receive_buffer; setup.rx_length = 6; transmit_buffer = GYRO_DATA_ADDRESS|ST_I2C_AUTOINCREMENT_ADDRESS; result = I2C_MasterTransferData(LPC_I2C0, &setup, I2C_TRANSFER_POLLING); if(result == ERROR) { return 1; } axis_enable_bit = GYRO_CTRL_REG_X_ENABLE; axis_data = (int16_t*)receive_buffer; unsigned int gyro[3]={0,0,0}; int index=0; do { if(gyro_ctrl_reg_1_value&axis_enable_bit) { gyro[index++] = (unsigned int)(*axis_data+GYRO_VALUE_OFFSET); } axis_enable_bit <<= 1; axis_data++; }while(axis_enable_bit <= GYRO_CTRL_REG_Z_ENABLE); /*------------Accel----------------*/ /*uint8_t receive_buffer[6]; I2C_M_SETUP_Type setup; int16_t* axis_data; Status result; uint8_t transmit_buffer; uint8_t axis_enable_bit; */ setup.sl_addr7bit = ACCEL_I2C_SLAVE_ADDRESS; //setup.retransmissions_max = MAX_ST_I2C_RETRANSMISSIONS; //setup.tx_data = &transmit_buffer; //setup.tx_length = 1; //setup.rx_data = receive_buffer; //setup.rx_length = 6; transmit_buffer = ACCEL_DATA_ADDRESS|ST_I2C_AUTOINCREMENT_ADDRESS; result = I2C_MasterTransferData(LPC_I2C0, &setup, I2C_TRANSFER_POLLING); if(result == ERROR) { return 1; } axis_enable_bit = ACCEL_CTRL_REG_X_ENABLE; axis_data = (int16_t*)receive_buffer; unsigned int accel[3]={0,0,0}; index=0; do { if(accel_ctrl_reg_1_a_value&axis_enable_bit) { accel[index++] = (unsigned int)(*axis_data+ACCEL_VALUE_OFFSET); } axis_enable_bit <<= 1; axis_data++; }while(axis_enable_bit <= ACCEL_CTRL_REG_Z_ENABLE); /*------------Mag----------------*/ /* uint8_t receive_buffer[6]; I2C_M_SETUP_Type setup;*/ unsigned int mag_x=0; unsigned int mag_y=0; unsigned int mag_z=0; /* Status result; uint8_t transmit_buffer; */ setup.sl_addr7bit = MAG_I2C_SLAVE_ADDRESS; //setup.retransmissions_max = MAX_ST_I2C_RETRANSMISSIONS; //setup.tx_data = &transmit_buffer; //setup.tx_length = 1; //setup.rx_data = receive_buffer; //setup.rx_length = 6; transmit_buffer = MAG_DATA_ADDRESS|ST_I2C_AUTOINCREMENT_ADDRESS; result = I2C_MasterTransferData(LPC_I2C0, &setup, I2C_TRANSFER_POLLING); if(result == ERROR) { return 1; } mag_x = MagDataToUInt32(&receive_buffer[0]); mag_y = MagDataToUInt32(&receive_buffer[2]); mag_z = MagDataToUInt32(&receive_buffer[4]); sprintf( (char*) str, "%x %lx %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\r\n",message_number++,length, rxbuf[0], rxbuf[1], rxbuf[2], rxbuf[3], rxbuf[4], rxbuf[5],rxbuf[6], rxbuf[7], rxbuf[8], rxbuf[9], rxbuf[10], rxbuf[11], rxbuf[12], rxbuf[13], rxbuf[14], rxbuf[15], rxbuf[16], rxbuf[17], adc_values[0], adc_values[1], adc_values[2],accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2],mag_x,mag_y,mag_z); writeUSBOutString(str); return 0; }