, 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
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
- 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
- EEWORLD University Hall----Application of TI Jacinto series products in ADAS
- EEWORLD University ---- ADC and DAC
- How does the STM32 interrupt behavior jump to the EXTI9_5_IRQHandler function?
- An Engineer's Guide to EMI - Noise Propagation and Filtering
- Detailed explanation of Bluetooth GattCallback usage and BLE data transmission
- Why are most PCBs green? Are black PCBs more advanced?
- Zero-knowledge ESP8266 tutorial: SmartConfig one-click network configuration
- Op amp power supply
- EEWORLD University ----TI EV/HEV 48V and Motor Drive Solutions
- Can RTthread be ported to nrf51822?