EmbeddedRelated.com
Forums

How to program Port E ADC to Port B and Random number

Started by yew alex April 21, 2005
Thanx Mike for the reply

Let me rephares what i meant, Im trying to built a kit, which uses 68hc11. Im trying to use Port E, as my input data (analog to digital) to Port B. When ever I, input something from Port E, it will capture the value and Output it to Port B which my LEDs will light up.

This seems to be simply kit, but what I dont understand is how to program on it during my coding (assembly language) how should I set my Port E which able to read my data. I need some explaination on how should I program it. It will be more helpful if someone show me some example of assembly language on how to program it. It seems like i have to set something on the OPTION #$1039 which the bit 7 have to be set up (boost up) #%1000 0000
am I right? Then I should set the ADR1 to ADR4 and etc etc. I know abit here and there but I do not know how actually it was to set up. My board (68hcllMC) is a 2MHz board.

I just dont have the basic, and I want to learnt more on how to set Port E able to read the input and capture it and generate it to Port B. I want examples of the assembly codes.

Another question is random number, which is something to do with the TCNT. Like everytime I push a momentory button, the hc11 will chooose a number randomly between 100-200 and capture it and store it in certain Port. Its a division calculation method which I dont understand. Again i want examples on assembly language. I just want examples and with examples at least i can see how it should "be correctly" being program rather than explaination on theory part.

I need to understand, its a very intresting thing to learn about this 68hc11

thanx

regards,

alex

ty <Mike.McCarty@Mike...> wrote:
kl_mistress wrote:

>dear alll,
>
>Hi im the new member name alex. >

Welcome aboard!

>Im a student, and currently im learning about duet board of 68hc11. I'm sorry not to know what a "duet board" is.

>I want to know to program a random number between 100-200 or anything
>I just dont understand. I read Peter spasov 68hc11 yet im still blur
>on it
>May i or please help me by showing some examples on how to do random
>number and whats is the purpose of it
There seem to be several questions, there. First, the MC68HC11 is a
deterministic
machine. It cannot generate random numbers on its own. However, with some
external hardware, random numbers can be generated. In particual, the A/D
connected to PORTE can be used with an external random noise source to
generate
random numbers. On the other hand, perhaps pseudo-random numbers will
suffice
for your application. Random numbers and pseudo-random numbers have many
applications. Since I don't know what your background is, it is
difficult for me
to give applications you might recognize. Monte Carlo methods use random or
pseudo-random numbers to perform numerical calcuations (like computing
volume of complicated objects in multi-dimensional spaces, for example)
which would otherwise be extremely difficult or impossible. They are used
in cryptography. There are many uses.

>Regarding port e, which is my project im doing of potiometer. I have
>to use to potionmeter to set to Port E for ADC to output to portb
>which the led will led.
I'm not sure I understand what you are saying. I don't know the verb "led".
I'll guess that you mean you want to use the A/D connected to PORTE to
get a value which is displayed as an 8 bits on PORTB.

The steps are:

Turn on the A/D. Select whether to use the E clock or the R/C clock.
Initiate a conversion on the selected input pin.
Wait for the completion flag to be set.
Read the value from the A/D result location.
Output the value to PORTB.

>ANyone can help? I don't know whether I have helped, yet, but give us more information
about your project and more information will be forthcoming.

No solutions to homework problems, but help to get you over a hurdle
if you need it.

Mike
--

p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
I speak only for myself, and I am unanimous in that!
---------------------------------
Yahoo! Groups Links

To __________________________________________________





In a message dated 4/21/05 5:19:43 A.M. Eastern Daylight Time,
kl_mistress@kl_m... writes:

what I dont understand is how to program on it during my coding (assembly
language) how should I set my Port E which able to read my data. =========================================
HC11 has been a popular microcontroller for 20 years or so. There are many
examples of how to read the a/d converter in assembler and c. Try typing HC11
into google. Give you a couple of thousand places to go look for examples?
Check out the hc11 web ring sponsored by Technological Arts. We can help you
debug your program if you get stumped once you write it and try to debug it till
it drives you batty.


yew alex wrote:

[snip]

>It seems like i have to set something on the OPTION #$1039 which the bit 7 have to be set up (boost up) #%1000 0000
>am I right? Then I should set the ADR1 to ADR4 and etc etc. I know abit here and there but I do not know how actually it was to set up. My board (68hcllMC) is a 2MHz board.
>
>I just dont have the basic, and I want to learnt more on how to set Port E able to read the input and capture it and generate it to Port B. I want examples of the assembly codes.
I gave you the steps to follow in my earlier message.

Step Zero: Select the clock to use. Set CSEL in OPTION to 0 to use the E
clock,
which is fine in your case. For a slower clock, use CSEL = 1.

Step One: Turn on the A/D. Set ADPU in OPTION to 1. Wait at least 100
microseconds to allow the A/D to stabilize. (This can be done at the same
time Step Zero is done, which is why I numbered it Zero.)

Step Two: Initiate a conversion. Write a value to ADCTL to start a
conversion
on your selected channel. I avoid channel 0, because BUFFALO uses PORTE.0
for other things.

Step Three: Wait for completion. Wait for CCF in ADCTL to be 1.

Step Four: Read results. Read ADRx appropriate for the channel you selected.

Repeat Step Two through Step Four as needed.

Output to PORTB I don't address, as you can probably figure that out. But
see my comment below about putting some code where we can see it.

>Another question is random number, which is something to do with the TCNT. Like everytime I push a momentory button, the hc11 will chooose a number randomly between 100-200 and capture it and store it in certain Port. Its a division calculation method which I dont understand. Again i want examples on assembly language. I just want examples and with examples at least i can see how it should "be correctly" being program rather than explaination on theory part. Well, we don't, either. What do you mean by "division calculation method"?
One way to get some random numbers is simply to capture the system clock
timer value when a key is pressed. There are several impediments to this
technique.
First, you need a debounced button. If you debounce in software, you will
inevitably degrade the quality of your random numbers. This may be ok.
Second, you will introduce bias into the numbers by polling or using a
periodic interrupt to look for the button. Imagine what would happen if
you use TCNT, and your main loop takes 65536 cycles to complete. If
your first "random" number was 48, then *every* "random" number would
be 48. The generation of good, especially cryptographically good,
random numbers is a serious challenge. But I'll guess you need only
something of very poor quality.

For "division", I'll guess. You need a random integer in a certain
rather small
range, say 1 to 6, like in a simulation of a die. One way to do this is
to take
a random integer in a larger range, say 0 to 65535, and divide by 6.
Take the
remainder and get a number in the range 0 to 5. Add one, and you've got
a number in the range 1 to 6. This is an extremely poor way to generate
random numbers.

A somewhat slower method is to use floating point. Take
your initial number which is in 0 to 65535 inclusive. Divide it by 65536.
You get a number in the interval [0,1). Multiply by 6, and you get a
number in the range [0,6). Round down, and you get an integer in the
range 0 to 5 inclusive. Add 1.

The same technique works with fixed point. Again divide by 65536 to
get a fixed point number in the interval [0,1), and multiply. Add 1 and
round down. This would be faster and easier on the MC68HC11. It
would only take two MUL instructions and an ADDD instruction. About
90 cycles. If you need numbers larger than 255, then it gets more
complicated, because the '11 doesn't have a 16 bit multiply.

One problem we are having is paucity of data. You present questions, but
no information by which we can interpret them.

My biggest suggestion: Try writing a little code, even though you *know*
it's wrong. Put it here, and we'll (gently) help you to improve it until you
have got a working program.

People learn much more from mistakes than from making something work,
but not knowing why it does. If you make a mistake, and then fix it, you
know why it was broken, and how you fixed it. This knowledge will stay
with you.
[snip]

Mike

--
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
I speak only for myself, and I am unanimous in that!


See below.

Emmett Redd Ph.D. mailto:EmmettRedd@Emme...
Associate Professor (417)836-5221
Department of Physics, Astronomy, and Materials Science
Southwest Missouri State University Fax (417)836-6226
901 SOUTH NATIONAL Dept (417)836-5131
SPRINGFIELD, MO 65804 USA
> -----Original Message-----
> From: m68HC11@m68H...
> [mailto:m68HC11@m68H...] On Behalf Of Mike McCarty
> Sent: Thursday, April 21, 2005 9:23 AM
> To: m68HC11@m68H...
> Subject: Re: [m68HC11] How to program Port E ADC to Port B
> and Random number > yew alex wrote:
>
> [snip]
>
> >It seems like i have to set something on the OPTION #$1039
> which the bit 7 have to be set up (boost up) #%1000 0000
> >am I right? Then I should set the ADR1 to ADR4 and etc etc.
> I know abit here and there but I do not know how actually it
> was to set up. My board (68hcllMC) is a 2MHz board.
> >
> >I just dont have the basic, and I want to learnt more on how
> to set Port E able to read the input and capture it and
> generate it to Port B. I want examples of the assembly codes.
> >
> >
> I gave you the steps to follow in my earlier message.
>
> Step Zero: Select the clock to use. Set CSEL in OPTION to 0
> to use the E
> clock,
> which is fine in your case. For a slower clock, use CSEL = 1.
>
> Step One: Turn on the A/D. Set ADPU in OPTION to 1. Wait at least 100
> microseconds to allow the A/D to stabilize. (This can be done
> at the same
> time Step Zero is done, which is why I numbered it Zero.)

Mike, His questions imply he did not read you earlier missive or he
needs (as a example):

LDAA #$C0 ;Set ADPU and CSEL when stored in
OPTION.
STAA $1039 ;OPTION has the address $1039.

Emmett

>
> Step Two: Initiate a conversion. Write a value to ADCTL to start a
> conversion
> on your selected channel. I avoid channel 0, because BUFFALO
> uses PORTE.0
> for other things.
>
> Step Three: Wait for completion. Wait for CCF in ADCTL to be 1.
>
> Step Four: Read results. Read ADRx appropriate for the
> channel you selected.
>
> Repeat Step Two through Step Four as needed.
>
> Output to PORTB I don't address, as you can probably figure
> that out. But
> see my comment below about putting some code where we can see it.
>
> >Another question is random number, which is something to do
> with the TCNT. Like everytime I push a momentory button, the
> hc11 will chooose a number randomly between 100-200 and
> capture it and store it in certain Port. Its a division
> calculation method which I dont understand. Again i want
> examples on assembly language. I just want examples and with
> examples at least i can see how it should "be correctly"
> being program rather than explaination on theory part.
> >
> >
> Well, we don't, either. What do you mean by "division
> calculation method"?
> One way to get some random numbers is simply to capture the
> system clock
> timer value when a key is pressed. There are several
> impediments to this
> technique.
> First, you need a debounced button. If you debounce in
> software, you will
> inevitably degrade the quality of your random numbers. This may be ok.
> Second, you will introduce bias into the numbers by polling or using a
> periodic interrupt to look for the button. Imagine what would
> happen if
> you use TCNT, and your main loop takes 65536 cycles to complete. If
> your first "random" number was 48, then *every* "random" number would
> be 48. The generation of good, especially cryptographically good,
> random numbers is a serious challenge. But I'll guess you need only
> something of very poor quality.
>
> For "division", I'll guess. You need a random integer in a certain
> rather small
> range, say 1 to 6, like in a simulation of a die. One way to
> do this is
> to take
> a random integer in a larger range, say 0 to 65535, and divide by 6.
> Take the
> remainder and get a number in the range 0 to 5. Add one, and
> you've got
> a number in the range 1 to 6. This is an extremely poor way
> to generate
> random numbers.
>
> A somewhat slower method is to use floating point. Take
> your initial number which is in 0 to 65535 inclusive. Divide
> it by 65536.
> You get a number in the interval [0,1). Multiply by 6, and you get a
> number in the range [0,6). Round down, and you get an integer in the
> range 0 to 5 inclusive. Add 1.
>
> The same technique works with fixed point. Again divide by 65536 to
> get a fixed point number in the interval [0,1), and multiply.
> Add 1 and
> round down. This would be faster and easier on the MC68HC11. It
> would only take two MUL instructions and an ADDD instruction. About
> 90 cycles. If you need numbers larger than 255, then it gets more
> complicated, because the '11 doesn't have a 16 bit multiply.
>
> One problem we are having is paucity of data. You present
> questions, but
> no information by which we can interpret them.
>
> My biggest suggestion: Try writing a little code, even though
> you *know*
> it's wrong. Put it here, and we'll (gently) help you to
> improve it until you
> have got a working program.
>
> People learn much more from mistakes than from making something work,
> but not knowing why it does. If you make a mistake, and then
> fix it, you
> know why it was broken, and how you fixed it. This knowledge will stay
> with you. >
> [snip]
>
> Mike
>
> --
> p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
> I speak only for myself, and I am unanimous in that! >
>
> Yahoo! Groups Links




Dear all,

Hi its me again. How are you guys. Thanx alot for the information regarding port E and random number. Well, the Port E problems seems to be solved 70% already. But I still not quite sure about random number. Yes I understand the theory part about it, but I do not know to write the source code about it and have some experiment on it. Anyone out there kind enough to show me how or examples on random numbers (source code) I need examples of asm file to understand. It would be much better if I can learnt from asm. file rather than just theory part. Any I would like to say Thank you to Mike for helping explainig so much to me . Thank you
Regards,
alex ---------------------------------


yew alex wrote:

>Dear all,
>
>Hi its me again. How are you guys. Thanx alot for the information regarding port E and random number. Well, the Port E problems seems to be solved 70% already. But I still not quite sure about random number. Yes I understand the theory part about it, but I do not know to write the source code about it and have some experiment on it. Anyone out there kind enough to show me how or examples on random numbers (source code) I need examples of asm file to understand. It would be much better if I can learnt from asm. file rather than just theory part. Any I would like to say Thank you to Mike for helping explainig so much to me . Thank you >
>Regards, >
>alex
Well, you don't give us much to go on. There are several techniques for
generating pseudo-random numbers (which I suppose you want).

I'll suppose you need only very crude pseudo-random numbers. One way
to generate these is by using keypresses as external events. You had
mentioned something like that before. I suppose that your application
is in the form of a "main loop" which looks for events to handle. One of
the events to handle might be a human keypress. So your application looks
something like this:

MainLoop:
jsr EventOne
jsr EventTwo
jsr EventThree
jsr EventFour
...
jsr KeyPress
jmp MainLoop

Now each of the events looks like this:

EventOne:
tst EventOneFlag
bne *+5
rts
<more code to handle whatever event one is >
rts

EventTwo:
tst EventTwoFlag
bne *+5
rts
<more code to handle whatever event two is>
rts

KeyPress:
tst KeyPressFlag
bne *+5

jsr SetupRandomNumber

<rest of code to handle a keypress, like handle a command>
rts

SetupRandomNumber:
ldd KeyEpoch
std PrevKeyEpoch
ldd TCNT
std KeyEpoch
rts

*
* GetRandomNumber -- get a random number
*
* Inputs:
* KeyEpoch
* PrevKeyEpoch
*
* Outputs:
* D = random number
*
* Side Effects:
* Flags altered
*
* Dependencies:
* Must not be called unles a keypress has occurred
* since the previous call.
*
GetRandomNumber:
ldd KeyEpoch
subd PrevKeyEpoch
rts As I mentioned, this gives you only very crude random numbers,
suitable for some simple game programming, perhaps, but not
for serious work, and certainly not for cryptographic
purposes.

A more robust routine would check a flag and return with carry set
or something if no keypress had occurred. The pseudo code would be:

SetupRandomNumber:
<code like above>

ldab #$FF Indicate we have one
stab RandomSetFlag
rts

GetRandomNumber:
tst RandomSetFlag Do we have one?
bne Grn1 Jump if so
sec Indicate no random number avail.
rts
Grn1:
clr RandomSetFlag We used it up!
<code like above>
clc Indicate have a random number
rts

Mike

--
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
I speak only for myself, and I am unanimous in that!


Mike McCarty wrote:

[snip]

>KeyPress:
> tst KeyPressFlag
> bne *+5 >
I needed an rts there. Hope that wasn't confusing.

[snip
]
Mike

--
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
I speak only for myself, and I am unanimous in that!