1. bigsam

    Thread Starter New Member

    Jul 8, 2011
    5
    0
    hie pals, iam encountering a problem in coming up with an assembly code which calculate distance using pic16f84. I want to use two infra red sensors which are spaced within a known distance. the project i want to do is of checking the speed of vehicles at a particular point. Pliz help me with the code

    thanks in advance


    sama
     
  2. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    We need more details, like the schematic of the project and the current code you are running and what difficulties you are having...
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
  4. bigsam

    Thread Starter New Member

    Jul 8, 2011
    5
    0
    Here is my code
    BSF STATUS, 5
    MOVLW 0XFF
    MOVWF PORTA
    MOVLW 0X00
    MOVWF PORTB
    MOVLW 0X07
    MOVWF OPTION_R
    BCF STATUS, 5
    ;…………………………………………………………………………………………………………….
    START BTFSS PORTA,0
    GOTO START
    CALL DELAY
    ;…………………………………………………………………………………………………………….
    DELAY CLRF TMRO
    MOVF TMRO,W
    NUM SUBWF REGISTER1
    BTFSS STATUS, ZEROBIT
    GOTO NUM
    BSF PORTB,2
    GOTO START
    ;………………………………………………………………………………………………………………..
    ISR SUBWF REGISTER2
    BTFSS STATUS, ZEROBIT
    BSF PORTB, 2
    BSF PORTB,1
    GOTO START
    I have skipped the other sections of the program like the equate section e.t.c. like I said last time I want to use 2 infrared sensors do determine the speed of vehicles. Knowing the distance and the time taken to cover that distance , I can determine whether the vehicle is above limit or within the limit. From the code I will use a BTFSS instruction to test the first sensor, if it is set I call the delay and that’s where the problem is. The second sensor is supposed to generate an interrupt while the delay is being executed. Once the interrupt has been generated, I want the current value of the working register which I will use to compare with a pre-set value(known time to cover that distance) using SUBWF instruction and I will know what to display
    PORTB,2 shows the speed is within limit
    PORTB,1 shows the speed is not within limit
    Thanks in advance
    bigsam
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I think you need to get to know the simulator in MPLAB, it will help you find some of the problems. I'm putting a few comments on your code to get you started:


    BSF STATUS, 5 ;bank 1
    MOVLW 0XFF
    MOVWF PORTA ;Change to TRISA - works the same but clearer
    MOVLW 0X00
    MOVWF PORTB ;ditto
    MOVLW 0X07
    MOVWF OPTION_R
    BCF STATUS, 5 ;bank 0
    ;…………………………………………………………………………………………………………….
    START BTFSS PORTA,0
    GOTO START
    CALL DELAY ;call without return, this will cause problems

    ;Suggest GOTO START here if you put RETURN into DELAY

    ;…………………………………………………………………………………………………………….
    DELAY CLRF TMRO ;should this be cleared every time?
    ;Also TMR0 should be used, TMRO doesn't exist
    MOVF TMRO,W
    NUM SUBWF REGISTER1 ;use ,W if you want the result in W
    BTFSS STATUS, ZEROBIT
    GOTO NUM
    BSF PORTB,2
    GOTO START
    ;………………………………………………………………………………………………………………..
    ISR SUBWF REGISTER2
    BTFSS STATUS, ZEROBIT
    BSF PORTB, 2
    BSF PORTB,1 ;this always gets executed
    GOTO START
     
  6. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Hey, bigsam, Over the years I've become a stronger and stronger believer in PDLs and Flowcharts.

    A PDL for your project might look like:

    ; LOOP
    ; Clear the counter
    ; Wait for first sensor to trip
    ; Start the counter
    ; Wait for second sensor to trip
    ; Stop the counter
    ; Set outputs depending on counter value
    ; ENDLOOP

    Now, how the counter works depends on the micro. You may have a built-in counter that's big enough to hold the counts. Or, you may need to have a timer & interrupt running and increment a software counter with every interrupt.

    And remember, the sensor inputs may need to be debounced. And you'll need to figure out what happens if the vehicle stops between the sensors (counter overflow), or if some sunlight gets reflected into the sensor, etc. And in the real world, things like dirt, dust, mud, rain, ice, and deep snow can affect your readings.

    Finally, that call to delay can be removed. Just drop through because "delay" jumps to start, anyway.

    Good Luck,

    --Rich
     
  7. bigsam

    Thread Starter New Member

    Jul 8, 2011
    5
    0
    hey Rich

    thanks for the help..can yu post me an assembly code of a timer which times for exactly 5sec and of the counter too

    regards

    bigsam
     
  8. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
  9. bigsam

    Thread Starter New Member

    Jul 8, 2011
    5
    0
    hey RiJoRi
    thanks for the PDL, that is exactly what i want my project to do but the problem is the assembly code. after the second sensor has triped i would like to compare its value with a preset value so that i know if the vehicle is within the limit or not. can u help with that code.i need a counter which is equivalent to 5sec and iam using a pic16f872

    regards

    bigsam
     
  10. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    I am not very conversant with the PIC instruction set; but, looking at the 16C71x IS, you'll need to work around the fact they do not have a compare instruction. The way to do it is to subtract, and test the STATUS bits:

    Code ( (Unknown Language)):
    1.  
    2.     MOVLW  TheDesiredNumber
    3.     SUBWF   CounterFile
    4. ; Test status bits
    5.  
    If the carry bit is set, CounterFile < TheDesiredNumber
    If the zero bit is set, CounterFile = TheDesiredNumber
    If neither is set, CounterFile > TheDesiredNumber

    Test this code out! It's been a while since I had to do this with an 8051, and I am trusting the macro in the 16Cxx notes is correct. Actually, you should test all code, whether you wrote it or someone else did.

    --Rich
     
Loading...