I need some code translated for me.
this code posted 2 or 3 years ago avr freaks , user moved on. therefore, have been trying understand going on line line. know user pulsing tx pin high , low specific delay times but, having trouble understanding lines bit shifting , compounding. basically, anywhere there things "bitwise" code, confused.
if have time like...///"this line compound x , y x(y)"
or other wise tx_port &= ~_bv(tx_bit); ///"this line means this".
grateful or direction me understand code doing.
thanks,
mark
if have time like...///"this line compound x , y x(y)"
or other wise tx_port &= ~_bv(tx_bit); ///"this line means this".
grateful or direction me understand code doing.
thanks,
mark
code: [select]
sure, here init function. send address 0x33 ,
my accord's ecu responded correctly. didn't bother using uart
such slow baud, software bit toggling.
code:
void send_address(uint8_t addr) {
uint8_t i;
uint8_t temp;
/* idle high */
tx_port |= _bv(tx_bit);
tx_ddr |= _bv(tx_bit);
/* required idle delay w0 (2ms), safe 4ms */
_delay_ms(4);
/* start bit */
tx_port &= ~_bv(tx_bit);
/* @ 5bps */
_delay_ms(200);
/* send byte */
for (i = 0; < 8; i++) {
temp = (addr >> i) & 0x01;
if (((tx_port & _bv(tx_bit)) >> tx_bit) == temp) {
/* @ correct level, nothing */
} else {
if (0 != temp) {
tx_port |= _bv(tx_bit);
} else {
tx_port &= ~_bv(tx_bit);
}
}
_delay_ms(200);
}
tx_port |= _bv(tx_bit);
_delay_ms(200);
/* idling high on tx */
return;
}
from avr-libc user manual (get here):
so, following statement clears tx_bit in tx_port register. refer datasheet understand register , bit mnemonics. not sure datasheet refer to, tx_port , tx_bit aren't in few avr datasheets i'm familiar with.
quote
11.6 _bv() stuff about?
when performing low-level output work, central point in microcontroller
programming, quite common particular bit needs set or cleared
in io register. while device documentation provides mnemonic names for
the various bits in io registers, , avr device-specific io definitions reflect
these names in definitions numerical constants, way needed convert bit
number (usually within byte register) byte value can assigned directly
to register. however, direct bit numbers needed (e. g. in
an sbi() instruction), definitions cannot usefully made byte values in the
first place.
so in order access particular bit number byte value, use _bv() macro.
of course, implementation of macro usual bit shift (which done
by compiler anyway, doesn't impose run-time penalty), following
applies:
_bv(3) => 1 << 3 => 0x08
however, using macro makes program better readable.
"bv" stands "bit value", in case might ask you. :-)
example: clock timer 2 full io clock (cs2x = 0b001), toggle oc2 output on
compare match (com2x = 0b01), , clear timer on compare match (ctc2 = 1). make
oc2 (pd7) output.
tccr2 = _bv(com20)|_bv(ctc2)|_bv(cs20);
ddrd = _bv(pd7);
so, following statement clears tx_bit in tx_port register. refer datasheet understand register , bit mnemonics. not sure datasheet refer to, tx_port , tx_bit aren't in few avr datasheets i'm familiar with.
code: [select]
tx_port &= ~_bv(tx_bit)
Arduino Forum > Using Arduino > Programming Questions > I need some code translated for me.
arduino
Comments
Post a Comment