Introduction: How to Use SPI in LabVIEW
This Instructable will explain how to communicate with an ambient light sensor Pmod (PmodALS) via SPI using chipKIT WF32, LabVIEW, and LabVIEW MakerHub LINX. This will also go over how to read the data sheet so you can use this as a guide to get other SPI devices working.
This project can be completed with the LabVIEW Physical Computing Kit from Digilent which includes chipKIT WF32 and LabVIEW Home Bundle.
Step 1: Materials
2) chipKIT WF32
3) LabVIEW MakerHub LINX
Installation is free and in depth instructions and video guides can be found here to learn how to install and use LINX. Just click "Getting Started."
Step 2: Install Software
If you have installed LabVIEW, chipKIT WF32 drivers, and LabVIEW MakerHub LINX already, you can skip this step.
First, install LabVIEW onto your computer.
Next, install NI VISA here if you did not include it in your LabVIEW installation. Once that is completed, install LabVIEW MakerHub LINX here by clicking on download now from the attached page. A detailed installation instruction video can be found by clicking on "Getting Started."
With all the required software installed, connect chipKIT WF32 to your PC using a USB A to mini B USB cable. Windows will most likely install the device drivers automatically. To make sure, open the device manager from the control panel and expand the Ports section. The COM port is the chipKIT WF32 and if the drivers were not installed correctly, a yellow exclamation mark will appear over the device.
If the drivers were not installed properly, download the drivers for your device which are included as part of MPIDE found at http://chipkit.net/started/install-chipkit-softwa... Once the drivers are installed, right click on the COM port for the chipKIT WF32 and click properties then click on port settings and choose advanced. Under the BM options, change the latency timer to 1 ms.
Now, launch LabVIEW and click tools --> MakerHub --> LINX --> LINX Firmware Wizard to deploy the LINX firmware to the chipKIT. Choose Digilent from device family and choose chipKIT WF32 from device type and click next (pictured above). Choose the COM port that the WF32 is connected to. Click next and then choose LINX serial/USB and click next again. The firmware will then be transferred to the WF32 and the on-board lights will flash.
Again, if you're having trouble with these steps, check out the guide on LabVIEW MakerHub for step-by-step video instructions.
Step 3: ChipKIT WF32 and PmodALS Setup
The next step is to connect PmodALS to chipKIT WF32. Connect CS on the PmodALS to channel 10 on chipKIT WF32. Connect SDO to channel 12, connect SCK to channel 13, connect GND to ground, and connect VCC to 3.3V on the WF32 (pictured above). These are the default channels for SPI on chipKIT WF32. Other SPI channels can be found by looking at the reference manual.
Make sure that the jumpers on JP6 and JP7 are placed in the master position (pictured above) so chipKIT WF32 acts as a master to the Pmod.
Step 4: LabVIEW Code
To code for the SPI specifications, the CS channel, SPI channel, serial clock frequency, how many bytes are passed, where the bits of information are placed, MSB or LSB first, and whether it is active high or active low are needed.
From the PmodALS reference sheet, it is placed in normal mode by bringing the CS pin low (active low), the serial clock is between 1 and 4 MHz, bits are placed on the falling edge of the SCLK and valid on the rising edge (Mode 0), and there are 16 SCLK clock cycles which means 16 bits (2 bytes) are transferred.
The CS channel was placed on pin 10 in our case. SPI channel is 0 since this is the default configuration. Different SPI configurations will give a different SPI channel based on the information in the data sheet for the WF32.
Step 5: Information Processing
Although there is no master out slave in connection, 2 bytes must still be transferred so that 2 bytes can be received from the slave. After reading the 2 bytes, each byte is placed into an entry in a 1D array so the two bytes are added together by splicing the two entries together. The array subset block is used to skip the first 4 bits and then the next 8 bits are placed into a new array.
This 8-bit integer can range from 0 to 255 based on the light level. Since there are no units for this value, the value is represented as a percent of 255. A reading of 0 is no light sensed and a reading of 255 is the maximum light the PmodALS can sense.
Step 6: Download and Run
Download the VI below and open it. On the front panel, make sure the CS channel is 10, the serial port you found for the WF32 is selected, and the SPI channel is set to 0.
Run the VI and you're done! Feel free to let me know if you have any questions.