Used as part of the OU_Davis_Old_Robot library

Committer:
DrewSchaef
Date:
Wed Nov 01 15:56:02 2017 +0000
Revision:
0:2cb766caa4c7
Committed to allow program to be published

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DrewSchaef 0:2cb766caa4c7 1 /**by Austin Saunders
DrewSchaef 0:2cb766caa4c7 2
DrewSchaef 0:2cb766caa4c7 3 */
DrewSchaef 0:2cb766caa4c7 4
DrewSchaef 0:2cb766caa4c7 5 #include <mbed.h>
DrewSchaef 0:2cb766caa4c7 6 #include "MCP3008.h"
DrewSchaef 0:2cb766caa4c7 7
DrewSchaef 0:2cb766caa4c7 8 MCP3008::MCP3008(PinName mosi, PinName miso, PinName clk, PinName cs)
DrewSchaef 0:2cb766caa4c7 9 : _spi(mosi,miso,clk),
DrewSchaef 0:2cb766caa4c7 10 _cs(cs),
DrewSchaef 0:2cb766caa4c7 11 _vref(3.3)
DrewSchaef 0:2cb766caa4c7 12 {
DrewSchaef 0:2cb766caa4c7 13 _spi.frequency(1000000);
DrewSchaef 0:2cb766caa4c7 14 _spi.format(8,0);
DrewSchaef 0:2cb766caa4c7 15 _cs = 1;
DrewSchaef 0:2cb766caa4c7 16 }
DrewSchaef 0:2cb766caa4c7 17
DrewSchaef 0:2cb766caa4c7 18 int
DrewSchaef 0:2cb766caa4c7 19 MCP3008::read(int ch)
DrewSchaef 0:2cb766caa4c7 20 {
DrewSchaef 0:2cb766caa4c7 21 _cs = 0;
DrewSchaef 0:2cb766caa4c7 22 _spi.write(0x01);
DrewSchaef 0:2cb766caa4c7 23 _data1 = _spi.write(0x80|(ch<<4));
DrewSchaef 0:2cb766caa4c7 24 _data2 = _spi.write(0x00);
DrewSchaef 0:2cb766caa4c7 25 int adb = (_data1<<8) | _data2;
DrewSchaef 0:2cb766caa4c7 26 adb = (adb & 0x03FF);
DrewSchaef 0:2cb766caa4c7 27 _cs = 1;
DrewSchaef 0:2cb766caa4c7 28 float ain = (adb*_vref)/1023;
DrewSchaef 0:2cb766caa4c7 29 return adb;
DrewSchaef 0:2cb766caa4c7 30 }