Help decoding formulas in a "C" example

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

Need help decoding (C)

http://www.mikrocontroller.net/a...

http://www.mikrocontroller.net/a...

In the above example (links) is a "C" program to decode a KeyPad
using the Analog to Digital converter of an AVR..

COOL..

Thing is (Not knowing) "C" if my life depended on it..

What are the "Answers" for the Formulas.. (To build the data table)

what is the "%" (Does that mean Integer) ??

also (u16) is confusing to me.. (Is that just a variable)
or is it a "Subroutine" label. ??

Like I say.. Can't follow "C" if my life depended on it..

Any Guidance to understanding these "Formulas"
and or loops would be great.

Thanks

"We look for things.. Things that make us go."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
what is the "%" (Does that mean Integer) ??
No, it means "modulus". That is, it preforms an integer divide, but returns the remainder instead of the result.
Quote:
also (u16) is confusing to me.. (Is that just a variable)
or is it a "Subroutine" label. ??
Neither, it is a type (in this case, likely an unsigned 16 bit int, but it is non-standard notation).

Exactly what is the "formula" that you don't understand? Do you mean "functions"?

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
typedef unsigned char  u8; **An 8 bit unsigned variable
typedef unsigned short u16; **A 16 bit unsigned variable

% means in this case Modulus (I think)

The code seems to read the voltage from the ADC (switches) and then compares the value read to a table to determine which key has been pressed if any.

edit beaten while looking things up, I'm soooo slowww

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The top part is just magic to get predefined values to check against into the array "Thresholds". As John said, the "key_no" functions just loop through the array until it finds a value that is greater than or equal to the input ADC value. The index of that value is then the index of the key that was pressed.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi wrote:
Quote:
what is the "%" (Does that mean Integer) ??
No, it means "modulus". That is, it preforms an integer divide, but returns the remainder instead of the result.
Quote:
also (u16) is confusing to me.. (Is that just a variable)
or is it a "Subroutine" label. ??
Neither, it is a type (in this case, likely an unsigned 16 bit int, but it is non-standard notation).

Exactly what is the "formula" that you don't understand? Do you mean "functions"?

"Modulus" great thanks !! Enough said
I'll try a calculation using that.

Only reason I don't understand the formula (was the %)
should work now knowing it means "MOD"

and (n) is a value 0 to 11 (I think) - (I Hope) :-)

"We look for things.. Things that make us go."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

typedef unsigned char  u8; **An 8 bit unsigned variable
typedef unsigned short u16; **A 16 bit unsigned variable

% means in this case Modulus (I think)

The code seems to read the voltage from the ADC (switches) and then compares the value read to a table to determine which key has been pressed if any.

edit beaten while looking things up, I'm soooo slowww

Duuuuu ... Thanks JS...

I Missed that at the top of the program.. :oops:

Thanks again you Guys..

CHOW

"We look for things.. Things that make us go."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
and (n) is a value 0 to 11 (I think)
Correct. The values being sent in are in the calls to THRESHOLD in the array.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok great.. This is going to be for a project using an
ATTiny25 (hope good enough) for it.. (Doubt It will use up all the 2K. RAM)

I Like to use ASM.. won't be using this kind of math translated to ASM.. (OMG)..
I would like to pre-calculate the table.. (or at least try).

Even the ADC formula in the ATTiny25 spec sheet retuns odd values..
(Guess I'm using the wrong formula) ?? Don't know.

Thanks for clearing up the (%) Meaning and Function in "C"

Another reason I don't like "C" too many abreviations at times.

"We look for things.. Things that make us go."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

(Doubt It will use up all the 2K. RAM)

Just to note that tiny25 only has 128 bytes of RAM (it is 2K of flash that it has not RAM). As such it would be very easy to use a large part of it if you either tried to implement floating point or even if you just go stuffing it full of PC calculated lookup tables.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The 2K was in reference to the Flash portion of RAM..

This is step one in trying to understand the ADC stage of and AVR.. (and all it's registers)

First time using it.. (ADC that is.)

Time to move on from "Push Button in" - "LED out" DDRB register projects.

Next is playing with the PWM to understand that,
and get it to work to drive a servo..

I've just recently purchased an "AVRISP mkii"
And want to use it.. :-) for some fun stuff.

"We look for things.. Things that make us go."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I Like to use ASM.. won't be using this kind of math translated to ASM.. (OMG)..
Keep in mind that there is not much in the macros that is particularly tied to C, it is all done by the pre-processor. You might be able to come up with a macro in ASM that does the same thing.

Or you can just learn C ;)

Quote:
The 2K was in reference to the Flash portion of RAM..
Flash is not RAM.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Flash is not RAM.

And if by "RAM" you just meant a generic term for "memory map" it's not even that either as AVR is a Harvard processor and flash and RAM have completely separate memory maps. It's not like ARM or something where some of the memory space is mapped to IO, some mapped to (S/D)RAM and some to flash. In a Harvard processor each is completely separate and each has their own location 0x0000.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OMG (what did I start now)..

The 2K storage part of the AVR that holds the instructions (is what I mean) aka Flash

RAM ??.. Well: (it's memory)

And is Randomly Accessed as the instructions
"Jump" and "Call" around or read some data tables.

Thank God it doesn’t erase when power is removed..

Gee..that sounds like an EEPROM

Now I'm really confused. (Guess you could say it’s both)

So what is it ?? (RAM or ROM)

let’s just call it “Flash” to avoid confusion.

But what ever it's called "RAM" "ROM" “FLASH”
It works and makes me happy.

Size does matter..

I just hope that the 2K (flash) won't be a problem in the long run.

(When I build) to enhance this project to Drive a Servo (to degree position)
based on what push button I push index/matrix by a resistor ladder connected
to the ADC. (takes a breath)

Now that the formulas are working (Calculating) to give (Make Sense) values.
With a voltage drop of 2.5 volts with the answer of 512 (Decimal)

Things are looking up.

Now I have to Configure the ADC Control registers.. (OMG) what next.

I’ll be using Pin 1 for the ADC (0) input.. (Also the Reset pin) guess
I’ll need to use that special Fuse bit.

And hope I don’t “Brick” the AVR.

Then I'll Try to learn "C".... Maybe Next Year.
I Should have some time then (after this project).

"We look for things.. Things that make us go."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The 2K storage part of the AVR that holds the instructions (is what I mean) aka Flash
Yes, flash, not RAM.
Quote:
Gee..that sounds like an EEPROM
Precisely, flash is a type of ROM (non-volatile memory) that can be written to only with special procedures.

RAM (volatile memory), on the other hand, can be written to or read from at any time.

Quote:
I just hope that the 2K (flash) won't be a problem in the long run.
But the tiny25 has the tiny45 and tiny85 as drop in replacements.
Quote:
I’ll be using Pin 1 for the ADC (0) input.. (Also the Reset pin) guess I’ll need to use that special Fuse bit.
A better solution is to get an AVR with enough pins for the application you intend to create.

Regards,
Steve A.

The Board helps those that help themselves.