PDA

View Full Version : 20 Digit Virtual LED Counter



T.Jackson
- 20th May 2007, 11:38
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1658&stc=1&d=1179653094" align="right">

Large scale, "real World" - LED counters can be quite expensive to build. Plus sometimes extremely difficult to drive if there's more than half a dozen or so digits. Purely hardware-based counters become increasingly more complex in circuitry with the more digits you have. But if you're using a µcontroller, writing the required software can be a daunting task. The biggest problem being, 20 digit variables as a whole simply just don't exist. Most programming languages for Windows provide direct support for the largest variable type being double precision. So as you can see, this virtual simulated approach is becoming increasingly more realistic by the second. Using your PC's monitor to display the counter which can be directly controlled by one of the PC's ports, in this case it's the serial - we can effectively achive almost anything that a real-World counter could. But before we press on, how in the World do manage 20 digits when we don't have a variable large enough? My solution here was to concatenate two double precision variables together. Messy stuff, but it works!, resulting with 20 usable digits that can be manipulated in almost any way. The counter can be fully controlled via a PIC or (similar) connected to the serial port. The PIC can load the counter with a starting value, increment or decrement that value with definable weights. The refresh rate measures in at a very modest 5 times a second @2,400 bps. Possibly much faster rates may be obtainable @9,600. (No testing done with this yet, in fact I don't even know that it works at this speed) Turning your attention to the screen shot of the Windows GUI, as you can see it's mostly all self explanatory - so I'll just make mention of a couple of things. RX control bytes are the bytes that the software expects to receive from the PIC for any given task. These are limited to 4 characters in length. You can assign anything that you wish. The first byte is critical, this is the synchronization byte that the Windows software expects to see, if it doesn't then it will totally discard any bytes that follow. The RX Values, other than shown haven't been tested but should be O.K. As you may have gathered after taking a glimpse at the screen shot of the Windows GUI, yes you can also directly control the counter via it!

<table width="100%" Border="0" align="center" Cellpadding="5">
<tr>
<td>
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1661&stc=1&d=1179655016">
</td>
<td>
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1660&stc=1&d=1179655045">
</td>
</tr>
</table>
<hr/>


<font color="#000000"> <font color="#008000"><i>'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
' - Demo Program For Windows-Based 20 Digit Virtual LED Counter - '
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''

</i></font><font color="#0000FF"><b>INCLUDE </b></font><font color="#FF0000">&quot;modedefs.bas&quot; </font><font color="#008000"><i>' Serial Protocol
</i></font><font color="#0000FF"><b>DEFINE </b></font>OSC <font color="#0000FF">4 </font><font color="#008000"><i>' 4MHz crystal
</i></font>CMCON <font color="#FF0000">= </font><font color="#0000FF">7 </font><font color="#008000"><i>' All digital
</i></font>TRISA <font color="#FF0000">= </font><font color="#0000FF">%00000000 </font><font color="#008000"><i>' All outputs
</i></font>TRISB <font color="#FF0000">= </font><font color="#0000FF">%00001101 </font><font color="#008000"><i>' Button inputs
'
</i></font>Inc_Button <font color="#0000FF"><b>VAR </b></font>PORTB<font color="#FF0000">.</font><font color="#0000FF">0 </font><font color="#008000"><i>' 3 push buttons inc, dec &amp; reset
</i></font>Dec_Button <font color="#0000FF"><b>VAR </b></font>PORTB<font color="#FF0000">.</font><font color="#0000FF">2 </font><font color="#008000"><i>'
</i></font>Rst_Button <font color="#0000FF"><b>VAR </b></font>PORTB<font color="#FF0000">.</font><font color="#0000FF">3 </font><font color="#008000"><i>'
</i></font>RX_To_PC <font color="#0000FF"><b>VAR </b></font>PORTA<font color="#FF0000">.</font><font color="#0000FF">0 </font><font color="#008000"><i>' RX line

</i></font><font color="#0000FF"><b>SEROUT </b></font>RX_To_PC<font color="#FF0000">, </font>N2400<font color="#FF0000">, [&quot;@rst&quot;] </font><font color="#008000"><i>' Reset counter
</i></font><font color="#0000FF"><b>PAUSE </b>200 </font><font color="#008000"><i>' Min required delay between commands

'================
</i></font>Wait_For_Buttons<font color="#FF0000">: </font><font color="#008000"><i>' Send commands if buttons are pressed
'================
</i></font><font color="#0000FF"><b>IF </b></font>Inc_Button <font color="#FF0000">= </font><font color="#0000FF">0 <b>THEN </b></font><font color="#008000"><i>' Increment button pressed ?
</i></font><font color="#0000FF"><b>SEROUT </b></font>RX_To_PC<font color="#FF0000">, </font>N2400<font color="#FF0000">, [&quot;@inc&quot;, &quot;1&quot;]
</font><font color="#0000FF"><b>PAUSE </b>200 </font><font color="#008000"><i>' Debounce delay + required delay
</i></font><font color="#0000FF"><b>ELSE</b></font><font color="#FF0000">:</font><font color="#0000FF"><b>IF </b></font>Dec_Button <font color="#FF0000">= </font><font color="#0000FF">0 <b>THEN </b></font><font color="#008000"><i>' Dec ?
</i></font><font color="#0000FF"><b>SEROUT </b></font>RX_To_PC<font color="#FF0000">, </font>N2400<font color="#FF0000">, [&quot;@dec&quot;, &quot;1&quot;]
</font><font color="#0000FF"><b>PAUSE </b>200 </font><font color="#008000"><i>'
</i></font><font color="#0000FF"><b>ELSE</b></font><font color="#FF0000">:</font><font color="#0000FF"><b>IF </b></font>Rst_Button <font color="#FF0000">= </font><font color="#0000FF">0 <b>THEN </b></font><font color="#008000"><i>' Reset ?
</i></font><font color="#0000FF"><b>SEROUT </b></font>RX_To_PC<font color="#FF0000">, </font>N2400<font color="#FF0000">, [&quot;@res&quot;]
</font><font color="#0000FF"><b>PAUSE </b>200
<b>ENDIF</b></font><font color="#FF0000">: </font><font color="#0000FF"><b>ENDIF</b></font><font color="#FF0000">: </font><font color="#0000FF"><b>ENDIF

GOTO </b></font>Wait_For_Buttons <font color="#008000"><i>' Maintain loop checking for buttons
</i></font>

<hr/>
The code above shows a brief sample of everything except for loading a particular value to the counter. Here's an example ...


SEROUT RX_To_PC, N2400, ["@set", "255"] 'Load counter with 255


Same as with inc & dec, rx bytes followed by the weight / value. So if we wished to increment the counter by 5 at a time we would do the following ...


SEROUT RX_To_PC, N2400, ["@inc", "5"] 'Inc counter by 5
<br/><hr/>You may download the Windows software counter.zip which is freeware but copyrighted.
But firstly you'll need to obtain msvbvm50.dll <a href="http://www.dll-files.com/dllindex/dll-files.shtml?msvbvm50" target="_blank">Click here to get it</a>.
<hr/>
Happy counting !!!

◄Trent Jackson►
<br/>

T.Jackson
- 20th May 2007, 13:35
Planning on writing an LCD emulator next. You'll be able to throw all those LCDs for prototyping in the bin, work directly on screen and save a bunch. Then once you've perfected your design go straight to PCB. Mister_e will like that :eek:

T.Jackson
- 20th May 2007, 16:05
<img src="http://www.mister-e.org/Pics/ComplaintDept.gif">

I've just employed this fine looking gentleman to handle any user related problems that might arouse from my projects. Hired him on the spot - excellent problem solver with the ability to communicate very well. According to his CV, people never have more than one problem.

mister_e
- 21st May 2007, 02:21
Seems we share the same guy ;)

http://www.picbasic.co.uk/forum/showpost.php?p=30820&postcount=8

T.Jackson
- 21st May 2007, 15:01
Yes - I think he has quite an appetite, so I'm not terribly too surprised that he has a couple of jobs.

T.Jackson
- 22nd May 2007, 03:50
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1669&stc=1&d=1179801589" align = "left">
&nbsp;&nbsp;Look realistic enough ? :D
<br/>
<br/>

Art
- 28th June 2007, 13:28
Good work, I did a calculator once that nneded to support large numbers,
and just used a byte for every digit, and a carry status bit which worked a charm :)

T.Jackson
- 28th June 2007, 17:35
My screen blitter that makes use of the GDI 32 library actually sees it as single digits. I could easily expand upwards of 100 digits or more.

tump
- 18th November 2007, 16:34
Did you ever get a chance to complete the LCD emulator looks really interesting.

T.Jackson
- 19th November 2007, 05:02
Nah, I don't plan to take it any further - it's all been done before. I need a project that is going to pay some bills. I need something that just hasn't ever been thought of. I need something that people will pay good money for. I need something that can't be easily reversed engineered and sold overseas for a fraction of the price. I think I might need a miracle :)