Using a keypad on the Dragon 12

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Using a keypad on the Dragon 12

5,569件の閲覧回数
gitmomike
Contributor I
Hi all, I hope someone can help me! I'm using the Dragon 12 project board which has the HCS12 on it.  A program that I'm writing has a menu structure.  Press the 'A' key to go to this menu, press 'B' to go to this menu and press 'F' to exit out of the menu etc...  The keypad I'm using comes with the kit, it is a 16 character keypad (0-9, A-F) that connects to the board.  I can get the program to go to the first menu, and enter data, but the problem is you can only go to the 'A' menu and it goes to that menu when you press any key!  So, my question is, does anyone know how to make the program go to the selected menu, when the corresponding key is pressed.  I'm writing in C, i think it is simple, I'm just over looking the obvious.  I've tried case structures, if statements.  My understanding of how the keypad works may be my downfall here.  Any help will be appreciated!
ラベル(1)
0 件の賞賛
返信
5 返答(返信)

1,462件の閲覧回数
JimDon
Senior Contributor III
Lets see your code.
0 件の賞賛
返信

1,462件の閲覧回数
gitmomike
Contributor I
Here is the code:
0 件の賞賛
返信

1,462件の閲覧回数
JimDon
Senior Contributor III

First, I would like to know why you have this in the code:   

asm("swi");

Next, I would recommend you remove the scan keypad code from main, and use the ScanKeyPad() function from the original sample code, and in the section marked "Application specific code " you call a function that you write take the key code as an argument to execute your menu. In your main function, in your for(;:smileywink: loop, you would call ScanKeyPad() and it would take of the rest.

As I said you will call a function of your own design from ScanKeyPad() every time a new key is detected. In this function you will have a variable that keeps track of the current state - it would start off as say state 0, which means "top" menu. When you are in this state 0, you will decode the key to mean a menu function is being selected - if it is an "A" you will move the state to 1, which means I am in menu state "A". Now when a key is pressed, you will do what menu function "A" would do with a key press. In state 0 you might simple ignore keys that are not valid menu items.

 Then there would be some way that the use ends menu state "A". For example pressing "#" ends menu function "A" and does something with the entered data and returns to the orginal state,  "top" menu state 0 as I mentioned.

 

When in state 0, pressing a 'B' would go to state 2 and so on.

 

So this function that is called from ScanKeyPad will first examine the current state then decide what to do with the key. It would not be a bad idea to have a separate function for each menu function. For example you might have a function called "byte MenuA( byte keycode)" that is called when in state 1. You would take the return value to determine the next state, so that when menu function A determines that it is done, it signals that it is time to return to state 0.


Consider what I have said. If it is not clear please ask further.

0 件の賞賛
返信

1,462件の閲覧回数
gitmomike
Contributor I
Thanks for the input Jim, I will try it when I get home. I also gave you the original code we used to see how the keypad works with the LCD.  The code I wanted to send you is slighty different in that it shows what I was describing in the original post.  I can upload that later.  As for the asm ("swi"), that is a way to use assembly code in C language, it stands for software interrupt.  My partner put that in, and I not sure why its there.  In the code I ment to send I don't think its there.   
0 件の賞賛
返信

1,462件の閲覧回数
JimDon
Senior Contributor III
Well, yes I know what SWI is - it will cause an interrupt to a vector that if you do not have set in the vector table will crash the code.

What I have given you is some high level design advise, that you need to think thru. You will have better success if you sit down and plan this out as far as how the user interface will work. As I mentioned, you need some sort of state machine to control it. Once you plan this out, what I have said will make sense, and it will be apearent what needs to be done.

I also noticed some code there to write to the seven segment leds. Take a look as the mux code in the example. Again, you really do not want to do the muxing on the forground loop (the for loop in main) or you will have problems. The mux code in the sample runs off a timer, and this is really what you need to do. You want this sort of think to be off some where else and not complicating the logic of you appilcation. You just want to call a function to set the display.

Again, feel free to ask more questions.



0 件の賞賛
返信