Hi all,
I normally turn to PICs when looking for a small microcontroller for a project, but I now have a project which for various reasons requires an AVR. So today, I've been doing the whole where-does-this-wire-go? thing with AVR Studio and a JTAGICE III.
It took a while before I could get anything to download and run, which had me (metaphorically) banging my head against the wall for half the afternoon. When the target, IDE, compiler and PC interface are all new, it's hard to know what to do when a simple 'hello world' pin wiggler won't even start running, much less actually work. But to cut a long story short...
I started by modifying some example code which set the configuration fuses thus:
FUSES = {
.extended = 0xFF,
.high = 0xDC,
.low = 0xC2,
};
... which seemed right, as I defined them using the programming window in AVR Studio.
As a general rule, I like to include all a device's configuration within the source files, rather than having to program them separately - but those of you familiar with the AVR will probably have spotted my bug already.
In my fuse settings, DWEN is not configured to allow debugWire to work. So it doesn't. Or rather, it works just fine right up until the point where the fuses get programmed, and then it doesn't work any more and I get a very unhelpful error message from AVR Studio, which is clearly upset and confused. I was too.
I thought I'd bricked my AVR, as I could neither debug it nor talk to it over SPI. To recover it, I eventually discovered that I had to comment out my fuse settings, then download my code again - which worked, fortunately. This then let me exit the debugger gracefully, which in turn makes DWEN inactive and allows SPI access once more. If I just set the fuses directly using the AVR Studio programming window, and don't set them in the code at all, then it all works fine.
In the PIC world, configuration settings are handled by individual compiler directives - one per setting - and the general advice is to let the IDE handle the one that enables debugging. It gets set when debugging, and cleared for normal operation, with no change to the source code required. It's simply left out of the code and gets handled by the IDE.
I can't figure out how to do this on the AVR, though. It appears that I must either activate DWEN if I want to debug, or deactivate it for production. I guess I could do this with a #ifdef, but that seems untidy.
Am I doing something dumb here?