program for external ADC ADS8320 Based on the program for the SCP1000. Still figuring out timing issues

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
marcelvandekamp
Date:
Fri Feb 18 13:46:46 2011 +0000
Commit message:
V3

Changed in this revision

ads8320/ads8320.cpp Show annotated file Show diff for this revision Revisions of this file
ads8320/ads8320.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ads8320/ads8320.cpp	Fri Feb 18 13:46:46 2011 +0000
@@ -0,0 +1,50 @@
+
+
+#include "ads8320.h"
+
+ads8320::ads8320(PinName mosi, PinName miso, PinName sclk, PinName cs)
+    : m_spi(mosi, miso, sclk)
+    , m_cs(cs) {
+    m_cs=1;
+    m_spi.frequency(1000000); // the fastest of the sensor
+    m_spi.format(8, 0); // duda son dos palabras de 8 bits? 
+   // wait(0.5);
+    //------------------------------------------------
+    // pc.printf("RESET\r\n");
+    write_register(0x06,0x01);
+  // wait(0.5);
+
+    // pc.printf("Initialize High Resolution Constant Reading Mode\r\n");
+    write_register(0x03,0x0A);
+    //wait(0.5);
+}
+
+
+ unsigned int ads8320::readTemperature() {  //was float
+    unsigned int temp_in = read_register16(TEMP);
+   // temp_in /= 20;
+    unsigned int Ti = temp_in;  
+    return Ti; //return temp_in;
+}
+
+void ads8320::write_register(char register_name, char register_value) {
+    register_name <<= 2;
+    register_name |= 0x02; //write command
+    m_cs=0; //Select SPI device
+    m_spi.write(register_name); //Send register location
+    m_spi.write(register_value); //Send value to record into register
+    m_cs=1;
+}
+
+unsigned int ads8320::read_register16(char register_name) {   
+    register_name <<= 2;
+    register_name &= 0xFC; //Read command
+    m_cs=0; //Select SPI Device
+    m_spi.write(register_name); //Write byte to device
+    unsigned int in_byte1 = m_spi.write(0x00);     //(was zonder unsigned)
+    unsigned int in_byte2 = m_spi.write(0x00);
+    m_cs=1;
+    //float in_word= (in_byte1<<=8) | (in_byte2); 
+    unsigned int in_word= (in_byte1<<=8) | (in_byte2);  
+    return(in_word);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ads8320/ads8320.h	Fri Feb 18 13:46:46 2011 +0000
@@ -0,0 +1,41 @@
+
+
+#ifndef _ads8320_H
+#define _ads8320_H
+
+#include "mbed.h"
+
+class ads8320 {
+    public:
+        /**
+         * Constructor.
+         *
+         * @param mosi SPI MOSI pin
+         * @param miso SPI MISO pin
+         * @param sclk SPI SCLK pin
+         * @param cs Chip select pin
+         */
+        ads8320(PinName mosi, PinName miso, PinName sclk, PinName cs);
+        
+        ~ads8320() { /* empty */ };
+        
+   
+        unsigned long readPressure();
+        
+ 
+   
+        unsigned int readTemperature(); //was float
+        
+    private:
+ 
+        static const char TEMP = 0x21;       //16 bit temp
+        SPI m_spi;
+        DigitalOut m_cs;
+        
+       // char read_register(char register_name);
+        void write_register(char register_name, char register_value);
+       // float read_register16(char register_name);
+        unsigned int read_register16(char register_name);
+};
+
+#endif // _
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Feb 18 13:46:46 2011 +0000
@@ -0,0 +1,68 @@
+#include "mbed.h"
+#include "ads8320.h"
+
+
+# define SAMPLENRS 7000
+# define SAMPLES_SEC 161364
+# define SAMPLERATE 9600
+
+Serial pc(USBTX, USBRX);
+ads8320 adsx8320(p5,p6,p7,p8);  
+Serial lcd (p28, p27);
+InterruptIn Potmeter(p23);
+DigitalOut led(LED1);
+
+void clear(void);
+void Measure(void);
+unsigned int samples[SAMPLENRS];
+unsigned int average;
+unsigned int sum;
+Timer t;
+
+int main() {
+    Potmeter.rise(&Measure); //if P5 high, then measure
+    while (1) {
+        led = !led;
+        wait(0.25);
+        pc.printf("say something");
+    }
+}
+
+
+
+void Measure() {
+__disable_irq();
+    unsigned int pause = (SAMPLES_SEC/SAMPLERATE) / SAMPLES_SEC;
+    int c=0;
+    clear();
+    lcd.baud(9600);
+    while (1) {
+
+        t.reset();
+        t.start();
+       /* for (int i=0; i < SAMPLENRS; i++) {
+            samples[i] = adsx8320.readTemperature();
+            sum+= samples[i];
+            wait(0.0001041);  //divider 0.0001355
+        }*/
+        pc.printf("waarde is: %u \n\r",adsx8320.readTemperature());
+        t.stop();
+        clear();
+        average = sum/SAMPLENRS;
+        //lcd.printf("gem is %u\n\r", average);
+        //lcd.printf("t: %f \n\r", t.read());
+        sum =0;
+        wait(1);
+        clear();
+        average=0;
+__enable_irq();
+
+    }
+}
+
+void clear(void) {
+    // lcd.putc(0xFE);
+    lcd.putc(0x01);
+    lcd.printf("                \n\r");
+    lcd.printf("                \n\r");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Feb 18 13:46:46 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912