PDA

View Full Version : looking for pointers, tips, etc...



bill12780
- 30th July 2007, 03:17
Hello all,

What an adventure this has been! I have completely fallen for this enbedded coding stuff and in paticular PBP. Best money I have spent in a long time. More fun then I have had in a long time...I actully like my job again!

Here is my little project so far...Just the keyboard scan part. Now I know of Mr._E's Keypad.bas include. But I wanted to write my own for educational purposes...

Here is my code...I should mention that the the 3x4 keypad is NOT a standard keypad its custom made for my company. So ignore that there is not "#" or "*"

Also, it is a neccessary for me to get the keypressed one at a time. This is so we can compare the entered code number by number and compare it to the stored number. Rather then using an array or for/next to gather a group of keypresses.



@ DEVICE HS_OSC
'SET OSC BIT TO HS


'Defines...

'*lcd defines*
DEFINE LCD_DREG PORTD 'Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 4 'Define first pin of portd connected to LCD DB4
DEFINE LCD_RSREG PORTD 'Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 2 'Define Portd pin used for RS connection
DEFINE LCD_EREG PORTD 'Define PIC port used for E line of LCD
DEFINE LCD_EBIT 3 'Define Portd pin used for E connection
DEFINE LCD_BITS 4 'Define the 4 bit communication mode to LCD
DEFINE LCD_LINES 2 'Define using a 2 line LCD
DEFINE LCD_COMMANDUS 2000 'Define delay time between sending LCD commands
DEFINE LCD_DATAUS 50 'Define delay time between data sent.

'*other defines*
DEFINE OSC 8 'DEFINE EXTERNAL OCS TO 8MHZ

'setup ports...
INIT:
'TRISC = %00000000 'SETS PORTC TO ALL OUTPUTS (future use)
'PORTC = %00000000 'TURNS ALL OF PORT C OFF (future use)
'OPTION_REG.7=0 'Enable the internal PORTB pullups (this does nt wk)
TRISB = %00001111 'Set Keypad I/O 1=input 0=output
PORTB = 0 'Set columns LOW


'setup variables...

key var byte

lcdout $fe, 1 ' Clear LCD

main: 'SCAN keypad for key press

colum0:
portb = %00010000 'turn on colum0 for row scan
if portb.1 = 1 then key = 9: goto display
if portb.2 = 1 then key = 6: goto display
if portb.3 = 1 then key = 3: goto display



colum1:
portb = %00100000 'turn on colum1 for row scan
if portb.0 = 1 then key = 0: goto display
if portb.1 = 1 then key = 8: goto display
if portb.2 = 1 then key = 5: goto display
if portb.3 = 1 then key = 2: goto display



colum2:
portb = %01000000 'turn on colum2 for row scan
if portb.1 = 1 then key = 7: goto display
if portb.2 = 1 then key = 4: goto display
if portb.3 = 1 then key = 1: goto display

goto main


DISPLAY:
' LCD Command codes
'------------------
' $01 Clear Display
' $02 home
' $0C Cursor off
' $0E Underline cursor on
' $0F Blinking cursor on
' $10 Move cursor left one character
' $14 Move cursor right one character
' $C0 Move cursor to begining of next line
' all commands must use the format LCDOUT $fe, XX

'Setup LCD...

lcdout $fe, 1 ' Clear LCD
lcdout $fe, 2 ' Position cursor at home
lcdout "you pressed..", dec key

goto main



Everything works just fine. I am now trying to figure out a good way to debounce. But for now I would love to hear from some of you more advance coders as to how I could make this...Well....more eligante. I looked at the "lookup" command and many examples. I just cant seem to get my head wrapped around how I would impliment it. So if your idea has anything to do with that please include a small tutorial on lookup tables.

I should also mention that this is just test code. I have broken down my project into several steps. I am making one step work then moving on to the next. Eventually, I will have the knowledge to pull them all together. For example, I figured out the LCD stuff....Moved on to the Scanning of the keypad....Next is reading and writing to the internal EEPROM...etc...


Oh...I am using the following apps and chips just as a reference....

PIC16f877a
EasyPIC4 Dev. board (mikroelektronika)
PBP 4.27
PICFlash 4.07 (mikro again)
pm.exe compiler

Your thoughts please ladies and gentlemen...

Best regards,
Bill12780

Darrel Taylor
- 30th July 2007, 09:45
This depends alot on how your keypad is wired, and how many resistors there are in the circuit, but there's a possibility you have a short circuit waiting to happen.

By setting PORTB to either High or LOW states, if you accidently press 2 buttons (on different colums), it put's a dead short across the outputs of the PIC.

Typically, it causes the PIC to reset before it can do any real damage. But over time it will take it's toll on the equipment. Especially the keypad.

It's better to control the TRIS register, so that only 1 column has an active output at any given time.

I know you're trying to do it yourself, but you might want to take a look at ...

keyx.bas - PICBASIC PRO program to display key number on LCD http://www.melabs.com/resources/samples/x1/pbp/keyx.bas

HTH,

bill12780
- 30th July 2007, 20:59
Hello Darrel,

Thank you for the tip. I did realize the short circuit situation and added series resistors in line with the row bits.

However, your tip is better because I can eliminate those resistors and gain some space on the board. controling the Tris register rather then setting bit high or low.

I looked over the code example...I will need to spend sometime so I understand what they are doing. But that does seem more efficent then my multiple "if" statements.

Thanks for your input. I appreciate it.

Best Regards,
Bill12780