NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Can's reprogram after changing tempsensor program

December 20, 2009
by promethean
promethean's Avatar

I successfully connected my nerdkit & wrote/compiled & loaded the tempsensor code. After this, curious about why a bitwise operator was used instead of logical equivalence, I decided to test it by changing this line:

while(ADCSRA & (1<<ADSC)) {


while(ADCSRA = 1) {

It compiled and loaded, and nothing happened, and now I can't re-program the chip. (I realize now that I used assignment instead of equivalence, "==".) In any case, now when I try to program the chip, nothing happens:

C:DOCUME~1JimDesktopCodeTEMPSE~1>make avrdude -c avr109 -p m168 -b 115200 -P COM5 -U flash:w:tempsensor.hex:a

Connecting to programmer: . Found programmer: Id = "" ""; type = Software Version = . ; Hardware Version = . avrdude: error: buffered memory access not supported. Maybe it isn't a butterfly/AVR109 but a AVR910 device? make: *** [tempsensor-upload] Error 1

Any ideas on how I can fix this?

Thanks for any help.

December 20, 2009
by Rick_S
Rick_S's Avatar

That really should not have caused a problem with the communication between your pc and the mcu.

However, to answer your 1st point 1st.

The Statement:

while(ADSCRA & (1<<ADSC))

is not the same as

while(ADSCRA == 1)

The 1st one is bit-wise and is checking the 6th bit in ADSCRA. If you look at the data sheet for the atmega168, you'll see that ADSC is bit 6 in the register ADSCRA. By checking this bit you can determine if the ACD process is finished.

With the 2nd method you were trying, you would be comparing the entire register to see if it was equal to one. Since various bits in that register are on or off at different times during the ADC operation, this will not work.

Setting this register equal to one... I don't think would do any thing that would not be recoverable. I definitely don't see it preventing communication between the micro-controller and the PC which is what your error seems to be indicating. I would check all the connections especially the pin-14 switch to ground (programming switch) , the wire from pin-1 to 5v, and the battery / regulator connections. This may "fix" your programming interface issues.

Good Luck and welcome,


December 20, 2009
by promethean
promethean's Avatar

Thanks Rick_S, It did turn out to be a power issue-- a bad lead inside the 9v batter connector. Unfortunately, its failure corresponded to my code change. Fortunately, I have an extra, sturdier one I swapped in, after which it I was able to program again.

On the code, what then is the value of ADCSRA? The bit may be set high, but when used in code in this way, is the comparison actually 01000000? If so, would this code work represent the same as the bitwise version:

while(ADCSRA == 64) {

I'm also not used to using loops without a logical operation-- do they return true as long as the value is > 0?


December 20, 2009
by Rick_S
Rick_S's Avatar

Because by using the bit logic, you are only looking at bit 6. If all other bits in ADCSRA were zero then you could say while(ADCSRA == 64) However, there are 6 bits below the one we are checking and one bit above. Depending on their state, that could make the register value any number between 0 and 255. By doing the bit level logic, the check is only being done on the 6th bit while bits 0 - 5 and 7 have no effect on the comparison.

If by "I'm also not used to using loops without a logical operation-- do they return true as long as the value is > 0?" you are asking under what condition does the:

while(ADCSRA &(1<<ADSC)){

stay in the while loop, then yes, I believe any non-zero condition equates to a logic true where zero equates to a logic false. (Someone please correct me if I'm wrong)

Bit logic is a difficult thing to grasp and I will admit, I'm still new to it and learning as well. It is however quite powerful and can greatly reduce code size and improve program speed when properly used.

Go through the exercises in the guide on bit logic it may help. I know it helped me grasp it much better.


December 20, 2009
by hevans
(NerdKits Staff)

hevans's Avatar

Hi all,

Boolean logic is a bit tricky in C, especially for people who are use to higher level languages that actually have a type for boolean values. In C there is no type for True and False like in Java or Python. Rick_S is correct, any non 0 value is considered a logical true, 0 is a logical false.


December 22, 2009
by Hewitt2
Hewitt2's Avatar

true digital 0=0 anything>0 =1 one needs no "high" language with this understanding (no snarkiness here just saying boolean can be simple

i live in a qam (digital rf) world it is all about zero and greater than zero.

Post a Reply

Please log in to post a reply.

Did you know that an analog comparator can tell when one voltage input crosses another? Learn more...