Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

2 Cycle pause using RJMP

Status
Not open for further replies.

Diver300

Well-Known Member
Most Helpful Member
Is there a shorthand jumping to the next line?

I want to put some 2 cycle pauses, using one line of code, with something like this:-

Code:
    rjmp  next_line
next_line:

Now in PIC assembler I can just write:-

Code:
    bra   $+1

Is there an equivalent for AVR?
 
The easyest way to insert short wait cycles is to use the NOP Command.
That generates a wayt cycle of 1 Clock.
NOP
NOP
will generate a wait cycle of 2 Clocks in Assembler.
When you use a Compiler ist is possible, the compiler optimizes one nop away.
Therfore you should switch off code optimizing before insert the NOP comand.
 
I realise that two NOPs can be used. However that takes two lines of program space while a jump to the next line is only one.

I just wondered if there was a way of doing a jump without having to name the destination.
 
I think, there should be no difference between 2 NOP's and one JMP, because the JMP command needs an Address Parameter.
Only in the Compiler are 2 Lines for the NOP's afford, but i guess that is no problem
 
I think, there should be no difference between 2 NOP's and one JMP, because the JMP command needs an Address Parameter.
Only in the Compiler are 2 Lines for the NOP's afford, but i guess that is no problem

I'm fairly new to the AVRs, but I think that NOPs and RJMPs each assemble to one line.

I wrote this:-
Code:
m1:
	nop
m2:
	rjmp	m3
m3:

And when I looked at the map file, it contained this:-

CSEG m1 00000000
CSEG m2 00000001
CSEG m3 00000002

Now that implies to me that both the NOP and the RJMP take up one line of assembled code. So RJMP [next line] is a delay of two cycles for one line of code, so there is a small advantage in using it.

Is there a quick way of writing it in one line without having a label on the following line?
 
I'm not sure if this helps, but the avr-libc library has this delay function defined (for avr-gcc):

Code:
static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline));

/** \ingroup util_delay_basic

    Delay loop using an 8-bit counter \c __count, so up to 256
    iterations are possible.  (The value 256 would have to be passed
    as 0.)  The loop executes three CPU cycles per iteration, not
    including the overhead the compiler needs to setup the counter
    register.

    Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
    can be achieved.
*/
void
_delay_loop_1(uint8_t __count)
{
	__asm__ volatile (
		"1: dec %0" "\n\t"
		"brne 1b"
		: "=r" (__count)
		: "0" (__count)
	);
}

If you need to delay only for a couple of clock cycles then using NOPs is the only way.. I think.
 
Last edited:
If you need to delay only for a couple of clock cycles then using NOPs is the only way.. I think.

rjmp flag
flag:

That is two cycles but only takes up one line of code.
I still can't work out how to write it without having a flag for the following line.

It assembles to 0xC000, while NOP assembles to 0x0000.

In the example that you gave, the line

brne 1b

is used, which seems to jump back one line. However, AVR studio doesn't like that syntax.

I'm looking for a syntax that AVR studio will accept.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top