32
with. step is the incremental/decremental PWM value for each successive loop. printchar is the
character to denote motor accelerating or decelerating.
The main body of the program contains four calls to loop() to demonstrate acceleration and
deceleration in each of two different rotational directions, with a short “aesthetic” pause in between
each loop, defined by rest = 0.013 (0.013s). When port 17 is 0, the motor rotates in one
direction. When port 17 is 1, it rotates the other way. As usual, the main body is contained in a try:
except: block to enable safe port reset, before exit, on keyboard interrupt.
Suggested safe tweaks to experiment with. Try changing these one at a time and see what they do...
rest = 0.013 – change 0.013 to 0 and see why it‟s there
loop(140, 1024, 1, '+') – change 140 to a positive number nearer 0
loop(140, 1024, 1, '+') – change 1024 to a lower number > 140
loop(140, 1024, 1, '+') – change + to another character
Digital to Analogue and Analogue to Digital Converters
In the Gertboard functional blocks diagram (Figure 2 on page 6), the components implementing the
converters are outlined in orange. Both the digital to analogue converter (D/A) and analogue to digital
converter (A/D) are 8-pin chips from Microchip, although oddly they are in different sorts of
packages. The A/D (labelled U6 on the circuit board) is in a dual in-line package whilst the A/D
(U10) is surface mounted. Each supports 2 channels.
Both use the SPI bus to communicate with the Raspberry Pi. The SPI pins on the two chips are
connected to the pins labelled SCLK, MOSI, MISO, CSnA, and CSnB in the header just above J2 on
the board (thus in the functional blocks diagram, these pins are also outlined in orange). SCLK is the
clock, MOSI is the output from the RPi, and MISO is the input to the RPi. CSnA is the chip select for
the A/D, and CSnB is the chip select signal for the D/A (the „n‟ in the signal name means that the
signal is „negative‟, thus the chip is only selected when the pin is low). Both A/D and D/A chips have
a 10K pull-up resistor on their chip-select pins, so the devices will not be accessed if the chip-select
pins are not connected.
The SPI pins are conveniently located just above GP7 to GP11 in header J2, because one of the
alternate functions of these pins is to drive the SPI signals. For example, the „ALT0‟ (alternative 0)
function of GPIO9 is SPI0_MISO, which is why the pin labelled MISO is just about the pin labelled
GP9. Thus to use the A/D and D/A, simply put jumpers connecting pins GP7 to GP11 to the SPI pins
directly about them (although technically you only need CSnA for the A/D and CSnB for the D/A).
In the schematics, the D/A and A/D converters are on the upper left of page A-6.
Digital to Analogue Converter
The Gertboard uses an MCP48x2 digital to analogue converter (D/A) from Microchip. The device
comes in three different types: 8, 10 or 12 bits. The one you get on your board is determined by
availability of parts. To see which one you have, look very closely at the small chip in location U10. It
should have a number 48x2 stamped on it, where x is either 0, 1, or 2.If x is 0, you have the 8-bit
version. If it‟s 1 you have the 10 bit version, and if it‟s 2 you have the 12-bit one. These chips are all
pin-compatible and are written to in the same way. In particular, the routine that writes to the D/A