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 00003 // DS18B20 converted to run on mbed 00004 00005 #define TEMP_PIN p20 00006 00007 Serial pc(USBTX, USBRX); // tx, rx 00008 00009 DigitalInOut temperature_pin(TEMP_PIN); 00010 00011 void OneWireReset() { // reset. Should improve to act as a presence pulse 00012 temperature_pin.output(); 00013 temperature_pin = 0; // bring low for 500 us 00014 wait_us(500); 00015 temperature_pin.input(); 00016 wait_us(500); 00017 } 00018 00019 void OneWireOutByte(unsigned char d) { // output byte d (least sig bit first). 00020 for(int n=8; n!=0; n--) { 00021 if ((d & 0x01) == 1) { // test least sig bit 00022 temperature_pin.output(); 00023 temperature_pin = 0; 00024 wait_us(5); 00025 temperature_pin.input(); 00026 wait_us(60); 00027 } else { 00028 temperature_pin.output(); 00029 temperature_pin = 0; 00030 wait_us(60); 00031 temperature_pin.input(); 00032 } 00033 00034 d=d>>1; // now the next bit is in the least sig bit position. 00035 } 00036 00037 } 00038 00039 unsigned char OneWireInByte() { // read byte, least sig byte first 00040 unsigned char d = 0, b; 00041 for (int n=0; n<8; n++) { 00042 temperature_pin.output(); 00043 temperature_pin = 0; 00044 wait_us(5); 00045 temperature_pin.input(); 00046 wait_us(5); 00047 b = temperature_pin; 00048 wait_us(50); 00049 d = (d >> 1) | (b << 7); // shift d to right and insert b in most sig bit position 00050 } 00051 return d; 00052 } 00053 00054 int main() { 00055 int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract; 00056 pc.printf("mbed 1 wire interface simple test!\r\n"); 00057 temperature_pin.output(); 00058 temperature_pin = 0; 00059 temperature_pin.input(); // sets the digital pin as input (logic 1) make sure external pullup resistor 4K7 00060 wait(1); 00061 pc.printf("temperature measurements:\r\n"); 00062 00063 while (1) { 00064 OneWireReset(); 00065 OneWireOutByte( 0xcc); //Skip ROM command 00066 OneWireOutByte( 0x44); // perform temperature conversion, strong pullup for one sec 00067 00068 OneWireReset(); 00069 OneWireOutByte( 0xcc); 00070 OneWireOutByte( 0xbe); //Read Scratchpad 00071 00072 LowByte = OneWireInByte(); 00073 HighByte = OneWireInByte(); 00074 TReading = (HighByte << 8) + LowByte; 00075 SignBit = TReading & 0x8000; // test most sig bit 00076 if (SignBit) { // negative 00077 TReading = (TReading ^ 0xffff) + 1; // 2's comp 00078 } 00079 Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25 00080 00081 Whole = Tc_100 / 100; // separate off the whole and fractional portions 00082 Fract = Tc_100 % 100; 00083 00084 if (SignBit) { // If its negative 00085 pc.printf("-"); 00086 } 00087 pc.printf("%d", Whole); 00088 pc.printf("."); 00089 if (Fract < 10) { 00090 pc.printf("0"); 00091 } 00092 pc.printf("%d", Fract); 00093 pc.printf("\r\n"); 00094 00095 wait(5); // 5 second delay. Adjust as necessary 00096 } 00097 }
Generated on Sun Jul 17 2022 11:57:31 by
1.7.2