I'm very new to using assembly code and having some trouble understanding what this code is doing and how it works. I sort of understand the first few lines of code but I have no clue what the "brne delay" is doing, if someone could give a run down of what this code is doing it would be much appreciated!
void setup() {
// put your setup code here, to run once:
asm("sbi 0x04, 5");
}
void loop() {
// put your main code here, to run repeatedly:
asm("start:");
asm("sbi 0x05, 5");
asm("ldi r16,40 \n\t rcall delay");
asm("cbi 0x05, 5");
asm("ldi r16,40 \n\t rcall delay");
asm("rjmp start");
asm("delay:");
asm("dec r8");
asm("brne delay");
asm("dec r9");
asm("brne delay");
asm("dec r16");
asm("brne delay");
asm("ret");
}
dec decrements the values held in those registers. It sets the zero flag if the result is 0.
brne is a conditional branch that is taken only if the zero flag is not set - and if it is, they go back to the delay label. decrementing the register that was previously 0 makes it roll over. First part of the delay happens 256 times for each time that the second part does, and the second part happens 256 times per
So it's a delay based on the contents of r16... (which gets set to 0x40), working out to 4.2 million passes through the delay, and hence like... a second or so?
The text of the instruction description: Tests the Zero flag (Z) and branches relatively to PC if Z is cleared. Meaning, the result of a previous instruction was Not Equal to zero.
The BREQ - branch (relative) if equal - Tests the Zero flag (Z) and branches relatively to PC if Z is set. Meaning the result of a previous instruction was EQual to zero.
It can be read as: If the result of an operation which affects the status register was zero, then set the Z flag. There's also a pair of instructions which do nothing but set/clear the zero flag (SEZ).
Walks:
Thank you Johnwasser, that actually helped a lot, I'm still a little fuzzy on why registers 8 and 9 are used though.
All those registers are 8-bit registers, so if you need to delay for longer than 255 iterations of the loop (which you do), you need to use more than 1 register to store the count-down.
Thanks every, I definitely have a lot to learn.... and last question, how would I go about finding the on off time in real time, I know i need to know the crystal frequency
Each datasheet has a section titled Instruction Set Summary. That section has timings for each instruction (usually 1 or 2 clock cycles). It helps to have a spreadsheet that does the instruction-to-timing lookups.