我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用machine.ADC。
def _config(self, duty_cycle=0): if self._function == 'dig': _mode = machine.Pin.OUT if self._mode == 'out' else machine.Pin.IN if self._pull == 'pu': _pull = machine.Pin.PULL_UP elif self._pull == 'pd': _pull = machine.Pin.PULL_DOWN else: _pull = None self._pin = machine.Pin(self._name, mode=_mode, pull=_pull, drive=machine.Pin.MED_POWER) elif self._function == 'ana': adc = machine.ADC(bits=12) self._apin = adc.channel(pin=self._name) else: self._pwm = machine.PWM(machine.Pin(self.pin_num),duty=duty_cycle * 100,freq=1000) #timer = machine.Timer(HwPin._TimerMap[self._name][0], mode=machine.Timer.PWM) #self._pwm = timer.channel(HwPin._TimerMap[self._name][1], freq=20000, duty_cycle=(duty_cycle * 100))
def read(self): self.pin.value(True) payload = { 'moisture': str(machine.ADC(self.config["gpio_ADC"]).read()) } self.pin.value(False) return payload
def __init__(self, pin, vhigh, vlow): self.sensor = machine.ADC(pin) self.vhigh = vhigh self.vlow = vlow
def __init__(self, name, precision=1, threshold=None, on_change=None, report_change=True): self.precision=precision self.threshold = None if threshold is not None: self.threshold=max(1,min(threshold,1023)) self.current_value=-10000 Device.__init__(self, name, ADC(0), on_change=on_change, report_change=report_change) self.getters[""] = self.value
def __init__(self, sensor_id='adc', min_rd=0, max_rd=1024, min_val=0, max_val=1): '''Initialize sensor min_rd and max_rd are used in sample for sensor calibration min_val and max_val are the sample limits ''' self.sensor_id = sensor_id self.min_rd = min_rd self.max_rd = max_rd self.min_val = min_val self.max_val = max_val self.coef = (max_val - min_val) / (max_rd - min_rd) self.adc = ADC(0)
def read(self) -> int: '''Get a sensor reading using Micropython API Return 0-1024 direct ADC (0~3.3v) reading ''' return self.adc.read()
def sample(self) -> float: '''Get an ADC interpolated reading using ThingFlow sensor API Return min_val~max_val ''' reading = self.read() return self.min_val + (reading - self.min_rd) * self.coef
def read_voltage(): """Read VDD voltage and LiPO battery level.""" voltage = ADC(ADC_NUM).read() / 1000 return { 'voltage': voltage, 'battery': 'low' if voltage < 3.5 else 'ok', }
def set_adc_mode(mode): sector_size = bdev.SEC_SIZE flash_size = esp.flash_size() # device dependent init_sector = int(flash_size / sector_size - 4) data = bytearray(esp.flash_read(init_sector * sector_size, sector_size)) if data[107] == mode: return # flash is already correct; nothing to do else: data[107] = mode # re-write flash esp.flash_erase(init_sector) esp.flash_write(init_sector * sector_size, data) print("ADC mode changed in flash; restart to use it!") return
def battery(): vcc = machine.ADC(1) return vcc.read()
def vcc(): import machine mv = machine.ADC(0) return mv.read() * 1.024
def __init__(self): self.timeout = time() try: self.pin_speed = Pin(self.PIN_SPEED, mode = Pin.IN, pull = Pin.PULL_UP) self.pin_speed.callback(Pin.IRQ_RISING, self.rotations_handler) self.adc = ADC() self.pin_dir = self.adc.channel(pin = self.PIN_DIR) except Exception as e: # Should throw exception to stop execution pass