Algorithm/code to update temperature plot in real time with PIC micro

Thread Starter

roxi60

Joined Sep 2, 2018
67
Hello.
I'm searching some code example (no matter the language) where the graph (for instance a temperature Vs time plot on a GLCD) is updated in real time, that is when a new data is acquired is put at the end of the graph, scrolling all the graph so that the oldest data disappear.

Thank you for any suggestion for such a code, or where to find nice examples.

Thank you

Regards
 

jpanhalt

Joined Jan 18, 2008
10,531
I am doing that with some constraints and in Assembly. The code is in my usual dirty state and has not been cleaned up. I can give some suggestions.
1) I am using an enhanced mid-range PIC (e.g. 16F1xxx)
2) Interface with GLCD is SPI, which precludes reading the screen
3) Because of #2, data are stored in a ring buffer (FIFO) using the linear memory ability of those chips
4) Data are collected every two seconds, averaged and plotted every minute. That was done because I wanted an hour of data to display on half of the 132 column wide GLCD
5) Screen scrolls in both x and y, but not with each result. I don't particularly like scrolling with each data point, and the axes are labeled. Scrolling data in chunks of, say 5 minutes or 5 degrees, doesn't require scrolling the labels on the axes
6) Time is plotted at a ratio of 1 minute per display column; temperature is 1° per bit
7) For scrolling X-axis (time), it is just a matter of changing an offset in the FIFO and recalculating the screen (see 1st link below)
8) Displaying and scrolling temperature is a little more difficult. If temperature changes were 2° per minute or less, plotting a single pixel for each data point doesn't look too bad. If the increment is more than 2°, then the vertical gap between data points is more obvious. The appearance is not horrible, but I decided to connect them by making the higher temperature display the same number of pixels as the increment (aka "proportional pixels"). Same goes for decreasing temperature, but it's a little bit more complicated as the earlier data point needs to be extended. Both are working in very rough, but not shareable form.
9) Y-axis plots are done in 2 steps. First the page is determined (quotient after divide by 8), then the remainder is used to construct the byte to be plotted. That is relatively easy to do when not plotting proportional pixels as there is a maximum of 1 byte per data point. With proportional pixels, a data point can bridge 2 bytes, which is an arbitrary limit I set. Those bytes are determined simply by rotations.

The PIC runs at 32 MHz and re-calculation of the whole 64x64 part of the screen has very little if any noticeable blink. One could store pre-calculated values inthe FIFO, but that would double or triple the amount of memory needed.

Attachments:

1) FIFO buffer:
https://forum.allaboutcircuits.com/threads/cooking-a-roast.165197/#post-1458941

2) Crude demo of scrolling. Delays added on purpose for viewing:
https://forum.allaboutcircuits.com/threads/glcd-graphing-advice-wanted.157089/page-2#post-1451836

3) Example of proportional pixels and non-proportional pixels (same thread as just above, posts #19 and 20 (page 1).

At this point, I am not prepared to share the entire code. I am also working on a new algorithm to calculate the proportional pixel byte. I will post the entire code here when it is done. That will not be until after the Spring/Summer/Fall hiatus I take. However, if you have more focused questions, I am anxious to help.

Edit: spelling and new algorithm mention
 
Last edited:

Thread Starter

roxi60

Joined Sep 2, 2018
67
Hello jpanhalt.
Many many thanks for your kind suggestions and help, and for the time you spent for me!
I will also inform you on my findings and progress.
My aim is to build a complete weather station, where pressure, humidity,rain, temperature and wind are recorded and plotted on a GLCD with real time updating of the graph (for instance every 10 minutes).
I would like to do this at first in assembly language, since is the best way to learn micro, at the beginning.
But I have some doubt and I have to clear my mind on a simple algorithm to manage and refreshing the data plotting on the GLCD.

Thank you again.

Regards
 

jpanhalt

Joined Jan 18, 2008
10,531
Yes, I remember an earlier post about an ST display.

I assumed you were already familiar with Assembly and character displays. If not, that would be a good place to start. Assembly is highly dependent on the choice of microcontroller. So you need to decide on that too.

Finally, why do you want to graph the data? Wouldn't a character display be able to show those 4 measures and the change or rate of change simultaneously? That is much easier to do than using a GLCD. The former has a built-in character set. The latter usually does not. You have to draw everything.
 

Thread Starter

roxi60

Joined Sep 2, 2018
67
Hello jpanhalt.

I guess that to have something like this you need a GLCD (and not a character lcd) that connects pixels with segments.

Thank you


1599903473367.png
 

jpanhalt

Joined Jan 18, 2008
10,531
Yes, that chart needs a GLCD or other monitor. Have you considered how to draw the lines between values? Look up Bresenham's algorithm (https://en.wikipedia.org/wiki/Bresenham's_line_algorithm ).

Since you are new to coding micros, my advice is the same. Get started with something simpler, show your data as numerical characters (e.g., on a 4x20 character LCD). Then move onto a graphical display.

If you are set on starting with a graphical display, then you should also get started, but instead of working with the sensors and data, light a single pixel on the screen and learn to move it around starting at the top of the screen and every other row -- not page -- below that. Then do the same with a pixel character like this:
1599909952311.png
and do the same.
 

jpanhalt

Joined Jan 18, 2008
10,531
Excited to see actual results on your display.

Columns are relatively easy to address; although, for your ST device and many others, it is a 2-byte command sent as separate 8-bit bytes. Rows a are a little more difficult. Assuming a 64-pixel high screen, that is divided into 8 pages that are 8-bits/rows high. The rows are usually numbered 0 at top to 7 at bottom as you view it. So, a value of "64" decimal is the highest pixel on Page 0. The vertically mapped bytes will also have the lsb at top. So to print "64" you go to Page 0 and print 0x01 (assuming the top pixel is 64 and not 63).

Your screen has a few controls for the direction things are written. The above is just one description. Flipping the bytes around is not that hard to do. The problem is when you want to add a pixel over another pixel on the same Page or scroll. You cannot read-modify-write a byte. For example, you cannot read a byte, change the column and print the same byte to effect scrolling. You need to recalculate or recover from memory that byte and modify it.
 

BobaMosfet

Joined Jul 1, 2009
1,234
Excited to see actual results on your display.

Columns are relatively easy to address; although, for your ST device and many others, it is a 2-byte command sent as separate 8-bit bytes. Rows a are a little more difficult. Assuming a 64-pixel high screen, that is divided into 8 pages that are 8-bits/rows high. The rows are usually numbered 0 at top to 7 at bottom as you view it. So, a value of "64" decimal is the highest pixel on Page 0. The vertically mapped bytes will also have the lsb at top. So to print "64" you go to Page 0 and print 0x01 (assuming the top pixel is 64 and not 63).

Your screen has a few controls for the direction things are written. The above is just one description. Flipping the bytes around is not that hard to do. The problem is when you want to add a pixel over another pixel on the same Page or scroll. You cannot read-modify-write a byte. For example, you cannot read a byte, change the column and print the same byte to effect scrolling. You need to recalculate or recover from memory that byte and modify it.
@jpanhalt- which GLCD did you use?
 

Thread Starter

roxi60

Joined Sep 2, 2018
67
Hello jpanhalt.
There's something in enclosed Bresenham explanation that I can't understand.
Why |exy| < |ex| can be translated in ex+exy > 0 ?
How can I demonstrate it?
There's some explanation in the part enclosed, but I can't understand the math passages behind that.

Thank you for any eventual clarification
Regards
 

Attachments

Top