Problem debugging PIC18F87J11 micro controller in assembly  - Page 1
Forum section: electronics / general

author: m.jo
author_rank: Newbie
timestamp: December 06, 2014, 05:28:04 am
content: 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 __18F87J11list p=18F87J11#include p18F87J11.incCONFIG FOSC = HSPLLCONFIG STVREN = OFF,XINST = OFF,WDTEN = OFF,CP0 = OFF,FCMEN = OFF,IESO = OFFCONFIG MODE = MM#elseCreate error#endifSTARTUP CODE 0PROG1 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 MACRObsf PORTF,2 ;go high front trigger pinmovlw 0x05movwf COUNT ;set number of times to run Countdown 5=10uscall Countdownbcf PORTF,2 ;go low front trigger pinfrontpulse btfss PORTG,1 ;look for rising edgebra frontpulsebsf T0CON,7endpulse btfsc PORTG,1 ;look for falling edgebra endpulsebcf T0CON,7movff TMR0L, FRONTPINGLmovff TMR0H, FRONTPINGH ;Store timer countmovlw 0x00movwf TMR0Hmovwf TMR0LENDMGETLEFTDIST MACRObsf PORTF, 3movlw 0x05movwf COUNTcall Countdownbcf PORTF,3leftpulse btfss PORTG,2bra leftpulsebsf T0CON,7endleftpulse btfsc PORTG,2bra endleftpulsebcf T0CON,7movff TMR0L, LEFTPINGLmovff TMR0H, LEFTPINGHmovlw 0x00movwf TMR0Hmovwf TMR0LENDMGETRIGHTDIST MACRObsf PORTF,1movlw 0x05movwf COUNTcall Countdownbcf PORTF,1rightpulse btfss PORTG,0bra rightpulsebsf T0CON,7endrightpulse btfsc PORTG,0bra endrightpulsebcf T0CON,7movff TMR0L, RIGHTPINGLmovff TMR0H, RIGHTPINGHmovlw 0x00movwf TMR0Hmovwf TMR0LENDMStart:movlw 0x00 ;setup tris registers and tablepointer to 0movwf TRISFmovwf TRISDmovwf TBLPTRUmovwf TBLPTRHbsf TRISA, 0bsf TRISA, 1bsf TRISB, 0movlw 0x02movwf TBLPTRH ;using low byte for referencemovlw 0xFFmovwf TRISGmovlw 0x03movwf T0CON ;set Timer0 to 16-bit, internal clock, 1:256 scalerclrf TMR0Hclrf TMR0Lmovlw 0xB5movff WREG, SCALEFACTORR ; option to set dvalefactormovff WREG, SCALEFACTORLmovlw 0xFFmovff WREG, SCALEFACTORMmovlw 0x04 ;0x55 for leds, 0x08 for temp potmovff WREG, denominatormovlw 0xB4movwf ADCON1MainLoop:clrf RESULTGETFRONTDISTGETLEFTDISTGETRIGHTDISTcall Scalerightcall Scaleleftcall Scalefrontmovff SCALEDHIGHL, WREGsubwf SCALEDHIGHR, 0movwf RESULTmovff RESULT, numeratorbn subgoto addsubmovlw 0x03movwf TBLPTRLcall Dividemovff counter, WREGsubwf TBLPTRL, 1bra doneaddmovlw 0x03movwf TBLPTRLcall Dividemovff counter, WREGaddwf TBLPTRL, 1done;----------------------------------------------------------------AD Convertersbtfss PORTB, 0call PotCheckbtfsc PORTB, 0 ;finger heat scale indexcall TempCheckbutton;----------------------------------------------------------------sensor LEDS;; TBLRD*; MOVFF TABLAT, PORTD; movlw 0xff; movff WREG, COUNT; call Countdownbra MainLoop;call EndhardDivide:movff denominator, WREGsubwf numerator, 0bn jumpmovff WREG, numeratorincf counterbra DividejumpreturnPotCheck:movlw 0x01movff WREG, ADCON0bcf PIR1, 6bsf ADCON0, 1here btfss PIR1, 6bra heremovff ADRESL, WREGmovff WREG, MEASURELmovff ADRESH, WREGmovff WREG, MEASUREHBCF STATUS, Crrcf MEASUREHrrcf MEASURELBCF STATUS, Crrcf MEASUREHrrcf MEASURELmovff MEASUREL, PORTDreturnTempCheck:movlw 0x05movff WREG, ADCON0bcf PIR1, 6bsf ADCON0, 1there btfss PIR1, 6bra theremovff ADRESH, WREGmovff WREG, MEASUREHmovff ADRESL, WREGmovff WREG, MEASURELmovff MEASUREH, numeratorclrf countercall Dividemovlw 0x08movff WREG, TBLPTRLmovff counter, WREGaddwf TBLPTRL, 1TBLRD*movff TABLAT, WREGmullw 0x02movff PRODL, WREGaddlw 0x20movff WREG, PORTDreturnCountdown:nopnopdecf COUNTbnz CountdownreturnScaleleft:movff LEFTPINGL, WREGmulwf SCALEFACTORLmovff PRODL, SCALEDLOWLmovff PRODH, SCALEDHIGHLreturnScaleright:movff RIGHTPINGL, WREGmulwf SCALEFACTORRmovff PRODL, SCALEDLOWRmovff PRODH, SCALEDHIGHRreturnScalefront:movff FRONTPINGL, WREGmulwf SCALEFACTORMmovff PRODL, SCALEDHIGHMmovff PRODH, SCALEDLOWMreturn;------------------------------------------------------------------------TablesCODE 0x200lED DB 0x80, 0x60, 0x20, 0x18, 0x04, 0x06, 0x01temp DB 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0fDB 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1fDB 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2fDB 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3fDB 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4fDB 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5fDB 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6fDB 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7fDB 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8fEndhard:End

author: westfw
author_rank: Super Contributor
timestamp: December 06, 2014, 06:29:53 am
content: Needs more comments.

author: dannyf
author_rank: Super Contributor
timestamp: December 06, 2014, 12:16:40 pm
content: If I were you, I would break down the code in to a few modules: two pwm generators for your left/right motors, three (potentially one if they are the same type of sensors) to read the sensors, and one to fuse the data together and calculate / update the pwm generators.Assembly is useful if you are working on really simple things (blinking an led), or you are pushing the envelope of the mcu; aka you are a beginner or you are exceptionally gifted.Anything in between, C wins.