# 2 bit random generator pic16F84A plz help!

Discussion in 'Programmer's Corner' started by Samantha.., May 22, 2011.

1. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
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.

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.

2. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
I am not sure what you are asking for. What part do you need help with? Have you done any code you post also

3. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
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.

4. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
that will give you some clues on how to do it.

5. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
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

6. ### Markd77 Senior Member

Sep 7, 2009
2,803
594
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).

7. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
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.

8. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
I want it in assembly pic16F84A. I simply do not know how to analyze not write the code of it plz help

9. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
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

10. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
MOVFW TMR0
MOVWF var1
BTFSS PortB, 0
RRF var2,d
RRF var2,d

is that close?

11. ### Markd77 Senior Member

Sep 7, 2009
2,803
594
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.

12. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
ok var1 is from the TMR0 but I have no idea what to load var2

13. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
Load var1 into var2. As you need copy of var1 to do some bit twiddling with

14. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
how would I do that?
( I am really new to this language so I am facing so much difficulties :S)

15. ### t06afre AAC Fanatic!

May 11, 2009
5,939
1,222
Take a look at this code
Code ( (Unknown Language)):
1.
2. MOVFW TMR0
3. MOVWF var1
4.
After doing this the W register still holds the TMR0 vaule So it sould be easy to load the var2 with the correct value

16. ### Samantha.. Thread Starter New Member

May 22, 2011
7
0
this is only a small function of a big project I have, so I really need your help

17. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
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;
Code ( (Unknown Language)):
1.
2. movf TMR0,w     ; save timer to var1
3. movwf var1
4. movlw 1         ; default result is 1
5. btfsc var1,0    ; test bit0
6. movlw 2         ; set, so result is 2
7. btfsc var1,1    ; test bit1
8. movlw 2         ; set, so result is 3
9.
10. btfsc var1,2    ; test for add 3 needed
12.
13. simple method results;
14. 000  1
15. 001  2
16. 010  3
17. 011  3
18. 100  4
19. 101  5
20. 110  6
21. 111  6
22.
23. Your method is much longer and gives these results;
24. 0000  1
25. 0001  2
26. 0010  3
27. 0011  3
28. 0100  2
29. 0101  3
30. 0110  4
31. 0111  4
32. 1000  4
33. 1001  5
34. 1010  6
35. 1011  6
36. 1100  4
37. 1101  5
38. 1110  6
39. 1111  6
40.
41. those odds are terrible...
42.
Did you actually want a better way to generate 1-6 from the first few bits of TMR0?