Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 16th July 2007, 05:40 PM   (permalink)
Default Jump if greater than x....how do we do it in Assembly?

Peace to all,

How do we implement it in assembly?

Say I want to jump if Accumulator is greater than 27D?
Is there a fool-proof way of doing it?

At the moment I am checking the Accumalator say for 28D, and when it reaches it I jump, this is using CJNE.

But its not giving me a reliable result as it may not be passing 28D due to other cycles being spent before the accululator being checked using CJNE so the 28D value may not be there when the CJNE instruction is being executed (though I have tried to implement ways to ensure 28D is reached on the CJNE instruction).

So is there a proper way to do it? ie. where whatever the value of Accumulator is, as long as it is greater than 28D, it will jump. Suppose it is 35D, it will jump...

Many thanks for your help.

This message board has been a great help!

Last edited by AceOfHearts; 16th July 2007 at 06:03 PM.
AceOfHearts is offline  
Reply With Quote
Old 16th July 2007, 10:02 PM   (permalink)
Default

Perhaps you might try mentioning the processor you're using? - if it was a PIC (which it isn't) the PICList has plenty of details.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply With Quote
Old 16th July 2007, 10:03 PM   (permalink)
Default

Quote:
Originally Posted by Nigel Goodwin
Perhaps you might try mentioning the processor you're using? - if it was a PIC (which it isn't) the PICList has plenty of details.

Appologies, it is an 8051.

Many thanks.
AceOfHearts is offline  
Reply With Quote
Old 16th July 2007, 10:54 PM   (permalink)
Default

Are you using this method of comparison?
Code:
		CJNE A, #27, NOT_EQ

EQ:		;A=27
		SJMP END_CMP

NOT_EQ:		JC A_LESS

A_GREATER:	;A > 27
		SJMP END_CMP

A_LESS:		;A < 27

END_CMP:	;Finished
__________________
"There is no way to peace, peace is the way!"

Last edited by kinjalgp; 16th July 2007 at 10:59 PM.
kinjalgp is offline  
Reply With Quote
Old 16th July 2007, 11:03 PM   (permalink)
Default

Quote:
Originally Posted by AceOfHearts
Appologies, it is an 8051.

Many thanks.
You dont have to use the CJNE instruction at all. You can do a subtraction and check the carry flag, which for subtraction is actually a borrow. the following instruction set reference from Atmel has examples in it for this kind of stuff.
http://www.atmel.com/dyn/resources/p...ts/doc0509.pdf

Good Hunting

PS I knew which processor you were talking about. CJNE is kinda unique, and some of us are fortunate to know more than one processor family.

Last edited by Papabravo; 17th July 2007 at 11:15 AM.
Papabravo is online now  
Reply With Quote
Old 16th July 2007, 11:46 PM   (permalink)
Default

kinjalgp, I was not monitoring the carry flag at all.

papabravo, thanks for this method.

This sounds like it will work!

Many thanks.
AceOfHearts is offline  
Reply With Quote
Old 17th July 2007, 07:41 PM   (permalink)
Default

Small detail, don't forget to clear the carry flag before your subb instruction otherwise you will end up with a bug.

Sometimes it work because the carry was cleared by previous instructions
Sometimes it won't work because the carry was set by previous instructions
mcs51mc is offline  
Reply With Quote
Old 17th July 2007, 10:19 PM   (permalink)
Default

Many thanks.

I am actually programming an 8051 for an ultrasonic rangefinder (SRF05) and trying to use this method to ascertain if the signal from the device has been on for a certain period of time (the length of this signal tells us the range).

Basically above a certain time period, object is out of range. And when within this time period, it is within range.

So far, I have not got it to work as I want!
AceOfHearts is offline  
Reply With Quote
Old 17th July 2007, 11:11 PM   (permalink)
Default

Quote:
Originally Posted by AceOfHearts
Many thanks.

I am actually programming an 8051 for an ultrasonic rangefinder (SRF05) and trying to use this method to ascertain if the signal from the device has been on for a certain period of time (the length of this signal tells us the range).

Basically above a certain time period, object is out of range. And when within this time period, it is within range.

So far, I have not got it to work as I want!
How are you measuring pulse duration? Show your complete code because without that it is difficult to pin-point whats going wrong in it.
__________________
"There is no way to peace, peace is the way!"
kinjalgp is offline  
Reply With Quote
Old 18th July 2007, 05:21 AM   (permalink)
Default

Quote:
Originally Posted by AceOfHearts
Many thanks.

I am actually programming an 8051 for an ultrasonic rangefinder (SRF05) and trying to use this method to ascertain if the signal from the device has been on for a certain period of time (the length of this signal tells us the range).

Basically above a certain time period, object is out of range. And when within this time period, it is within range.

So far, I have not got it to work as I want!
Did you read this one: http://www.interq.or.jp/japan/se-inoue/e_pic6_6.htm
or this one: http://instruct1.cit.cornell.edu/cou...7/jjl49_mar97/

Should be the starting point of every US porject

How do you generate the US burst?
mcs51mc is offline  
Reply With Quote
Old 20th July 2007, 03:48 PM   (permalink)
Default

mcs51mc,

SRF05 sends out 8 cycles of U.S. bursts upon a trigger. So all I do is triffer the relevant pin SRF05.

kinjalgp,

I dont have an oscilloscope so I cant test whats going on at the hardware level. At the moment, when I download the program and connect it up on the breadboard, The object detected and object not detected LEDs come on and off randomly regardless of distance of an object in fron of the sensor. Sometimes I notice some consistancy, but it is pretty random most of the time. If I keep an object at close range without moving it, the LEDs keep switching.


Here is the code:



Quote:
;P1.0 = object detected, P1.1 no object detected P0.0 connected to the trigger pin of SRF05. Echo
;line reponse appears 700uS after the end of trigger pulse


AGAIN: MOV A, #0
MOV P0, A ;output port

SETB P0.0
ACAll DELAY_TWENTY
CLR P0.0 ;Trigger


MOV A, #0FFH
MOV P0, A ;input port
STAY: JNB P0.0, STAY

ACALL MONITOR
ACALL DELAY_FIFTY

SJMP AGAIN


;================================================
;=========DELAY_TWENTY============================ =====

DELAY_TWENTY: MOV TMOD, #01 ;(1)

MOV TL0, #0F1H ;(1) ;Will count up 14D, TOT 20
MOV TH0, #0FFH ;(1)


SETB TR0 ;(1)
AGAIN1: JNB TF0, AGAIN1


CLR TR0 ;(1)
CLR TF0 ;(1)

RET

;================================================= =============
;======DELAY_FIFTY============================

DELAY_FIFTY: MOV TMOD, #01 ;(1)

MOV R5, #7
FIRE: MOV TL0, #0
MOV TH0, #0


SETB TR0 ;(1)
AGAIN2: JNB TF0, AGAIN2

CLR TR0 ;(1)
CLR TF0 ;(1)
DJNZ R5, FIRE
RET

;#0FCH ;(1)
;#04BH ;(1)
;================================================= ===========
;=================Monitor=================

MONITOR: CLR C ;clear carry
MOV TMOD, #01 ;(1)

MOV TL0, #0H
MOV TH0, #0H

MOV A, #100 ;(1)

SETB TR0


REMAIN1: JB P0.0, REMAIN ;(1)

SETB P1.0
CLR P1.1
SJMP FINISH

REMAIN: MOV R5, TL0 ;(1)
;CJNE A, #32H, REMAIN1 ;(2)

NOP ;(1)
NOP ;(1)
SUBB A, R5 ;(1)
JNC REMAIN1 ;(2)

SETB P1.1
CLR P1.0


FINISH: CLR TR0
CLR TF0

RET

Last edited by AceOfHearts; 20th July 2007 at 04:00 PM.
AceOfHearts is offline  
Reply With Quote
Old 21st July 2007, 07:00 PM   (permalink)
Default

Quote:
Originally Posted by AceOfHearts
REMAIN1: JB P0.0, REMAIN ;(1)
Is this correct?
I think your idea is to stay here as long P0.0 is high so you should jump to "REMAIN1" not "REMAIN".
In the mean time T0 is counting 1ms intervals (if you have a 12MHz xtal).

What happens after that line is complete unclear to me.
According to what I read in the SRF05 spec, you should divide the measured time by 58 to get distance in cm. Where is your division?

Unless you just want to know if the US see something and check for 30ms time-out of the SRF05 But even then the sequence is wrong.

Explain your DELAY_FIFTY: routine
mcs51mc is offline  
Reply With Quote
Old 23rd July 2007, 08:31 PM   (permalink)
Default

Quote:
Originally Posted by mcs51mc
Is this correct?
I think your idea is to stay here as long P0.0 is high so you should jump to "REMAIN1" not "REMAIN".
In the mean time T0 is counting 1ms intervals (if you have a 12MHz xtal).

What happens after that line is complete unclear to me.
According to what I read in the SRF05 spec, you should divide the measured time by 58 to get distance in cm. Where is your division?

Unless you just want to know if the US see something and check for 30ms time-out of the SRF05 But even then the sequence is wrong.

Explain your DELAY_FIFTY: routine
Firstly sorry for the late reply.

I am not looking to measure any distance numerically to any precision. All I want to do is detect if something is there at close range (at roughly any distance at first) and turn on P1.0 and P1.1 pff if anything is detected, and vice versa if nothing is detected.

If I may run through the MONITOR bit with you... basically, P0.0 is measuring the echo pulse. At REMAIN1 it is checking to see if it still at logic 1, if it is, it jumps to REMAIN. Bear in mind Timer 0 is rolling all this time. At REMAIN, we transfer TL0 into R5.

BTW, the CJNE instruction there is commented out and it not part of the code. We then subtract R5 (which has the the Timer value) from the accumulator which has the decimal 100 in it. At the beginning , 100 minus R5 will give ninety-something as the timer had just started. Carry flag will not be sat. We check to see if carry is not set, if it is not set, we jump back to REMAIN1 to check if P0.0 is still at logic 1. if it is, we again jump to REMAIN and minus the timer value from the accumulator. This time, the timer will have an even larger value, hence the accumulator will decrease by more in this go...if after several cycles of P0.0 being at logic 1 and performing this routine, the accumulator reaches below zero, carry flag is set and detected. It means P0.0 has stayed on for too long and so no object has been detected. P1.1 will be set and P1.0 is cleared.

If P0.0 is detected as low before the the carry flag is detected, it means there is an object there and P1.0 is set and P1.1 is cleared.

I expect the lights to remain constant when a object is fixed in front of the sensors, but they keep alternating; as if the objects kep moving randomly - in and out of range.

About the DELAY_FIFTY, it is not actually delaying for 50 milliseconds, but much longer. This is so that I could see the SFR06 LED flash...I dont have an osciloscope; I wanted to make sure everything happened before I trigger the next one, just until I get it working.

Thanks.

Last edited by AceOfHearts; 23rd July 2007 at 08:47 PM.
AceOfHearts is offline  
Reply With Quote
Old 24th July 2007, 10:32 AM   (permalink)
Default

Basicly you just want to know if there's something in front of the SRF05. You must drop the "close range" in you first sentence because the SRF05 detects over the complete range (not found in datasheet(?) of SRF05 )

Now I understood your algorithm. But it's too complicated to only detect presence/absence of object

Some problems in you code:
1) 100-TL0 is done only once, next time you pass the SUBB the acc still holds 100-TL0 from previous pass.

2) You have to clear the carry flag before the SUBB instruction

What I would do:
Code:
orl   P0,#00000001b     ;P0.0 is input
mov  TMOD, #01      ;Prepare T0 to time P0.0
mov  TL0, #0 
mov  TH0, #0 

jnb    P0.0, $           ;Wait for +edge of echo pulse
setb  TR0                ;Echo pulse active, start timer
jb      P0.0, $          ;Wait for -edge of echo pulse
clr    TR0                ;end of echo pulse is stop timer

mov   a,TL0           ;Load LB timed value
clr     c
subb  a,#29          ;Time out SRF05 is 30ms
jc      ObjectDetected        

clr     P1.0          ;No object: led off
jmp   AGAIN

ObjectDetected:
setb  P1.0          ;Object: set led on
jmp   AGAIN
Problem is the accuracy of T0 and the time out value (30ms) of the SRF05. I used 29 but maby you have to adapt this according to the hardware.
To evaluate it you can use the CJNE instruction in combination with the carry flag. That you will have greather, equal and less situations. Put each one on a different output led and see what happen...

This code can easily be modified to calculate the distance to the object.

Please notice that I didn't check that code with the assembler, just typed it while I was thinking, so a typo is always possible
mcs51mc is offline  
Reply With Quote
Old 25th July 2007, 01:47 PM   (permalink)
Default

Hi there,

thanks for the code...I really appreciate it.

I am going to look at it further to see how I can adapt it to what I need.

But very quickly, the problem I see is that as I am using a crystal of 11.0592MHZ, each cycle is 1.085uS, so for 30ms, I will need 30ms/1.085uS = 27650 cycles which LB of T0 will not represent 2^8=256.

I think I can use HB of T0...(2^16) - (2^8) = 65280 max.; lowest value on HB would be 256, next highest 512 etc.

Thanks again.

Last edited by AceOfHearts; 25th July 2007 at 01:58 PM.
AceOfHearts is offline  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Need help with Intel Assembly Language and PIC16F877 avinsinanan Micro Controllers 10 4th March 2008 10:02 AM
Need some help with a code provided by ATMEL ikalogic Micro Controllers 1 23rd January 2007 02:45 PM
Assembly? Marks256 Micro Controllers 43 28th July 2006 03:44 PM
Learning Assembly Programming Language Johnson777717 Micro Controllers 4 22nd March 2004 02:00 PM
Assembly Language Question Jeggyman General Electronics Chat 6 30th January 2004 09:53 PM



All times are GMT. The time now is 11:26 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.