Stefan Nielsen / BV4205
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BV4205.cpp Source File

BV4205.cpp

00001 #include "mbed.h"
00002 #include "BV4205.h"
00003 
00004 BV4205::BV4205(PinName sda, PinName scl, int devaddr)
00005 {
00006     device = devaddr;
00007     adc = new I2C(sda, scl);
00008 
00009     // Set SCL freq to 75Khz
00010     adc->frequency(75000);
00011 
00012     // Reset device.
00013     adc->start();
00014     adc->write(device);
00015     adc->write(0x95);
00016     adc->stop();
00017 
00018     // Enable ADC.
00019     adc->start();
00020     adc->write(device);
00021     adc->write(0x05);
00022     adc->write(0x01);
00023     adc->stop();
00024 
00025     // Start Autoscan.
00026     adc->start();
00027     adc->write(device);
00028     adc->write(0x06);
00029     adc->write(0x01);
00030     adc->stop();
00031 }
00032 
00033 int BV4205::readChannel(int ch)
00034 {
00035     if(ch >= 0 && ch <= 9) 
00036     {
00037         adc->start();
00038         adc->write(device);
00039         adc->write(0x07);
00040         adc->write(ch*2);
00041         adc->start();
00042         adc->write(device+1);
00043         wait_us(50);
00044         int H = adc->read(1);
00045         int L = adc->read(0);
00046         adc->stop();
00047         return (H<<8 | L);
00048     } else
00049         return 0;
00050 }
00051 
00052 int BV4205::readRange(int minch, int maxch, int *array)
00053 {
00054     if(minch >= 0 && minch <= 9 && maxch >= 0 && maxch <= 9 && maxch>=minch ) 
00055     {
00056         adc->start();
00057         adc->write(device);
00058         adc->write(0x07);
00059         adc->write(minch*2);
00060         adc->start();
00061         adc->write(device+1);
00062         wait_us(50);
00063         
00064         int i = 0;
00065         while(i < maxch-minch)
00066         {
00067             int H = adc->read(1);
00068             int L = adc->read(1);
00069             array[i] = (H<<8 | L);
00070             i++;
00071         }
00072         int H = adc->read(1);
00073         int L = adc->read(0);
00074         adc->stop();
00075         array[i] = (H<<8 | L);
00076         return 1;
00077     } else
00078         return 0;
00079 }