hideaki matsukawa / Mbed 2 deprecated SCP1000_Test01

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //---------------1-------------------------
00002 
00003 #include "mbed.h"
00004 #include "TextLCD.h"
00005 
00006 TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
00007 SPI spi(p5, p6, p7); // mosi, miso, sclk
00008 DigitalOut cs(p8);
00009 
00010 Serial pc(USBTX, USBRX); // tx, rx
00011 
00012 
00013 //-----------------2------------------------
00014 //Addresses
00015 #define REVID 0x00    //ASIC Revision Number
00016 #define OPSTATUS 0x04   //Operation Status
00017 #define STATUS 0x07     //ASIC Status
00018 #define START 0x0A      //Constant Readings
00019 #define PRESSURE 0x1F   //Pressure 3 MSB
00020 #define PRESSURE_LSB 0x20 //Pressure 16 LSB
00021 #define TEMP 0x21       //16 bit temp
00022 
00023 //---------------3-----------------------
00024     
00025 float temp_in;
00026 unsigned long pressure_lsb;
00027 unsigned long pressure_msb;
00028 unsigned long temp_pressure;
00029 unsigned long pressure;
00030 
00031 char read_register(char register_name)
00032 {
00033     register_name <<=2;
00034     register_name &= 0xFC;
00035     cs=0; //Select SPI device
00036     spi.write(register_name); //Send register location
00037     char register_value=spi.write(0x00);
00038     cs=1;
00039     return register_value;  
00040 }
00041 
00042 
00043 void write_register(char register_name, char register_value)
00044 {
00045     register_name <<= 2;
00046     register_name |= 0x02; //le estamos diciendo que escriba
00047     cs=0; //Select SPI device
00048     spi.write(register_name); //Send register location
00049     spi.write(register_value); //Send value to record into register
00050     cs=1;
00051 } 
00052 
00053 float read_register16(char register_name)
00054 {   
00055     register_name <<= 2;
00056     register_name &= 0xFC; //Read command
00057     cs=0; //Select SPI Device
00058     spi.write(register_name); //Write byte to device
00059     int in_byte1 = spi.write(0x00);    
00060     int in_byte2 = spi.write(0x00);
00061     cs=1;
00062     float in_word= (in_byte1<<=8) | (in_byte2);   
00063     return(in_word);
00064 }
00065 
00066 
00067 //-----------------4------------------------
00068 
00069 int main() {
00070 // configuracion---------------------------------
00071     cs=1;
00072     spi.frequency(500000); // the fastest of the sensor
00073     spi.format(8, 0); // duda son dos palabras de 8 bits? 
00074     wait(0.5);
00075 //------------------------------------------------
00076 // pc.printf("RESET\r\n");
00077 write_register(0x06,0x01);
00078 wait(0.5);
00079 
00080 // pc.printf("Initialize High Resolution Constant Reading Mode\r\n");
00081 write_register(0x03,0x0A);
00082 wait(0.5);
00083 
00084 while (1) {  
00085   pressure_msb = read_register(PRESSURE);
00086   pressure_msb &= 0x07;
00087   pressure_lsb = read_register16(PRESSURE_LSB);
00088   pressure = ((pressure_msb<<16)| pressure_lsb);
00089   pressure /= 4;
00090   temp_in = read_register16(TEMP);
00091   lcd.cls();
00092   if (temp_in > 0x3FFF) {
00093         temp_in = 0x3FFF - temp_in;
00094         lcd.locate(0, 0);
00095         lcd.printf("t:%.2f",temp_in);
00096         temp_in = temp_in / 20;
00097         lcd.locate(0, 1);
00098         lcd.printf("Temp :-");
00099         lcd.printf("%.1f",temp_in);
00100   // pc.printf("TEMPERATURA: %.2f,PRESION:  %.2u\r",temp_in ,pressure);
00101   } else {
00102         lcd.locate(0, 0);
00103         lcd.printf("t:%.2f",temp_in);
00104         lcd.locate(0, 1);
00105         temp_in = temp_in / 20;
00106         lcd.printf("Temp :+");
00107         lcd.printf("%.1f",temp_in);
00108   // pc.printf("TEMPERATURA: %.2f,PRESION:  %.2u\r",temp_in ,pressure);
00109   }
00110   // temp_in = temp_in / 20;
00111   wait(5);
00112 }
00113 
00114 }