DYNAMIC PULL-DOWN MENU 3x5 FOR KS0108

General discussion on mikroBasic.
Author
Message
MAN
Posts: 437
Joined: 11 Jan 2006 18:32
Location: Brasil

DYNAMIC PULL-DOWN MENU 3x5 FOR KS0108

#1 Post by MAN » 24 Aug 2007 01:25

Hi all;


This is a example of menu pull-down for GLCD with driver KS0108 and use a P16F877A to do this. He have 3 top menu and 5 item menu each. Five button's was used to control this menu like this, 2 button's control the horizontal direction and 2 button control the vertical direction the last is the button select/Enter that pull down the 5 items menu each. We can navigate through the menus and items when we select a menu item it go out menu and return the result to do the flux control of program. We can adapt this program for several finalities and can control the position of menu just changing three values that determine it.

Code: Select all

'*
'* Project name:
'          Menu Pull-Down Example for KS0108
'  Copyright:
'         ©Marcio Nassorri
'* Revision History:
'       01/08/2007:
'       - initial release.
' * Description:
'       Simulates a Pull-Down Menu and the user make a choice
'       for one these itens. The Menu return a pair number
'       inner a integer var as Lo(Byte) having the item
'       menu and Hi(Byte) having main menu choice.
'
' * Test configuration:
'     MCU:             PIC16F877A
'     Dev.Board:       BPIC3 with custom processor card
'     Oscillator:      HS, 8.000 MHz
'     Ext. Modules:    Lib_MDStrArray
'     SW:              mikroBasic v5.0 or higher
' * NOTES:
'     For control Menu in this example PORTB was used
'     but other can be used.
'     Port Description:
'          PORTB.0 = Select/Enter
'          PORTB.1 = UpArrow
'          PORTB.2 = DwArrow
'          PORTB.3 = LeftArrow
'          PORTB.4 = RightArrow
'     all button have your active state at logic Low.
'     even so all these pins of PORTB must have pull-up resistor.
'     With minor modifications can adapted to other MCU's.
'
'

program PullDownMenu

Include "Lib_MDStrArray"

Symbol SWPORT       = PORTB 'Port for Swtich control

'for this example five pin's was used.
'But, just three pin can be used.
Symbol SwUP         = 1     'Switch pin's Ctrl
Symbol SwDW         = 2     '
Symbol SwLT         = 3     '
Symbol SwRT         = 4     '
Symbol SwEnter      = 0     '

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'To change the position of Menu on the Screen just change these
'values.
Symbol ColInit      = 14  'Start Menu in the XPOS, in point's(0..127)
Symbol LineInit     = 0   'Start Menu in the YPOS (0..7..) 7 = bot menu
Symbol MainMnuSpace = 30  'Space between MnuTop, 6 char (5x6..8)
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

'+++++++++++++++++++++++++++++++++++++++++++++
'To Add more number of MnuTop or MnuItem
'change this values.
Symbol MnuTop       = 3  'Number of Top Menu
Symbol MnuItem      = 5  'Number of item Menu
'+++++++++++++++++++++++++++++++++++++++++++++


Const MainMnu as String[MnuTop][4]  = ("File","Edit","View")

Const MnuFile as String[MnuItem][7] = ("Open",
                                       "Close",
                                       "Save",
                                       "Save as",
                                       "Print")
                                  
Const MnuEdit as String[MnuItem][5] = ("Undo",
                                       "Redo",
                                       "Copy",
                                       "Paste",
                                       "Find")

Const MnuProp as String[MnuItem][9] = ("Show",
                                       "Statist",
                                       "Messages",
                                       "P.Setup",
                                       "Cod.Expl.")

Const SomeTxtInit1 as Char[14] = "Pull-Down Menu"
Const SomeTxtInit2 as char[11] = "for P16F87X"
Const SomeTxt1     as char[14] = "Do Your choice"
Const LineBlank    as char[20] = "                    "
Const ChceMnu      as Char[5]  = "Menu "
Const ChceItem     as Char[6]  = ",Item "


Const _YPOS      as Byte = (LineInit)
Const _XPOS      as Byte = (ColInit)
Const _XOFFSET   as Byte = (MainMnuSpace)


Dim Text  as String[20] 'absolute $20
Dim Digit as String[1]
Dim J     as integer
Dim k     as Byte


Sub Procedure Glcd_ClearScr()
Dim K as Byte

    CopyArrayStr(Text,@LineBlank,0,20,0)
    For K = 7 to 0 step - 1
        GLCD_Write_Text(Text,0,K,1)
        Glcd_Write_Text("  ",120,k,1)
    Next K
End Sub

Sub Procedure Delay_3sec()
    Delay_ms(3000)
End Sub

Sub Function PullDownMenu() as Integer
Dim J,
    K,
    UDArrow,
    LRArrow,
    Selection as Byte
    
Result = 0
UDArrow = 0
LRArrow = 0
Selection = 0

Glcd_ClearScr()
CopyArrayStr(Text,@SomeTxtInit1,0,14,0)
Glcd_Write_Text(Text,10,2,1)
CopyArrayStr(Text,@someTxtInit2,0,11,0)
Glcd_Write_Text(Text,10,3,1)
Delay_3Sec
Glcd_ClearScr()
For J = 0 to (MnuTop - 1)
    CopyArrayStr(Text,@MainMnu,J,4,0)
    Glcd_Write_Text(Text,(j*_XOFFSET)+_XPOS,_YPos,1)
Next J
Glcd_Write_Text(MainMnu[0],_XPOS,_YPos,0)
Glcd_Line(0,(_YPOS*8)+8,127,(_YPOS*8)+ 8,1)

CopyArrayStr(Text,@SomeTxt1,0,14,0) 'put a Text "Do your choice"
Glcd_Write_Text(Text,20,((6-_YPOS) div 2+1),1) '..............

While TRUE
     If Selection = 0 then
         if Button(SWPORT,SwRT,1,0) then
            Inc(LRArrow)
            If LRArrow > (MnuTop - 1) then LRArrow = (MnuTop - 1) end if
            CopyArrayStr(Text,@MainMnu,LRArrow,4,0)
            Glcd_Write_Text(Text,(LRArrow*_XOFFSET)+_XPOS,_YPos,0)
            CopyArrayStr(Text,@MainMnu,LRArrow-1,4,0)
            Glcd_Write_Text(Text,((LRArrow-1)*_XOFFSET)+_XPOS,_YPos,1)
            While SWPORT.SwRT = 0 Wend
         End if
         If Button(SWPORT,SwLT,1,0) then
            dec(LRArrow)
            If LRArrow > (MnuTop - 1) then LRArrow = 0 end if
               CopyArrayStr(Text,@MainMnu,LRArrow,4,0)
               Glcd_Write_Text(Text,(LRArrow*_XOFFSET)+_XPOS,_YPos,0)
               CopyArrayStr(Text,@MainMnu,LRArrow+1,4,0)
               Glcd_Write_Text(Text,((LRArrow+1)*_XOFFSET)+_XPOS,_YPos,1)
            While SWPORT.SwLT = 0 Wend
         End if
     End if
     If Selection = 1 then
         If Button(SWPORT,SwDW,1,0) then
            inc(UDArrow)
            if UDArrow > (MnuItem - 1) then UDArrow = (MnuItem - 1) end if
            If LRArrow = 0 then
               CopyArrayStr(Text,@MnuFile,UDArrow,7,0)
               Glcd_Write_Text(Text,_XPOS,UDArrow + 1 + _YPos,0)
               CopyArrayStr(Text,@MnuFile,UDArrow-1,7,0)
               Glcd_Write_Text(Text,_XPOS,UDArrow + _YPos,1)
            End If
            If LRArrow = 1 then
               CopyArrayStr(Text,@MnuEdit,UDArrow,5,0)
               Glcd_Write_Text(Text,(_XOFFSET + _XPOS),UDArrow+1+_YPos,0)
               CopyArrayStr(Text,@MnuEdit,UDarrow-1,5,0)
               Glcd_Write_Text(Text,(_XOFFSET + _XPOS),UDArrow+_YPos,1)
            End if
            If LRArrow = 2 then
               CopyArrayStr(Text,@MnuProp,UDArrow,9,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,UDArrow+1+_YPos,0)
               CopyArrayStr(Text,@MnuProp,UDArrow-1,9,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,UDArrow+_YPos,1)
            End if
            While SWPORT.SwDW = 0 Wend
         End If
         If Button(SWPORT,SwUP,1,0) then
            dec(UDArrow)
            if UDArrow > MnuItem then
               UDArrow = 0
               For K = (MnuItem + _YPos) to (_YPos + 1) step - 1
                   GLCD_Write_Text("          ",(LRArrow * _XOFFSET)+_XPOS,k,1)
               Next k

               CopyArrayStr(Text,@someTxt1,0,14,0)  'put "Do your Choice"
               Glcd_Write_Text(Text,20,((6-_YPOS) div 2+1),1) '........
               
               Glcd_Line(0,(LineInit*8)+8,127,(LineInit*8)+ 8,1)
               CopyArrayStr(Text,@MainMnu,LRArrow,4,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,_YPos,0)
               Selection = 0
               UDArrow = 0
               goto EndPullDown
            end if
            If LRArrow = 0 then
               CopyArrayStr(Text,@MnuFile,UDArrow,7,0)
               Glcd_Write_Text(Text,_XPOS,UDArrow+1+_YPos,0)
               CopyArrayStr(Text,@MnuFile,UDArrow+1,7,0)
               Glcd_Write_Text(Text,_XPOS,UDArrow+2+_Ypos,1)
            End If
            If LRArrow = 1 then
               CopyArrayStr(Text,@MnuEdit,UDArrow,5,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,UDArrow+1+_YPos,0)
               CopyArrayStr(Text,@MnuEDit,UDArrow+1,5,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,UDArrow+2+_YPos,1)
            End if
            If LRArrow = 2 then
               CopyArrayStr(Text,@MnuProp,UDArrow,9,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,UDArrow+1+_YPos,0)
               CopyArrayStr(Text,@MnuProp,UDArrow+1,9,0)
               Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,UDArrow+2+_YPos,1)
            End if
            While SWPORT.SwUP = 0 Wend
         End if
     end if
     EndPullDown:
     If Button(SWPORT,SwEnter,1,0) then
    'Select Option
    '....
    inc(Selection)
    Select case Selection
           case 1
                CopyArrayStr(Text,@LineBlank,0,10,0) 'Clear message "Do Your choice"
                GLCD_Write_Text(Text,20,((6 -_YPOS) div 2+1),1) '.................

                CopyArrayStr(Text,@MainMnu,LRArrow,4,0)
                Glcd_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,_YPos,1)
                if LRArrow = 0 then
                   for K = 0 to (MnuItem - 1)
                       CopyArrayStr(Text,@MnuFile,k,7,0)
                       GLCD_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,K+1+_YPos,1)
                   Next k
                   Glcd_Write_Text(MnuFile[0],(LRArrow * _XOFFSET)+_XPOS,_YPos+1,0)
                end if
                If LRArrow = 1 then
                   for K = 0 to (MnuItem - 1)
                       CopyArrayStr(Text,@MnuEdit,k,5,0)
                       GLCD_Write_Text(Text,(LRArrow * _XOFFSET) + _XPOS,K+1+_YPos,1)
                   Next k
                   Glcd_Write_Text(MnuEdit[0],(LRArrow * _XOFFSET)+_XPOS,_YPos+1,0)
                End if
                If LRArrow = 2 then
                   For K = 0 to (MnuItem - 1)
                       CopyArrayStr(Text,@MnuProp,k,9,0)
                       GLCD_Write_Text(Text,(LRArrow * _XOFFSET)+_XPOS,K+1+_YPos,1)
                   Next k
                   Glcd_Write_Text(MnuProp[0],(LRArrow * _XOFFSET)+_XPOS,_YPos+1,0)
                End if
           case 2
           'make definitive selection /
           'The Ho(Byte) have the Menu Top number
           'The Li(Byte) have the Menu item associated
           Selection = 0
           Result = LRArrow + 1
           Result = (Result << 8) or (UDArrow + 1)
           Break
    End Select
    While SWPORT.SwEnter = 0 Wend
 End if
Wend
End Sub

main:
ADCON1 = 7
ADCON0 = 0
TRISB  = 0xFF
PORTB = 0

Glcd_Init(PORTC, 0, 1, 2, 3, 4, 5, PORTD)
Glcd_Set_Font(@Font5x7,5,7,32)

J = PullDownMenu()

'Show choice of menu.
Digit[0] = Hi(j) + 48
CopyArrayStr(Text,@ChceMnu,0,5,0)
strcat(Text,digit)
CopyArrayStr(Text,@ChceItem,0,6,6)
Digit[0] = Lo(j) + 48
strcat(Text,Digit)
GLCD_Write_Text(Text,14,7,1)
End.
Note: for to use this example is necessary to get the Lib_MDStrArray.mcl at ftp://www.ttelecom.com.br/ unzip and copy this mcl into \uses\p16.

Enjoy.
Working with you, for you!
MAN

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#2 Post by Kalain » 01 Sep 2007 21:56

Hi,

Code: Select all

Const MainMnu as String[MnuTop][4]  = ("File","Edit","View") 
I'm learning about your code but couldn't find anywhere in MB Help some explanations about code above, specialy about [MnuTop][4].

Thanks for any comment about this Const declaration.
Alain

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#3 Post by Kalain » 01 Sep 2007 22:16

Hi,

Ok, Now I understand more the program. I should wear some glasses. :(

Code: Select all

Symbol MnuTop       = 3  'Number of Top Menu
Symbol MnuItem      = 5  'Number of item Menu 

....

Const MainMnu as String[MnuTop][4]  = ("File","Edit","View") 

This is a multi dimensionnal string array. :roll:
Alain

MAN
Posts: 437
Joined: 11 Jan 2006 18:32
Location: Brasil

#4 Post by MAN » 02 Sep 2007 01:48

Hi;
Ok, Now I understand more the program. I should wear some glasses.
You don't need to use glasses not, I already took more time for much smaller things than this.

Very well; I see the time that you take to understanding the code. 20 minutes. :)
Is this same kalain, const multi dimensional array and if I had not used this technique certainly this program it would not run in the P16.
But, did you get to run the program.
Working with you, for you!
MAN

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#5 Post by Kalain » 02 Sep 2007 08:38

MAN wrote:Hi;
Ok, Now I understand more the program. I should wear some glasses.
You don't need to use glasses not, I already took more time for much smaller things than this.

Very well; I see the time that you take to understanding the code. 20 minutes. :)
This is more that the line was a bit hidden by comments.
But, did you get to run the program.
I add library as you suggested in P16 directory, modified some lines in order to run it in EasyPic3 board. (16F877A) but didn't (yet) succeded to run correctly the program.
Menu are displayed correctly but they doesn't not respond correctly when press buttons.(Port C)
I guess that program need a bit more some code modifications.
Independently, program is still a good way to learn how to do such things.

Code: Select all

...
'Symbol SWPORT       = PORTB 'Port for Swtich control
Symbol SWPORT       = PORTC 'Port for Swtich control
....
'TRISB  = 0xFF
'PORTB = 0
TRISC  = 0xFF
PORTC = 0
....
Glcd_Init(PORTB, 2, 3, 4, 5, 7, 6, PORTD)
'Glcd_Init(PORTC, 0, 1, 2, 3, 4, 5, PORTD)
....
Alain

MAN
Posts: 437
Joined: 11 Jan 2006 18:32
Location: Brasil

#6 Post by MAN » 02 Sep 2007 14:28

Hi;
Menu are displayed correctly but they doesn't not respond correctly when press buttons.(Port C)
Simple configure your board with buttons to GND
and make new test.
Working with you, for you!
MAN

Kalain
Posts: 1093
Joined: 11 Mar 2005 18:26
Location: Aubenas, France

#7 Post by Kalain » 04 Sep 2007 18:41

MAN wrote:Hi;
Menu are displayed correctly but they doesn't not respond correctly when press buttons.(Port C)
Simple configure your board with buttons to GND
and make new test.
I tried with button to GND, and pull up res for PortC. but didn't yet succed to works correctly. (Do not mind about that.... I'm just trying to use similar code for hobby.)

A comment about your code, you could also use MB built in Clear GLCd instruction :

Code: Select all

Glcd_Fill(0)  ' Clear screen
rather than yours

Code: Select all

Sub Procedure Glcd_ClearScr()
This save 245 Rom. :wink:
Alain

MAN
Posts: 437
Joined: 11 Jan 2006 18:32
Location: Brasil

#8 Post by MAN » 05 Sep 2007 14:32

Hi kalain;
I tried with button to GND, and pull up res for PortC. but didn't yet succed to works correctly.
Well, maybe you need review your configuration.
(Do not mind about that.... I'm just trying to use similar code for hobby.)
I've started to hobby too but now I control thirteen different subprograms and I use RB0/INT to return from sub program to menu. Work fine....
A comment about your code, you could also use MB built in Clear GLCd instruction :
Code:
Glcd_Fill(0) ' Clear screen

rather than yours
Code:

Sub Procedure Glcd_ClearScr()
I know, this is because my GLCD not respond correctly to this function. Just remove it.
I made same modification to the PORTC on my GLCD and the response was OK.

below this some photos (bad but you can have a idea).
Image Image Image Image
Image
Regards;
Working with you, for you!
MAN

hansolo
Posts: 344
Joined: 28 Oct 2005 07:28

#9 Post by hansolo » 19 Sep 2007 23:31

The Lib_MDStrArray.mcl library is for PIC16.

If I want to utilise your program for PIC18 chips, how can we do it?

Your Lib_MDStrArray.mcl library is not open source.

Hansolo
PIC Rules!!

MAN
Posts: 437
Joined: 11 Jan 2006 18:32
Location: Brasil

#10 Post by MAN » 20 Sep 2007 15:56

Hi hansolo;
The Lib_MDStrArray.mcl library is for PIC16.
Yes the lib_mdstrArray is only for P16 and let's you to work with multi dimensional const string on P16 series.
If I want to utilise your program for PIC18 chips, how can we do it?
For to use this program with P18 I advise you to use RAM array. The P18 series have enough RAM to do this, how you see, this program never will work in a P16 if we want to use RAM.
Ho, of course the program will need some modifications for P18, but certainly will work.
Your Lib_MDStrArray.mcl library is not open source.
It is not this, I just keep certain routines that I create for applications that I do in format mcl to facilitate in the hour of reusing and, some times I delete the source because for more that we modify these routines not will change much. Like you can see, this lib they count a procedure that just copy the data position from ROM into a RAM area. The name of procedure say this "CopyArrayStr"

Regards;
Working with you, for you!
MAN

octal
Posts: 534
Joined: 09 Feb 2005 14:15
Location: France
Contact:

#11 Post by octal » 20 Sep 2007 16:50

MAN wrote:
Your Lib_MDStrArray.mcl library is not open source.
It is not this, I just keep certain routines that I create for applications that I do in format mcl to facilitate in the hour of reusing and, some times I delete the source because for more that we modify these routines not will change much.

Regards;
Very nice this one ... :D :D :D :D
http://www.pocketmt.com

Bobo!
Posts: 7
Joined: 16 Nov 2007 13:23
Location: Naples,Italy!!!

#12 Post by Bobo! » 07 Dec 2008 08:13

Hi,
could I have Lib_MDStrArray.mcl,please?
ftp://www.ttelecom.com.br/ doesn't work!

Thank you very much...

zan
Posts: 434
Joined: 15 May 2007 19:35

#13 Post by zan » 07 Dec 2008 11:05

Just copy the link and paste it to windows explorer, not internet explorer.

Bobo!
Posts: 7
Joined: 16 Nov 2007 13:23
Location: Naples,Italy!!!

#14 Post by Bobo! » 08 Dec 2008 13:43

zan wrote:Just copy the link and paste it to windows explorer, not internet explorer.
Thank you very much...

I have a project and I was using the PIC16F877.

When I recompiled the code, I've got the following error:

0:0 E-0 Linker error: Routine ____DoICP not defined


Can I help me,please?
Best regards,Ciao
Roberto

zan
Posts: 434
Joined: 15 May 2007 19:35

#15 Post by zan » 08 Dec 2008 18:33

I guess there is something in the library that is not working in mikroBasic v7.

Post Reply

Return to “mikroBasic General”