Bus Pirate Edu Kit Exercise no.7
From DP
Contents |
Overview
In this exercise we'll use an external 10bit analog to digital converter (MCP3001) to read values from a potentiometer. The protocol used to communicate with the IC is SPI.
The protocol diagram pictured above explains how to communicate with the MCP3001. Two consecutive bytes need to be read from the device. The lower 5 bits of the first byte are the 5 most significant bits of the 10bit analog value. While the upper 5 bits of the second byte are the lower 5bits of the 10bit value.
Because of this quirky nature of the protocol used, the received result is not easy to interpret. One thing that is making slightly easier for us to convert the received bytes into a value we could use is the fact that the 3 most significant bits of the first byte are read a 0 always. This alowes us to simply shift the received value by 3 bits to the left, and the resulting value is the 10bit analog conversion.
The Circuit
Breadboard legend
| IC Location | Breadboard wiring legend | |||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
- The (+) and (-) next to PIN1 and PIN2 are intended for devices such as LEDs, where PIN1 should be the anode and connected to a higher potential then PIN2.
Bus Pirate Terminal
HiZ>m<<<the mode command
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. LCD
x. exit(without change)
(1)>5<<<select the SPI mode<<<
Set speed:
1. 30KHz
2. 125KHz
3. 250KHz
4. 1MHz
(1)>3<<<set the SPI speed to 250 KHz<<<
Clock polarity:
1. Idle low *default
2. Idle high
(1)>1<<<set clock polarity to idle low
Output clock edge:
1. Idle to active
2. Active to idle *default
(2)>2<<<set output clock edge to active to idle
Input sample phase:
1. Middle *default
2. End
(1)>1<<<select the middle input sample phase
CS:
1. CS
2. /CS *default
(2)>2<<<select the inverted CS signal (active low)
Select output type:
1. Open drain (H=Hi-Z, L=GND)
2. Normal (H=3.3V, L=GND)
(1)>2<<<set the output type to normal
Ready
SPI>W<<<turn on the power supplies
POWER SUPPLIES ON
First we need to setup the SPI protocol as per MCP4131's datasheet. Once the protocol is setup we turn on the power supplies via the 'W' command.
Move the potentiometer all the way to the right to read the highest most value in the range.
SPI>[rr]<<<read two bytes from the MCP3001
/CS ENABLED<<<sets the CS pin to LOW
READ: 0x1F<<<first received byte
READ: 0xFF<<<second received byte
/CS DISABLED<<<sets the CS pin to HIGH
The two bytes we received when reading the potentiometer at the rightmost position are '0x1F' and '0xFF'. This translates to '0001 1111 1111 1111' in a 16bit binary value. If you shift the value 3bits to the left, you get '0000 0011 1111 1111' or 1023.
Now move the potentiometer to the leftmost position to read the lowest value.
SPI>[rr]
/CS ENABLED
READ: 0x00
READ: 0x00
/CS DISABLED
The two bytes we received when reading the potentiometer at the rightmost position are '0x00' and '0x00'. This translates to '0000 0000 0000 0000' in a 16bit binary value. If you shift the value 3bits to the left, you obviously get the same 0 value.
Now feel free to move the potentiometer at any position and read the values you get. Always remember that you need to shift the values to the left by 3 bits.
SPI>m<<<the mode command
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. LCD
x. exit(without change)
(1)>1<<<select the HiZ mode
Ready
HiZ>
Once you are done with the exercise and want to shut down the Bus Pirate, just enter the HiZ mode as shown above.

