bits arithmetics

[use case]
some time it is very useful if we can read special bits from a register in c code.
Things like the bits from position 4 to position 8 in a 32bits register.

In other case it could be more useful to rewrite the bits in a register, things like the bits from position 4 to postion 8 should habe a new hex value 0xf.


int bits_read(int reg, int pos, int width) { 
  // mask ~(~0 << width) should be like 0...0 1...1 
  return reg >> pos & ~(~0 << width);

this function can read the bits from a start position and with a width of the bits you would like to get from the start bit postion.

int bits_write(int reg, int pos, int width, int new_value) {
 // create dynamic mask like 0...0 1...1 0...0
 int mask = (~(~0 << width)) << pos;
 return ( new_value << pos ) & mask | (reg & ~mask);

this function allows you to rewrite the bits from the start postion with a certain width of bits with the given new value.


why do we need this kind of function with dynamic masks?

The answer to this question is because the most time we don’t know how long is a register, ist it 8 bits or 32 bits. With these two funktion you can read and rewrite the subbits of a register and it works with register with what ever length.


AVR Fuse

I used avrdude in vmware guest windows and opensuse linux to set the ATtiny45 and ATmega8 fuse.
Unfortunately the fuse is reported sucessfully, but i can’t reach the AVR chip after fuse setting anymore.

It seems that i have destroyed this two chips with fuse setting.

Now i am wonderung if it is because the vmware or it is because of my windows or opensuse.
It seems to work in not virtualised Ubuntu and Mac OS.

upload hex data to atmel chips


uploading code with avrdude to atmel chips, get error:
avrdude: stk500v2_command(): command failed
avrdude: stk500v2_ReceiveMessage(): timeout


it has worked a day ago, just connected a potentiometer to pin sck, GND and VCC.

remove the potentiometer connection to VCC pin or remove the connection from potentiometer to SCK pin during the upload, because it courses a closed-loop during
the uploading.

IT is allways better just load your chip without any other element connected.

AVR initilization failed

AVR initilization failed


This means that the programmer couldn’t talk to the chip. If you are using a “simple” programmer such as a serial or parallel port bitbang programmer, it could mean the programmer is at fault. Otherwise, it usually means the programmer is OK but it couldnt find the chip.

Check that the chip is powered, plugged into the socket or programmer properly, the programming cables are plugged in correctly, the header is wired correctly, etc. 99% of the time, it is a problem with wiring.


[My Solution]
I bought a new Programmer and new ISP cable, the error never ever comes
again. In my case is not the wiring, but the hardware is broken.




the ISP cable delivered with mysmartUSB Light has the following pin orders.
The connector has an arrow on the side, which marks the PIN 1 in row1. The opposite row of pin1 is pin 2, in the next colume is then pin 3 in row1 and the opposite of that is pin 4, pin 5 then follows Pin6

Pin1 = MISO
Pin2 = VTG / VCC
Pin3 = SCK
Pin4 = MOSI
Pin5 = RST
Pin6 = GND


********[in german]
das mitgelieferte ISP Kabel des mysmartUSB Light.
an diesem Stecker ist seitlich ein Pfeil, dieser markiert PIN 1. Der gegenüberliegende davon ist PIN 2, in der nächsten Reihe ist dann Pin3 u. Pin4, anschließend folgt Pin5 u. Pin6