I'm using an Atmega32 16PU 1222 chip. I'm guessing that would be a normal Atmega32, just indicating the DIP package.
I'm succesfully programming it with an ArduinoISP: I've made it communicate with an arduino nano over I2C.
But I also tried to make it output a 1Khz square wave.
It's uploaded with an ArduinoISP, so i should be able to ignore the other settings related to the bootloader? (file, upload speed, protocol, ...)...
I can tell you are learning a lot from this project, good job! Yes, build.f_cpu=16000000L doesn't match the actual clock frequency, that is very likely. When you upload a sketch via ISP like you did, it does not write the fuses, and whatever fuses your chip came programmed with are what you are using. The fuses are probably set at the factory for internal 1MHz oscillator or something like that.
Even if you're not going to use a bootloader and you're going to upload sketches directly using ISP programming, it is a good idea to do the Arduino IDE Burn Bootloader at least one time, because that is when the fuses are set. Or set the fuses by running avrdude command line to set them with ISP. If you're curious, you can use ISP programming to read what the current fuse values are with avrdude.
I suggest use the ATmega32 datasheet and use an online fuse calculator to figure out the fuse values you want. If you use an external crystal instead of internal oscillator, it will stay on-frequency and be more accurate. Something for you to be aware of, if you set the fuses for external crystal, and you don't have a crystal attached, the chip will immediately stop working. Don't be alarmed, just go ahead and connect a crystal and try again.
I've had my suspicion that I'd have to burn the bootloader first to get the fuses working. So I did hit the Burn Bootloader button, and even went to verify the fuses with avrdude. They are indeed set to BF and DE.
However, still no stable clock. The square wave it outputs sometimes completly stops working if I move around the room (even at 2m distance). Or if it doesn't completly stop, the duty cycle is at least greatly influenced (between 20 to 80%).
So I tried if I could measure the crystal's oscillations. However the scope I own is limited to 2Mhz. So all I see is some banding if I set the probe at x10. Otherwise I just get a fixed voltage of 0.7v.
I've also experienced some irregularities on microcontrollers where full swing oscillator aren't an option. How close to the pins are your 100nF bypass caps? They need to be as CLOSE AS POSSIBLE in order to get the microcontroller to function properly, ideally just a few millimeters.
Yes, hansibull is wise. When hansibull replied, I was in the middle of typing up this:
The data sheet has some interesting stuff.
The CKOPT Fuse selects between two different Oscillator amplifier modes. When CKOPT is programmed, the Oscillator output will oscillate will a full rail- to-rail swing on the output. This mode is suitable when operating in a very noisy environment or when the output from XTAL2 drives a second clock buffer. This mode has a wide frequency range. When CKOPT is unprogrammed, the Oscillator has a smaller output swing. This reduces power consumption considerably. This mode has a limited frequency range and it can not be used to drive other clock buffers.
For resonators, the maximum frequency is 8MHz with CKOPT unprogrammed and 16 MHz with CKOPT programmed. C1 and C2 should always be equal for both crystals and resonators. The optimal value of the capacitors depends on the crystal or resonator in use, the amount of stray capacitance, and the electromagnetic noise of the environment.
I suggest study the data sheet regarding fuse settings. I see in your other thread CrossRoads and hansibull were talking about full swing crystal, I think that is where the CKOPT fuse comes into play, but I am not an expert on that. You might try setting the CKOPT fuse and see what you get.
And double check your wiring. I suspect most unexplained weird crud is caused by bad ground connections. Check your power supply. Remove any of those motor connections if you have them connected. Make sure you have short wires or traces going to your crystal, and you may need different capacitor values on the crystal pins. Check the connection to ground on your capacitors.
I think it is interesting you can successfully communicate with Nano and can successfully program it, but have trouble with a simple program outputting a square wave. That makes me suspect a bad ground connection again... I guess you can tell I have an obsession with grounds.
Disclaimer: I don't have experience with this particular processor, ATmega32. OMG I need to get away from reading data sheets and go for a walk....
Have you mounted the ATmega32 on a breadboard, or are you using a development board? A photo of your setup would make it a little easier for us to spot obvious hardware errors.
I'd start by connecting both ground pins to ground. Hook the AREF and the AVCC pin to 5v, and add a 100nF cap close as possible, to ground. This is good practice
Since the LED actually blinks (but at ~70Hz instead of ~1kHz) it seems like the fuses haven't been burnt.
Like dmjlambert mentionied, The ATmega32 are most likely using the internal 1MHz oscillator (which is default from the factory).
It looks like you're using and ArduinoISP adapter to program. Have you ever used AVRdude? AVRdude is actually built into the Arduino IDE, and can be used externally. If you're using a Windows based computer, you shound install WinAVR and use AVRdude to check your fuse settings.
After installing, connect your ISP programmer to the board, and run this command in Windows command prompt:
I can't tell very much from this. I recommend giving a solderless plug-in breadboard a try, and make your wiring neat and upload a photo of that. I can't tell by looking at these photos how you have it connected, and therefore can't verify anything for you. I'm not critisizing, just saying sometimes it is good to have others double-check. But for this board, without physically being there I don't thing somebody could check.
After trying that, tell us what is the specific problem, is it still that you can program it just fine but duty cycle of a square wave output from a simple sketch is having a problem? Or something else? Is it the duty cycle changing, or just not the frequency you expect?
I recommend connecting both grounds and both VCC and AVCC and don't leave them unconnected. I don't think a 10K resistor belongs between your power supply and AVCC. Make the power part of the circuit more like this:
If you're not already familiar with Nick Gammon's web site, I recommend having a look. Gammon Forum : Electronics : Microprocessors : How to make an Arduino-compatible minimal board
Although it concerns ATmega328P, other than the different pin numbers on the ATmega32, I think **a lot of the principles apply [EDIT-- note that on Nick's web page the Atmega1284P minimal board is covered, and the pinout of the 40 pin ATmega1284P is the same as the ATmega32, that could be very useful info. ] ** .
If you haven't already done so, it may be a good idea to start with ATmega328P standalone on a breadboard, get familiar with working with and modifying a custom boards.txt file, and get familiar with avrdude commands with an ATmega328P. Then advance to the ATmega32 after a few days messing with ATmega328P. You may have already done this, I don't know.
When working with avrdude on the command line, what I like to do is go into the IDE preferences and check the option show verbose output on upload. Then do the burn bootloader function, and the IDE shows me the avrdude command line. I can then copy that command line to a text editor and make any alterations I want, and copy and paste to the command line.
The chip is perfectly programmable but the program runs horrible. Timing is completly off and inconsistent.
The resistor is missing a capacitor for an LC filter, but since i didn't actually bother with the adc as i first want to get the clock working correctly and i forgot to include it on the schematic... so that's still missing.
I've tried some different fuse settings that make sense or make completly no sense (internal clock) and the results are always the same. But avrdude confirms that the fuses i've writting, are actually written.
Ok, good, it sounds like you're making progress. I recommend changing the VCC, AVCC, and 2 GNDS with 2 decoupling caps, and AREF wiring, to be like my pencil sketch, and see if you have any improvement.