![]() |
Forum Index : Microcontroller and PC projects : My brain hurts.....
Author | Message | ||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9593 |
I have some code which is working fine for an SPI LED display(standard 595's in daisy-chain arrangement), but I would like to be able to specify the placement of the decimal point as part of the string. Here is my current working code: '====================================== 'CONFIGURATION SETTINGS FOR TOP OF CODE '====================================== LATCH=2 'Display latch line - can be any I/O pin you want. SetPin LATCH,dout:Pin(LATCH)=0 '====================================== 'LOAD SIMPLE ARRAY TO HOLD SEGMENT DATA '====================================== Dim INTEGER DIGIT(&H0F) DIGIT(&H00)=&B00111111:DIGIT(&H01)=&B00000110:DIGIT(&H02)=&B01011011 DIGIT(&H03)=&B01001111:DIGIT(&H04)=&B01100110:DIGIT(&H05)=&B01101101 DIGIT(&H06)=&B01111101:DIGIT(&H07)=&B00000111:DIGIT(&H08)=&B01111111 DIGIT(&H09)=&B01101111:DIGIT(&H0A)=&B01110111:DIGIT(&H0B)=&B01111100 DIGIT(&H0C)=&B00111001:DIGIT(&H0D)=&B01011110:DIGIT(&H0E)=&B01111001 DIGIT(&H0F)=&B01110001 '============== 'MAIN CODE HERE '============== 'Demo code - can be removed. Do Input "Number to display "; N$ Input "Decimal Position "; DP LED (N$,DP) Loop 'End of demo code. End Sub LED (N$,DP) if len(N$)<>4 or DP<0 or DP>4 then N$="BAD_" DP=3 Endif D1=DIGIT(Val(Mid$(N$,1,1))) 'Build digit 1 If Mid$(N$,1,1)="_" Then D1=0 If Mid$(N$,1,1)="A" Then D1=DIGIT(10) If Mid$(N$,1,1)="B" Then D1=DIGIT(11) If Mid$(N$,1,1)="C" Then D1=DIGIT(12) If Mid$(N$,1,1)="D" Then D1=DIGIT(13) If Mid$(N$,1,1)="E" Then D1=DIGIT(14) If Mid$(N$,1,1)="F" Then D1=DIGIT(15) D2=DIGIT(Val(Mid$(N$,2,1))) 'Build digit 2 If Mid$(N$,2,1)="_" Then D2=0 If Mid$(N$,2,1)="A" Then D2=DIGIT(10) If Mid$(N$,2,1)="B" Then D2=DIGIT(11) If Mid$(N$,2,1)="C" Then D2=DIGIT(12) If Mid$(N$,2,1)="D" Then D2=DIGIT(13) If Mid$(N$,2,1)="E" Then D2=DIGIT(14) If Mid$(N$,2,1)="F" Then D2=DIGIT(15) D3=DIGIT(Val(Mid$(N$,3,1))) 'Build digit 3 If Mid$(N$,3,1)="_" Then D3=0 If Mid$(N$,3,1)="A" Then D3=DIGIT(10) If Mid$(N$,3,1)="B" Then D3=DIGIT(11) If Mid$(N$,3,1)="C" Then D3=DIGIT(12) If Mid$(N$,3,1)="D" Then D3=DIGIT(13) If Mid$(N$,3,1)="E" Then D3=DIGIT(14) If Mid$(N$,3,1)="F" Then D3=DIGIT(15) D4=DIGIT(Val(Mid$(N$,4,1))) 'Build digit 4 If Mid$(N$,4,1)="_" Then D4=0 If Mid$(N$,4,1)="A" Then D4=DIGIT(10) If Mid$(N$,4,1)="B" Then D4=DIGIT(11) If Mid$(N$,4,1)="C" Then D4=DIGIT(12) If Mid$(N$,4,1)="D" Then D4=DIGIT(13) If Mid$(N$,4,1)="E" Then D4=DIGIT(14) If Mid$(N$,4,1)="F" Then D4=DIGIT(15) If DP=1 Then D1=D1+128 'Decimal place 1 If DP=2 Then D2=D2+128 'Decimal place 2 If DP=3 Then D3=D3+128 'Decimal place 3 If DP=4 Then D4=D4+128 'Decimal place 4 SPI open 1000000,3,8 'Open SPI port to LED display module D=SPI(D4) 'Clock out digit 4 bits D=SPI(D3) 'Clock out digit 3 bits D=SPI(D2) 'Clock out digit 2 bits D=SPI(D1) 'Clock out digit 1 bits Pulse 2,1 'Clock the latch to make display visible SPI CLOSE End Sub The simplicity of this code, is in the way that the sub uses the array as an index to lookup the bit-data for the separate digits. This makes building any number from 0-9 dead easy with one single line of code, and letters A-F then take one line each if you want to use them. The problem I am having is that if I want to specify the DP with the likes of N$="12.34". This would make the LED sub much more user friendly, as just about any numeric variable could be passed to the sub without having to re-format it. A simple line of code along the lines of NUM$=STR$(NUMBER) would convert the numeric variable into a string variable for processing in the routine. Interpreting where the DP is in the scheme of things is the problem I am having, and various experiments tonight have not produced the correct results. The current code DOES work, and it works fine, but you have to know where you want the DP to be, and specify that as part of the parameters of calling the LED sub. Does anyone have any better ideas or suggestions for things to try, to simplify the sending of the DP along with the wanted display data? Smoke makes things work. When the smoke gets out, it stops! |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10247 |
My code for the MAX7219 does this - see the examples I'm using in the main program. It should be possible to modify to use in your version because I'm using the MAX7219 to give direct access to the LED segments in the same way as a 74HC595. |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1000 |
Have you tried INSTR() function. Should return position of the period or 0 if not found. e.g DP=INSTR(N$,".") Latest F4 Latest H7 FotS |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2933 |
Hi G, The following will resolve your issue. ![]() The trick is (as already mentioned) to use INSTR. However, you need to do so additional checking and more importantly you need to add a 'pointer' within your 'VAL' & 'MID' lines of code (a decimal point will alter your 'lookup' position within N$). I have shown the code for the first two digits (using x as a pointer); hopefully you can see what is going on to alter your code for the last two digits. Note the DP checking is done within each Digit chunk of code rather than at the end. Have fun . . . . . WW [code] Sub LED(N$) DP=INSTR(N$,".") 'see if N$ contains a decimal point IF DP = 0 THEN 'if no decimal point then check N$ is 4 characters long IF len(N$)<>4 THEN N$="BAD_" DP=3 ENDIF ELSE IF len(N$)<>5 THEN 'if there is a decimal point then check N$ is 5 characters long N$="BAD_" DP=3 ELSE DP=DP-1 'converts location of decimal point within the string into correct DP value for this code ENDIF ENDIF x=1 'use x as a location pointer (rather than using an absolute position that will not be correct if there is a decimal point '------------------------------------------------ D1=DIGIT(Val(Mid$(N$,x,1))) 'Build digit 1 If Mid$(N$,x,1)="_" Then D1=0 If Mid$(N$,x,1)="A" Then D1=DIGIT(10) If Mid$(N$,x,1)="B" Then D1=DIGIT(11) If Mid$(N$,x,1)="C" Then D1=DIGIT(12) If Mid$(N$,x,1)="D" Then D1=DIGIT(13) If Mid$(N$,x,1)="E" Then D1=DIGIT(14) If Mid$(N$,x,1)="F" Then D1=DIGIT(15) IF DP=1 THEN D1=D1+128 'Decimal place 1 x=x+2 'increment N$ location pointer by two so that you jump over the decimal point and onto the third character within N$ ELSE x=x+1 'point to the second character within N$ as no decimal point after first digit ENDIF '------------------------------------------------ D2=DIGIT(Val(Mid$(N$,x,1))) 'Build digit 2 If Mid$(N$,x,1)="_" Then D2=0 If Mid$(N$,x,1)="A" Then D2=DIGIT(10) If Mid$(N$,x,1)="B" Then D2=DIGIT(11) If Mid$(N$,x,1)="C" Then D2=DIGIT(12) If Mid$(N$,x,1)="D" Then D2=DIGIT(13) If Mid$(N$,x,1)="E" Then D2=DIGIT(14) If Mid$(N$,x,1)="F" Then D2=DIGIT(15) IF DP=2 THEN D2=D2+128 'Decimal place 2 x=x+2 'increment N$ location pointer by two so that you jump over the decimal point and onto the fourth character within N$ ELSE x=x+1 'point to the third character within N$ as no decimal point after second digit ENDIF . . . .[/code] |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9593 |
Thanks to all for your posts. ![]() I went to bed to rest my little grey cells after this problem stumped me last night, but thanks for all the comments, and especially to WhiteWizzard for his comprehensive reworking of the code - I will try this out later today, and let you know if it works(assuming that you have not actually tested it with an LED display yourself). Special thank you to WW for his code example, and welcome to the 1000+ posts club, BTW... ![]() ![]() ![]() Smoke makes things work. When the smoke gets out, it stops! |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9593 |
Works a charm for the numbers ![]() I will play with it a bit more, but the A-F is not important to me, really - I just included it cos you can do those letters with a 7-seg display.... IE: I don't really care about the A-F, it was the numbers I needed. Smoke makes things work. When the smoke gets out, it stops! |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |