0

您好,目前使用GP2Y1014空污感測器遇到感測器電壓無法透過ADC讀取電壓的問題,我想由於sample code 中的analogin_read_u16()無法在感測器的在LED打開後的0.32ms內完成讀取,進而影響整個感測器的smaple timing,請問有沒有什麼方式能加快讀取ADC電壓的速度?
http://www.beck-elektronik.de/fileadmin/templates/beck_folder/opto/sensor/sharp/an-gp2y1010au.pdf
沒有加analogin_read_u16() function

有加analogin_read_u16() function, LED ON/OFF的時間差了快10ms

#include "device.h"
#include "analogin_api.h"
#include <sys_api.h>
#include "gpio_api.h"

#define ADC_CALIBRATION 0
#define GPIO_SHARP_P2 PB_5
#define MBED_ADC_EXAMPLE_PIN_2 AD_2 // HDK, A1

#if defined (__ICCARM__)
analogin_t adc1;
#else
volatile analogin_t adc1;
#endif

void adc_delay(void)
{
int i;
for(i=0;i<1600000;i++)
asm(" nop");
}
uint16_t adcdat1 = 0;

int32_t v_mv1;

/*
* OFFSET: value of measuring at 0.000v, value(0.000v)
* GAIN_DIV: value(1.000v)-value(0.000v) or value(2.000v)-value(1.000v) or value(3.000v)-value(2.000v)
*
* MSB 12bit of value is valid, need to truncate LSB 4bit (0xABCD -> 0xABC). OFFSET and GAIN_DIV are truncated values.
*/
#define OFFSET 0x298
#define GAIN_DIV 0x34C
#define AD2MV(ad,offset,gain) (((ad/16)-offset)*1000/gain)

VOID
main (
VOID
)
{
gpio_t gpio_sharp_p2; // gpio for sharp p2 led on/off
gpio_init(&gpio_sharp_p2, GPIO_SHARP_P2);
gpio_dir(&gpio_sharp_p2, PIN_OUTPUT);
gpio_mode(&gpio_sharp_p2, PullNone);
uint16_t offset, gain;
analogin_init(&adc1, MBED_ADC_EXAMPLE_PIN_2);
#if ADC_CALIBRATION
sys_adc_calibration(0, &offset, &gain);
printf("ADC:offset = 0x%x, gain = 0x%x\n", offset, gain);
if((offset==0xFFFF) || (gain==0xFFFF))
#endif
{
offset = OFFSET;
gain = GAIN_DIV;
printf("ADC:offset = 0x%x, gain = 0x%x\n", offset, gain);
}
for (;;){
gpio_write(&gpio_sharp_p2, 0);
RtlUdelayOS(270);
adcdat1 = analogin_read_u16(&adc1);
RtlUdelayOS(10);
gpio_write(&gpio_sharp_p2, 1);

RtlUdelayOS(9680);

}
}

sean 回答了