I got a question about some broken code. I'm doing a project where I need to sync the Arduino (UNO) with a external 1 MHz clock.
Too keep the code execution fast and simple I choose to try out with some inline assembly. Here is some test code:
int phi = 8;
int led = 13;
void sync(){
asm volatile("sbis 0x5,0x0"); //PORTB0
asm volatile("sbi 0x5,0x5"); //PORTB5
}
void setup(){
pinMode(phi,INPUT);
pinMode(led,OUTPUT);
}
void loop(){
sync();
}
So, phi is the clock input. Sync is executing sbis, which is controlling PORTB bit 0. If the bit is set the following instruction should get skipped.
But that does not happen. The sbi instruction, which turns on the LED at pin 13, gets executed when I connect a wire between 5v and pin 8 (portb0).
I'm kinda lost why sbi still gets executed when pin 8 is high?!
Yes. Reading PORT gives you what was last written to PORT which would be the state of the pullup resistor. (Incidentally, writing to PIN toggles the pin, sometimes this is useful to know)
You have to use local labels in inline assembler. they look like numbers (eg 1: instead of label:) and when you branch you have to say whether you are going forwards or backwards (eg brne 1b , jmp 2f).