2 bit random generator pic16F84A plz help!

Thread Starter

Samantha..

Joined May 22, 2011
7
Hello everyone, I have a project that includes random generator for a die. We are required to do it in this format:

When the interrupt is received from RB0/INT (push button), we will read the value of the TMR0 register and compute a random number as follows:

The first two least significant bits (bit 0 and 1), will generate a number between 0 (00 in binary) and 3 (11 in binary). This number will be added to one of the following three possibilities: 1, 2, or 3. This addition should also be randomly selected by reading the third and the fourth (bit 2 and 3) bits of the TMR0. Now, after the addition, we will get a number between 1 and 6.
Now, we still have to know when we have to add 1, 2, or 3. We will take the third and the
forth (bit 2 and 3) bits of TMR0 as a random selection of the value to be added. The
following table shows the different possibilities.

TMR0 (bit 2, 3) ADD
00 1
01 2
10 or 11 3


Example: If the TMR0 was 00001011, the random number will be

3 (bit 0 and 1 are 11) + 3 (bit 2 and 3 are 10) = 6.
 

THE_RB

Joined Feb 11, 2008
5,438
That's terrible! you have a 25% chance of getting 1 or 2 and a 50% chance of getting a 3!

I take it the goal is to generate a number with 6 possibilities; 1-6 from a Timer capture gated by a button press?

A better way (that works for any number) is to count 1 to 6 in a loop, and test the button release between each number.

Then when the user releases the button you get a number 1-6 with perfect odds of being any number 1-6.
 

Thread Starter

Samantha..

Joined May 22, 2011
7
I know that the probabilities aren't equal but that is how we are required to do it. I searched using google but I didn't know what to do :confused:
 

Markd77

Joined Sep 7, 2009
2,806
Assembly or C?
I'd recommend taking a copy of the timer because if you work with it directly it will be changing which could give unpredictable results (which might make it more or less random).
 

t06afre

Joined May 11, 2009
5,934
I know that the probabilities aren't equal but that is how we are required to do it
That is OK. But still what part of this do you struggle with? Is it the interrupt function, The timer, or parsing and adding the bits from the timer. What kind of language do you use.
 

t06afre

Joined May 11, 2009
5,934
I want it in assembly pic16F84A.
I do not mean to sound grumpy. But I do not think anybody will just do your school work for you. At least not without a signed contract with you agreeing to pay standard development price. You should post whatever you have of coding then we can take it from that point.


But some hint from top of my head
  1. read the timer into a variable say var1
  2. make copy of this to variable var2
  3. mask the bit not used in var1
  4. Use the Rotate Right f through Carry two times on var2
  5. mask the bit not used in var2
  6. add var1 and var2
 

Markd77

Joined Sep 7, 2009
2,806
It's a start, there are a few problems.
You haven't loaded var2 with anything.
The third instruction shouldn't be there.
Have a look at the ANDWF instruction to keep only the bits you want.
The last 3 instructions have the wrong syntax, have a look at the datasheet for the correct way to use those instuctions.
 

t06afre

Joined May 11, 2009
5,934
Take a look at this code
Rich (BB code):
MOVFW TMR0
MOVWF var1
After doing this the W register still holds the TMR0 vaule:rolleyes: So it sould be easy to load the var2 with the correct value
 

THE_RB

Joined Feb 11, 2008
5,438
There's no "good" way to get a evenly balanced modulus 6 from any number of binary bits (especially a small number of binary bits!) as binary bits never divide equally by 3 or 6.

If you must use such a biased way of getting a number 1-6 here is simple asm code that does it your way with the first 2 bits then adds 3 for the third bit;
Rich (BB code):
movf TMR0,w     ; save timer to var1
movwf var1
movlw 1         ; default result is 1
btfsc var1,0    ; test bit0
movlw 2         ; set, so result is 2
btfsc var1,1    ; test bit1
movlw 2         ; set, so result is 3

btfsc var1,2    ; test for add 3 needed
addlw 3         ; set, so add 3 

simple method results;
000  1
001  2
010  3
011  3
100  4
101  5
110  6
111  6

Your method is much longer and gives these results;
0000  1
0001  2
0010  3
0011  3
0100  2
0101  3
0110  4
0111  4
1000  4
1001  5
1010  6
1011  6
1100  4
1101  5
1110  6
1111  6

those odds are terrible...
Did you actually want a better way to generate 1-6 from the first few bits of TMR0?
 
Top