hex for ATmega168 only runs if connected to AVRISP

Hi all,

I have an arduino-compatible board whose USB port fell out (I tried, but failed to solder it back on due to lack of skills), so I am now resorting to programming that board via the ISP interface instead (I do not need the USB interface for my application).

Unfortunately, I am facing a strange problem that I’m not sure how to solve. Basically, I am trying to program (using the AVRISP mkII) that Arduino-compatible board equipped with ATmega168, but the codes that I download into the ATmega168 only runs if the board’s ISP socket is connected to the AVRISP mkII (which is connected & powered by USB).

I have tried 2 ways to compile my codes into hex files:

  1. using Arduino IDE (via shift + compile)'s temporary hex file, after compiling their sample code (e.g. servo)
  2. using AVR Studio 4 (below is an example code):
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  DDRD = 0xFF;

  int onState=1;
  while(1)
  {
    if(onState)
    {
      PORTD = 0x04;
    }
    else
    {
      PORTD = 0x00;
    }
    onState = 1 - onState;
    _delay_ms(5000);
  }      
  return 1;
}

I’ve verified that the above code works using a multimeter (when the board is connected to the AVRISP mkII, pin D2 fluctuates between 0V and 5V, and other pins stay at 0V). But the moment I remove the AVRISP mkII from the USB port, the above code no longer runs (pin D2 just remains at 0V, but pin D1 stays at 3V, whereas pin D0 stays at 5V, where pin D0 & D1 are Serial TX & RX respectively). The schematics of this arduino-compatible board is uploaded at (Just a moment...) if it helps.

I have no idea why this happens, hence I have no idea how to fix this problem. I believe that once the hex file is downloaded into the ATmega168, it should remain there forever, and start running once it is powered? But this isn’t the case for me…

I am sincerely hoping for any suggestions on what might be the cause of this weird behavior…

After consulting from other forums, I realize that I am also not so sure how to check if my board’s original arduino bootloader was erased when I downloaded the hex file into my ATmega168. All that I can describe is that whenever I program a new hex file into my board’s ATmega168’s Flash memory, I enabled (by default) both “Erase device before programming” and “verify device after programming”. And the screenshots below show the current settings read by the AVRISP mkII programmer:

Are my fuse & lock bits correct? Unfortunately, I am lacking in knowledge & understanding regarding all these fuse & lock bits…
but I did observe (according to http://www.arduino.cc/playground/Learning/Burn168), that my ATmega168’s fuse-settings are correct (for ATmega168 Arduino’s bootloader) but my LockBits are wrong (my ATmega168 did not have “LPM and SPM prohibited in Boot Loader Section”).

Is it possible to “erase the Arduino bootloader” but still be able to download the Arduino IDE (via shift + compile)'s temporary hex file into my ATmega168 using the AVRISP mkII programmer, and make my code run? If yes, what should my fuse & lock bits settings be? I am most worried that I will “brick” my board by choosing the wrong settings…

I am hoping that it is the Arduino bootloader that is causing all these problems, since the moment I remove the AVRISP mkII from the USB port, pin D0 (Serial TX) stays at 5V, whereas pin D1 (Serial RX) stays at 3V. But I am unsure why/how the no-longer soldered USB port is responsible for this (if at all). All I know is that according to http://arduino.cc/en/Tutorial/Bootloader :

It’s possible to “confuse” the bootloader so that it never starts your sketch. In particular, if you send serial data to the board just after it resets (when the bootloader is running), it may think you’re talking to it and never quit.

This is my current guess, but I’m not sure how to check/test if this is true, or even how to correct it, due to my lack of knowledge & understanding…

Sincerely hoping for any advice :slight_smile:

Update:

I have tried to read up on the BOOTRST Fuse at http://www.atmel.com/dyn/resources/prod ... oc2545.pdf where it stated in page 57, that:

When the BOOTRST Fuse is programmed, the device will jump to the Boot Loader address at reset,

So I assume that since my BOOTSZ = "Boot Flash size=1024 words start address=$1C00" hence if my BOOTRST Fuse is programmed, when the ATmega168 resets, its program counter will jump to address $1C00 ? Whereas if my BOOTRST Fuse is not enabled, then the ATmega168's program coubter will start at address 0 upon reset ? And I assume that my hex file downloaded into the ATmega168 starts at address 0 ?

If my understanding is correct, I have already tried to disable (untick) the BOOTSZ Fuse, then program it, but the result is still the same i.e. the moment I remove the AVRISP mkII from the USB port, the above code no longer runs (pin D2 just remains at 0V, but pin D1 stays at 3V, whereas pin D0 stays at 5V, where pin D0 & D1 are Serial TX & RX respectively).

Hoping for any advice

But the moment I remove the AVRISP mkII from the USB port, the above code no longer runs (pin D2 just remains at 0V, but pin D1 stays at 3V, whereas pin D0 stays at 5V, where pin D0 & D1 are Serial TX & RX respectively).

How do you power the board when the AVRISP mkII is not connected?