??? 10/18/05 12:29 Read: times |
#102550 - tablebot |
http://www.geocities.com/adempha/CIMG0732.JPG
I messed around with this last night. Its the 8051 equivalent of the basic stamp II board I used to use. This thing basically has 2 IR sensors on the front left and right. Originally, I used a routine in the BS2 to sample each IR sensor 16 times and average them to avoid IR noise; I wrote this realizing the 8051 is much faster than the BS2, so it actually is sampling 255 times. My question is this : Since I'm still in the ASM basics, looking through my book I didn't see anything that could be considered the equivalent of an "if X=> then" statement. I noticed the CJNE statement is a condition jump, but (correct me if I'm wrong on this) it doesn't allow for an equal or greater than condition, only an equal condition. I thought about it for awhile and made a loop with DJNZ; and it seems to work as well but to me it seems messy and "engineered". Any thoughts on cleaning this up, or perhaps finding a more elegant approach? (Specifically, to the part about checking the values of R3/R4 to see if they are greater than 128, which indicates a "half-on" time for the IR sensor.) This is the code I wrote : ; Sound addresses : P2, #53 - Belch; P2, #63 - Doh!; P2, #0 - Blank-avoid at all costs. ; The ISD chip has a defect which can erase beginning locations of chip memory. ; R0,R1,R2 reserved for time delays ; R3, R4 reserved for IR sensor routines; R5 for checking sensor values SOUNDCLK EQU P0.0 ; Clock pin to the ISD chip, setting this LOW plays sound on P2 Address MOTORPWR EQU P1.5 ; Setting this pin HIGH turns both motors on, reverse by default LEFTMTR EQU P1.3 ; Setting this HIGH causes H bridge to make left motor go foreward RIGHTMTR EQU P1.4 ; Setting this HIGH causes H bridge to make right motor go foreward ; (To make robot go foreward, all three lines must be set HIGH) LeftIRLine EQU P1.6 RightIRLine EQU P1.7 MOV P0, #255 ; Set P0 high to turn off LEDs on P0, as well as set SOUNDCLK high. MOV P1, #0 ; Set P1 low, P1 is motor control / IR sensors MOV P2, #0 ; Set P2 low, ISD address &00h. MOV P3, #0 ; Set P3 low, not used at this time. Begin: SETB MOTORPWR ; These three lines make robot go foreward by energizing all three SETB LEFTMTR ; motor relays, power and two H bridge relays, one for each wheel. SETB RIGHTMTR MOV A, #0 ; Set A to #0 MOV R3, #255 ; Set R3 to 255, the number of times to scan the Left IR Sensor LeftIRScan: LCALL Shortdelay ; Slow this routine down. IR runs at 40Khz, not 8Mhz. JNB LeftIRLine, Addleft ; If LeftIRLine is low (its active low), jump and add #1 to the ACC, DJNZ R3, LeftIRScan ; If not, rescan. SJMP LeftFinished Addleft: ADD A, #1 DEC R3 SJMP LeftIRScan LeftFinished: MOV R3, A ; Store final value of A in R3 MOV A, #0 ; Set A to #0 MOV R4, #255 ; Set R4 to 255, the number of times to scan the Right IR Sensor RightIRScan: LCALL Shortdelay ; Slow this routine down. IR runs at 40Khz, not 8Mhz. JNB RightIRLine, AddRight ; If RightIRLine is low (its active low), jump and add #1 to the ACC, DJNZ R4, RightIRScan ; If not, rescan. SJMP RightFinished AddRight: ADD A, #1 DEC R4 SJMP RightIRScan RightFinished: MOV R4, A ; Store final value of A in R4 ; At this point, we should have R3 and R4 set based on the sensor inputs. ; Had no sensor input been detected (that is, sensors remained high), the ; values of R3 and R4 should be 0. The closer an object was, the higher ; these values will be. MOV R5, #125 ; We're gonna see if either value is higher than 125 by adding to them. CheckIRLoop: ; After 125 loops, each loop adding #1, if neither hits a value 255, they INC R3 ; were under 125 and should not be considered a trigger. INC R4 CJNE R3, #255, Trigger1 ; If R3 ever reaches 255 here, it was originally higher than 125. CJNE R4, #255, Trigger2 ; If R4 ever reaches 255 here, it was originally higher than 125. DJNZ R5, CheckIRLoop LJMP Begin Shortdelay: MOV R0, #25 Loop: DJNZ R0, Loop RET Longdelay: MOV R0, #250 MOV R1, #250 MOV R2, #250 Loop2: DJNZ R0, Loop2 DJNZ R1, Loop2 DJNZ R2, Loop2 RET Trigger1: ; We've got to change direction cause we're gonna run into something. MOV P2, #63 ; P2 ISD address for DOH LCALL Shortdelay ; Give the ISD chip some type to see the address before we clock it. CLR LEFTMTR ; Turn off one relay, so this wheel will reverse, and turn the robot. CLR SOUNDCLK ; Make it say DOH while it turns :) LCALL Longdelay ; Wait SETB SOUNDCLK SETB LEFTMTR ; Go straight again. MOV R3, #0 ; Set R3 to 0; we don't want it to go back into the check loop and trigger RET ; each cycle. That would take forever, and royally suck. Trigger2: ; We've got to change direction cause we're gonna run into something. MOV P2, #63 ; P2 ISD address for DOH LCALL Shortdelay ; Give the ISD chip some type to see the address before we clock it. CLR RIGHTMTR ; Turn off one relay, so this wheel will reverse, and turn the robot. CLR SOUNDCLK ; Make it say DOH while it turns :) LCALL Longdelay ; Wait SETB SOUNDCLK SETB RIGHTMTR ; Go straight again. MOV R3, #0 ; Set R3 to 0; we don't want it to go back into the check loop and trigger RET ; each cycle. That would take forever, and royally suck. END |
Topic | Author | Date |
tablebot | 01/01/70 00:00 | |
RTFM | 01/01/70 00:00 | |
from the tutorial | 01/01/70 00:00 | |
thank | 01/01/70 00:00 | |
the tutorial explanation is incomplete | 01/01/70 00:00 | |
Only the Bible? | 01/01/70 00:00 | |
OT: Maxwell's equations | 01/01/70 00:00 | |
OT: Life | 01/01/70 00:00 | |
thanks for the input all, but krap! | 01/01/70 00:00 | |
no acc load needs | 01/01/70 00:00 | |
of course | 01/01/70 00:00 | |
revised : | 01/01/70 00:00 | |
comments | 01/01/70 00:00 | |
Only the bible? | 01/01/70 00:00 | |
bible? | 01/01/70 00:00 | |
The point of "the bible" is not that any | 01/01/70 00:00 | |
Daunting? | 01/01/70 00:00 | |
pos comp | 01/01/70 00:00 | |
The female must have seen the bike :) | 01/01/70 00:00 | |
alternative pdf readers![]() | 01/01/70 00:00 |