def __init__(self, pin_1, pin_2, timer): ''' Creates a motor encoder by initializing GPIO pins and initializes the timer and its channels, 1 and 2, by using the two pins for the encoder and the timer number as inputs for the class. @param pin_1 The pin corresponding to the A encoder channel. @param pin_2 The pin corresponding to the B encoder channel. @param timer The number of the timer for the specified pins. ''' import pyb self.pin_1 = pin_1 self.pin_2 = pin_2 self.timer = timer self.pin_1 = pyb.Pin(self.pin_1, pyb.Pin.ALT) self.pin_2 = pyb.Pin (self.pin_2, pyb.Pin.ALT) self.timer = pyb.Timer(self.timer, period = 0xFFFF, prescaler=0) self.ch1 = self.timer.channel(1, pyb.Timer.ENC_A, pin=self.pin_1) self.ch2 = self.timer.channel(2, pyb.Timer.ENC_B, pin=self.pin_2) self.previous_pos = 0 # set previous position to 0 self.motor_pos = 0 # set motor position to 0
def __init__(self, sensor_id, uart_bus, int_pin, timer_num): assert not (int_pin == timer_num == None) super().__init__(sensor_id, ['f', 'f', 'f', 'u8']) self.gps_ref = MicropyGPS() self.prev_lat = None self.prev_long = None self.lat = 0.0 self.long = 0.0 self.altitude = 0.0 GPS.uart = pyb.UART(uart_bus, 9600, read_buf_len=1000) # using class variables because lists can't be created in callbacks # MicropyGPS creates a list in one of its functions GPS.pps_pin = int_pin GPS.ext_pin = pyb.ExtInt(GPS.pps_pin, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, GPS.pps_callback) self.timer = pyb.Timer(timer_num, freq=1) self.timer.callback(lambda t: GPS.timer_callback(t))
def displayString(self, string, delay=300, timer=None): """ Clear the display and display a string of characters, using displayAlpha. If the string has less than 4 characters, it will be displayed flushed left. If the string has more than 4 characters, it will scroll toward the left with a delay of 'delay' milliseconds between each character. """ self.stopDisplayString() n = len(string) self.clear() if n > 4: self.string = string + ' ' self.length = n + 3 self.offset = 0 if not timer: timer = pyb.Timer(1) self.timer = timer timer.init(freq=round(1e3/delay, 0)) timer.callback(self.dispStringInterruptHandler) for dig in range(min(n, 4)): self.rawDisplayAlpha(dig, string[dig])
def __init__(self, pin, prescaler=4.5, freq=50, min_us=665, max_us=2360, angle=190): ## The Timer desired for the servo at a specified frequency self.tim = pyb.Timer(2, freq=freq) ## Output pin used to control the servo self.pin = pyb.Pin(pin, pyb.Pin.OUT_PP) ## Channel used to initialize the servo for PWM self.ch2 = self.tim.channel(1, pyb.Timer.PWM, pin=self.pin) # The minimum signal length supported by the servo self.min_us = min_us # The maximum signal length supported by the servo self.max_us = max_us # The signal length variable initialized to zero self.us = 0 # The frequency of the signal, in hertz self.freq = freq # The angle between the minimum and maximum positions self.angle = angle # electronic counting circuit used to reduce a high frequency # electrical signal to a lower frequency self.prescaler = prescaler self.t_freq = 0
def __init__(self, timer, EN_Pin, Pin_1, Pin_2): ''' Creates a motor driver by initializing GPIO pins and turning the motor off for safety. ''' print('Creating a motor driver') ## Set Pin PA10 toas open-drain output with pull up resistors self.EN_Pin = pyb.Pin(EN_Pin, pyb.Pin.OUT_OD, pull=pyb.Pin.PULL_UP) ## Set Pin PB4 as push-pull with the correct alternate function (timer) self.Pin_1 = pyb.Pin(Pin_1, pyb.Pin.AF_PP, af=2) ## Set Pin PB5 as push-pull with the correct alternate function (timer) self.Pin_2 = pyb.Pin(Pin_2, pyb.Pin.AF_PP, af=2) self.timer = pyb.Timer( timer, freq=20000) # Set Timer 3 to a frequency of 20,000 Hz self.ch1 = self.timer.channel( 1, pyb.Timer.PWM, pin=self.Pin_1) # Set Timer 3 Channel 1 to PWM for pin PB4 self.ch2 = self.timer.channel( 2, pyb.Timer.PWM, pin=self.Pin_2) # Set Timer 3 Channel 2 to PWM for pin PB5 self.EN_Pin.low() # Set Pins Low on startup self.Pin_1.low() self.Pin_2.low()
def __init__(self, timer, enc_A, enc_B, name): ''' Initializes the pins and timer channels for an encoder object. To create PB6 and PB7 Encoder reader: \n pin1 = pyb.Pin.board.PB6 # Pin A \n pin2 = pyb.Pin.board.PB7 # Pin B \n timer = 4 \n\n To create PC6 and PC7 Encoder reader: \n pin1 = pyb.Pin.board.PC6 # Pin A \n pin2 = pyb.Pin.board.PC7 # Pin B \n timer = 8 \n @param timer: Specifies the timer for the encoder @param pin1: First pin (A) used to read the encoder @param pin2: Second pin (B) used to read the encoder ''' ## First encoder pin associated with the pin1 (A) input parameter self.pin_object_1 = pyb.Pin(enc_A) ## Second encoder pin associated with the pin2 (B) input parameter self.pin_object_2 = pyb.Pin(enc_B) ## Timer number associated with the assigned pins self.timer_val = pyb.Timer(timer) ## Timer number associated with the assigned pins (Set timer to have one count per encoder count and a period of 65535) self.timer_val.init(prescaler=0, period=0xFFFF) ## Initializes channel 1 for pin1 (A) timer input self.ch1 = self.timer_val.channel(1, pyb.Timer.ENC_AB, pin=self.pin_object_1) ## Initializes channel 2 for pin2 (B) timer input self.ch2 = self.timer_val.channel(2, pyb.Timer.ENC_AB, pin=self.pin_object_2) # Instantaneous encoder reading at time of call self.__current_count = 0 # Difference between last count and current count self.__delta_count = 0 # Encoder reading from previous call self.__last_count = 0 # Current absolute position of the encoder self.__encoder_val = 0 print('successfully created ' + name)
def __init__(self): self.white = 1 self.pin = Pin('PA9') self.tim = pyb.Timer(1, freq=1000) self.brightnessChannel = self.tim.channel(2, pyb.Timer.PWM, pin=self.pin) self.brightnessChannel.pulse_width_percent(50) self.ledMatrix = [ Pin("PB14", Pin.OUT_PP), Pin("PA10", Pin.OUT_PP), Pin("PA14", Pin.OUT_PP), Pin("PA15", Pin.OUT_PP), Pin("PA8", Pin.OUT_PP), Pin("PB13", Pin.OUT_PP), Pin("PB15", Pin.OUT_PP), Pin("PB6", Pin.OUT_PP), Pin("PA13", Pin.OUT_PP), Pin("PC14", Pin.OUT_PP), Pin("PB12", Pin.OUT_PP), Pin("PB2", Pin.OUT_PP), Pin("BOOT0", Pin.OUT_PP), Pin("PC15", Pin.OUT_PP), Pin("PB8", Pin.OUT_PP), Pin("PB1", Pin.OUT_PP), Pin("PB0", Pin.OUT_PP), Pin("PA4", Pin.OUT_PP), Pin("PH1", Pin.OUT_PP), Pin("PB9", Pin.OUT_PP), Pin("PA7", Pin.OUT_PP), Pin("PA6", Pin.OUT_PP), Pin("PA1", Pin.OUT_PP), Pin("PA0", Pin.OUT_PP), Pin("PH0", Pin.OUT_PP) ] for pin in range(len(self.ledMatrix)): self.ledMatrix[pin].low()
def main(): global pinC0 global queue global adc global pinC1 tim = pyb.Timer(1) tim.init(freq= 1000) tim.callback(interrupt) while True : pinC1.high () start_time = utime.ticks_ms() running_time = utime.ticks_ms() while (start_time + 1000) > running_time: running_time = utime.ticks_ms() if queue.empty() == False: print(queue.get()) pinC1.low() print("END HEREEEEEEEEEEEEEEEE") time.sleep(30)
def __init__(self, enable_pin, positive_pin, negative_pin, timer): '''Creates a motor driver by intializing GPIO pins and turning the motor off for safety. @param enable_pin this pin enables the motor driver to supply power to the motor. @param positive_pin this pin supplies voltage to the 'positive' side of the motor @param negative_pin this pin supplies voltage to the 'negative' side of the motor @param timer this timer controls the pwm timing of the output pins.''' self.en_pin = pyb.Pin(enable_pin, pyb.Pin.OUT_PP) #sets EN_A pin #self.pinB4 = pyb.Pin (pyb.Pin.board.PB4, pyb.Pin.OUT_PP) self.pos_pin = pyb.Pin(positive_pin, pyb.Pin.OUT_PP) #sets IN1_A pin #self.pinB5 = pyb.Pin (pyb.Pin.board.PB5, pyb.Pin.OUT_PP) self.neg_pin = pyb.Pin(negative_pin, pyb.Pin.OUT_PP) #sets IN2_A pin self.en_pin.low() #sets motor to off self.timer = pyb.Timer(timer, freq=1000) self.ch1 = self.timer.channel(3, pyb.Timer.PWM, pin=self.en_pin)
def put_triangle(self): self.function = "TR" if (self.display_present()): s = "TR %4.2fV" % self.volts( self.min_v) + " - %4.2fV" % self.volts(self.max_v) self.oled.draw_text(0, 10, s) self.oled.draw_text(10, 20, "Fsig: %5.1fHz" % self.sig_freq) self.oled.draw_text(10, 30, "Fsamp: %5.1fHz" % self.samp_freq) self.oled.display() pyb.delay(150) # Create the samples in triangle array self.dac.write(0) # Quirk of Pyboard - need to write to DAC once first delta_v = (self.max_v - self.min_v) / (0.5 * self.N_samp) tri_array = array('H', 0 for i in range(self.N_samp)) for i in range(int(self.N_samp / 2)): tri_array[i] = int(self.min_v + i * delta_v) tri_array[self.N_samp - i - 1] = tri_array[i] # Generate the triangular wave self.dac.write_timed(tri_array, pyb.Timer(2, freq=int(self.N_samp * self.sig_freq)), mode=DAC.CIRCULAR)
def initialize(self): self.connected = False self.sendTimer = pyb.Timer(self.txTimer, freq=self.freq) # default is 200 ms self.init() self.writeIt( self.setType(self.type) ) # set type to 35 (WeDo Ultrasonic) 61 (Spike color), 62 (Spike ultrasonic) self.writeIt(self.defineModes(self.modes)) # tell how many modes self.writeIt(self.defineBaud(115200)) self.writeIt(self.defineVers(2, 2)) num = len(self.modes) - 1 for mode in reversed(self.modes): self.setupMode(mode, num) num -= 1 utime.sleep_ms(5) self.writeIt(b'\x04') #ACK # Check for ACK reply self.connected = self.waitFor(b'\x04') print('Success' if self.connected else 'Failed') # Reset Serial to High Speed # pull pin low self.uart.deinit() if self.connected: tx = machine.Pin(self.txPin, machine.Pin.OUT) tx.value(0) 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
def __init__(self, loop): super(MembraneNumpad, self).__init__(loop) # No idea how to pick a safe timer number. self.timer = pyb.Timer(7) self.cols = [ Pin(i, Pin.IN, pull=Pin.PULL_UP) for i in ('M2_COL0', 'M2_COL1', 'M2_COL2') ] self.rows = [ Pin(i, Pin.OUT_OD, value=0) for i in ('M2_ROW0', 'M2_ROW1', 'M2_ROW2', 'M2_ROW3') ] # We scan in random order, because Tempest. # - scanning only starts when something pressed # - complete scan is done before acting on what was measured self.scan_order = array.array('b', list(range(NUM_ROWS))) # each full scan is pushed onto this, only last one kept if overflow self.scans = deque((), 50, 0) # internal to timer irq handler self._history = None # see _start_scan self._scan_count = 0 self._cycle = 0 self.waiting_for_any = True # time of last press self.lp_time = 0 for c in self.cols: c.irq(self.anypress_irq, Pin.IRQ_FALLING | Pin.IRQ_RISING) # ready to start self.loop = loop
def __init__(self, sensor_id, analog_pin, lower, upper): super(HallEncoder, self).__init__(sensor_id, 'u64') self.pin_ref = pyb.ADC(pyb.Pin(analog_pin, pyb.Pin.ANALOG)) self.in_range = False self.enc_dist = 0 self.hall_value = 0 self.sum = 0 self.count = 0 # need to be calibrated to real life values # for RoboQuasar, upper = 3100, lower = 2900 self.upper_threshold = upper self.lower_threshold = lower self.data_recved = False self.timer1 = pyb.Timer(1, freq=5000) self.timer1.callback(lambda t: self.on_interrupt()) assert analog_pin == "X8" or analog_pin == "Y11" or analog_pin == "Y12"
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)
def put_square(self): self.function = "SQ" if (self.display_present()): s = "SQ %4.2fV" % self.volts( self.min_v) + " - %4.2fV" % self.volts(self.max_v) self.oled.draw_text(0, 10, s) self.oled.draw_text(10, 20, "Fsig : %5.1fHz" % self.sig_freq) self.oled.draw_text(10, 30, "Duty_cycle: %3d" % self.duty_cycle) self.oled.display() pyb.delay(150) # Create the samples in square array self.dac.write(0) # Quirk of Pyboard - need to write to DAC once first square_array = array('H', 0 for i in range(self.N_samp)) h_index = int(self.N_samp * self.duty_cycle / 100 + 0.5) for i in range(h_index): square_array[i] = int(self.max_v) for i in range(h_index, self.N_samp): square_array[i] = int(self.min_v) # Generate the triangular wave self.dac.write_timed(square_array, pyb.Timer(2, freq=int(self.N_samp * self.sig_freq)), mode=DAC.CIRCULAR)
def put_sine(self): self.function = "AC" if (self.display_present()): s = "AC %4.2fV" % self.volts( self.min_v) + " - %4.2fV" % self.volts(self.max_v) self.oled.draw_text(0, 10, s) self.oled.draw_text(10, 20, "Fsig : %5.1fHz" % self.sig_freq) self.oled.draw_text(10, 30, "Fsamp: %5.1fHz" % self.samp_freq) self.oled.display() pyb.delay(500) # Create the samples in sine array self.dac.write(0) # Quirk of Pyboard - need to write to DAC once first sine_array = array('H', 0 for i in range(self.N_samp)) mid_v = (self.max_v + self.min_v) / 2 amp_v = (self.max_v - self.min_v) / 2 for i in range(self.N_samp): sine_array[i] = int(mid_v + amp_v * math.sin(2 * math.pi * i / self.N_samp)) # Generate the sinewave self.dac.write_timed(sine_array, pyb.Timer(2, freq=int(self.N_samp * self.sig_freq)), mode=DAC.CIRCULAR)
def __init__(self, pin_en, pin_a, pin_b, tmr_num): """ Creates a motor driver by initializing GPIOpins and turning the motor off for safety. @param pin_en pin used to enable motor controller @param pin_a positive motor pin @param pin_b negative motor pin @param tmr_num timer number used to drive motor. Must be compatible with chosen pins """ pin_en.init(pyb.Pin.OUT_PP) pin_a.init(pyb.Pin.OUT_PP) pin_b.init(pyb.Pin.OUT_PP) # Enables motor controller channel pin_en.high() # Initializes Timer and sets and sets PWM pulse width tmr = pyb.Timer(tmr_num, freq=20000) self._tmr_ch1 = tmr.channel(1, pyb.Timer.PWM, pin=pin_a) self._tmr_ch1.pulse_width_percent(0) self._tmr_ch2 = tmr.channel(2, pyb.Timer.PWM, pin=pin_b) self._tmr_ch2.pulse_width_percent(0)
def task_trigger_fun(): ''' Function that implements the Trigger Actuation Task. This task powers a servo motor which is used to pull the trigger of the gun in order to push the dart into the motor chamber.''' #pinA2 = pyb.Pin (pyb.Pin.board.PA2, pyb.Pin.OUT_PP) #tim2 = pyb.Timer (2, freq=50) pinA5 = pyb.Pin(pyb.Pin.board.PA5, pyb.Pin.OUT_PP) tim2 = pyb.Timer(2, freq=50) md = CRSBotBrainMD.CRSBotBrainMD(pinA5, tim2) md.set_duty_cycle(10.5) state = 0 while True: if state == 0: if fire.get() > 0: #if fire.get() > 0 and dart_motor.get() > 0: vcp.write('Dart fired!'.encode()) md.set_duty_cycle(1.3) counter = 0 state = 1 #elif fire.get() > 0 and dart_motor.get() == 0: #print ("Enable the dart motor before firing a dart!") elif state == 1: counter = counter + 1 if fire.get() == 0 or counter == 5: fire.put(0) state = 0 md.set_duty_cycle(10.5) yield (state)
def distance_in_cm(self): start = 0 end = 0 micros = pyb.Timer(1, prescaler=83, period=0x3fffffff) micros.counter(0) # Send a 10us pulse self.trigger.high() pyb.udelay(10) self.trigger.low() # Wait for the pulse and calc its duration while self.echo.value() == 0: start = micros.counter() while self.echo.value() == 1: end = micros.counter() micros.deinit() # Calc the duration of the recieved pulse, divide the result by # 2 (round-trip) and divide it by 29 (the speed of sound is # 340 m/s and that is 29 us/cm). dist_in_cm = ((end - start) / 2) / 29 return dist_in_cm
def __init__(self, stby_pin=pyb.Pin.board.X17, ain1_pin=pyb.Pin.board.Y6, ain2_pin=pyb.Pin.board.Y5, pwma_pin=pyb.Pin.board.Y7, bin1_pin=pyb.Pin.board.X11, bin2_pin=pyb.Pin.board.X12, pwmb_pin=pyb.Pin.board.Y8): self._stby = stby_pin # standby # Motor A self._ain1 = ain1_pin self._ain2 = ain2_pin self._pwma = pwma_pin # Motor B self._bin1 = bin1_pin self._bin2 = bin2_pin self._pwmb = pwmb_pin self._stby.init(pyb.Pin.OUT_PP) self._ain1.init(pyb.Pin.OUT_PP) self._ain2.init(pyb.Pin.OUT_PP) self._bin1.init(pyb.Pin.OUT_PP) self._bin2.init(pyb.Pin.OUT_PP) timer = pyb.Timer(12, freq=1000) self._pwma_ch = timer.channel(1, pyb.Timer.PWM, pin=self._pwma, pulse_width=210000) self._pwmb_ch = timer.channel(2, pyb.Timer.PWM, pin=self._pwmb, pulse_width=420000)
def int_timer(): ahb1enr[0] = 1 # Port A clock pa_moder[PA_OUT] = 1 # Output pa_odr[PA_OUT] = 1 # LED on print("Before setup...") dump(regs_tmr_int) tmr_int = pyb.Timer(2, freq=4, callback=callback_tmr_int) print("After setup...") dump(regs_tmr_int) while True: update = input("Update status (Y/N)? ") if not update or update.upper()[0] != "Y": break print("{}: Event {}, Count = {}".format(source, event, ctr)) print() print("After usage...") dump(regs_tmr_int) pa_odr[PA_OUT] = 0 # LED off
def __init__(self): ''' Creates a motor driver by initializing GPIO pins and turning the motor off for safety. ''' print('Creating a motor driver') ## Set Pin PA10 toas open-drain output with pull up resistors self.pinPA10 = pyb.Pin(pyb.Pin.board.PA10, pyb.Pin.OUT_OD, pull=pyb.Pin.PULL_UP) ## Set Pin PB4 as push-pull with the correct alternate function (timer) self.pinPB4 = pyb.Pin(pyb.Pin.board.PB4, pyb.Pin.AF_PP, af=2) ## Set Pin PB5 as push-pull with the correct alternate function (timer) self.pinPB5 = pyb.Pin(pyb.Pin.board.PB5, pyb.Pin.AF_PP, af=2) self.tim3 = pyb.Timer( 3, freq=20000) # Set Timer 3 to a frequency of 20,000 Hz self.ch1 = self.tim3.channel( 1, pyb.Timer.PWM, pin=self.pinPB4) # Set Timer 3 Channel 1 to PWM for pin PB4 self.ch2 = self.tim3.channel( 2, pyb.Timer.PWM, pin=self.pinPB5) # Set Timer 3 Channel 2 to PWM for pin PB5 self.pinPA10.low() # Set Pins Low on startup self.pinPB4.low() self.pinPB5.low() print('Motor driver successfully created')
import pyb, micropython import time micropython.alloc_emergency_exception_buf(100) class Foo(object): def __init__(self, timer, led): self.led = led timer.callback(self.cb) def cb(self, tim): self.led.toggle() blue = Foo(pyb.Timer(2, freq=2), pyb.LED(1)) # LED(1) -> PE3 while True: time.sleep(1000)
import pyb, micropython micropython.alloc_emergency_exception_buf(100) class Foo(object): def __init__(self, timer, led): self.led = led timer.callback(None) def cb(self, tim): self.led.toggle() pyb.LED(1).on() pyb.LED(2).off() red = Foo(pyb.Timer(4, freq=1), pyb.LED(1)) green = Foo(pyb.Timer(2, freq=1), pyb.LED(2)) pyb.LED(1).off() pyb.LED(2).off()
def enable_pulse(self): # Setup a hardware timer to allow pulsed output self.timer = pyb.Timer(available_timers.pop()) self.freq_multipliers = {10: 10, 25: 4, 50: 2, 75: 4} self.off_inds = {10: 1, 25: 1, 50: 1, 75: 3}
from board import Board SIZE = 300 COLS = 8 display = Strips(SIZE) switch = pyb.Switch() BLACK = 0, 0, 0 WHITE = 3, 3, 3 FLASH = 255, 255, 255 BG_FULL = 0, 0, 1 BG_HALF = 0, 0, 0 tick_count = 0 timer = pyb.Timer(1, freq=10) @timer.callback def add_tick(t): global tick_count tick_count += 1 class Button: def __init__(self, desc): self.pin = pin = pyb.Pin(desc, pyb.Pin.IN, pyb.Pin.PULL_DOWN) self.prev = self.pin.value() self.reset() extint = pyb.ExtInt(pin, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, self.callback)
## LEDS AANMAKEN ## red_led = pyb.LED(3) infrared_led = pyb.LED(4) ## INITIALISATIE ## red_led.on() infrared_led.off() read_counter = 0 message = '' enable_encryption = 0 encryptie_counter = 0 uart = pyb.UART(4, 1382400) tim1 = pyb.Timer(1, freq=FREQ) tim1.callback(lambda t: read(NB_READINGS)) #lezen tim2 = pyb.Timer(2, freq=FREQ / NB_READINGS) tim2.callback(lambda t: uart.write(message)) #versturen tim7 = pyb.Timer(7, freq=FREQ / (NB_READINGS)) tim7.callback(lambda t: toggle_enable_encryption()) #encrypt en reform sw = pyb.Switch() sw.callback(lambda: pyb.LED(2).toggle()) lst_ecg = [ 0, ] * NB_READINGS lst_po = [ 0, ] * (NB_READINGS // 3)
#to use: .on() , .off() , .toggle() onBoardSwitch = pyb.Switch() #onBoardSwitch.callback(function) uses interrupt to execute function pyb.delay(ms) # ACCELEROMETER accel = pyb.accel() x = accel.x() y = accel.y() z = accel.z() #all three returns signed int from {-30 , 30} #Timer-- there are 14 independent timers-- 3 is internal, 5/6 is for servos/ADC/DAC tim = pyb.Timer(timer) tim.init(freq=10) tim.counter() tim.callback(lambda t: thing to control) # pin control from pyb import Pin p_out = Pin('X1', Pin.OUT_PP) p_out.high() p_out.low() p_in = Pin('X2', Pin.IN, Pin.PULL_UP) p_in.value() # get value, 0 or 1 # pin PWM control
def timer2_init(): tim = pyb.Timer(4) tim.init(freq=1) tim.callback(timer2_handler)
A = 0 B = 0 def exti_callback(line): global A global B if line == 9: A = A + 1 if line == 8: B = B + 1 extint1 = pyb.ExtInt(pyb.Pin.cpu.B9, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, exti_callback) extint2 = pyb.ExtInt(pyb.Pin.cpu.B8, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, exti_callback) #extint3 = pyb.ExtInt(pyb.Pin.cpu.C7, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, exti_callback) #extint4 = pyb.ExtInt(pyb.Pin.cpu.C6, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_NONE, exti_callback) t2 = pyb.Timer(2, freq=2000) oc1 = t2.channel(1, pyb.Timer.OC_TOGGLE, pin=pyb.Pin.board.X1) oc2 = t2.channel(2, pyb.Timer.OC_TOGGLE, pin=pyb.Pin.board.X2) oc1.compare(0) oc2.compare(t2.period() // 2) while True: pyb.delay(500) print('A = ', A, ' B = ', B)