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.
Enjoy.