# A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1 # is seen in the file COPYING up one directory from this. ## @file # @brief Sample code to illustrate use of # PIC24/dsPIC33-specific Python functions # # Load in the PIC24/dsPIC33 library import pic24_dspic33 as pic # Demo some of the PIC hardware functions # --------------------------------------- # Toggle a pin # port pin isInput isOpenDrain pullDir dio = pic.digital_io(1, 1, False, False, 0) dio.set(not dio.get()) # Do some analog input ain = pic.analog_input(0) print ain.getVoltage() # Do some PWM # freq isTimer2 oc ocPin pwm1 = pic.pwm(1000, True, 2, 0) pwm1.set(0.5) # Now run main.py to start up ipm import main
# properly. dio = pic.digital_io(0, 0, False, False, 1) ain = pic.analog_input(0) testConfigAnalogPin() # Works only in Simulation mode. Follow the directions in # http://www.microchip.com/forums/tm.aspx?m=170556 # to set everything up; this simulation assumes use of # system_test.sbs, which uses adc_injection.txt as input. # From this, the generated system_test.scl file provides # stimulus for the ADC. assert(ain.getCode() == 0x1A4) # Test PWM # -------- # freq isTimer2 oc ocPin pwm1 = pic.pwm(1000, True, 2, 0) testConfigPwm() pwm1.setTime(500) testPwmSetCounts() pwm1.set(0.5) testPwmSet() # Test data transfer # ------------------ dx = pic.dataXfer() assert(dx.get(0) == None) assert(dx.get(7) == None) dx.set(0, 42) print dx.receive(False) print dx.receiveAllData()
line_sensor_right = pic.digital_io(1, 9, True) print "Line sensor 4: Is a black line present?", print line_sensor_middle.get() # The distance sensor is an analog input. Create one. print "Distance sensor: voltage is", # The only argument to analog_input is the ANx number of # the analog pin to use. Below, the distance sensor is # connected to AN4. dist_sensor = pic.analog_input(4) print dist_sensor.getVoltage(), "V" # The servos are controlled by pulse-width modulation. print "Driving left..." # Frequency isTimer2 OC module RPx pin number left_wheel = pic.pwm(50, True, 1, 6) right_wheel = pic.pwm(50, True, 2, 5) # The motor speed is controlled by the parameter to the setCounts method. # In this case, 1 count = 1.6 us. # Values to use: # Max forward: 1300 us = 1300/1.6 counts = 813 counts # Stopped : 1500 us = 1500/1.6 counts = 938 counts # Max reverse: 1700 us = 1700/1.6 counts = 1063 counts # Values between these maxima control the forward/reverse speed. # # NOTES: # - The actual counts to make each wheel stop vary slightly # from wheel to wheel. So, tune these until the wheels actually do stop. # - Because the wheels are physically installed in opposite directions, # "forward" for one wheel makes it spin in reverse. # To keep track of this, use variables to record forward/stop/reverse