Example #1
0
# 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
Example #2
0
dio = pic.digital_io(1,   1,  False,  False,      0)
dio.set(True)
assert(dio.get())
assert(dio.getPin())
assert(dio.getLatch())

# Test default args
dio = pic.digital_io(1, 1, False);

# Test analog input
# -----------------
# Set up a digital input, then change to analog to
# verify that the analog config reset everything
# 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)
Example #3
0
print "Testing line sensors.",
# For port and pin, a label of RA1 is port = 0 (A), pin = 1;
#                   a label of RB9 is port = 1 (B), pin = 9.
#                             port pin isInput
line_sensor_left =   pic.digital_io(1,   7,  True)
line_sensor_middle = pic.digital_io(1,   8,  True)
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.
#