Sign in

username:

password:



Not a member?

Search avrclub



Search tips

Subscribe to avrclub



avrclub by Keywords

AT90S2313 | AT90S8515 | ATMega | ATmega128 | ECL | FETS | IAR | Keyboard | LCD | STK50 | TMOS | UART

Ads

Discussion Groups

Discussion Groups | AVRclub | [AVR club] Help on Keypad decoder.

Atmel AVR Microcontroller discussion group.

[AVR club] Re: RS232 Baud Rate questions - David Jones - Aug 19 19:26:00 2004

>>> YahooGroups@Yaho... 20/08/2004 2:26:21 am >>>
>* Go ahead and run from the internal RC oscillator.
>* Connect a 32.768 kHz watch crystal to TOSC1 and TOSC2.
>* Set the U2X bit, so you need a UART clock of 19.2 * 8 = 153.6 kHz.
>* Using a timer running from the timer oscillator and another timer
>running from the main clock, calibrate the main clock. You can aim
>for either 921.6 kHz or 1075.2 kHz, whichever is easier to get to.
>* Set the UART bit rate divider to 5 or 6, depending on your choice
>above.
>* Recalibrate as necessary to cope with temperature-induced drift.
>* See the Butterfly for details on how this can be done.

>Can anyone see a problem with this?

Yep - the words "calibrate" and "temperature drift" when you are using
an RS232 interface.
You are just asking for trouble when using anything but a direct
crystal oscillator for an RS232 Interface.
I have not checked out the details, but I presume that the crystal is
used as the reference for the "calibration" of the higher frequency RC
oscillator (1MHz, 4MHz?) and then the UART register value is
continuously varied to compensate for temperature?
Sounds like a neat trick, but a bit convoluted and fraught with
danger.
The internal RC oscillators are fast and a chew fair bit of power, so
you might not be gaining any power benefit. What happens with the power
down modes?, does the software have to wake up and then spend time
compensating for the temperature?
The OP said that the device only has to operate periodically once every
15minutes or so. If the application needs to be woken up by the UART,
then will this technique still work? Any other software/hardware traps?
32.768KHz crystals are not really any smaller than a standard 1MHz
surface mount crystal. Wouldn't it be simpler and easier to just use a
1MHz crystal and put the part into sleep mode?

Dave :)





(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )


[AVR club] Re: RS232 Baud Rate questions - Graham Davies - Aug 20 9:41:00 2004

--- In avrclub@avrc..., "David Jones" <david.jones@s...>
wrote:

> I have not checked out the details,
> but I presume that the crystal is
> used as the reference for the
> "calibration" of the higher
> frequency RC oscillator (1MHz,
> 4MHz?) and then the UART register
> value is continuously varied to
> compensate for temperature?

No. The internal RC oscillator is adjusted to a frequency that you
choose to be an even multiple of the bit rate clock. Read what I said
even if you don't want to check out the details.

You are also wrong about UARTs needing the accuracy of a crystal
oscillator. 2 percent is fine.

When you say "... internal RC oscillators ... chew fair bit of
power ..." we should note that they take less power than any other
type of oscillator for a given frequency and less than an external
oscillator of lower frequency.

You say "32.768KHz crystals are not really any smaller than a
standard 1MHz surface mount crystal." but this is also not true. They
are designed to go into watches, after all. Also, using a 1MHz
crystal would just plain not work, go back and see what we're trying
to do here.

For the rest, sleep modes, waking up and performing calibration, etc,
we would need the original poster to stay in the thread. Since he
appears to have given up on getting a coherent answer from us, we
might as well put the thread to bed. You might want to have a last
word, but I will not post again on this subject.

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Re: RS232 Baud Rate questions - Andrew Lim - Aug 22 9:24:00 2004

Hi Graham,

Sorry for replying now cos I just ran thru my mail.
I still have on idea how to use that watch crystal to calibrate my RS232.
What if I add a 1MHz crystal for my uC ? How do I activate that?
Thanks.

Regards,
N2

Graham Davies <YahooGroups@Yaho...> wrote:
--- In avrclub@avrc..., "David Jones" <david.jones@s...>
wrote:

> I have not checked out the details,
> but I presume that the crystal is
> used as the reference for the
> "calibration" of the higher
> frequency RC oscillator (1MHz,
> 4MHz?) and then the UART register
> value is continuously varied to
> compensate for temperature?

No. The internal RC oscillator is adjusted to a frequency that you
choose to be an even multiple of the bit rate clock. Read what I said
even if you don't want to check out the details.

You are also wrong about UARTs needing the accuracy of a crystal
oscillator. 2 percent is fine.

When you say "... internal RC oscillators ... chew fair bit of
power ..." we should note that they take less power than any other
type of oscillator for a given frequency and less than an external
oscillator of lower frequency.

You say "32.768KHz crystals are not really any smaller than a
standard 1MHz surface mount crystal." but this is also not true. They
are designed to go into watches, after all. Also, using a 1MHz
crystal would just plain not work, go back and see what we're trying
to do here.

For the rest, sleep modes, waking up and performing calibration, etc,
we would need the original poster to stay in the thread. Since he
appears to have given up on getting a coherent answer from us, we
might as well put the thread to bed. You might want to have a last
word, but I will not post again on this subject.

Graham.
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Re: RS232 Baud Rate questions - Graham Davies - Aug 23 10:11:00 2004

--- In avrclub@avrc..., Andrew Lim <normaxcite@y...> wrote:

> I still have on idea how to use
> that watch crystal to calibrate
> my RS232.
> What if I add a 1MHz crystal for
> my uC ? How do I activate that?

OK, if you have "no idea" how to calibrate the the internal RC
oscillator against a watch crystal reference using two timers, this
may not be a viable solution for you. As you seemed quite clear about
the low-power and small-size objectives, I assumed you were up for a
non-trivial method of achieving them.

So, let's look at David's solution, which is to *not* use the
internal RC oscillator, but to use an external crystal instead.

Are you able to relax your power and size objectives enough to allow
this? At first, you said this was not possible.

Let's first establish that you cannot use a 1 MHz crystal. I don't
know why David keeps suggesting this. Perhaps he means a crystal of
some frequency around 1 MHz. Is it clear to you that you need to pick
a crytal frequency that is within 2% of the bit rate clock you need
for the UART? If you set the U2X bit, and you need the UART to run at
19.2 kbit/s, then you need a UART clock of 19.2 * 8 = 153.6 kHz.

Before you go further, you need to find such a crystal. It will be
hard to get one as low as 1 MHz and it may be expensive. This is a
very low frequency for an AT-cut crystal. I've looking in the Mouser
catalog and they have crystals at 1.8432 MHz costing $1.20. This
would work - you'd just set the bit rate divider to 11 so it would
divide by 12. But, this is almost twice the frequency you said you
wanted to clock at and the external crystal oscillator will take much
more power than the internal RC oscillator.

If this is acceptable, how to connect up the crystal is described in
the Atmel data sheets. You need to program fuses to select this clock
source, but this is also described in the data sheets. Let us know
how you want to proceed from here.

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Re: RS232 Baud Rate questions - Andrew Lim - Aug 24 1:38:00 2004

Hi Graham,

Since I'm still a newbie here, I will use the 1.8432MHz external crystal to solve my RS232 problem for now. However, I am really keen to learn up how to synchronize the 32.768kHz watch crystal for my RS232.

Thanks a bunch dude !! U rock ! Regards,
Andrew

Graham Davies <YahooGroups@Yaho...> wrote:
--- In avrclub@avrc..., Andrew Lim <normaxcite@y...> wrote:

> I still have on idea how to use
> that watch crystal to calibrate
> my RS232.
> What if I add a 1MHz crystal for
> my uC ? How do I activate that?

OK, if you have "no idea" how to calibrate the the internal RC
oscillator against a watch crystal reference using two timers, this
may not be a viable solution for you. As you seemed quite clear about
the low-power and small-size objectives, I assumed you were up for a
non-trivial method of achieving them.

So, let's look at David's solution, which is to *not* use the
internal RC oscillator, but to use an external crystal instead.

Are you able to relax your power and size objectives enough to allow
this? At first, you said this was not possible.

Let's first establish that you cannot use a 1 MHz crystal. I don't
know why David keeps suggesting this. Perhaps he means a crystal of
some frequency around 1 MHz. Is it clear to you that you need to pick
a crytal frequency that is within 2% of the bit rate clock you need
for the UART? If you set the U2X bit, and you need the UART to run at
19.2 kbit/s, then you need a UART clock of 19.2 * 8 = 153.6 kHz.

Before you go further, you need to find such a crystal. It will be
hard to get one as low as 1 MHz and it may be expensive. This is a
very low frequency for an AT-cut crystal. I've looking in the Mouser
catalog and they have crystals at 1.8432 MHz costing $1.20. This
would work - you'd just set the bit rate divider to 11 so it would
divide by 12. But, this is almost twice the frequency you said you
wanted to clock at and the external crystal oscillator will take much
more power than the internal RC oscillator.

If this is acceptable, how to connect up the crystal is described in
the Atmel data sheets. You need to program fuses to select this clock
source, but this is also described in the data sheets. Let us know
how you want to proceed from here.

Graham.
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Re: RS232 Baud Rate questions - Graham Davies - Aug 24 8:54:00 2004

--- In avrclub@avrc..., Andrew Lim <normaxcite@y...> wrote:

> Since I'm still a newbie here, I
> will use the 1.8432MHz external
> crystal to solve my RS232 problem
> for now. However, I am really keen
> to learn up how to synchronize the
> 32.768kHz watch crystal for my RS232.
>
> Thanks a bunch dude !! U rock !

Thanks. Since I spent a while with my Mouser catalog last night, let
me rock a little more.

I was right that a few megahertz is low for an AT-cut crystal. There
is one available priced at 1.000 MHz, but it costs about $12.00. The
1.8432MHz is still several times the price of, say, 3.6864 MHz.
Plus, something I didn't realize when searching on-line, it is only
available in the big ole HC49/U case, not the smaller HC49/U-S. So,
even to use 1.8432MHz you're going to really have to relax your size
constraints.

I also looked at tuning fork crystals. I was right that they are
quite a bit smaller than the smallest AT-cut. Almost all are 32.768
kHz, but there are a few other frequencies available, including 1 MHz
(which isn't a multiple of your bit rate clock). The most interesting
was 153.6 kHz, Mouser stock # 520-ECS-1536-12.5-13 which is in stock
at 66 cents.

So, here are a couple of things to try.

You could do a Web search and see if you can find a tuning fork
crystal that is an even multiple of 153.6 kHz and also a clock
frequency you feel would be suitable.

You could go back to my original suggestion, but use the 153.6 kHz
crystal instead of the 32.768 kHz. The advantage of this would be
that the calibration procedure would be simpler since you're looking
for the RC oscillator to be a small even multiple of the timer
oscillator. Still, now that you describe yourself as a "newbie", I
think that Dave's solution of just using a low frequency crystal, is
the better one if you can relax your requirements enough. Where does
a newbie get such tricky requirements, anyway?

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Re: RS232 Baud Rate questions - jay marante - Aug 24 11:47:00 2004

i haven't been following this thread. i'd like to ask some questions if its alright with the pips here.

andrew, why won't you use a crystal instead of te RC oscillator?

again, sorry...

jay

Graham Davies <YahooGroups@Yaho...> wrote:
--- In avrclub@avrc..., Andrew Lim <normaxcite@y...> wrote:

> Since I'm still a newbie here, I
> will use the 1.8432MHz external
> crystal to solve my RS232 problem
> for now. However, I am really keen
> to learn up how to synchronize the
> 32.768kHz watch crystal for my RS232.
>
> Thanks a bunch dude !! U rock !

Thanks. Since I spent a while with my Mouser catalog last night, let
me rock a little more.

I was right that a few megahertz is low for an AT-cut crystal. There
is one available priced at 1.000 MHz, but it costs about $12.00. The
1.8432MHz is still several times the price of, say, 3.6864 MHz.
Plus, something I didn't realize when searching on-line, it is only
available in the big ole HC49/U case, not the smaller HC49/U-S. So,
even to use 1.8432MHz you're going to really have to relax your size
constraints.

I also looked at tuning fork crystals. I was right that they are
quite a bit smaller than the smallest AT-cut. Almost all are 32.768
kHz, but there are a few other frequencies available, including 1 MHz
(which isn't a multiple of your bit rate clock). The most interesting
was 153.6 kHz, Mouser stock # 520-ECS-1536-12.5-13 which is in stock
at 66 cents.

So, here are a couple of things to try.

You could do a Web search and see if you can find a tuning fork
crystal that is an even multiple of 153.6 kHz and also a clock
frequency you feel would be suitable.

You could go back to my original suggestion, but use the 153.6 kHz
crystal instead of the 32.768 kHz. The advantage of this would be
that the calibration procedure would be simpler since you're looking
for the RC oscillator to be a small even multiple of the timer
oscillator. Still, now that you describe yourself as a "newbie", I
think that Dave's solution of just using a low frequency crystal, is
the better one if you can relax your requirements enough. Where does
a newbie get such tricky requirements, anyway?

Graham.
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Re: RS232 Baud Rate questions - Graham Davies - Aug 24 12:33:00 2004

--- In avrclub@avrc..., jay marante <jaythesis@y...> wrote:

> i haven't been following this thread.
> i'd like to ask some questions if its
> alright with the pips here.
>
> andrew, why won't you use a crystal
> instead of te RC oscillator?

Well, why not just review the thread? Andrew gave his reasons (size
and power) in message 1081:
http://groups.yahoo.com/group/avrclub/message/1081

I think we're coming to the conclusion that Andrew needs to relax his
contraints and use the crystal anyway as he's not up for calibrating
the RC oscillator.

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Re: RS232 Baud Rate questions - jay marante - Aug 24 12:59:00 2004

thanks, graham.

well, RS Components have lots of crystals of different package. Graham Davies <YahooGroups@Yaho...> wrote:
--- In avrclub@avrc..., jay marante <jaythesis@y...> wrote:

> i haven't been following this thread.
> i'd like to ask some questions if its
> alright with the pips here.
>
> andrew, why won't you use a crystal
> instead of te RC oscillator?

Well, why not just review the thread? Andrew gave his reasons (size
and power) in message 1081:
http://groups.yahoo.com/group/avrclub/message/1081

I think we're coming to the conclusion that Andrew needs to relax his
contraints and use the crystal anyway as he's not up for calibrating
the RC oscillator.

Graham.
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Crystal Question - Daniel Boyer - Aug 24 13:18:00 2004

Last time I ordered some stuff from digikey, I ordered some crystals and
appearntly wasn't paying attention when I wrote down the part numbers...
Now I have a bunch of Serial crystals. What does one use serial
crystals for?
Thanks,
Daniel
KC9DAG




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Re: RS232 Baud Rate questions - Graham Davies - Aug 24 15:38:00 2004

--- In avrclub@avrc..., jay marante <jaythesis@y...> wrote:

> ... RS Components have lots of
> crystals of different package.

That's great! Good info!

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: Crystal Question - Graham Davies - Aug 24 16:36:00 2004

--- In avrclub@avrc..., "Daniel Boyer" <dpboyer@d...> wrote:

> ... I have a bunch of Serial crystals.
> What does one use serial crystals for?

What you probably did was order crystals specified for use in series
reasonant mode. This just means that the crystal has been cut to have
its minimum impedance at the specified frequency for use with a
series mode oscillator. Microprocessors don't use series mode
oscillators, but you can make one from a couple of inverters. There
are plently of places on the Web you can find circuits. If you're not
too fussy about the frequency, just go ahead and use them with any
parallel mode oscillator, such as an MCU. They will oscillate just
fine but the frequency will be a bit off. I don't know by how much.
Probably no more than one percent. For more information, do a Google
search.

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Help on Keypad decoder. - Andrew Lim - Aug 31 8:46:00 2004


Hi Graham,

Thanks for the help on my RS232. Now, I'm facing another challenge. I'm trying to use a 4X4 keypad (with interrupt) as my input and I have written my code to read it. However, I did not get consistant output from my keypad. For example, If I keep on pressing the 'Down' button, I should be able to get a consistant value from my keypad but that wasn't so. I have my down button to be 0x90 value after I read from my row and collumn.

Is it because of the debouncing issue? I have included a delay before I read the input. My delay runs about 5ms. ( I think this should be good enough to solve the debouncing issue)
To make things clear, here is the flow of my code.

1. Set row0 and row1 as pull-up

2. Set col0 and col1 output low

3. Delay for 5ms

4. read pin value and store in r16 ( in r16, PINA )

===========================

5. Set col0 and col1 as pull-up

6. Set row0 and row1 output low

7. Delay for 5ms

8. read pin value and store in r17 ( in r17, PINA )

==========================

4. add both r16 and r17 ( OR r16, r17 )

5. Store r16 into SRAM as my Keypad value. ( sts Keypad_Data, r16 )

6. Then I use the keypad value (Keypad_Data) in SRAM to process my LCD display.
Problem : Inconsistant keypad value.
Hope you can help me out. I'm rushing for my dateline... :(((((
Thanks. Regards,

Andrew
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

RE: [AVR club] Help on Keypad decoder. - Dave Hylands - Aug 31 9:32:00 2004

Hi Andrew,

In keypad stuff that I've done, I've never changed the input/output
functions of the rows/cols.

You said you have a 4x4 keypad right? Your code seems to be for a 2x2
keypad.

Once you've decided that some key is pressed, you perform a scan, to
figure out exactly which key was pressed. A simple strategy might be:

Delay for debounce period
For col = 0; col < numCols; col++
set 'col' to 0, other cols to 1
read rows, if any row == 0, stop

You now have the row/col of the key being pressed.

There's a really good article on debouncing (both hardware and software)
over here:
http://www.ganssle.com/debouncing.pdf

--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

> -----Original Message-----
> From: Andrew Lim [mailto:normaxcite@norm...]
> Sent: Tuesday, August 31, 2004 5:47 AM
> To: avrclub@avrc...
> Subject: [AVR club] Help on Keypad decoder. >
> Hi Graham,
>
> Thanks for the help on my RS232. Now, I'm facing another
> challenge. I'm trying to use a 4X4 keypad (with interrupt) as
> my input and I have written my code to read it. However, I
> did not get consistant output from my keypad. For example, If
> I keep on pressing the 'Down' button, I should be able to get
> a consistant value from my keypad but that wasn't so. I have
> my down button to be 0x90 value after I read from my row and collumn.
>
> Is it because of the debouncing issue? I have included a
> delay before I read the input. My delay runs about 5ms. ( I
> think this should be good enough to solve the debouncing issue)




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - jay marante - Aug 31 11:50:00 2004

hey andrew, first, is it really a 4x4 or a 2x2 keypad?

there's an application notes at the atmel website, www.atmel.com, about the 4x4 keypad. you might want to take a look at it.

also, debouncing at 5ms is too fast (for me). if you'd try to press a key for 1sec and a debounce of 5ms, that would be 1000ms/5ms = 200. which means that there will be 200 characters per second if you'd try to display that on the LCD. take a look at your keyboard. usually, it only displays 10 to 20 characters per second. i suggest you set your debounce at 50ms or 100ms so as to display a keypressed of about 10 to 20. just a suggestion. :)

also, the time i allocate for reading between the rows and columns is only a counter set at 0xFF and decrementing until 0, and running at 7.3728MHz.

cheers!

Jay
laguna, philippines

Andrew Lim <normaxcite@norm...> wrote:

Hi Graham,

Thanks for the help on my RS232. Now, I'm facing another challenge. I'm trying to use a 4X4 keypad (with interrupt) as my input and I have written my code to read it. However, I did not get consistant output from my keypad. For example, If I keep on pressing the 'Down' button, I should be able to get a consistant value from my keypad but that wasn't so. I have my down button to be 0x90 value after I read from my row and collumn.

Is it because of the debouncing issue? I have included a delay before I read the input. My delay runs about 5ms. ( I think this should be good enough to solve the debouncing issue)
To make things clear, here is the flow of my code.

1. Set row0 and row1 as pull-up

2. Set col0 and col1 output low

3. Delay for 5ms

4. read pin value and store in r16 ( in r16, PINA )

===========================

5. Set col0 and col1 as pull-up

6. Set row0 and row1 output low

7. Delay for 5ms

8. read pin value and store in r17 ( in r17, PINA )

==========================

4. add both r16 and r17 ( OR r16, r17 )

5. Store r16 into SRAM as my Keypad value. ( sts Keypad_Data, r16 )

6. Then I use the keypad value (Keypad_Data) in SRAM to process my LCD display.
Problem : Inconsistant keypad value.
Hope you can help me out. I'm rushing for my dateline... :(((((
Thanks. Regards,

Andrew
---------------------------------
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - Graham Davies - Aug 31 13:16:00 2004

--- In avrclub@avrc..., Andrew Lim <normaxcite@y...> wrote:

> I'm trying to use a 4X4 keypad ...

Like the others, I am confused by your algorithm with two rows and
two columns because it looks like it is intended for a 2x2 pad.

> ... (with interrupt) ...

Do you mean that you're trying to have a keypress cause an interrupt?
Or, do you mean you are using a timer interrupt to trigger polling?

> I did not get consistant output from
> my keypad ... Is it because of the
> debouncing issue?

I would say yes, inconsistency strongly suggests key bounce. Read the
article by Jack Ganssle (see previous post).

> I have included a delay before I
> read the input ... about 5ms.

Just adding a delay does not address key bounce. You're just sampling
the keys a bit later than without the delay. What you need to do is
to sample the keys repeatedly before making the decision to declare a
key pressed. You should look for many consecutive samples with the
key down. Also, once the key is up again you need to see it up for
many samples before you start looking again for a new keypress. Read
Jack's article (I'm assuming its the one I read in Embedded Systems
Programming a few months back).

> ... here is the flow of my code ...

I think you should start again. Flipping the row and column around as
inputs and outputs is most peculiar. Why did you decide to do that?

I think the basic method of scanning a matrix keypad is this (using
polling):

All rows and columns are normally inputs, rows have pullups.
Every polling interval, do this
BEGIN

make column 0 an output and put it low
read row inputs and store
put column 0 back to an input
repeat for columns 1, 2 and 3

look for one and only one bit set in the row inputs read
if this is true, figure out which key is down
if it is not true, no keys or several keys are down
treat several keys down as no keys (for simplicity)

debounce

END

For debouncing, I suggest a counter with a ceiling. If the key down
is the same as it was last time, increment the counter, but not above
the ceiling. When it first hits the ceiling, declare the keypress and
set a flag to indicate a keypress has been declared. If there is no
key down, decrement the counter, but not below zero. When it hits
zero clear the keypress declared flag and start looking for another
key press. You will have to fix this up but I think the general idea
will work.

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - chris atwan - Aug 31 15:09:00 2004

Graham,

I just finished a project with a 4x4 keypad. I can send you a copy if you would like.

Chris Atwan

Graham Davies <YahooGroups@Yaho...> wrote:
--- In avrclub@avrc..., Andrew Lim <normaxcite@y...> wrote:

> I'm trying to use a 4X4 keypad ...

Like the others, I am confused by your algorithm with two rows and
two columns because it looks like it is intended for a 2x2 pad.

> ... (with interrupt) ...

Do you mean that you're trying to have a keypress cause an interrupt?
Or, do you mean you are using a timer interrupt to trigger polling?

> I did not get consistant output from
> my keypad ... Is it because of the
> debouncing issue?

I would say yes, inconsistency strongly suggests key bounce. Read the
article by Jack Ganssle (see previous post).

> I have included a delay before I
> read the input ... about 5ms.

Just adding a delay does not address key bounce. You're just sampling
the keys a bit later than without the delay. What you need to do is
to sample the keys repeatedly before making the decision to declare a
key pressed. You should look for many consecutive samples with the
key down. Also, once the key is up again you need to see it up for
many samples before you start looking again for a new keypress. Read
Jack's article (I'm assuming its the one I read in Embedded Systems
Programming a few months back).

> ... here is the flow of my code ...

I think you should start again. Flipping the row and column around as
inputs and outputs is most peculiar. Why did you decide to do that?

I think the basic method of scanning a matrix keypad is this (using
polling):

All rows and columns are normally inputs, rows have pullups.
Every polling interval, do this
BEGIN

make column 0 an output and put it low
read row inputs and store
put column 0 back to an input
repeat for columns 1, 2 and 3

look for one and only one bit set in the row inputs read
if this is true, figure out which key is down
if it is not true, no keys or several keys are down
treat several keys down as no keys (for simplicity)

debounce

END

For debouncing, I suggest a counter with a ceiling. If the key down
is the same as it was last time, increment the counter, but not above
the ceiling. When it first hits the ceiling, declare the keypress and
set a flag to indicate a keypress has been declared. If there is no
key down, decrement the counter, but not below zero. When it hits
zero clear the keypress declared flag and start looking for another
key press. You will have to fix this up but I think the general idea
will work.

Graham.
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - Graham Davies - Aug 31 18:26:00 2004

--- In avrclub@avrc..., chris atwan <chrisatwan2000@y...>
wrote:

> Graham,
>
> I just finished a project with
> a 4x4 keypad. I can send you
> a copy if you would like.
>
> Chris Atwan

It's Andrew who is looking for help here. See messsage 1120. I would
think he'd appreciate a look at what you've done. It's very nice of
you to offer it. Let's see what Andrew says to all the replies to his
post. I think perhaps he checks in in the evening.

Graham.




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - Andrew Lim - Aug 31 21:44:00 2004

"...I just finished a project with a 4x4 keypad. I can send you a copy if you would like...." Hi Chris,

Its very nice of you to offer this help to me. I really appreciate that. Can you send it to me? Thanks a bunch ! Hi Graham,

I'm going through the article and your recommendation on the keypad scanning now.
Oh yeah.... I guess after the keypad. I will have to deal with EEPROM with TWI interface. Hahahahaha... I'm gonna get another headache there. *dizzy*

By the way, do u know how to use the .IF .ELIF and .ELSE directive in AVR Studio 4 ? I tried using it with my code below but it doesn't work.

out Temp1, PINA
.IF Temp1 == 0x80
ldi Temp2, 0xA0
.ELIF Temp1 == 0x40
ldi Temp2, 0x90
.ELIF Temp1 == 0x20
ldi Temp2, 0x50
.ELSE
ldi Temp2, 0xF0
.ENDIF

Is the code above correct? Guess there must be some bugs. Andrew

---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - Andrew Lim - Sep 1 7:10:00 2004

Hi Graham,

Just to clear up the confusion. Below is my reply. > I'm trying to use a 4X4 keypad ...

Like the others, I am confused by your algorithm with two rows and
two columns because it looks like it is intended for a 2x2 pad.
[Andrew]> It is intended for 2x2 keypad. > ... (with interrupt) ...

Do you mean that you're trying to have a keypress cause an interrupt?
Or, do you mean you are using a timer interrupt to trigger polling?
[Andrew]> I'm trying to have keypress cause an interrupt while my uC is in sleep mode > I did not get consistant output from
> my keypad ... Is it because of the
> debouncing issue?

I would say yes, inconsistency strongly suggests key bounce. Read the
article by Jack Ganssle (see previous post).

[Andrew]> OK. I'm reading it now. Trying to figure out the logic.

[Andrew]> If there anyway we can chat live? It will be easier to sort things out. Thanks

Regards,
Andrew ---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - chris atwan - Sep 1 9:58:00 2004

Andrew,

I have only sent you the important stuff, sorry about the lack of comments.

Chris Atwan

Andrew Lim <normaxcite@norm...> wrote:
"...I just finished a project with a 4x4 keypad. I can send you a copy if you would like...." Hi Chris,

Its very nice of you to offer this help to me. I really appreciate that. Can you send it to me? Thanks a bunch ! Hi Graham,

I'm going through the article and your recommendation on the keypad scanning now.
Oh yeah.... I guess after the keypad. I will have to deal with EEPROM with TWI interface. Hahahahaha... I'm gonna get another headache there. *dizzy*

By the way, do u know how to use the .IF .ELIF and .ELSE directive in AVR Studio 4 ? I tried using it with my code below but it doesn't work.

out Temp1, PINA
.IF Temp1 == 0x80
ldi Temp2, 0xA0
.ELIF Temp1 == 0x40
ldi Temp2, 0x90
.ELIF Temp1 == 0x20
ldi Temp2, 0x50
.ELSE
ldi Temp2, 0xF0
.ENDIF

Is the code above correct? Guess there must be some bugs. Andrew

---------------------------------
Yahoo! Groups SponsorADVERTISEMENT ---------------------------------
Yahoo! Groups Links

To

---------------------------------

//************************************************************************************************
unsigned char checkbuttons(void)
{
{
// read keyboard 4 * 4 matrix
keypress = 0;

PORTF=0x01;
delay_us(10);
if (PINA.1)
keypress = 1;
else if (PINA.2)
keypress = 5;
else if (PINA.3)
keypress = 9;
else if (PINA.4)
keypress = 13;
PORTF=0x02;
delay_us(10);
if (PINA.1)
keypress = 2;
else if (PINA.2)
keypress = 6;
else if (PINA.3)
keypress = 10;
else if (PINA.4)
keypress = 14;
PORTF=0x04;
delay_us(10);
if (PINA.1)
keypress = 3;
else if (PINA.2)
keypress = 7;
else if (PINA.3)
keypress = 11;
else if (PINA.4)
keypress = 15;
PORTF=0x08;
delay_us(10);
if (PINA.1)
keypress = 4;
else if (PINA.2)
keypress = 8;
else if (PINA.3)
keypress = 12;
else if (PINA.4)
keypress = 16;
}
return(keypress);
}

//************************************************************************************************
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
unsigned char tempkey=0;
newkey=0;
tempkey=checkbuttons();
if (tempkey!=oldkey) newkey=tempkey;
}

//************************************************************************************************

while(newkey==0); //wait here for a new key
if ((newkey==oldkey)||(newkey==0))
goto TheEnd; //if the key is still pressed wait more
putchar1(newkey);
oldkey=newkey;
switch (newkey)
{
// 0 button
case 1:
putchar1('A');
//ClearScreen();
PrintCharacter(10, 1, 48, On); // xPos, yPos, character to print, Action. Note 129 here is beyond
UpdateDisplay();
a=a+6;
break;

[Non-text portions of this message have been removed]




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

Re: [AVR club] Help on Keypad decoder. - Andrew Lim - Sep 2 1:17:00 2004


Hi Chris, Graham,

Thanks a bunch !! I've got my keypad working smoothly without any debouncing anymore. hehe...
Actually I used Graham's idea of sampling my keypress before declaring a keypress.

Thanks for the code Chris, I appreciate it.
Here is another quick question. (Sorry I'm always full of questions..hahaha)

Q: What is the difference between TWI and I2C ?

Q: I'm trying to interface my EEPROM to uC. Which method is better way? It seems like I2C dont have much procedure to follow compare to TWI.
Thanks.
Regards,

Andrew ---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )

[AVR club] Help on EEPROM interface using I2C - Andrew Lim - Sep 2 6:56:00 2004


Hi All,

Its me again. I have problem with my EEPROM now. I'm using I2C to interface with it and I can't get ACK from the EEPROM to proceed.
Below are the setting of my EEPROM.
1. Connected SDA and SCL to a pull-up resistor. (Using 2kOhm for both resistors).

2. My Vcc is 3.6V. Using ATmega8535L.

3. I measured a 2.3V(1.15mA) at SDA pin and 3.4V(1.7mA) at SCL pin.

4. Set SCL and SDA to output high.

sbi DDRC,0

sbi DDRC,1

sbi PORTC,0

sbi PORTC,1
Thanks. Regards,

Andrew
---------------------------------




(You need to be a member of avrclub -- send a blank email to avrclub-subscribe@yahoogroups.com )