{"id":274,"date":"2020-05-22T20:02:25","date_gmt":"2020-05-22T20:02:25","guid":{"rendered":"http:\/\/socmaker.com\/?p=274"},"modified":"2020-05-22T20:06:59","modified_gmt":"2020-05-22T20:06:59","slug":"floating-point-reading-temperature-over-usbuart","status":"publish","type":"post","link":"https:\/\/socmaker.com\/?p=274","title":{"rendered":"Floating Point Reading: Temperature Over USBUART"},"content":{"rendered":"\n<p>If you have been following the posts from the beginning, your application can print messages to the USB connected serial port, and get typed characters from the keyboard.   See the previous post, PSoC USB UART: A Debugging Tool, here <a href=\"https:\/\/socmaker.com\/?p=168\">https:\/\/socmaker.com\/?p=168<\/a>.  (Note I updated it as I found another issue.)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Header File Collection Bin<\/h4>\n\n\n\n<p>In my projects, I am often adding header files as I create new modules.  Keeping track of what to add where becomes an issue at times.  Since I am inherently lazy, my shortcut is to create a &#8220;main.h&#8221; file and put all my header files in it.<\/p>\n\n\n\n<p>This slows the compile process.  However, with a small program that is written for PSOC&#8217;s, you usually won&#8217;t notice it.  Here is my main.h now:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef _MAIN_H_\n    #define _MAIN_H_\n\/* ========================================\n * Copyright Wade Maxfield, 2020\n * All Rights Reserved\n * LICENSED SOFTWARE.\n *  Under the GPL v3 license\n * This license does not override previous licenses\n * Some information may be Proprietary to \n * Cypress (http:\/\/www.cypress.com) for their\n * PSoC 5LP\u00ae--Cypress Semiconductor and\n * only usable on their devices.\n * PROPERTY OF Wade Maxfield.\n * Commercial license available\n * ========================================\n*\/  \n#include &lt;project.h>\n#include \"parseCommands.h\"\n#include \"USBSerial.h\"\n#include \"ds18b20.h\"\n\n#endif\n<\/code><\/pre>\n\n\n\n<p>In all of my &#8220;c&#8221; files, I now have one line:<\/p>\n\n\n\n<p><strong><code>#include \"main.h\"<\/code><\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Get Ready To Print!<\/h4>\n\n\n\n<p>In order to print out the temperature as a real number, we need to use floating point.  In C, printf() and sprintf() are used to do floating point.  (there are tutorials for sprintf and printf, google &#8220;sprintf tutorial.&#8221;)<\/p>\n\n\n\n<p>These two library functions require a lot of workspace (called &#8220;heap&#8221;) and stack space (where return from function address information is kept).  The stack space is needed because a lot of recursive (google it) function calls are made when parsing the command string.<\/p>\n\n\n\n<p>Unfortunately, the default settings of the PSOC Creator do not support floating point.  Even so, it is very easy to set up.   <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Build Settings<\/h4>\n\n\n\n<p>Open your project in PSOC Creator, go to the Project menu, and near the bottom is &#8220;Build Settings&#8230;&#8221;  Choose that, and a dialog appears.  Open the &#8220;ARM GCC&#8221; by clicking on the &#8220;+&#8221; and open Linker by clicking on the &#8220;+&#8221; and the click on General.  In the list on the right, near the bottom is &#8220;use newlib-nano Float Formatting.&#8221;  Click on the <strong>False<\/strong> to make it <strong>True<\/strong>.  Click OK.<\/p>\n\n\n\n<p>It should look something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"641\" height=\"486\" src=\"https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/BuildSetting.jpeg\" alt=\"\" class=\"wp-image-279\" srcset=\"https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/BuildSetting.jpeg 641w, https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/BuildSetting-300x227.jpeg 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/figure>\n\n\n\n<p>This change is enough to compile and attempt to run with an sprintf() statement included in your code.  However, it will act peculiarly, and crash at odd times.  That is because you don&#8217;t have enough room allocated for the resources used by sprintf.   To handle this, you must modify system wide settings.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Heap and Stack<\/h4>\n\n\n\n<p>To use floating point, we have to increase the amount of heap and stack space available to our program.   Click on the &#8220;+&#8221; by Design Wide Resources, and when it drops down, locate &#8220;System&#8221; and double click on it. On the next screen, near the middle, you will find the selections for  heap and stack.  I have shrunk the window to show it here:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"917\" height=\"607\" src=\"https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/HeapStack.jpeg\" alt=\"\" class=\"wp-image-278\" srcset=\"https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/HeapStack.jpeg 917w, https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/HeapStack-300x199.jpeg 300w, https:\/\/socmaker.com\/wp-content\/uploads\/2020\/05\/HeapStack-768x508.jpeg 768w\" sizes=\"auto, (max-width: 917px) 100vw, 917px\" \/><\/figure>\n\n\n\n<p>Change the <strong>Heap Size<\/strong> setting by clicking in the yellow part under the &#8220;Value&#8221; heading.  Make it<strong> 0x7ff<\/strong>.  This value (around 2,000 bytes) is enough for a lot of code.  Change <strong>Stack Size<\/strong> to <strong>0x1000<\/strong>.  This value is around 4,000 bytes, also enough for most programs.  If you are using the PSOC 5 CY8CKIT-059, you have 64,000 bytes of ram, so you have plenty of space still left.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Adding Code to do the Print<\/h4>\n\n\n\n<p>Add in a command letter for printing the Temperature in the parseCommands.c file.  I used the letter &#8220;T&#8221;.  Here is my code.  Note two things:  1) #include &#8220;main.h&#8221;, and 2) in the <strong>case &#8216;t&#8217;:<\/strong> I added <strong>case &#8216;T&#8217;:<\/strong>  (That way, I don&#8217;t worry about the caps lock setting), and 3) The curly bracket <strong>{<\/strong> after the case and before the break <strong>}break;<\/strong> (the bracket allows me to define variables and externs without complaints from the compiler.)  See following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* ========================================\n * Copyright Wade Maxfield, 2020\n * All Rights Reserved\n * LICENSED SOFTWARE.\n *  Under the GPL v3 license\n * This license does not override previous licenses\n * Some information may be Proprietary to \n * Cypress (http:\/\/www.cypress.com) for their\n * PSoC 5LP\u00ae--Cypress Semiconductor and\n * only usable on their devices.\n * PROPERTY OF Wade Maxfield.\n * Commercial license available\n * ========================================\n*\/\n#include \"main.h\"\n\nchar outMsg&#91;64];\nchar commandBuffer&#91;64];\nint16_t commandBufferIndex;\n\nint16_t debugPrint;\n\n\/\/-------------------------------------------\n\/\/-------------------------------------------\nvoid init_parse() {\n    uint16_t c=1;\n    parse((char *)\"v\",&amp;c);\/\/ show sign on message\n}\n\/\/-------------------------------------------\n\/\/-------------------------------------------\nvoid parse(char *RXBuffer, uint16_t *rxCountp) {\n    int16_t rxCount = *rxCountp;\n    static int16_t bufferIndex;    \n    *rxCountp=0;\/\/ reset indicating we consumed buffer\n    char c;\n     \n    for (bufferIndex =0 ; bufferIndex &lt; rxCount; bufferIndex++) {\n        c=commandBuffer&#91;commandBufferIndex++]=RXBuffer&#91;bufferIndex];\n        printChar(c);\n        if (c != '\\r')\n            continue;\n        \n        printChar('\\n');\n        commandBufferIndex=0;\n        \/\/ we have received data from usb.  look at it and determine what to do\n        switch(commandBuffer&#91;0]) {\n            case 0:\n            case '\\n':\n            case '\\r':\n            default:                               \n            case 'v':\n            case 'V':\n            printSignon:\n                printMessage(\"\\n\\n\\r--------\\n\\r\");\n                printMessage(\"commands: (press enter after command to execute)\\n\\r---------\\n\\r\");\n                printMessage(\" D      -- Toggle Debug Print\\n\\r\");\n                printMessage(\" T      -- Print Current Temperature\\n\\r\");\n                printMessage(\" V      -- Version \/ help\\n\\r\");\n                printMessage(\"--\\n\\renter command>\");\n                \n                return;\n            \n            \n            case 'd': case 'D':\n                debugPrint=!debugPrint;\n                printMessage(\"Debug print now \");\n                if (debugPrint)\n                    printMessage(\"on\\n\\r\");\n                else\n                    printMessage(\"off\\n\\r\");\n                    \n                    goto printSignon;\n            break;\n                    \n            case 't': case 'T':{\n                extern float temperature;\n                sprintf(outMsg,\"\\n\\rTemperature=%f\\n\\r\",temperature);\n                printMessage(outMsg);\n                goto printSignon;\n            }break;                   \n            }\/\/switch(commandBuffer\n    }\/\/for()\n    return;   \n}\n\/* &#91;] END OF FILE *\/\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">One More Thing!<\/h4>\n\n\n\n<p>You may have tried to compile and had it fail.  The reason is there is a header file include which is missing.  Add &lt;stdio.h&gt; to main.h:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef _MAIN_H_\n    #define _MAIN_H_\n\/* ========================================\n * Copyright Wade Maxfield, 2020\n * All Rights Reserved\n * LICENSED SOFTWARE.\n *  Under the GPL v3 license\n * This license does not override previous licenses\n * Some information may be Proprietary to \n * Cypress (http:\/\/www.cypress.com) for their\n * PSoC 5LP\u00ae--Cypress Semiconductor and\n * only usable on their devices.\n * PROPERTY OF Wade Maxfield.\n * Commercial license available\n * ========================================\n*\/  \n#include &lt;project.h>\n#include &lt;stdio.h>\n#include \"parseCommands.h\"\n#include \"USBSerial.h\"\n#include \"ds18b20.h\"\n\n#endif\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Compile and Run<\/h4>\n\n\n\n<p>If you have done everything without error (never the case for me the first few times), you can run your program on the PSOC and plug in the USBUART on the end of the CY8CKIT-059.<\/p>\n\n\n\n<p>Open in your terminal application at 115,200 baud (see previous posts, I use &#8216;screen&#8217;), and press enter.  You should see the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--------\ncommands: (press enter after command to execute)\n---------\n D      -- Toggle Debug Print\n T      -- Print Current Temperature\n V      -- Version \/ help\n--\nenter command>\n<\/code><\/pre>\n\n\n\n<p>Press T and then enter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--------\ncommands: (press enter after command to execute)\n---------\n D      -- Toggle Debug Print\n T      -- Print Current Temperature\n V      -- Version \/ help\n--\nenter command>t\n\nTemperature=71.048752\n\n\n--------\ncommands: (press enter after command to execute)\n---------\n D      -- Toggle Debug Print\n T      -- Print Current Temperature\n V      -- Version \/ help\n--\nenter command>\n<\/code><\/pre>\n\n\n\n<p>Notice the temperature is printed out to a ridiculous number of digits.  That is fine, and will be useful in the future.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Next Time<\/h4>\n\n\n\n<p>If everything works out, next time we will look at adding a millisecond timer and blinking an LED.  This timer will be useful in the future.<\/p>\n\n\n\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have been following the posts from the beginning, your application can print messages to the USB connected serial port, and get typed characters from the keyboard. See the previous post, PSoC USB UART: A Debugging Tool, here https:\/\/socmaker.com\/?p=168. (Note I updated it as I found another issue.) Header File Collection Bin In my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-blogposts"],"_links":{"self":[{"href":"https:\/\/socmaker.com\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/socmaker.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/socmaker.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/socmaker.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/socmaker.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":12,"href":"https:\/\/socmaker.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":288,"href":"https:\/\/socmaker.com\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions\/288"}],"wp:attachment":[{"href":"https:\/\/socmaker.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/socmaker.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/socmaker.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}