Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 00004 SPI _spi(p5,p6,p7); //mosi, miso, sck 00005 DigitalOut cs(p8); // chip select of MCP3008 00006 TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3 00007 00008 00009 int main() { 00010 int _data1; 00011 int _data2; 00012 cs=1; // deselect everything 00013 wait(0.001); 00014 _spi.format(8,0); //see page1 of MCP3008 manual 00015 _spi.frequency(1000000); //see page4 of MCP3008 manual 00016 00017 //****************************************************************** 00018 // MCP3008 clock sequence and control bits 00019 //Once cs low, send start bit ==> 0x01 Not expecting any return 00020 //next, send control bits Expecting return B9, B8 00021 //next, send anything, say 0x00, expecting return B7-B0 00022 //next shift B9,B8 left 8bits 00023 //next merge B9,B8 and B7-B0 to form 16bits data 00024 //next, mask off bit15-bit10, to get a clean 10 bit data 00025 //******************************************************************* 00026 00027 //******************************************************************* 00028 // 1st 8bits 2nd 8bits 3rd 8bits 00029 //ch0 0x01 0x80 0x00 00030 //ch1 0x01 0x90 0x00 00031 //ch2 0x01 0xA0 0x00 00032 //ch3 0x01 0xB0 0x00 00033 //ch4 0x01 0xC0 0x00 00034 //ch5 0x01 0xD0 0x00 00035 //ch6 0x01 0xE0 0x00 00036 //ch7 0x01 0xF0 0x00 00037 //******************************************************************* 00038 00039 00040 00041 while (1) { 00042 00043 //ch0. 00044 cs=0; //cs low. start data collection 00045 _spi.write(0x01); //write start bit. Not expecting return 00046 _data1=_spi.write(0x80); //write control bit, expecting return B9, B8 00047 _data2=_spi.write(0x00); //write anything, expecting return B7-B0 00048 int ch0 =(_data1<<8) | _data2; //merge two 8bits data becoming 16bits data 00049 ch0 = ch0 & 0x03FF; // mask off bit15 - bit10 00050 cs=1; //cs high. end of data collection 00051 00052 //ch1 00053 cs=0; 00054 _spi.write(0x01); 00055 _data1=_spi.write(0x90); 00056 _data2=_spi.write(0x00); 00057 int ch1 =(_data1<<8) | _data2; 00058 ch1 = ch1 & 0x03FF; 00059 cs=1; 00060 00061 00062 //ch2 00063 cs=0; 00064 _spi.write(0x01); 00065 _data1=_spi.write(0xA0); 00066 _data2=_spi.write(0x00); 00067 int ch2 =(_data1<<8) | _data2; 00068 ch2 = ch2 & 0x03FF; 00069 cs=1; 00070 00071 00072 //ch3 00073 cs=0; 00074 _spi.write(0x01); 00075 _data1=_spi.write(0xB0); 00076 _data2=_spi.write(0x00); 00077 int ch3 =(_data1<<8) | _data2; 00078 ch3 = ch3 & 0x03FF; 00079 cs=1; 00080 00081 00082 //ch4 00083 cs=0; 00084 _spi.write(0x01); 00085 _data1=_spi.write(0xC0); 00086 _data2=_spi.write(0x00); 00087 int ch4 =(_data1<<8) | _data2; 00088 ch4 = ch4 & 0x03FF; 00089 cs=1; 00090 00091 00092 //ch5 00093 cs=0; 00094 _spi.write(0x01); 00095 _data1=_spi.write(0xD0); 00096 _data2=_spi.write(0x00); 00097 int ch5 =(_data1<<8) | _data2; 00098 ch5 = ch5 & 0x03FF; 00099 cs=1; 00100 00101 00102 //ch6 00103 cs=0; 00104 _spi.write(0x01); 00105 _data1=_spi.write(0xE0); 00106 _data2=_spi.write(0x00); 00107 int ch6 =(_data1<<8) | _data2; 00108 ch6 = ch6 & 0x03FF; 00109 cs=1; 00110 00111 00112 //ch7 00113 cs=0; 00114 _spi.write(0x01); 00115 _data1=_spi.write(0xF0); 00116 _data2=_spi.write(0x00); 00117 int ch7 =(_data1<<8) | _data2; 00118 ch7 = ch7 & 0x03FF; 00119 cs=1; 00120 00121 00122 //Display the results 00123 lcd.cls(); 00124 lcd.locate(0,0); 00125 lcd.printf("%d%d%d%d",ch0,ch1,ch2,ch3); 00126 lcd.locate(0,1); 00127 lcd.printf("%d%d%d%d",ch4,ch5,ch6,ch7); 00128 00129 wait(0.01); 00130 } 00131 } 00132
Generated on Tue Jul 19 2022 20:00:56 by
1.7.2