1
votes

I am trying to mess around with some electronics, and like it so far! Got a PICKit 2 with a PIC16f1824 to code on. And i got myself a breadboard, with some leds, wires and a ultrasonic sensor. The sensor has 2 pins, 1 for trigger, and 1 for echo. This is my assembly code, but it does not work. The red led does not light up, at all :S

The breadboard is working and all is connected correctly, as far as i can tell. I have tested with simply turning on and off leds.

#include 

CBLOCK 0x70 ; DEFINE VARIABLES USED
d1
d2
d3
d4
Count
ENDC

ORG 0x00     ;RESET VECTOR

BANKSEL TRISA
movlw b'00000010' ;Setting all but RA1 to output, RA1 is input
movwf TRISA

MAINLOOP
CALL TRIG ;short burst to trigger pin
GOTO COUNT ;cout untill a signal is received
MLOOP
GOTO COUNTCHECK ;RA1, or echo, receives a signal, check how far
GOTO MAINLOOP

TRIG
CLRF Count
BANKSEL LATA
BSF LATA,0 ;trigger signal on
CALL Delay
BCF LATA,0 ;trigger signal off
RETURN

COUNT
INCF Count,1 ;increase by 1
CALL Delay ;delay 0.001 seconds
BANKSEL PORTA
BTFSS PORTA,1 ;checking if RA1 has recevied anything
GOTO COUNT ;loops
GOTO MLOOP ;RA1 received, go to main loop middle

COUNTCHECK
movlw d'6' ;gives w a value of 6
;movf Count,w ;
subwf Count,w ;count - w
BTFSC STATUS,C ;cheking c, is anything borrowed from w(nagative result) C is 0, nothing borrowed C is 1
;if result is 0, Z is 1(set), negative or positive is 0(clear)
GOTO REDLED ;turn on red led, Count is less than w(6) which means the obstacle is 1 meter away
GOTO GREENLED ;Count is larger than w(6), took more than 0.6 seconds before echo return, which means more than 1 meter away

REDLED
BANKSEL LATA
BCF LATA,3 ;turn off red led
BSF LATA,2 ;turn on green led
GOTO MAINLOOP ;go back to main loop, new trigger

GREENLED
BANKSEL LATA
BCF LATA,2 ;turn off green led
BSF LATA,3 ;turn on red led
GOTO MAINLOOP ;go back to main loop, new trigger

Delay       ;1 millisecond, 0.001 seconds
            ;993 cycles
    movlw   0xC6
    movwf   d1
    movlw   0x01
    movwf   d2
Delay_0
    decfsz  d1, f
    goto    $+2
    decfsz  d2, f
    goto    Delay_0

            ;3 cycles
    goto    $+1
    nop

            ;4 cycles (including call)
    return

END
2

2 Answers

0
votes

I suggest you to start with modifiing the example codes. Here is the link: http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=dm164130-9 There is a link "PICkit 3 Starter Kit Source Code" with a lot of examples in assembler and C.

0
votes

I'm fairly new to PICs so I don't know if my help is going to be useful.

I thought that the command BTFSC, Status,C meant that the next command, GOTO REDLED, would be skipped if STATUS, C is 0 - i.e. if the result of Count-w is positive. However, there are a couple of things that strike me. Count will be positive if it is 7 or larger (ignoring the 0 condition), but this will be 7 or larger if the total delay is more than 0.007 seconds as your delay is a 0.001 second delay. Not 0.6 seconds as per your comments. That has an effect on what your expected proximity threshold is.

GOTO REDLED will only be called if the result of Count-w is negative, which requires Count to be no more than 5 (0.005 seconds delay).

Also, your comment next to the command GOTO REDLED suggests that this turns on the Red LED, but the subroutine REDLED appears to turn OFF the Red LED.

I don't know whether using the Watch windows and Special Function register window to debug this would help as I haven't played with stepping through a real-time port monitoring routine before.

However, I think it's an attractive piece of code and it was fun to go through. Sorry if I can't give a definite answer.