Thursday, May 19, 2011

Using an MCP3002 ADC for Interfacing an FPGA with a Photocell

The MCP3002 ADC Chip is a very handy device. You can interface with it via a very simple SPI (Serial Peripheral Interface) protocol. The Verilog module that I designed to interface with this chip works beautifully.

The module I created uses both channels of the MCP3002 in an "interlaced" single-ended mode. The module is strictly configured to work with a local oscillator of 24MHz, but minor adjustments to the process that converts the local clock to a 1.2MHz clock is all that needs to be modified. The 1.2MHz clock is the maximum frequency that the IC can operate at while powered at 2.7V as specified in the datasheet (pg3) under the "Timing Parameters". In my case my FPGA uses 3.3V logic so Fclk(max) should reside somewhere between this and 3.2MHz. If we assume a linear relationship between voltage and operating frequency, then the maximum expected operating frequency at 3.3V would be ~1.688MHz, thus operating at 1.2MHz "should" be a safe level of operation while pushing the IC to work at its maximum.

As far as the code goes, it is really self explanatory. I would recommend anyone that is attempting to use this code to have a copy of the datasheet on hand so that you can see the relationship from my code and the datasheet's timing diagram. Figure 5-1 was specifically used to design this Verilog Module.

On a hardware interfacing note, be sure to pay attention to Figure 4-2 in the datasheet, this clearly states that the ADC's CH0 and CH1 input signals NEED to be buffered (i.e. use an opamp in a buffer configuration or similar). If you skip this, your maximum operating frequency will be lower that the one defined in the datasheet.

The code that I provide here is free to use however you would like, but I would appreciate it if you give me credit for my work. Also constructive criticism is welcome, if you see something that could be done better another way, let me know.

Download: MCP3002 ADC Module

Monday, May 2, 2011

Setting up OpenCV in Eclipse on Windows (OpenCV2.2)

The latest release of OpenCV (version 2.2) has undergone dramatic changes to the library. Because of these changes, my older guide is now outdated. For OpenCV version 2.2, please use this guide.

First download and setup the basics:
  • Get MinGW
  • Download Eclipse C/C++ IDE
Note: I have a tutorial on these two steps already so just head over HERE for more info.

  • Next download/install OpenCV 2.2
  • Now launch Eclipse and start a new project by going to:
  1. File->New->C++ Project (or File->New->C Project)
  2. Give your project a name in the "Project name" box
  3. Select the "Hello World" option under the "Project Type" section under the"Executable" folder. I recommend this over the "Empty Project" as it creates the c/c++ file for you instead of having to do it manually (it also creates a "src" folder and a"Debug" folder which helps keep things a little more organized)
  4. Make sure the "MinGW" Toolchain is selected in the "Toolchains" section
  5. Hit NEXT
  6. Fill in your Author and other file information, then hit NEXT
  7. In the next window select "Advanced settings...". This will bring you to the "Project Settings" which can always be accessed later by going to Project->Properties
  8. Under the "C/C++ Build" Section go to the "Settings" and select the "Tool Settings"Tab. Then select the "Includes" folder (on older versions of eclipse it is the "Directories" folder) in the GCC Compiler branch and add the opencv include directory to Include paths (-I): "C:\OpenCV2.2\include\". Of course, change C:\OpenCV2.2 to match the installed path that you used.
  9. Now, under the MinGW Linker select the "Libraries" folder and add the following to the Libraries (-l) section (note not all of these are necessarily needed for your project but these are all the libraries available in opencv version 2.2):
  • opencv_calib3d220
  • opencv_contrib220
  • opencv_core220
  • opencv_features2d220
  • opencv_ffmpeg220
  • opencv_flann220
  • opencv_gpu220
  • opencv_highgui220
  • opencv_imgproc220
  • opencv_legacy220
  • opencv_ml220
  • opencv_objdetect220
  • opencv_ts220
  • opencv_video220
In most cases you will only need opencv_core220 and opencv_highgui220 to get started

NOTE: Versions 2.2 and later postfix the libraries names with a three digit number that corresponds to the version of OpenCV that you are linking to.

FINALLY, under the "Library search path (-L)" section add:
  • "C:\OpenCV2.2\lib"
  1. Hit OK when done
  2. Hit Finish to create and start the Project
Write up a simple program to test if opencv works properly. You can use this EXAMPLE CODE I made to test if your video camera and environment are working properly.

NOTE: The way this guide was written will require you to use a relative path to specify the header files you wish to include. OpenCV breaks up the library into two sections, "legacy libraries" and "new libraries".

If you wish to specify one of the new opencv libraries use the following format:

#include "opencv2\[subfolder]\[library.hpp]"

ADDITIONALLY, if you wish to include all of the primary opencv2 header files in a project you can use:

#include "opencv2\opencv.hpp"

Note: opencv.hpp does not include every library in OpenCV2.2. It may be helpful for you to view the "opencv.hpp" library (located by default in: C:\OpenCV2.2\include\opencv2\opencv.hpp) to see what is included and how you can reference the new libraries in your code. To see the complete list of libraries available, go to "C:\OpenCV2.2\include\" and browse around the directory.

Legacy libraries can be defined by: #include "opencv\[library.hpp]"


If you get an error, during compile time, pertaining to:
__exchange_and_add
Then visit this page and follow the instructions under:
Building on Windows using MinGW 3.4.5

Hope this helps!