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 omitted
....
Principle diagram of 16-bit random number generation
8-bit shift register generates random number cyclic combination
bit number cyclic 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 16-bit random number subroutine
mov temp,udata ;Generate 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 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 to
7.3.8 Button guessing program
source code: SLAVR738.ASM
In many occasions, such as button guessing (computer lottery, computer selecting lucky numbers), the game start button waits for an irregular and unordered data to be generated, that is, a random number generator is required. The seed of the random number is set by the program (it can also be set by an external switch). After the seed is started, the shift register returns to the cycle with 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 register generates random number cyclic 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
8X8 LED array, in order to prevent users from predicting the change value of the random number corresponding to the pressing time when the machine is turned on, the LED subtitles are displayed with advertising animation screens, and the random number is changed so that the starting value of the random number cannot be predicted. There are four advertising animation screens, each with 8 bits of data. See "org dpfstb";
by pressing the button (PD1), AVR generates a random number with 8 bits of data, and outputs the random number from PORTA and PORTC, and displays the fun and real button guessing on the 8X8 LED.
.include "8515def.inc"
.def peed = r16
.def DSP n = 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 output
out ddrc,peed ;set port C as output
ldi peed,0xfd ;PD1 as input, and connect internal pull-up resistor
out ddrd,peed ;PD1 as input, the rest as output
ldi peed,0xff ;turn off port D
out portd,peed
ldi peed,0x13 ;display screen times
start: ldi dspn,0x06 ;display initial animation
ldi zh,high(dpfstb*2)
ldi zl,low(dpfstb*2)
dsp FM : rcall ldtb8 ;call program area number and send it to RAM
ldi temp2,0xa0 ;display animation screen times
dspfm1: rcall s CAN 1 ;Call to get data from memory and display it once
sbis PIN d,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 ; Number of initial screens -1
brne dspfm ; If not 0, go to
rjmp start ;Go to display initial animation
getseed:inc temp ;+`1, select random number seed according to the time PD1 is pressed
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 add 0X0F
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 generation
subrcall dspnumber ;Call and display 8 different random numbers
dec dspn ;-1
brne repeet ;dspn is not 0
rcall randm ; call the random number generating subroutine
guess1: rcall dspnumber ; call and display the same random number until a key is pressed
sb IC 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 the button to be pressed
ldi rdata9,0x03 ; display the 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 get data from the program area pointed to by Z and send it to the 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 ;Show the initial animation times -1
brne dspfm0 ;If not 0, go to
dec rdata9 ;Show animation three times -1
brne start0 ;If not 0, go to
rjmp next ;Go to display 8 different random numbers
dspnumber: ;Display a subroutine with 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 repeatedly displayed A0H times
dspn1: rcall scan1
dec temp2
brne dspn1
ret
scan1: push xl ;Fetch 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 them 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: ;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 0≤RDATA9≤9
lsl rdata9
lsl rdata9
lsl rdata9
ret
.cseg
.org dpfstb; ;Size and size 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,0b01
111110,0b00000000 ;big o
.db 0b11111111,0b10000001,0b10000001,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数字字形表
;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,0b00100 010,0b00100000,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,0b1111111 0,0b00100000,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,0b0100000 0,0b00100000,0b00010000 .db 0b00001000,0b00001000,0b00001000,0b00001000 ;8 .db 0b00111000,0b01000100,0b01000100,0b00111000 .d b 0b01000100,0b01000100,0b01000100,0b00111000 ;9 .db 0b00111000,0b01000100,0b01000100,0b01111000
.db 0b01000000,0b01000000,0b01000100,0b00111000
Previous article:HT1621 Driver - AVR
Next article:How to set up the C compiler in AVRStudio
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [SAMR21 New Gameplay] 12. Driving OLED
- [NXP Rapid IoT Review] Adding USB Serial Port Simulation Function to Rapid
- Detailed explanation of five classic power supply circuits
- MSP430 MCU Development Record (8)
- Newbie help! Wireless remote control LED constant current drive circuit. Which part of the receiving circuit is the mixing circuit?
- Image recognition system based on STM32H745
- Brief analysis of the working principle of DSP
- When writing Linux drivers, where can I find the prototypes or usage instructions of these API functions?
- [Power amplifier case] Application of power amplifier in giant magnetostrictive transducer hole crack defect detection
- TI's GaN technology brings half the size and double the power to machines