def redac(self): self.dac = DAC(Pin('X5'),buffering=self.buffer_dac) self.bmv = memoryview(self.buf)[:len(self)] self.dac_tim = Timer(6, freq=int(self.hres*1000000/self.line_time)) self.dac.write_timed(self.bmv,self.dac_tim,mode=DAC.CIRCULAR) self.frame_tim = Timer(5, prescaler=self.dac_tim.prescaler(),period=self.dac_tim.period()*len(self)) self.frame_tim.counter(self.phase)
def luminance(self, mode): """Sample luminance (in lux), using specified sensor mode.""" # continuous modes if mode & 0x10 and mode != self.mode: self.set_mode(mode) # one shot modes if mode & 0x20: self.set_mode(mode) # earlier measurements return previous reading if mode in (0x13, 0x23): time.sleep_ms(24) else: time.sleep_ms(180) data = self.bus.recv(16,addr=self.addr) if mode in (0x11, 0x21): factor = 2.0 else: factor = 1.0 print ((data[0]<<8 | data[1]) / (1.2 * factor)) buf = bytearray(16) for i in range(len(data)): buf[i]=data[i] print(buf) # output the sine-wave at 400Hz dac = DAC(1) dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
def __init__(self, amps=[], freqs=[], bufs=[], ch=1): # amps: 幅值序列 # bufs: 对应幅值的正弦序列 self.timer = Timer(6) self.dac = DAC(ch, bits=12) self.amps = amps self.bufs = bufs self.freqs = freqs self.indx = range(len(amps)) self.newtask = False
def __init__(self, bufs, freqs, DAC_ch, sync, BoardCtrl, repeat=50, timer_num=6): ''' bufs: list, DAC output array typ: list, the same length with bufs, 'tiny','big','cut' to describe which type of the buf freqs: list, tested frequences ch: DAC output channel, 1: 'X5' 2: 'X6' sync: object for output synchronus signal to record client resistset: object for adjusting resist according to the magnet strength timer: internal timer, default 6 ''' self.bufs = bufs self.freqs = freqs if DAC_ch == 'X5': self.dac = DAC(1, bits=12) elif DAC_ch == 'X6': self.dac = DAC(2, bits=12) else: raise ValueError('DAC pin should be X5 or X6') self.sync = sync self.boardctrl = BoardCtrl self.timer = Timer(timer_num) self.buf_indx = 0 self.fre_indx = 0 self.fre_len = len(self.freqs) self.buf_len = len(self.bufs) self.repeat = repeat self.rcount = 0 self.end_flg = False self.new_block = False self.stimulus_level = 0
def __init__(self, dac=DAC(1), timerno=4): global lwr lwr = self self._f = None self._rate = 0 self._amount = 0 self._freq = 4 # in Hertz self._dac = dac self._have = [False, False] self._buf = [None, None] self._doneindex = 0 self._speed = 100 self._timer = pyb.Timer(timerno, freq=self._freq) self._running = False self._fill()
def __init__(self): self.p_SICL = Pin(config.SICL, Pin.OUT_PP) self.p_SIBL = Pin(config.SIBL, Pin.OUT_PP) self.p_CK = Pin(config.CK, Pin.OUT_PP) self.p_LAT = Pin(config.LAT, Pin.OUT_PP) self.p_CH = Pin(config.CH, Pin.OUT_PP) self.p_NCHG = Pin(config.NCHG, Pin.OUT_PP) self.p_LAT.value(0) self.p_NCHG.value(0) self.dac = DAC(1) self.dac.write(127) waveform = config.WAVEFORM factor = max(waveform) for i in range(len(waveform)): waveform[i] = int(255*(waveform[i]/factor)) self.waveform = array('i', waveform)
def __init__(self, freqs=[], bufs=[], ch=1, **kwargs): # freqs:待测试的频率 # bufs: 对应幅值的正弦序列等消息,每个元素包含(amp,buf,port) # 其中port是指,使用可变电阻时对应控制的端口 # ch:测试信号输出端口选择 # kwargs: r_port,电阻选择的端口列表 self.timer = Timer(6) self.dac = DAC(ch, bits=12) self.freqs = freqs self.bufs = bufs self.r_adapt = False if len(kwargs) > 0: if 'r_port' in kwargs: #使用电阻自适应调整 self.r_adapt = True (pyb.Pin(pin, pyb.Pin.OUT_PP) for pin in kwargs['r_port']) self.freqs = freqs self.indx = range(len(amps)) self.newtask = False
def pins_test(): i2c = I2C(1, I2C.MASTER) spi = SPI(2, SPI.MASTER) uart = UART(3, 9600) servo = Servo(1) adc = ADC(Pin.board.X3) dac = DAC(1) pin = Pin('X4', mode=Pin.AF_PP, af=Pin.AF3_TIM9) pin = Pin('Y1', mode=Pin.AF_OD, af=3) pin = Pin('Y2', mode=Pin.OUT_PP) pin = Pin('Y3', mode=Pin.OUT_OD, pull=Pin.PULL_UP) pin.high() pin = Pin('Y4', mode=Pin.OUT_OD, pull=Pin.PULL_DOWN) pin.high() pin = Pin('X18', mode=Pin.IN, pull=Pin.PULL_NONE) pin = Pin('X19', mode=Pin.IN, pull=Pin.PULL_UP) pin = Pin('X20', mode=Pin.IN, pull=Pin.PULL_DOWN) print('===== output of pins() =====') pins() print('===== output of af() =====') af()
sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" must be set to merge overlapping color blobs for color codes. while(True): clock.tick() img = sensor.snapshot() for blob in img.find_blobs([thresholds[1]], pixels_threshold=100, area_threshold=100, merge=True): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) img.draw_string(blob.x() + 2, blob.y() + 2, "verde") dac = DAC('P6')#Select pin whit DAC or ADC dac.write(255) # output between 0 and 255 for blob in img.find_blobs([thresholds[0]], pixels_threshold=100, area_threshold=100, merge=True): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) img.draw_string(blob.x() + 2, blob.y() + 2, "rojo") dac = DAC('P6')#Select pin whit DAC or ADC dac.write(100) # output between 0 and 255 for blob in img.find_blobs([thresholds[2]], pixels_threshold=100, area_threshold=100, merge=True): img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) img.draw_string(blob.x() + 2, blob.y() + 2, "amarillo") dac = DAC('P6')#Select pin whit DAC or ADC dac.write(50) # output between 0 and 255 print(clock.fps())
def __init__(self, buf=array('H', [100]), ch=1): self.timer = Timer(6) self.dac = DAC(ch, bits=12) self.buf = buf self.dac_on = 0
from pyb import DAC dac = DAC(1) # create DAC 1 on pin X5 dac.write(128) # write a value to the DAC (makes X5 1.65V) dac = DAC(1, bits=12) # use 12 bit resolution dac.write(4095) # output maximum value, 3.3V
版本:v1.0 日期:2019.9 作者:01Studio 说明:通过KEY按键让DAC输出不同频率的方波来驱动蜂鸣器。 ''' #导入相关模块 from pyb import DAC,ExtInt from machine import Pin,I2C from ssd1306x import SSD1306_I2C #初始化相关模块 i2c = I2C(sda=Pin("P0"), scl=Pin("P2"),freq=80000) oled = SSD1306_I2C(128, 64, i2c, addr=0x3c) dac = DAC(Pin("P6")) #定义DAC对象名字为dac,输出引脚为P6 #定义4组频率值:1Hz、200Hz、1000Hz、5000Hz freq=[1,200,1000,5000] # 定义8位精度下方波的值。0、255分别对应输出0V、3.3V。需要定义成字节数组。 buf = bytearray(2) buf[0]=0 buf[1]=255 key_node = 0 #按键标志位 i = 0 #用于选择频率数组 ############################################## # 按键和其回调函数 ##############################################
state = 'front' control_dict = { "__START__": 1000, "__SPEED15__": 1001, "__SPEED20__": 1002, "__SPEED25__": 1003, "__STATUSUPDATE__": 1004, "__TURNRIGHT__": 2500, "__TURNLEFT__": 1500, "__BREAK__": 4000, "__SET__": 6000, "__RESETPIBOARD__": 5000 } #Here __RESETPIBOARD__ is to stop the vehicle (ie., to set DAC=0) # ------- Complex--------# tim = pyb.Timer(10, freq=10) #timer dacA, dacB = DAC(1, bits=12), DAC(2, bits=12) #DAC Initialization pidL = PID.PID(45, 0, 90) #PID pidR = PID.PID(40, 0, 80) # -------- PINS---------# #Y1,Y2,Y3 are for ExtInt motor_relay_L = Pin( 'Y4', Pin.OUT_PP) #relay for brakes; connected in a normally open connection motor_relay_R = Pin('Y5', Pin.OUT_PP) motor1_switch = Pin( 'Y6', Pin.OUT_PP) #Relay for DAC; connected in a normally open connection motor2_switch = Pin('Y7', Pin.OUT_PP) motor_L_brake = Pin('X9', Pin.OUT_PP) #regenerative braking motor_R_brake = Pin('X10', Pin.OUT_PP) #############################################################################################################
from pyb import DAC dac = DAC(1) # DAC 1 sur la broche A0 (Feather) ou X5 (Pyboard) dac.write(128) # Ecriture d'une valeur sur 8 bit dac = DAC(1, bits=12) # DAC 1 en 12 bit dac.write(4095) # Ecriture de la valeur maximale
from pyb import DAC from pyb import LED import math # Turn on the light so you know something is happening led = LED(4) led.on() # Write out a sinusoidal curve to channel X5, which is wired via a # female-female jumper to X2 # -- # create a buffer containing a sine-wave, using half-word samples buf = array( 'H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128)) dac = DAC(1, bits=12) # Wired to X5 dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR) # Instatiate the USB serial object u = USB_VCP() u.write('Hello world!!') # Enter into an infinite loop while True: # Get a line from the serial connection line = u.readline() # If there is nothing, line will be None if not line is None and line:
from pyb import DAC sa1 = DAC(1) sa1.triangle(400*2048)
tim = Timer(2, freq=1000) ch = Timer.channel(1, Timer.PWM, pin=p) ch.pulse_width_percent(50) ''' 1.9 ADC ''' from pyb import Pin, ADC adc = ADC(Pin('X19')) adc.read() # read value, 0 - 4095 ''' 1.10 DAC ''' from pyb import Pin, DAC dac = DAC(Pin('X5')) dac.write(120) # output between 0 and 255 ''' 1.11 UART ''' from pyb import UART uart1 = UART(1, 9600) uart1.write('hello') uart1.read(5) # read up to 5 bytes ''' 1.12 SPI Bus ''' from pyb import SPI spi1 = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)
# DAC Timed Write Example # # This example shows how to use the DAC pin output onboard your OpenMV Cam. import math from pyb import DAC # create a buffer containing a sine-wave buf = bytearray(100) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) # output the sine-wave at 400Hz dac = DAC("P6") dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
try: from pyb import DAC from pyb import LED from pyb import Pin from pyb import USB_VCP sensitivity, maxn, minn = .5, 255, 0 com = USB_VCP() light = DAC(2) y1, y2, y3 = Pin('Y1', Pin.IN), Pin('Y2', Pin.IN), Pin('Y3', Pin.IN) intensity, oldStr = maxn, str(y1.value()) + str(y2.value()) directTab = { '1000': 1, '0001': 1, '0111': 1, '1110': 1, '1011': -1, '1101': -1, '0100': -1, '0010': -1 } def setByte(i): return i.to_bytes(1) def limitNums(num, maxn, minn): return max(minn, min(maxn, num)) def handleLEDs(intensity, value):
regen_in = pyb.ADC(Pin.cpu.A7) # Outputs # Conflict with Key (Kelly v1.0) # buzzer = Pin(Pin.cpu.B0, Pin.OUT_PP) pyb_red_led = LED(1) pyb_green_led = LED(2) pyb_blue_led = LED(4) horn = Pin(Pin.cpu.C0, Pin.OUT_PP) headlight = Pin(Pin.cpu.C1, Pin.OUT_PP) key = Pin(Pin.cpu.B0, Pin.OUT_PP) # DAC throttle_out = DAC(1, bits=12) regen_out = DAC(2, bits=12) # Other bike_on = False light_on = False horn_on = False ############### END PIN SETUP def init(): # print("Initialize Buzzer") # buzzer.low() print("Initialize LEDs") pyb_red_led.off()
from pyb import DAC from array import array import math dac = DAC(1, bits=12, buffering=True) # use 12 bit resolution dac.write(4095) # output maximum value, 3.3V # To output a continuous sine-wave at 12-bit resolution: # create a buffer containing a sine-wave, using half-word samples buf = array("H", 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128)) # output the sine-wave at 200Hz dac.write_timed(buf, 200 * len(buf), mode=DAC.CIRCULAR)
# main.py -- put your code here! import pyb from pyb import DAC #----------DAC--------------# dac = DAC(1, bits=12) #X15 pin dac.write(4095) dac2 = DAC(2, bits=12) #X16 pin dac2.write(2048)
from pyb import DAC import utime,math dac = DAC(1,buffering=True) # create DAC 1 on pin X5 dac.write(128) # write a value to the DAC (makes X5 1.65V) # To output a continuous sine-wave: # create a buffer containing a sine-wave buf = bytearray(100) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) # output the sine-wave at 400Hz dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
from pyb import Pin, ADC, UART, I2C, DAC import char_lcd import time import math import binascii import pyb from light import lightvalue print("kek") #Define sensors tempsensor = ADC(Pin('X1')) motionsensor = Pin('Y12', Pin.IN, Pin.PULL_UP) beeper = DAC(1) waterportion = Pin('X11', Pin.OUT_PP) doortrigger = Pin('Y7', Pin.IN, Pin.PULL_UP) doortrigger2 = Pin('Y8', Pin.IN, Pin.PULL_UP) #Define UART bus and I2C bus uart = UART(6, 115200) i2c = I2C(1, I2C.MASTER, baudrate=9600) i2c2 = I2C(2, I2C.MASTER, baudrate=9600) #Define LCD d = char_lcd.HD44780(i2c2) def changetemp(temp):
#>>> volume(0) # minimum volume #>>> volume(127) # maximum volume #To play a sound, use the write_timed method of the DAC object. For example: import math from pyb import DAC # create a buffer containing a sine-wave buf = bytearray(100) for i in range(len(buf)): buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf))) # output the sine-wave at 400Hz dac = DAC(1) dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR) # or playing a wave file from the SD Card import wave from pyb import DAC from pyb import delay dac = DAC(1) def play(filename): f = wave.open(filename, 'r') total_frames = f.getnframes() framerate = f.getframerate()
import pyb, math from array import array import uio from pyb import Pin, ADC, DAC import time tim = pyb.Timer(6, freq=10) #buf = array('u',range(128)) # create a buffer containing a sine-wave, using half-word samples buf_dac = array( 'H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128)) buf_adc = array('H', range(128)) # output the sine-wave at 400Hz dac = DAC(1, bits=12) dac.write_timed(buf_dac, 128, mode=DAC.CIRCULAR) adc = pyb.ADC(Pin('Y12')) # create an analog object from a pin adc.ADCALL(bits=12) adc.read_timed(buf_adc, tim) file = uio.open('3.txt', mode='w') file.write('DAC') file.write(str(buf_dac)) file.write('ADC') file.write(str(buf_adc)) file.close()
''' import pyb, time from pyb import LED, DAC, ADC, Pin, Timer from oled_938 import OLED_938 from mpu6050 import MPU6050 from motor import MOTOR from g24_pid_controller import PIDC from array import array from mic import MICROPHONE from g24_choreo_functions import * import micropython micropython.alloc_emergency_exception_buf(100) # Define ports, pins and peripherals a_out = DAC(1, bits=12) pot = ADC(Pin('X11')) b_LED = LED(4) # IMU connected to X9 and X10 imu = MPU6050(1, False) # Use OLED to say what it is doing oled = OLED_938(pinout={'sda': 'Y10', 'scl': 'Y9', 'res': 'Y8'}, height=64, external_vcc=False, i2c_devid=61) oled.poweron() oled.init_display() oled.draw_text(0, 0, 'Group 24') oled.draw_text(0,10, 'Balance & Dance') oled.draw_text(0,20, 'Press USR button') oled.display()
#import numpy as np #import commpy.channelcoding.convcode as cc #from commpy.utilities import dec2bitarray import binascii from pyb import DAC import utime #En esta prueba se crea un arreglo predefinido #con decimales del 0 al 255 y se envia cada #elemento del arreglo al DAC(1) #*********************************************** # DAC parameters configuration #*********************************************** dac = DAC(1, bits=8) #*********************************************** #Conversion from text message into single decimal byte# #*********************************************** msg = bytearray([ 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0 ]) #msg = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255]) #Ascci converted to hex and then to dec for s in range(len(msg)): #msg_dec = int(binascii.hexlify(msg[s]), 16) dac.write(msg[s]) print(msg) print(len(msg))
# DAC Control Example # # This example shows how to use the DAC pin output onboard your OpenMV Cam. import time from pyb import DAC dac = DAC("P6") # Must always be "P6". while(True): # The DAC has 8-12 bits of resolution (default 8-bits). for i in range(256): dac.write(i) time.sleep_ms(20) for i in range(256): dac.write(255-i) time.sleep_ms(20)
utime.sleep_ms(10) #change baudrate self.uart.init(baudrate=115200, bits=8, parity=None, stop=1) self.load_payload(2) #start callback - MAKE SURE YOU RESTART THE CHIP EVERY TIME (CMD D) to kill previous callbacks running self.sendTimer.callback(self.sendCall) return micropython.alloc_emergency_exception_buf(200) p_inw1 = Pin('X2', Pin.IN, Pin.PULL_DOWN) p_inb2 = Pin('X3', Pin.IN, Pin.PULL_DOWN) p_ing1 = Pin('X4', Pin.IN, Pin.PULL_DOWN) led1 = DAC(Pin('X5')) led2 = DAC(Pin('X6')) red_led = pyb.LED(1) green_led = pyb.LED(2) red_led.on() lpf2 = LPF2(1, 'Y1', 'Y2', timer=4, freq=5) # OpenMV lpf2.initialize() value = 0 # Loop while True: if not lpf2.connected: lpf2.sendTimer.callback(None) red_led.on()