Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

reading a 4x4 switch matrix

Status
Not open for further replies.
Maybe just 0 to 8 and delete, enter, new game? Blank would be 00 etc. You need enter so ....... never mind.

Mike.
 
My plan is 1 to 15 (only have a set of double 12 and a set of double 15 but common to play with double 12)
Then have the extra key for the double blank which is 50 points so only 16 keys are needed.
Thanks. Have gone back to the T28 for use of the usart app. Going to locate the switches I have (3p3t) to swap the ICSP to RX/TX without removing any cables etc. Maybe a simple development board using a 40 pin dip ?
 
Going to locate the switches I have (3p3t) to swap the ICSP to RX/TX without removing any cables etc. Maybe a simple development board using a 40 pin dip ?

Seems like a lot of trouble to go to for something only useful during development, and only then if the upper 2 bits of port B are in use.

In most cases, I use a software UART to output serial data on the appropriate pin of the ICSP connector. The procedure is described here: Not Quite Trivial - A Tip For Using The Software UART with the PICkit 2. It's very slick - shoot in the code, then switch to the UART tool to monitor your program. No need to move a cable at all.

But on my latest version of the TAP-28 board, there's an even easier method. I've added a CH340 UART - USB chip connected to the serial port. I just connect a USB cable to the board and use a terminal emulator on the pc or my phone.
 
I stand corrected. MrDeb appears to have been planning for double 15 dominos all along. I don't know how I lost track of that detail.
 
easy to lose track.
talk about losing track, I am hacking away at trying to add up keypresses. Should be easy but have yet to get the order correct to add up the key press and add it to the total of key press inputs without using an extra key for the +
I am really feeling stupid as this should be 1st grade math!!
On the uart, I completely forgot the software uart Will have a look
thanks
 
Look at how I only print out one value for KeyPressed in my 2nd listing.

KeyPressed = 0 means the key has been released. Add the value of KeyPressed to total, then Wait until KeypPressed = 0. Add KeyPressed to total the next time it doesn't equal zero and repeat.
 
which listing are you referring to?
started with a FUNCTION but don't think it is the right road.
Trying to use keypressed then add the two together to equal a total but it doesn't seem to work right
 
I provided a listing that output one value each time a key was pressed and not again until the key was released. Hard hard can it possibly be to add

Total = total + keypressed

Print Total

to that code???
 
I tried that solution yesterday and yes it adds the keypress to the total but fails to add the next keypress. Will re-examine my code.
I realize this can't be that hard.
Here is what I have tried. Will post result but it seems to be doubling the keypress value and not adding to the next keypress.
USART.Write ("Key pressed = ", decToStr(Keypressed), 13, 10)
keypress1 = Keypressed //sum_total is previous total

DELAYMS(2000)
USART.Write ("sum = ", decToStr(keypress1), 13, 10)
Total = keypress1
Keypressed = 0 // zero out the keypressed
keypress1 = 0
Total = Total +Total
'WriteAt(2,1,"sum Total= ",DecToStr(sum_total,2))//, " Cnt: "))//,DecToStr(Counter,2))
USART.Write ("total = ", decToStr(Total), 13, 10)
DELAYMS(2000)
'total1 = total + keypress1
'USART.Write ("total1 = ", decToStr(total1), 13, 10)
Keypressed = 0
'keypress1=0
'total2= total1 + total
'USART.Write ("total2 = ", decToStr(total2), 13, 10)
END IF
 
This took half an hour to modify my 2nd program. I know it was half an hour because I was watching a TV program while I was working on it.

There are a number of ways to handle the double zero key. I made it equal the 16 key and the if/then/else statement results in the right value being printed and the correct value added to the total.

Literally, this is a 25 line program (not counting the setup info) and an extremely small step added to what I already posted. I did not add a switch press to zero the counter, as my switches on the TAP-28 are both connected to port B. This is literally as simple as : If switch = pressed, total = 0.

Code:
Device = 18f25k22
Clock = 20
       
Config   'for K-series device
    FOSC = HSHP ,'HS oscillator (high power > 16 MHz)
    PLLCFG = Off ,'Oscillator used directly
    PRICLKEN = Off ,'Primary clock can be disabled by software
    FCMEN = Off ,'Fail-Safe Clock Monitor disabled
    IESO = Off ,'Oscillator Switchover mode disabled
    'PWRTEN = Off ,'Power up timer disabled
    PWRTEN = on ,'Power up timer enabled
    BOREN = Off ,'Brown-out Reset disabled in hardware and software
    'BOREN = on ,'Brown-out Reset enabled 
    BORV = 285 ,'VBOR set to 2.85 V nominal
    WDTEN = Off ,'Watch dog timer is always disabled. SWDTEN has no effect.
    WDTPS = 256 ,'1:256
    PBADEN = Off ,'PORTB<5:0> pins are configured as digital I/O on Reset
    HFOFST = Off ,'HFINTOSC output and ready status are delayed by the oscillator stable status
    MCLRE = EXTMCLR ,'MCLR pin enabled, RE3 input pin disabled
    STVREN = On ,'Stack full/underflow will cause Reset
    'LVP = On ,'Single-Supply ICSP enabled if MCLRE is also 1
    LVP = Off ,'Single-Supply ICSP disabled
    XINST = Off ,'Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    Debug = Off'Disabled
Include "keypad16pullup.bas"
Include "usart.bas"
Include "convert.bas"
Dim Keypressed As Byte
Dim Keyvalue As Byte
dim Total as word
SetBaudrate(br9600)
DelayMS(5000)
USART.Write ("Test program",13, 10, 10)
Keypressed = 0
total = 0
While 1 = 1
    While Keypressed = 0            'print key and exit when key pressed
        Keypressed = Keypad16.Value
        If Keypressed <> 0 Then
            if keypressed = 16 then   'Keypressed = double zero
                    total = total + 50
                    USART.Write ("Key pressed = 00","   Total = ", dectostr(total), 13, 10)
                else 
                    total = total + keypressed
                    USART.Write ("Key pressed = ", decToStr(Keypressed), "   Total = ", dectostr(total), 13, 10)
            end if
        End If
    Wend
    delayms(100)
    While Keypressed <> 0                'loop until key not pressed
        Keypressed = Keypad16.Value
    Wend   
Wend


The output:

Code:
Test program
Key pressed = 1   Total = 1
Key pressed = 2   Total = 3
Key pressed = 3   Total = 6
Key pressed = 4   Total = 10
Key pressed = 5   Total = 15
Key pressed = 6   Total = 21
Key pressed = 7   Total = 28
Key pressed = 8   Total = 36
Key pressed = 9   Total = 45
Key pressed = 10   Total = 55
Key pressed = 11   Total = 66
Key pressed = 12   Total = 78
Key pressed = 13   Total = 91
Key pressed = 14   Total = 105
Key pressed = 15   Total = 120
Key pressed = 00   Total = 170
 
Geez MrDEB,

You aren't even reading the key matrix in that random bunch of words you've posted. Apparently you have more random words in your program than what you've posted here? I wonder if they make any more sense????
 
After my last post I made a few slight changes to my code and it seems to work now but needs more tests.
as for the double blank = 50 I plan to just change the values i the keypad module as suggested and hopefully it will work.
Will run your code to see what happens.
here is my revised code
WHILE True
Keypressed = Keypad16.Value
IF Keypressed<>0 THEN
DELAYMS(50)
USART.Write ("Key pressed = ", decToStr(Keypressed), 13, 10)
delayms(1000)
END IF
total = total + keypressed
delayms(1000)
USART.Write ("total = ", decToStr(Total), 13, 10)
delayms(500)
WEND
 
No one suggested you change the keypad module. You should never change an include module except as necessary to accomodate new chips. It's not necessary and in fact is stupid.

The keypad module is intended to read keys from a keypad. Period. Adapting those keys for a specific program should be done in the program itself.
 
I re-thought about the change to the keypad module ( changing the output but that won't work but when using the perfboard with the 40 pin dip and the LCD is connected to portC I changed the port allocation in the module but to avoid loading the wrong module I tucked the modified module in a different folder along with the code for same.
I tinkered with your suggestion last night and it seems to work but need to make some changes to adapt to the LCD. That's my chore this morning.
Been thinking about the CH340 and need to research.
 
Kept getting weird output display and it was due to keypad bounce.
Using some suggestions I finally got correct output but needs some additional housekeeping. This appears to work. Need to rid display of unwanted "0"'s
WHILE True
keypressed = Keypad16.Value
IF keypressed<>0 THEN
DELAYMS(1000) // kept getting key bounce
'USART.Write ("Key pressed = ", decToStr(Keypressed), 13, 10)
WriteAt(1,1,"key = ",DecToStr(keypressed,2))
DELAYMS(10)
END IF
if keypressed = 16
then total = total + 50 - keypressed
end if
Total = keypressed + Total
DELAYMS(10)
'USART.Write ("total = ", decToStr(Total), 13, 10)
WriteAt(2,1,"Total= ",DecToStr(Total,2))
DELAYMS(10)
WEND
 
25 lines. The total program is 25 lines. 15 pages of forum posts to make a 25 line program work.

I gave you the code that properly indicated double zero pressed. it's all been done for you. All you have to do is change the print statements in two places to have the proper logic to display the correct result.

But instead of taking the working code that displays the correct results, you do something completely different. Something completely different because you apparently either don't understand or can't be bothered to use an if/then/else statement. Geez oh grief.

Get real. A 25 line program done for you with 15 pages of posts and you still don't get it? I did your program for you in less than an hour and you still can't get it to work??? Maybe you should stick to Mexican Train and put away the soldering iron.
 
I tried your suggestion and it didn't appear to output the correct info BUT that was before I discovered the key bounce issue.
When I get back at it tonight I will re try it .
here is a newer revision that takes care of the double blank
WHILE True
keypressed = Keypad16.Value
IF keypressed<>0
THEN
DELAYMS(1000) // kept getting key bounce
WriteAt(1,1,"key = ",DecToStr(keypressed,2))
DELAYMS(10)
END IF
if keypressed = 16
then total = total + 50 - keypressed
WriteAt(1,1,"double blank=",DecToStr(double0,2))// double0 = 50
end if
Total = keypressed + Total
DELAYMS(10)
WriteAt(2,1,"Total= ",DecToStr(Total,4))
DELAYMS(10)
WEND
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top