![]() ![]() On post 2016 AVRs, the register layout is different, PORTB/DDRB/PINB is replaced with PORTB.OUT, PORTB.DIR, PORTB.IN. ![]() PORTB |= 0x03 // 3 word, 3 clocks, and not atomic. PORTB = 0x01 // 2 word 2 clocks, *and not atomic* If (PORTB & 0x03) // 2 word 2 clocks for the test then 1 word for a breq/brne - and potentially an extra 1 and 2 ontop of that for an rjmp since conditional branches can only go 64 words in either direction. If(PORTB & 0x01) // 1 word 1 clock for the test, and 1 word for an rjmp (2 if it for some reason needs a jump, but the linker rarely does) - the test instructions are sbic/sbis (Skip if Bit in Io register Clear/Set), that is, they test the bit (taking 1 clock to do so, and depending on the value of the bit either skip (takes 1 clock) or don't skip (takes however long it normally takes) the next instruction, which is almost always a jump of some sort rjmp takes 2 clocks, jmp 3 clocks) Either way, this is atomic, and you don't have to worry about an interrupt firing in the middle PORTB |= 0x01 //1 word, 2 clocks on ATmega2560. PORTx (and DDRx (Data Direction Register) and PIN (PortINput) are "magic" registers because they are located in the "low I/O space", which makes it possible to set, clear, or test them with a single instruction work that executes in 2 clocks on a pre-2016 AVR.Since according to notifications people are still reading this, there are a few things also worth noting: Note that you can also toggle the pin in a single operation by writing a 1 to the PINx register, ex: They typically show the port pin as P, followed by the letter of the port, then the bit within the port (for example PB0 for the pin you referred to). See a pinout chart (google image search will find nice pinout diagrams - just search the board and the word pinout).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |