1. 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.
    Dismiss Notice

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

Discussion in '8051/8951' started by Greital, Apr 19, 2016.

  1. Greital

    Greital New Member

    Joined:
    Mar 8, 2016
    Messages:
    10
    Likes:
    0
    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 (text):

        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 :(
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    So, use a counter from 0 to 89 and then add 10.

    Mike.
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    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!!
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Greital

    Greital New Member

    Joined:
    Mar 8, 2016
    Messages:
    10
    Likes:
    0

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

    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

    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
     

Share This Page