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.
Dependencies: mbed
Revision 0:e546fd169f96, committed 2014-08-18
- Comitter:
- emarin
- Date:
- Mon Aug 18 13:04:41 2014 +0000
- Commit message:
- default
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DS1Wire.cpp Mon Aug 18 13:04:41 2014 +0000
@@ -0,0 +1,65 @@
+#include "DS1Wire.h"
+#include "mbed.h"
+#include <stdint.h>
+
+// Timing delay for 1-wire serial standard option
+enum DELAY { A = 5, B = 70, C = 70, D = 5, E = 10, F = 53, G = 0, H = 480, I = 70, J = 410 };
+
+int Reset(DigitalInOut& pin)
+{
+ pin.output();
+ pin = 0; // drive bus low
+ wait_us(H);
+ pin.input(); // release bus
+ wait_us(I);
+ uint32_t result = pin; // read bus value
+ wait_us(J);
+ return result;
+}
+
+void WriteBit(DigitalInOut& pin, uint32_t bit)
+{
+ if (bit) {
+ pin = 0; // drive bus low
+ pin.output();
+ wait_us(A); // delay A
+ pin.input(); // release bus
+ wait_us(B); // delay B
+ } else {
+ pin = 0; // drive bus low
+ pin.output();
+ wait_us(C); // delay C
+ pin.input(); // release bus
+ wait_us(D); // delay D
+ }
+}
+
+uint32_t ReadBit(DigitalInOut& pin)
+{
+ uint32_t bit_value;
+ pin.output();
+ pin = 0; // drive bus low
+ wait_us(A); // delay A
+ pin.input(); // release bus
+ wait_us(E); // delay E
+ bit_value = pin; // master sample bus
+ wait_us(F);
+ return bit_value;
+}
+
+void WriteByte(DigitalInOut& pin, uint32_t byte)
+{
+ for (uint32_t bit = 0; bit < 8; ++bit) {
+ WriteBit(pin, byte & 0x01); // lsb to msb
+ byte >>= 1; // right shift by 1-bit
+ }
+}
+
+uint32_t ReadByte(DigitalInOut& pin)
+{
+ uint32_t byte = 0;
+ for (uint32_t bit = 0; bit < 8; ++bit) {
+ byte |= (ReadBit(pin) << bit); // Reads lsb to msb
+ }
+ return byte;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1Wire.h Mon Aug 18 13:04:41 2014 +0000 @@ -0,0 +1,15 @@ +#ifndef __DS_1_WIRE__ +#define __DS_1_WIRE__ +#include <stdint.h> +#include "mbed.h" + +int Reset(DigitalInOut& pin); + +void WriteBit(DigitalInOut& pin, uint32_t bit); +uint32_t ReadBit(DigitalInOut& pin); + +void WriteByte(DigitalInOut& pin, uint32_t byte); +uint32_t ReadByte(DigitalInOut& pin); + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX31820.cpp Mon Aug 18 13:04:41 2014 +0000
@@ -0,0 +1,114 @@
+#include "MAX31820.h"
+#include "DS1Wire.h"
+#include "mbed.h"
+#include <stdint.h>
+
+DigitalOut conversionInProgress(LED2); // conversion in progress
+DigitalOut resetFailure(LED3); // for error reporting
+extern DigitalInOut sensor; // sensor connected to pin
+
+static void inError()
+{
+ while (1) {
+ resetFailure = !resetFailure;
+ wait(0.2);
+ }
+}
+
+ROM_Code_t ReadROM()
+{
+ ROM_Code_t ROM_Code;
+ if (Reset(sensor) != 0) {
+ inError();
+ } else {
+ WriteByte(sensor, READ_ROM); // Read ROM
+ for (uint32_t i = 0; i < 8; ++i) {
+ ROM_Code.rom[i] = ReadByte(sensor);
+ }
+ }
+ return ROM_Code;
+}
+
+void setResolution(uint8_t resln)
+{
+ uint32_t config = 0;
+ config |= (resln << 5);
+ WriteByte(sensor, WRITE_SCRATCHPAD);
+ WriteByte(sensor, TH_ALARM);
+ WriteByte(sensor, TL_ALARM);
+ WriteByte(sensor, config);
+ Reset(sensor);
+ WriteByte(sensor, SKIP_ROM); // Skip ROM
+ WriteByte(sensor, COPY_SCRATCHPAD);
+ wait_ms(15);
+}
+
+void DoConversion()
+{
+ if (Reset(sensor) != 0) {
+ inError();
+ } else {
+ conversionInProgress = 1; // led on
+ WriteByte(sensor, SKIP_ROM); // Skip ROM
+ WriteByte(sensor, CONVERT); // Convert
+ while (ReadBit(sensor) == 0) {
+ // wait for conversion to complete
+ }
+ wait(1);
+ conversionInProgress = 0; // led off
+ }
+}
+
+float calculateTemperature(ScratchPad_t *lecture)
+{
+ uint32_t read_temp = ((lecture->MSB << 8) | lecture->LSB);;
+ bool signBit = false;
+ if (lecture->MSB & 0x80)
+ signBit = true;
+ if (signBit) {
+ read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement
+ read_temp *= -1;
+ }
+ uint32_t resolution = (lecture->config & 0x60) >> 5; // mask off bits 6,5 and move to 1,0
+ switch (resolution) {
+ case NINE_BITS: // 0.5 deg C increments
+ read_temp &= 0xFFF8; // bits 2,1,0 are undefined
+ break;
+ case TEN_BITS: // 0.25 deg C increments
+ read_temp &= 0xFFFC; // bits 1,0 are undefined
+ break;
+ case ELEVEN_BITS: // 0.125 deg C increments
+ read_temp &= 0xFFFE; // bit 0 is undefined
+ break;
+ case TWELVE_BITS: // 0.0625 deg C increments
+ break;
+ }
+ float realTemp = (float)read_temp/16 ;
+ return realTemp;
+}
+
+float GetTemperature()
+{
+ float temperature = 0;
+ if (Reset(sensor) != 0) {
+ inError();
+ } else {
+ ScratchPad_t dataPad;
+ WriteByte(sensor, SKIP_ROM); // Skip ROM
+ WriteByte(sensor, READ_SCRATCHPAD); // Read Scrachpad
+ dataPad.LSB = ReadByte(sensor);
+ dataPad.MSB = ReadByte(sensor);
+ Reset(sensor);
+
+ temperature = calculateTemperature(&dataPad);
+ }
+ return temperature;
+}
+
+void displayTemperature(Serial& screen)
+{
+ DoConversion();
+ float temp = GetTemperature();
+ screen.printf("\n\rTemp is %2.1f\n\r", temp); // display in 2.1 format
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX31820.h Mon Aug 18 13:04:41 2014 +0000
@@ -0,0 +1,49 @@
+#ifndef _MAX31820_
+#define _MAX31820_
+
+#include <stdint.h>
+#include "mbed.h"
+
+#define DEVICE 0x28
+#define SCRATCHPAD_SIZE 9
+#define TH_ALARM 0x7D // 125ºC
+#define TL_ALARM 0xC9 // -50ºC
+
+// Device byte commands over 1-wire serial
+enum COMMANDS { READ_ROM = 0x33, CONVERT = 0x44, READ_SCRATCHPAD = 0xBE,
+ SKIP_ROM = 0xCC, WRITE_SCRATCHPAD = 0x4E, COPY_SCRATCHPAD = 0x48, MATCH_ROM = 0x55
+ };
+
+// Temperature read resolutions
+enum RESOLUTION {NINE_BITS , TEN_BITS, ELEVEN_BITS, TWELVE_BITS};
+
+// Device Faimly ID and Setial number information
+typedef union {
+ uint8_t rom[8];
+ struct {
+ uint8_t familyCode;
+ uint8_t serialNo[6];
+ uint8_t CRC;
+ } BYTES;
+} ROM_Code_t;
+
+ROM_Code_t ReadROM() ;
+
+// device onboard register layout
+typedef struct {
+ uint8_t LSB;
+ uint8_t MSB;
+ uint8_t TH;
+ uint8_t TL;
+ uint8_t config;
+ uint8_t reserved0xFF;
+ uint8_t reserved0xCH;
+ uint8_t reserved0x10;
+ uint8_t CRC;
+} ScratchPad_t;
+
+// Display temperature
+void displayTemperature(Serial& screen) ;
+void setResolution(uint8_t resln);
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Aug 18 13:04:41 2014 +0000
@@ -0,0 +1,61 @@
+#include <mbed.h>
+#include <stdint.h>
+#include "MAX31820.h"
+
+
+extern "C" void $Sub$$SystemInit(void)
+{
+// System clock to the IOCON & the SWM need to be enabled or
+// most of the I/O related peripherals won't work.
+ LPC_SYSCON->SYSAHBCLKCTRL |= ( (0x1 << 7) | (0x1 << 18) );
+ LPC_IOCON->PIO0_8 &= ~(0x3 << 3); /* remove the pull-up and pull-down resistors */
+ LPC_IOCON->PIO0_9 &= ~(0x3 << 3);
+ LPC_SWM->PINENABLE0 &= ~(0x3 << 4);
+ LPC_SYSCON->PDRUNCFG &= ~(0x1 << 5); /* Power-up System Osc */
+ LPC_SYSCON->SYSOSCCTRL = 0x00;
+ for (uint8_t i = 0; i < 200; i++) __NOP();
+
+ LPC_SYSCON->SYSPLLCLKSEL = 0x01; /* Select PLL Input */
+ LPC_SYSCON->SYSPLLCLKUEN = 0x01; /* Update Clock Source */
+ while (!(LPC_SYSCON->SYSPLLCLKUEN & 0x01)); /* Wait Until Updated */
+
+
+ LPC_SYSCON->SYSPLLCTRL = 0x00000023; /* P=2 (6:5), M=4 (4:0)-> 48MHz*/
+ LPC_SYSCON->PDRUNCFG &= ~(0x1 << 7); /* Power-up SYSPLL */
+ while (!(LPC_SYSCON->SYSPLLSTAT & 0x01)); /* Wait Until PLL Locked */
+
+ LPC_SYSCON->MAINCLKSEL = 0x03; /* Select PLL Clock Output */
+ LPC_SYSCON->MAINCLKUEN = 0x01; /* Update MCLK Clock Source */
+ while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); /* Wait Until Updated */
+
+ LPC_SYSCON->SYSAHBCLKDIV = 0x01;
+}
+
+DigitalInOut sensor(P0_7); /* sensor connected to pin 7 */
+
+int main(){
+ SystemCoreClockUpdate();
+ Serial pc(USBTX, USBRX); /* serial comms over usb back to console */
+ sensor.mode(PullUp);
+
+ pc.printf("\n\r=====================================================\n\r");
+ pc.printf("MAX31820 Configuration\n\r");
+ printf("LPC812 SystemCoreClock = %d Hz\n\r", SystemCoreClock);
+
+ ROM_Code_t ROM_Code = ReadROM();
+ setResolution(TWELVE_BITS);
+
+ pc.printf("Family code: 0x%X\n\r", ROM_Code.BYTES.familyCode);
+ pc.printf("Serial Number: ");
+ for (uint32_t i = 6; i != 0; --i) {
+ pc.printf("%02X%s", ROM_Code.BYTES.serialNo[i-1], (i != 1)?":":"\r\n");
+ }
+ pc.printf("CRC: 0x%X\r\n", ROM_Code.BYTES.CRC);
+ pc.printf("\n\rRunning temperature conversion...\n\r");
+
+ while (1) {
+ displayTemperature(pc);
+ wait(2);
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Aug 18 13:04:41 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013 \ No newline at end of file