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