Generate random numbers using AVR microcontroller

Publisher:灵感驿站Latest update time:2013-12-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

, Star light
source program: SLAVR737.ASM
uses AVR microcontroller 8-bit data to generate random numbers, outputs random numbers from PORTA and PORTC, and displays them on 8X8 LEDs. For hardware wiring circuit, see "7.3.8 Button Guessing". The seed of the random number is set by the program (or an external switch). After the seed is started, the shift register returns to the cycle with a mutually exclusive XOR logic combination.
.include"8515def.inc"
rjmp RESET
.def temp =r16 ; register.def
temp1 =r17 ; register 1
.def udata =r21 ; store random number and send to port A.def
ddata =r22 ; store random number and send to port C.cseg
.org
0x10
RESET: ldi temp,high(RAMEND); set stack pointer
out SPH,temp
ldi temp,low(RAMEND)
out SPL,temp

ldi temp,0xff ; set port A and port C as output
out ddra,temp ; send direction register A
out ddrc,temp ; send direction register C
start: wdr ; turn off watchdog
ldi udata,0x6a ; set initial value of random number
ldi ddata,0x3c ;
startp: out porta,udata ; output to port A
out portc,ddata ; output to port C
ldi temp,0x80 ; Set delay constant
rcall delay ; Call delay subroutine
rcall randm ; Call 16-bit random number subroutine
rjmp startp
delay: ; General delay subroutine is omitted
....
Principle diagram of 16-bit random number generation
8-bit shift register generates random numbers Circular combination bit
number Circular input combination S=2^n-1 Qn XOR Qm
8 Q2 ⊕ Q3 ⊕ Q4 ⊕ Q8 (The current program button guesses the number using 8 digits)
9 Q5 ⊕ Q9
10 Q7 ⊕ Q10
11 Q9 ⊕ Q11
12 Q2 ⊕ Q10 ⊕ Q11 ⊕ Q12
13 Q1 ⊕ Q11 ⊕ Q12 ⊕ Q13
14 Q2 ⊕ Q12 ⊕ Q13 ⊕ Q14
15 Q14 ⊕ Q15
16 Q4 ⊕ Q13 ⊕ Q15 ⊕ Q16

randm: ;Generate a 16-bit random number subroutine
MOV temp,udata ;Generate a random number for port A
MOV temp1,udata ;
rol temp ;Left circular shift through the carry bit
eor temp1,temp ;XOR
rol temp ; Left circular shift through the carry bit
rol temp ; Left circular shift through the carry bit
eor temp1,temp ;XOR
MOV temp,ddata ; Generate a random number for port C
swap temp ; Left circular shift through the carry bit
eor temp,temp1 ;XOR Left circular shift through the carry bit
rol temp ; Left circular shift through the carry bit
rol ddata ; Left circular shift through the carry bit
rol udata ; Left circular shift through the carry bit
ret ;Subroutine returns
7.3.8 Button guessing program
Source program: SLAVR738.ASM
In many occasions such as button guessing (computer lottery, computer selecting lucky numbers), the game start button waits for an irregular and unsequenced data to be generated, that is, a random number generator is required. The random number seed is set by the program (or can be set by an external switch). After the seed is started, it is returned to the loop by the shift register in a mutually exclusive XOR logic combination. The schematic diagram for generating random numbers is as follows:
Principle diagram of 16-bit random number generation
8-bit shift registers generate random numbers Circular combination
bit number Circular input combination S=2^n-1 Qn XOR Qm
8 Q2 ⊕ Q3 ⊕ Q4 ⊕ Q8 (The current program button guesses the number using 8 digits)
9 Q5 ⊕ Q9
10 Q7 ⊕ Q10 11 Q9
⊕ Q11
12 Q2 ⊕ Q10 ⊕ Q11 ⊕ Q12
13 Q1 ⊕ Q11 ⊕ Q12 ⊕ Q13
14 Q2 ⊕ Q12 ⊕ Q13 ⊕ Q14
15 Q14 ⊕ Q15
16 Q4 ⊕ Q13 ⊕ Q15 ⊕ Q16


In order to prevent users from predicting the change of random number corresponding to the pressing time when the LED array is turned on, the LED subtitles are displayed as advertising animation pictures, and the random number is changed so that the starting value of the random number cannot be predicted. There are four advertising animation pictures, each with 8 bits of data. See "org dpfstb";

When the button (PD1) is pressed, AVR generates a random number using 8-bit data, outputs the random number through PORTA and PORTC, and displays the fun and realistic button guessing number on the 8X8 LED.
.include "8515def.inc"
.def peed = r16
.def dspn = r17 ;Store the initial animation times.def
temp2 = r18
.def temp1 = r19
.def temp = r20
.def scndp = r21
.def cnt = r22
.def rdata = r23 ;Store the random seed number.def
rdata9 = r24
.equ dpfstb = 0x01e0 ;The first address of the size rectangle chart.equ
randtb = 0x0210 ;The first address of the random number seed table.equ
numbertb = 0x0240 ;The first address of the 0-9 digital table.org
$0000
rjmp RESET ;Reset Handle
.cseg
.org $0010
RESET: ldi peed, high (RAMEND) ;Set the stack $25F, see the device configuration file "8515def.inc"
out SPH,peed
ldi peed,low(RAMEND)
out SPL,peed
ldi peed,0xff ;initialize the port,
out ddra,peed ;set port A as outputout
ddrc,peed ;set port C as outputldi
peed,0xfd ;PD1 as input, and connect internal pull-up resistorout
ddrd,peed ;PD1 as input, the rest as outputldi
peed,0xff ;turn
off port Dout portd,peed
ldi peed,0x13 ;display screen timesstart
: ldi dspn,0x06 ;display initial animationldi
zh,high(dpfstb*2)
ldi zl,low(dpfstb*2)
dspfm: rcall ldtb8 ;call program area number to RAM
ldi temp2,0xa0 ;display animation screen timesdspfm1
: rcall scan1 ;call to fetch data from memory and display
oncesbis pind,01 ;The bit of I/O port is set to jump, detect PD1 is pressed or not
rjmp getseed ; detect PD1 is pressed and go to
dec temp2 ;-1
brne dspfm1 ;If not 0, go to
dec dspn ; Initial screen times -1
brne dspfm ;If not 0, go to
rjmp start ;Go to display initial animation
getseed:inc temp ;+`1, according to the time PD1 is pressed, select random number seed
sbis pind,01 ;The bit of I/O port is set to jump, detect PD1 is pressed or not
rjmp getseed ; detect PD1 is pressed and continue counting
andi temp,0x1f ;The button is released, get the random number seed and 0X0F plus
ldi zh,high(randtb*2)
ldi zl,low(randtb*2)
add zl,temp
lpm
MOV rdata,r0 ;Get the random number seed
next: ldi dspn,0x08 ;display 8 different random numbers;
repeet: rcall randm ;call the random number generating subroutine
rcall dspnumber ;call and display 8 different random numbers
dec dspn ;-1
brne repeet ;dspn is not 0, jump to
rcall randm ; call the random number generating subroutine
guess1: rcall dspnumber ;call and display the same random number until a key is pressed
sbic pind,01 ;release and then execute (I/O port clears and jumps)
rjmp guess1 ;switch to display the same random number until a key is pressed
wait: rcall dspnumber ;
sbis pind,01
rjmp wait ;wait for button to be pressed
ldi rdata9,0x03 ;display animation three times
start0: ldi dspn,0x06 ;display six pictures each time
ldi zh,high(dpfstb*2)
ldi zl,low(dpfstb*2)
dspfm0: rcall ldtb8 ;Call to fetch data from the program area pointed to by Z and send it to memory 0080-0087
ldi temp2,0xa0 ;Display times
dspfm1a:rcall scan1 ;Call to fetch data from memory 0080-0087 and display it once
dec temp2 ;-1
brne dspfm1a ;If not 0, go to
dec dspn ;Display initial animation times -1
brne dspfm0 ;If not 0, go to
dec rdata9 ;Display animation three times -1
brne start0 ;If not 0, go to
rjmp next ;Go to display 8 different random numbers
dspnumber: ;Display a subroutine for a number from 0 to 9
ldi zh,high(numbertb*2)
ldi zl,low(numbertb*2)
add zl,rdata9
rcall ldtb8 ;Get data
ldi temp2,0xa0 ;The number is displayed A0H times repeatedly
dspn1: rcall scan1
dec temp2
brne dspn1
ret
scan1: push xl ;Get data from memory 0080-0087 and display it once
ldi temp,0b01111111
MOV scndp,temp
ldi cnt,0x08
col1: out portc,scndp ;Display a column of the screen
ld r1,x+
out porta,r1
rcall delay
sec
ror scndp
dec cnt
brne col1
pop xl
ret
ldtb8: ldi xl,0x80 ;Get data from the program area pointed to by Z and send it to memory 0080-0087
ldi xh,0x00
ldi temp1,0x08
push xl
nexld1: lpm
st x+,r0
ld r0,z+
dec temp1
brne nexld1
pop xl
ret
delay: ;The general delay subroutine is omitted
....
randm: MOV temp,rdata ;Generate 8N (0≤N≤9) random number subroutine
MOV temp1,rdata
swap temp1
eor temp,temp1
rol temp1
eor temp,temp1
rol temp1
eor temp,temp1
rol temp
rol rdata
MOV rdata9,rdata
andi rdata9,0x0f
cpi rdata9,0x0a
brsh randm ;Generate a random number of 0≤RDATA9≤9
lsl rdata9
lsl rdata9
lsl rdata9
ret

.cseg
.org dpfstb; ;Small and large box font table
; small o
.db 0b00000000,0b00000000,0b00000000,0b00011000
.db 0b00011000,0b00000000,0b00000000,0b00000000
.db 0b00000000,0b00000000,0b00111100,0b00100100
.db 0b00100100,0b00111100,0b00000000,0b00000000
.db 0b00000000,0b01111110,0b01000010,0b01000010
.db 0b01000010,0b01000010,0b01111110,0b00000000
;big o
.db 0b11111111,0b10000001,0 b10000001,0b10000001
.db 0b10000001,0b10000001,0b10000001,0b11111111
.db 0b00000000,0b01111110,0b01000010,0b01000010
.db 0b01000010,0b01000010,0b01111110,0b00000000
.db 0b00000000,0b00000000,0b00111100,0b00100100
.db 0b00100100,0b00111100,0b00000000,0b00000000
.cseg
.org randtb ;随机数种子表
.db 0x5a,0x7b,0x5b,0x4f,0x66,0x6d,0x7d,0x07
.db 0x3b,0x8c,0x67,0x9a,0x99,0x7e,0x2d,0x3e
.db 0x5c,0x6d,0x5b,0x7e,0xf6,0xe7,0x4c,0xc8
.db 0x69,0x9c,0xe2,0x75,0x6c,0xd3,0xe8,0x9a
.cseg
.org numbertb ;0-9 digital glyph table
;0
.db 0b00111000,0b01000100,0b01000100,0b01000100
.db 0b01000100,0b01000100,0b01000100,0b00111000
;1
.db 0b00010000,0b00011000,0b00010000,0b00010000
.db 0b00010000,0b00010000,0b00010000,0b00111000
;2
.db 0b00011100,0b00100010,0b 00100000,0b00010000
.db 0b00001000,0b00000100,0b00000010,0b00111110
;3
.db 0b00111100,0b00010000,0b00001000,0b00010000
.db 0b00100000,0b00100000,0b00100010,0b00011100
;4
.db 0b00100000,0b00110000,0b00101000,0b00100100 .db 0b00100010,0b11111110,0b 00100000,0b00100000 ;5 .db 0b01111110,0b00000010,0b00111110,0b01000000 .db 0b01000000,0b01000000,0b01000010,0b00111100
; 6 .db 0b00110000,0b00001000,0b00000100,0b00111100 .db 0b01000100,0b01000100,0b01000100,0b00111000 ;7 .db 0b01111100,0b01000000,0b 00100000,0b00010000 .db 0b00001000,0b00001000,0b00001000,0b00001000 ;8 .db 0b00111000,0b01000100,0b01000100,0b00111000 .db 0b01000100,0b01000100,0b01000100,0b00111000 ;9 .db 0b00111000,0b01000100,0b01000100,0b01111000 .db 0b01000000,0b01000000,0b 01000100,0b00111000














Reference address:Generate random numbers using AVR microcontroller

Previous article:The overall design scheme of M2M terminal using ARM processor combined with GPRS module
Next article:Low-cost and high-precision digital constant current source based on AVR microcontroller PWM function

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号