This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

compare voltage

Hi,

my program:

EP: MOV A,ADC0H
CJNZ A,#175D,EP

i have problem jumping to the next subrountine

when i detect 1.75V from power supply
it will start to display
if not it will display 0V

any advice?

  • Long time since I had to care abot '51 assembly but:

    CJNZ = Compare Jump Not Zero

    You are comparing if the voltage is exactly 1.75V or not. Are you sure you want to catch exactly 1.75V, and not want to check for voltages above or below? AD converters are not exact. They always measure with a tolerance and with jitter from noise.

    Also, if you want to jump to another routine, don't you think your code should specify a jump to that other routine? Where do you think your code will jump?

  • i just change to 4BH
    and i am able to detect 0.75V
    but i still not sure what hexer for 1.75V

  • i am sorry to mislead my question, haha
    i do not wan to display voltage lesser than 1.75 but
    above,

    after my program it will just move on to my next subrountine itself,

    something like this

    MOV A,ADC0H
    EP:CJNE A,#4BH,EP

    ;============
    ;Print Result
    ;============
    CO:MOV A,#'C' LCALL DISPLAY MOV A,#'O' LCALL DISPLAY

    correct me if i am wrong.

  • oh ya sorry i am using CJNE not CJNZ

  • Please don't retype your code. Use copy and paste.

    Note that your first code specified a jump label on the line reading out the analog value.

    Your second post instead specifies the jump label on the same line as the jump instruction. Do you really want an infinite loop?

  • i do not wan to display voltage lesser than 1.75 but
    above,

    Do you think it matters if you use CJNE or CJNZ if you want to display above or below 1.75V?

    Don't you think you should compare if a value is above or below your target value instead of checking if it is exactly the target value or not?

    How will

    while (ADC_VALUE == 1.75V) ;
    

    or

    while (ADC_VALUE != 1.75V) ;
    

    help you?

    And by the way - even if you used the correct assembler instructions, do you really want an infinite loop until the voltage is correct. The application hanging without being able to do anything else...

  • hmm, i want to show value above 1.75v
    and show 0.00v when it is lower than 1.75v

    im not sure what to use for assembly language, i cant find anyways to do it.

    any advice.

  • can i do it this way?

    ;============
    ;Print Result
    ;============
    ZERO:                   MOV A,#'C'
                                            LCALL DISPLAY
                                            MOV A,#'O'
                                            LCALL DISPLAY
                                            MOV A,#' '
                                            LCALL DISPLAY
                                            MOV A,#'L'
                                            LCALL DISPLAY
                                            MOV A,#'E'
                                            LCALL DISPLAY
                                            MOV A,#'V'
                                            LCALL DISPLAY
                                            MOV A,#'E'
                                            LCALL DISPLAY
                                            MOV A,#'L'
                                            LCALL DISPLAY
    
                                            LCALL ROW2
    
                                            MOV A,#':'
                                            LCALL DISPLAY
                                            MOV A,#'0'
                                            LCALL DISPLAY
                                            MOV A,#'0'
                                            LCALL DISPLAY
                                            MOV A,#'0'
                                            LCALL DISPLAY
                                            MOV A,#' '
                                            LCALL DISPLAY
                                            MOV A,#'P'
                                            LCALL DISPLAY
                                      MOV A,#'P'
                                      LCALL DISPLAY
                                            MOV A,#'M'
                                            LCALL DISPLAY
    
                                            MOV A,ADC0H
    EP:                             CJNE A,#4BH,EP
    
    ;============
    ;Print Result
    ;============
    CO:                             MOV A,#'C'
                                      LCALL DISPLAY
                                            MOV A,#'O'
                                            LCALL DISPLAY
                                            MOV A,#' '
                                            LCALL DISPLAY
                                            MOV A,#'L'
                                            LCALL DISPLAY
                                            MOV A,#'E'
                                            LCALL DISPLAY
                                            MOV A,#'V'
                                            LCALL DISPLAY
                                            MOV A,#'E'
                                            LCALL DISPLAY
                                            MOV A,#'L'
                                            LCALL DISPLAY
    
                                            LCALL ROW2
    
                                            MOV A,#':'
                                            LCALL DISPLAY
                                            MOV A,62H
                                            LCALL DISPLAY
                                            MOV A,61H
                                            LCALL DISPLAY
                                            MOV A,60H
                                            LCALL DISPLAY
                                            MOV A,#' '
                                            LCALL DISPLAY
                                            MOV A,#'P'
                                            LCALL DISPLAY
                                      MOV A,#'P'
                                      LCALL DISPLAY
                                            MOV A,#'M'
                                            LCALL DISPLAY
    
                                            MOV A,ADC0H
    POLL2:          CJNE A,00H,ZERO
    

  • My guess is that this is a school project.

    Are you required to use assembler?

    It is quite obvious that you are not comfortable with assembler and I feel that it will take a significant time for you to learn it. Assembler isn't the easiest to learn.

    I have pointed out the problem with infinite loops in earlier posts. If you do a compare while waiting for something, the loop must also contain the sampling of new values, or the software will always lock up.

    Comparing for higher or lower can be implemented by comparing the retrieved value with a constant. Then you can use a conditional jump depending on the affected flags - and the processor (all processors) have more flags than just the Z (zero) flag. Look for borrow/carry/sign flags.

    Also, whatever language you use, you should try to create submodules/functions to help you out. You have a routine called 'DISPLAY'. Maybe it should be called 'DISPLAY_CHAR' since it only emits a single character. I would recommend you to also create a 'DISPLAY_STR' routine, to avoid the long MOV A,#xx/DISPLAY sequences.

    Why do your code have a line:

    POLL2:          CJNE A,00H,ZERO
    

    Compare the retrieved ADC value, and jump to a routine ZERO if the ADC value was NOT zero. Does that make sense? And what use is it to compare with the value 0 if your code is expected to treat all values below 1.75V as zero?

    Why do you have several places in the code that reads the ADC value? Shouldn't you just do it at a single place, and then decide what display code to jump to. After displaying anything, returning back to the ADC read code again to get a new sample?

    What do you think the following line does (already mentioned in a previous post):

    EP:                             CJNE A,#4BH,EP
    

    Depending on the value in A, the processor can take the jump, or it can continue to the next instruction. If it does take the jump, where will it jump? How many times will it continue to take this jump? Will it ever break this loop?

    Hint: If you really, really, really have to turn in this assignment using assembler, do yourself a big favor. Write the code in C. Let the compiler generate the code. Look at the generated code, and try to understand how - and why - the compiler handles the different if/while situations. However, do not try to copy/paste the assembler output from the C compiler into your project - any decent teacher should spot the difference between hand-written and compiler-generated assembler code.

  • If you really, really, really have to turn in this assignment using assembler, do yourself a big favor. Write the code in C ...
    even better, and also needxed if you follow Pers suggestion, read "the bible" chapter 2. It is clear from your posts that just reading CJNE in this paper would have eliminated this whole thread

    Erik

    here are the links to "the bible"
    Chapter 1 - 80C51 Family Architecture:
    www.nxp.com/.../80C51_FAM_ARCH_1.pdf

    Chapter 2 - 80C51 Family Programmer’s Guide and Instruction Set:
    www.nxp.com/.../80C51_FAM_PROG_GUIDE_1.pdf

    Chapter 3 - 80C51 Family Hardware Description:
    www.nxp.com/.../80C51_FAM_HARDWARE_1.pdf