def __init__(self): self.LED_slope = LED_calibration['slope'] self.LED_offset = LED_calibration['offset'] self.volts_per_division = ADC_volts_per_division self.ADC1 = pyb.ADC(pins['analog_1']) self.ADC2 = pyb.ADC(pins['analog_2']) self.DI1 = pyb.Pin(pins['digital_1'], pyb.Pin.IN, pyb.Pin.PULL_DOWN) self.DI2 = pyb.Pin(pins['digital_2'], pyb.Pin.IN, pyb.Pin.PULL_DOWN) self.LED1 = pyb.DAC(1, bits=12) self.LED2 = pyb.DAC(2, bits=12) self.ovs_buffer = array('H', [0] * 64) # Oversampling buffer self.ovs_timer = pyb.Timer(2) # Oversampling timer. self.sampling_timer = pyb.Timer(3) self.usb_serial = pyb.USB_VCP() self.running = False
def __init__(self, pin='A4', pin_adc='C1', currentmax=3.0, vmax=1.5, vref=2.96, amp_gain=10, shunt_resistor=0.05): """Class for different settings of QCL driver. Keyword Arguments: pin {str} -- pin name (default: {'A4'}) currentmax {float} -- maximum current for a given QCL (default: {3.0}) vmax {float} -- maximum voltage for a given QCL (default: {1.5}) vref {float} -- reference voltage of the board (default: {3.0}) amp_gain {int} -- amplifier gain (default: {10}) shunt_resistor {float} -- low resistance precision resistor used to measure current (default: {0.05}) """ self.currentmax = currentmax self.vmax = vmax self.vref = vref self.pin = pyb.Pin(pin) self.pin_adc = pyb.Pin(pin_adc) self.amp_gain = amp_gain self.shunt_resistor = shunt_resistor self.dac = pyb.DAC(self.pin, bits=12) self.adc = pyb.ADC(self.pin_adc)
def __init__(self, pins): if pins['a_out'] == 'X5': self.dac = pyb.DAC(1, bits=12) else: self.dac = pyb.DAC(2, bits=12) self.adc = pyb.ADC(pins['a_in']) self.mic = pyb.ADC(pins['mic']) self.pot = pyb.ADC(pins['pot']) # Virtual Instrument Parameters at start up self.sig_freq = 1000.0 # sinewave frequency self.dc_v = 2020 # DC voltage level self.max_v = 4095 # maximum voltage self.min_v = 0 # minimum voltage self.N_samp = 256 # number of sample in one cycle to generate self.samp_freq = 100 self.buf_size = 4096 self.N_window = 1000 self.duty_cycle = 90 self.function = "Idle" self.buf_max = 8192 self.dac.write(0) # Initialise OLED display self.test_dev = pyb.I2C('Y', pyb.I2C.MASTER) if (not self.test_dev.scan()): self.oled = None else: self.oled = OLED_938(pinout={ 'sda': 'Y10', 'scl': 'Y9', 'res': 'Y8' }, height=64, external_vcc=False, i2c_devid=61) self.oled.poweron() self.oled.init_display() self.oled.draw_text(0, 0, "-- PyBench v1.1 --") self.oled.draw_text(30, 40, "** READY **") self.oled.display() # Initialise IMU - connected to I2C(1) - add handling missing IMU later self.imu = MPU6050(1, False)
def __init__(self, channel=1): assert channel in [1, 2], '! Channel number invalid, must be 1 or 2.' self._DAC = pyb.DAC(channel) self._timer = pyb.Timer(hw.available_timers.pop()) self._func = None # Function currently being used for sweeped sound (sine, square or noise) self._freq = 0 self._freq_ind = 0 self.off() hw.assign_ID(self)
def __init__(self,l=512): self.out = lambda x:0 self.rate = 1 self._outHBuf1 = array('H',(0 for i in range(l*2))) self._outHBuf2 = array('H',self._outHBuf1) self._outHTop1 = memoryview(self._outHBuf1)[l:] self._outHTop2 = memoryview(self._outHBuf2)[l:] self._outFBuf = array('f',self._outHBuf1) #*2 for stereo self._outFShift = memoryview(self._outFBuf)[1:] self.dac_timer = pyb.Timer(6)#16 bit counter has enough accuracy self.buf_timer = pyb.Timer(5)#32 bit for callback self.dac1 = pyb.DAC(1,bits=12) self.dac2 = pyb.DAC(2,bits=12) self._phase = 0 self.error = None self._buffArgs1 = array('f',[0,4095,2048, 0,0,1,0,0,0,0, 0,0,1,0,0,0,0, 0,0,1/(1<<32),0,0,0,0,.9,0, 0]) self._buffArgs2 = array('f',[0,4095,2048, 0,0,1,0,0,0,0, 0,0,1,0,0,0,0, 0,0,1/(1<<32),0,0,0,0,.9,0, 0]) self._nanInds = [8,9,15,16,22,23,25] self.underrun = 0
async def signal(): # Could use write_timed but this prints values dac = pyb.DAC(1, bits=12, buffering=True) v = 0 while True: if not v & 0xf: print('write', v << 4) # Make value u16 as per ADC read dac.write(v) v += 1 v %= 4096 await asyncio.sleep_ms(50)
def test_3(self): dac = pyb.DAC(1) buf = bytearray(8) buf[0]=0 buf[1]=0 buf[2]=8 buf[3]=0 buf[4]=16 buf[5]=0 buf[6]=2 buf[7]=0 r = dac.write_timed(buf, 10000*(int(len(buf)/2)), mode=pyb.DAC.CIRCULAR) self.assertEqual(8,r,"DAC with DMA")
def __init__(self, pin, timer, channel, freq): self.freq = freq self.timer = timer self.channel = channel self.pin = pin if pin == 'X5' or pin == 'X6': self.dac = pyb.DAC(pin) self.buf = bytearray(100) self.dac.write_timed(self.buf, freq * len(self.buf), mode=pyb.DAC.CIRCULAR) elif pin == 'P18': self.ch = pyb.LED(4) else: self.pinpwm = pyb.Pin(pin) timerset = pyb.Timer(self.timer, freq=self.freq) self.ch = timerset.channel(self.channel, pyb.Timer.PWM, pin=self.pinpwm)
import pyb,micropython,machine import uPID micropython.alloc_emergency_exception_buf(100) """ PA1 pwm tim5 ch2 teplota vykon PA3 pwm tim2 ch4 pwmled PA5 DAC 2 Iled PA7 AD PT1000 teplota [-10 - 40C]-> 0-2V """ miliamper = micropython.const(20.0) # max proud v mA prevod = micropython.const(miliamper/4096.0) dacproud = pyb.DAC(2, bits=12) # zapnuti DAC s 12 bity pinLED1 = micropython.const(pyb.Pin.board.PF6) #F6 pin pro zapnuti led pinLED2 = micropython.const(pyb.Pin.board.PG3) #G3 pinLED3 = micropython.const(pyb.Pin.board.PG2) #G2 LED1 = pyb.Pin(pinLED1,pyb.Pin.OUT_PP) # konfigurace vystupu led LED2 = pyb.Pin(pinLED2,pyb.Pin.OUT_PP) LED3 = pyb.Pin(pinLED3,pyb.Pin.OUT_PP) #LED1.value(1) # priklad zapnuti led MHz = micropython.const(84000000) # zakladni frekvence prescal2 = micropython.const(84) # hodnota pro nastaveni preddelice perioda2 = micropython.const(1000000) casovac2 = pyb.Timer(2,prescaler=prescal2-1,period=perioda2-1) # casovac na 1Hz kanal4 = casovac2.channel(4,pyb.Timer.PWM,pin=pyb.Pin.board.PA3) # kanal na PWM kanal4.pulse_width(100) # 100 us prescal5 = micropython.const(4) # hodnota pro nastaveni preddelice perioda5 = micropython.const(1050) casovac5 = pyb.Timer(5,prescaler=prescal5-1,period=perioda5-1) # casovac na 20kHz
import pyb if not hasattr(pyb, 'DAC'): print('SKIP') raise SystemExit dac = pyb.DAC(1) print(dac) dac.noise(100) dac.triangle(100) dac.write(0) dac.write_timed(bytearray(10), 100, mode=pyb.DAC.NORMAL) pyb.delay(20) dac.write(0) # test buffering arg dac = pyb.DAC(1, buffering=True) dac.write(0)
# Demo program for FIR filter module # Author: Peter Hinch # 12th Feb 2015 # Outputs a swept frequency sine wave on Dac1 # Timer interrupt reads the analog input, filters it, and outputs the result on Dac 2. # Requires a link between X5 and X7 import math import pyb import array from fir import fir import micropython micropython.alloc_emergency_exception_buf(100) # Define hardware dac1 = pyb.DAC(1) dac2 = pyb.DAC(2) adc = pyb.ADC("X7") tim = pyb.Timer( 4, freq=2000 ) # Sampling freq 10KHz is about the limit 14KHz without constraint # Data for FIR filter Pass (@2Ksps) 0-40Hz Stop 80Hz-> coeffs = array.array( 'i', (72, 47, 61, 75, 90, 105, 119, 132, 142, 149, 152, 149, 140, 125, 102, 71, 33, -12, -65, -123, -187, -254, -322, -389, -453, -511, -561, -599, -622, -628, -615, -579, -519, -435, -324, -187, -23, 165, 375, 607, 855, 1118, 1389, 1666, 1941, 2212, 2472, 2715, 2938, 3135, 3303, 3437, 3535, 3594, 3614, 3594, 3535, 3437, 3303, 3135, 2938, 2715, 2472, 2212, 1941, 1666, 1389, 1118, 855, 607, 375, 165, -23, -187, -324, -435, -519, -579, -615, -628, -622, -599, -561, -511, -453,
def test_1(self): dac = pyb.DAC(1) r = dac.write(512) self.assertEqual(True,r,"DAC write value")
def polarprint(objDFT): print("Polar: mag (dB) phase (degs)") fstr = "{:6d}{:8.2f} {:8.2f}" for x in range(objDFT.length // 2): # Only the first half is valid print(fstr.format(x, objDFT.re[x], int(math.degrees(objDFT.im[x])))) # ************************** Output waveform generator ********************* # Sinewave amplitude 3.25*127/255 = 1.68Vpk # = 20*log10((3.25*127/255)/sqrt(2)) = 1.17dB relative to 1VRMS OSLEN = const(100) outputsamples = bytearray(128 + int(127 * math.cos(2 * math.pi * x / OSLEN)) for x in range(OSLEN)) dac = pyb.DAC(1) # Pin X5 tim = pyb.Timer(4) tim.init(freq=10000) # 100Hz sinewave dac.write_timed(outputsamples, tim, mode=pyb.DAC.CIRCULAR) # ******************************* Input test ******************************* print('''Issue dftadc.test() Expected result: bin 0 contains the DC offset. bin 10 (sampling 100Hz for 100mS) should contain about +1dB with random phase. ''') mydft = DFTADC(128, 'X7') # Use default timer 6
@micropython.asm_thumb def addr(r0): pass #leds: #1 - red #2 - grn #3 - yel #4 - blu pyb.LED(2).toggle() pyb.delay(250) pyb.LED(2).toggle() mix.dac1 = pyb.DAC(1, bits=12) mix.dac2 = pyb.DAC(2, bits=12) mix.dac_timer = pyb.Timer(6) mix.buf_timer = pyb.Timer(5) #update() #now fpga management stuff #is it ready? fpga.on() with open("hardware.bin", "rb") as f: fpga.viper_load(f) fpga.spi.init(pyb.SPI.MASTER, polarity=0, phase=0, baudrate=10000000) fpga.alt2.init(pyb.Pin.OUT_PP) fpga.alt2(0) for i in range(12): if fpga.spi.send_recv(b'\x91')[0] != 1: #send a dummy bit-depth command
oled.display() A1 = Pin('X3', Pin.OUT_PP) # A is right motor A2 = Pin('X4', Pin.OUT_PP) B1 = Pin('X7', Pin.OUT_PP) # B is left motor B2 = Pin('X8', Pin.OUT_PP) PWMA = Pin('X1') PWMB = Pin('X2') tim = Timer(2, freq=10000) motorA = tim.channel(1, Timer.PWM, pin=PWMA) motorB = tim.channel(2, Timer.PWM, pin=PWMB) mic = ADC(Pin('Y11')) MIC_OFFSET = 1523 dac = pyb.DAC(1, bits=12) b_LED = LED(4) #flash for beats on blue LED N = 160 # size of sample buffer s_buf = array('H', 0 for i in range(N)) # reserve buffer memory ptr = 0 # buffer index buffer_full = False # semaphore - ISR communicate with main program def flash(): b_LED.on() pyb.delay(30) b_LED.off() def energy(buf): # Compute energy of signal in buffer
sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(60) # LED Setup red_led = pyb.LED(1) green_led = pyb.LED(2) blue_led = pyb.LED(3) red_led.off() green_led.off() blue_led.off() # DAC Setup dac = pyb.DAC("P6") if analog_out_enable else None if dac: dac.write(0) # Servo Setup min_s0_limit = min(s0_lower_limit, s0_upper_limit) max_s0_limit = max(s0_lower_limit, s0_upper_limit) min_s1_limit = min(s1_lower_limit, s1_upper_limit) max_s1_limit = max(s1_lower_limit, s1_upper_limit) s0_pan = pyb.Servo(1) # P7 s1_tilt = pyb.Servo(2) # P8 s0_pan.pulse_width(int((max_s0_limit - min_s0_limit) // 2)) # center
movw(r3, 1<<14) strh(r3, [r2, stm.GPIO_BSRRL]) # delay for a bit movwt(r7, 2) label(delay_on2) sub(r7, r7, 1) cmp(r7, 0) bgt(delay_on2) strh(r3, [r2, stm.GPIO_BSRRH]) mov(r3, r0) # reset counter to waveform length mov(r7, r1) # reset waveform address # loop 4 times sub(r4, r4, 1) label(loop_four) cmp(r4, 0) bgt(loop2) init_dac() while True: pyb.LED(2).toggle() pyb.DAC(1).write(127) run_dac(len(waveform), addressof(array('i', waveform))) pyb.DAC(1).write(127)
import pyb dac = pyb.DAC(2) countdown = 10 while (countdown): dac.write(countdown*20) pyb.delay(300) countdown -= 10
# boot.py # import packages for all python scripts import machine, pyb, array, math, lcd160cr, random, utime # initializes DAC on pin X5, change to "2" to use X6 dac = pyb.DAC(2, bits=12) dacref = pyb.DAC(1, bits=12) lcd = lcd160cr.LCD160CR('X') # define the on-board LEDs red = pyb.LED(1) green = pyb.LED(2) yellow = pyb.LED(3) blue = pyb.LED(4) lcd.set_orient(lcd160cr.LANDSCAPE) lcd.erase() lcd.set_pos(0, 0) lcd.set_text_color(lcd.rgb(255, 255, 255), lcd.rgb(0, 0, 0)) lcd.set_pen(lcd.rgb(0, 255, 255), lcd.rgb(0, 0, 0)) lcd.set_font(3,0) lcd.write('INITIALIZED.\n') lcd.set_pos(0, 20) lcd.write('Welcome.\n') red.on() # wait 1000 ms for the switch to be pressed and hold. pyb.delay(1000) sw = pyb.Switch()() # press the switch to activate as storage device
def __init__(self): self.adc = pyb.ADC(pyb.Pin('PC5')) self.dac = pyb.DAC(2, bits = 12) self.myIIR = IIR(1, 1, -1022) self.timer = pyb.Timer(2, freq = 1000)
import utime, ustruct, pyb import math from array import array from pyb import USB_VCP usb = USB_VCP() usb.setinterrupt(-1) dac1 = pyb.DAC(pyb.Pin('X5'), bits=12) dac2 = pyb.DAC(pyb.Pin('X6'), bits=12) pwmGain = pyb.Pin('X1') pwmSlew = pyb.Pin('X2') pwmBias = pyb.Pin('X3') tim2 = pyb.Timer(2, freq=10000) tim5 = pyb.Timer(5, freq=1000000) pwmBiasCh = tim5.channel(3, pyb.Timer.PWM, pin=pwmBias) pwmGainCh = tim5.channel(1, pyb.Timer.PWM, pin=pwmGain) pwmBiasCh.pulse_width_percent(50) pwmSlewCh = tim2.channel(2, pyb.Timer.PWM, pin=pwmSlew) amplitude, Stop = 999, True frequency = 10000 duty = 10 while Stop: cmd = usb.recv(4, timeout=5000) usb.write(cmd) if (cmd == b'strt'): pyb.LED(1).on() pyb.LED(2).off() pyb.LED(3).off() utime.sleep(1) tim2 = pyb.Timer(2, freq=10000)
'sda': 'Y10', 'scl': 'Y9', 'res': 'Y8' }, height=64, external_vcc=False, i2c_devid=61) oled.poweron() oled.init_display() oled.draw_text(0, 0, 'Milestone 3: Beat Detection') oled.display() # define ports for microphone, LEDs and trigger out (X5) mic = ADC(Pin('Y11')) MIC_OFFSET = 1523 # ADC reading of microphone for silence dac = pyb.DAC(1, bits=12) # Output voltage on X5 (BNC) for debugging b_LED = LED(4) # flash for beats on blue LED N = 160 # size of sample buffer s_buf[] s_buf = array('H', 0 for i in range(N)) # reserve buffer memory ptr = 0 # sample buffer index pointer buffer_full = False # semaphore - ISR communicate with main program def flash(): # routine to flash blue LED when beat detected b_LED.on() pyb.delay(30) b_LED.off() def energy(buf): # Compute energy of signal in buffer
import pyb CRITICAL_THRESHOLD = 150 WARNING_THRESHOLD = 100 dac = pyb.DAC(1) # create a DAC object adc = pyb.ADC('X22') # setup X22 pin ADC channel tim = pyb.Timer(6, freq=20000) critical_pin = pyb.Pin.board.LED_RED warning_pin = pyb.Pin.board.LED_YELLOW works_pin = pyb.Pin.board.LED_BLUE def record(): buf = bytearray(20000) # create a 20000 byte array to store samples adc.read_timed(buf, tim) # start the ADC sampling return buf while True: # 1 sec buf = record() delta = max(buf) - min(buf) if delta >= CRITICAL_THRESHOLD: critical_pin.on() warning_pin.off() works_pin.off() elif WARNING_THRESHOLD <= delta <= CRITICAL_THRESHOLD: critical_pin.off()
def init_dac(): dac = pyb.DAC(1) init_dac_assy()
# The playback timer. This is used to invoke our chosen 'playback' function # at the required rate, which is PLAYBACK_FREQUENCY_HZ (or # 2 x CAPTURE_FREQUENCY_HZ if we're over-sampling the playback). # Configured in `_init()`. playback_timer = pyb.Timer(13) # LED objects red_led = pyb.LED(1) grn_led = pyb.LED(2) amb_led = pyb.LED(3) blu_led = pyb.LED(4) # ADC (Microphone) and DAC (loudspeaker) adc = pyb.ADC(pyb.Pin.board.X22) dac = pyb.DAC(1, bits=CAPTURE_BITS) # Switch object. During initialisation this will be used # to attach a handler function (`_user_switch_callback`) for the USER switch. sw = pyb.Switch() # Hardware timing pins. # This pin voltage is lowered on entry to the time-critical capture and # playback functions and raised on exit form the function. # Attach an oscilloscope to these pins to measure # the collection or playback callback duration. capture_timing_pin = pyb.Pin(pyb.Pin.board.Y1, pyb.Pin.OUT_PP) playback_timing_pin = pyb.Pin(pyb.Pin.board.Y2, pyb.Pin.OUT_PP) # ------------------------------ # Audio storage (sample buffers) # ------------------------------
def __init__(self, port): assert port.DAC is not None, '! Analog LED needs port with DAC.' self.DAC = pyb.DAC(port.DAC, bits=12) self.off()
# Description: ADC test import pyb import math print("Test DAC") dac = pyb.DAC(1) print(dac) #dac.triangle(5000) #dac.noise(5000) # sawtooth #buf = bytearray(200) #j=0 #for i in range (0,len(buf)/2): # v = int(i*10) # buf[j+1] = (v >> 8) & 0xff # buf[j] = v & 0xff # j=j+2 # sine buf = bytearray(200) j = 0 for i in range(0, len(buf) / 2): v = 512 + int(511 * math.sin(2 * math.pi * i / (len(buf) / 2))) buf[j + 1] = (v >> 8) & 0xff buf[j] = v & 0xff j = j + 2
def main(): dac1 = pyb.DAC(1) sine_sweep(dac1, 10, 400, 1.33)
def test_2(self): dac = pyb.DAC(1) r = dac.write(2000) self.assertEqual(False,r,"DAC write invalid value")