Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help with :::Generate a random number using interrupt:::

Status
Not open for further replies.

Greital

New Member
hi

still working on my 8051 project using assembly language, and now I'm in phase 4 --->>>> the last phase ;)

in this phase we should generate 2 random numbers as following:
1. one digit (1-9)
2. two digit (10-99)
and by using push button we pick randomly the number

we have done one digit perfectly, but we face a problem with the two digit :(

we used a counter with 99 numbers and decrements it each time until the user push a button to interrupt the function and show the number randomly
but the problem that it shows the one digit also :facepalm:

we tried to make the counter to start from 10 and increment it each time until we reach 99 --->>> but also it didn't work :sorry:

we tried and tried and tried :arghh: but nothing work

the code :

Code:
    ORG 0H
    LJMP MAIN
    ORG 0003H ; EX0 interrupt vector address
    LJMP EX0ISR
    ORG 0030H
MAIN:
    MOV IE,#10000001B ; Enable EX0 interrupts
    SETB IT0 ;Make EX0 edge-triggered interrupt
    LCALL INLCD ; Initialize LCD
LABEL:
    MOV    R4,#99
LOOP:
    DJNZ    R4,LOOP
    SJMP LABEL
;---------------------------------------

EX0ISR:
   
    MOV    A,R4
    CJNE    R4,#09,GENERATE
COME_HERE:
    DJNZ    R4,COME_HERE
    SJMP    OUT
       
GENERATE:
    MOV    44H,A
    MOV A,#1000B ; Move cursor to 1st line - send high nibble
    LCALL CMD
    MOV A,#0000B ; send low nibble
    LCALL CMD
    LCALL LDELAY ; 4.1 msec required for this command
    MOV    A,44H
    MOV    B,#10
    DIV    AB
    MOV    36H,B
    ADD A,#30H
    LCALL WCHR
    MOV    A,36H
    ADD    A,#30H
    LCALL WCHR
OUT:
    RETI

please help :(
 
Two things....

First off... Why does CMD take a nibble argument whereas WCHR takes a byte ( using my own LCD routines it works okay )

Second... You test for zero... R4 will always contain a number between 0~99.... Use 0~89 ( as Pommie so rightly said!!) then add 10 in the interrupt!!
 
So, use a counter from 0 to 89 and then add 10.

Mike.

didn't think about it :woot: will try it, hope it will work, Thanks :happy:

Two things....
First off... Why does CMD take a nibble argument whereas WCHR takes a byte ( using my own LCD routines it works okay )

the instructor teach us to make it this way ----->>>> the CMD and WCHR are given to us and they forbid us to change a thing on it

Second... You test for zero... R4 will always contain a number between 0~99.... Use 0~89 ( as Pommie so rightly said!!) then add 10 in the interrupt!!

ya that's why we think later to decrement the 99 by 1 inside a loop and gets out when it reach 9 but it shows numbers less than 9 also, and we didn't think it will be efficient to to compare for all the number from 9 to 0 it will consume time and will be long ------>>> our project is already too long :eek: without this phase

so will try Pommie's way ;) and hope it will work
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top