The object of the code is to program a collision avoidance car that will only be going forward. This is to be accomplished by using the PWM to create two signals, one that goes to the left side of the car and one that goes to the right side of the car. These signals will slow the duty cycle of the side in question, causing the car to turn.
The way we know that the car needs to turn is by using 3 distance sensors that are shown in the add on document.
The problems that we are currently having with the code is a matter of scaling, getting the car to make decisions, and of a more immediate concern, the fact that while working with the code, all of a sudden the sensors stopped working and as far as we could tell the problem did not occur in the car or the equipment we used to attach the car to the computer.
We are using the MPLABX compiler, using the PIC18F87J11 micro controller, and are required to use assembly language to program this thing.
ANY HELP IS wonderful. The project is due Wednesday and we are getting frustrated.
Here is the code:
#ifdef __18F87J11
list p=18F87J11
#include p18F87J11.inc
CONFIG FOSC = HSPLL
CONFIG STVREN = OFF,XINST = OFF,WDTEN = OFF,CP0 = OFF,FCMEN = OFF,IESO = OFF
CONFIG MODE = MM
#else
Create error
#endif
STARTUP CODE 0
PROG1 CODE 0x18
;--------------------------------------------------------Memory-declarations
#define COUNT 0xFF
#define counter 0x0E
#define FRONTPINGH 0x10
#define FRONTPINGL 0x11
#define LEFTPINGH 0x00
#define LEFTPINGL 0x01
#define RIGHTPINGH 0x20
#define RIGHTPINGL 0x21
#define SCALEFACTORR 0x22
#define SCALEFACTORM 0x12
#define SCALEFACTORL 0x02
#define SCALEDHIGHL 0x03
#define SCALEDHIGHM 0x13
#define SCALEDHIGHR 0x23
#define SCALEDLOWL 0x04
#define SCALEDLOWM 0x14
#define SCALEDLOWR 0x24
#define RESULT 0x05
#define MEASUREH 0x06
#define MEASUREL 0x07
#define numerator 0x16
#define denominator 0x17
;--------------------------------------------------------Macros-------------
GETFRONTDIST MACRO
bsf PORTF,2 ;go high front trigger pin
movlw 0x05
movwf COUNT ;set number of times to run Countdown 5=10us
call Countdown
bcf PORTF,2 ;go low front trigger pin
frontpulse btfss PORTG,1 ;look for rising edge
bra frontpulse
bsf T0CON,7
endpulse btfsc PORTG,1 ;look for falling edge
bra endpulse
bcf T0CON,7
movff TMR0L, FRONTPINGL
movff TMR0H, FRONTPINGH ;Store timer count
movlw 0x00
movwf TMR0H
movwf TMR0L
ENDM
GETLEFTDIST MACRO
bsf PORTF, 3
movlw 0x05
movwf COUNT
call Countdown
bcf PORTF,3
leftpulse btfss PORTG,2
bra leftpulse
bsf T0CON,7
endleftpulse btfsc PORTG,2
bra endleftpulse
bcf T0CON,7
movff TMR0L, LEFTPINGL
movff TMR0H, LEFTPINGH
movlw 0x00
movwf TMR0H
movwf TMR0L
ENDM
GETRIGHTDIST MACRO
bsf PORTF,1
movlw 0x05
movwf COUNT
call Countdown
bcf PORTF,1
rightpulse btfss PORTG,0
bra rightpulse
bsf T0CON,7
endrightpulse btfsc PORTG,0
bra endrightpulse
bcf T0CON,7
movff TMR0L, RIGHTPINGL
movff TMR0H, RIGHTPINGH
movlw 0x00
movwf TMR0H
movwf TMR0L
ENDM
Start:
movlw 0x00 ;setup tris registers and tablepointer to 0
movwf TRISF
movwf TRISD
movwf TBLPTRU
movwf TBLPTRH
bsf TRISA, 0
bsf TRISA, 1
bsf TRISB, 0
movlw 0x02
movwf TBLPTRH ;using low byte for reference
movlw 0xFF
movwf TRISG
movlw 0x03
movwf T0CON ;set Timer0 to 16-bit, internal clock, 1:256 scaler
clrf TMR0H
clrf TMR0L
movlw 0xB5
movff WREG, SCALEFACTORR ; option to set dvalefactor
movff WREG, SCALEFACTORL
movlw 0xFF
movff WREG, SCALEFACTORM
movlw 0x04 ;0x55 for leds, 0x08 for temp pot
movff WREG, denominator
movlw 0xB4
movwf ADCON1
MainLoop:
clrf RESULT
GETFRONTDIST
GETLEFTDIST
GETRIGHTDIST
call Scaleright
call Scaleleft
call Scalefront
movff SCALEDHIGHL, WREG
subwf SCALEDHIGHR, 0
movwf RESULT
movff RESULT, numerator
bn sub
goto add
sub
movlw 0x03
movwf TBLPTRL
call Divide
movff counter, WREG
subwf TBLPTRL, 1
bra done
add
movlw 0x03
movwf TBLPTRL
call Divide
movff counter, WREG
addwf TBLPTRL, 1
done
;----------------------------------------------------------------AD Converters
btfss PORTB, 0
call PotCheck
btfsc PORTB, 0 ;finger heat scale index
call TempCheck
button
;----------------------------------------------------------------sensor LEDS
;; TBLRD*
; MOVFF TABLAT, PORTD
; movlw 0xff
; movff WREG, COUNT
; call Countdown
bra MainLoop
;call Endhard
Divide:
movff denominator, WREG
subwf numerator, 0
bn jump
movff WREG, numerator
incf counter
bra Divide
jump
return
PotCheck:
movlw 0x01
movff WREG, ADCON0
bcf PIR1, 6
bsf ADCON0, 1
here btfss PIR1, 6
bra here
movff ADRESL, WREG
movff WREG, MEASUREL
movff ADRESH, WREG
movff WREG, MEASUREH
BCF STATUS, C
rrcf MEASUREH
rrcf MEASUREL
BCF STATUS, C
rrcf MEASUREH
rrcf MEASUREL
movff MEASUREL, PORTD
return
TempCheck:
movlw 0x05
movff WREG, ADCON0
bcf PIR1, 6
bsf ADCON0, 1
there btfss PIR1, 6
bra there
movff ADRESH, WREG
movff WREG, MEASUREH
movff ADRESL, WREG
movff WREG, MEASUREL
movff MEASUREH, numerator
clrf counter
call Divide
movlw 0x08
movff WREG, TBLPTRL
movff counter, WREG
addwf TBLPTRL, 1
TBLRD*
movff TABLAT, WREG
mullw 0x02
movff PRODL, WREG
addlw 0x20
movff WREG, PORTD
return
Countdown:
nop
nop
decf COUNT
bnz Countdown
return
Scaleleft:
movff LEFTPINGL, WREG
mulwf SCALEFACTORL
movff PRODL, SCALEDLOWL
movff PRODH, SCALEDHIGHL
return
Scaleright:
movff RIGHTPINGL, WREG
mulwf SCALEFACTORR
movff PRODL, SCALEDLOWR
movff PRODH, SCALEDHIGHR
return
Scalefront:
movff FRONTPINGL, WREG
mulwf SCALEFACTORM
movff PRODL, SCALEDHIGHM
movff PRODH, SCALEDLOWM
return
;------------------------------------------------------------------------Tables
CODE 0x200
lED DB 0x80, 0x60, 0x20, 0x18, 0x04, 0x06, 0x01
temp DB 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
DB 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
DB 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
DB 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
DB 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
DB 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f
DB 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f
DB 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
DB 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f
Endhard:
End