Used as part of the OU_Davis_Old_Robot library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP3008.cpp Source File

MCP3008.cpp

00001 /**by Austin Saunders
00002 
00003 */
00004 
00005 #include <mbed.h>
00006 #include "MCP3008.h"
00007 
00008 MCP3008::MCP3008(PinName mosi, PinName miso, PinName clk, PinName cs)
00009 :   _spi(mosi,miso,clk),
00010     _cs(cs),
00011     _vref(3.3)
00012 {
00013     _spi.frequency(1000000);
00014     _spi.format(8,0);
00015     _cs = 1;
00016 }
00017  
00018 int
00019 MCP3008::read(int ch)
00020 {
00021     _cs = 0;
00022     _spi.write(0x01);                   
00023     _data1 = _spi.write(0x80|(ch<<4));
00024     _data2 = _spi.write(0x00);
00025     int adb = (_data1<<8) | _data2; 
00026     adb = (adb & 0x03FF);
00027     _cs = 1;
00028     float ain = (adb*_vref)/1023;
00029     return adb;
00030 }