Cannot program ATMEGA328P - crystal not oscillating - Page 1
Forum section: electronics / beginners

author: akupila
author_rank: Newbie
timestamp: June 13, 2013, 10:58:14 am
content: Hey guys,Fairly common problem i guess, at least based on Google results. I not able to talk to a TQFP atmega328p on a custom pcb.Problem:When i try to program using avrdude i get:Code:avrdude: stk500v2_command(): command failedavrdude: stk500v2_program_enable(): bad AVRISPmkII connection status: Unknown status 0x00avrdude: initialization failed, rc=-1avrdude: AVR device initialized and ready to accept instructionsavrdude: Device signature = 0x000000avrdude: Yikes! Invalid device signature.avrdude: Expected signature for ATMEGA328P is 1E 95 0FSo yeah, something is wrong. The power is 3.8v, i have a 10k pullup on the reset line and a 16MHz crystal with 22pF caps. Every vcc pin has a .1uF cap. All normal stuff, but for some reason it' not working.I've measured continuity on all SPI pins, everything looks good. However when i look at the crystal pins with a scope i get a steady 1.8v on one pin and 2.6v on on the other. It's no oscillating as it should, which i'm guessing is causing the problems..Board layout with the crystal lines highlighted:I've tried replacing the crystal with another one. I also replaced the MCU. No change in either case..I also tried building a barebones arduino based design on a breadboard and hooked the crystal lines into this board. This worked (!?). So maybe it's no the crystal after all?There's a MAX6675 on the SPI lines too, could this cause issues with programming?Any suggestions on what i should try next to try to find the problem? Is there a way to somehow test that the MCU is still alive, i guess it's possible it got fried from the heat while soldering?..Here's the schematic too:Any pointers are greatly appreciated!

author: amyk
author_rank: Super Contributor
timestamp: June 13, 2013, 11:04:32 am
content: The extra capacitance of a scope probe can be enough to cause the oscillator to stop; they're quite sensitive to that. Better to scope the programming signals and verify that they're within tolerance.You should also read the layout guidelines for the oscillator as your layout looks nothing like the one Atmel recommends:http://www.atmel.com/Images/doc8128.pdf

author: bingo600
author_rank: Super Contributor
timestamp: June 13, 2013, 11:27:42 am
content: You could have 2 issues here.1: An AVR is factory set to use the internal Oscillator @1Mhz (On a M328P - 8Mhz Internal Osc and "divide by 8"is selected)So the Xtal isn't driven on a "Virgin AVR"Note: The programming speed must be Max Fosc/4 , This means on a "Virgin AVR" Max programming speed is 250Khz2: The External SPI chip could interfere with the ISP/SPI programming lines of the AVR.Assuming that the external SPI chip doesn't interfere (seriously).First make sure the programming frequency is 250Khz or lower.Then set the fuses for external highspeed Xtal , seehttp://www.engbedded.com/fusecalcwhere it will output avrdude settings.This might not be bad for a start :Code:avrdude .... -U lfuse:w:0xff:m -U hfuse:w:0xdb:m -U efuse:w:0xfd:mElse look at the Atmel design guides AVR042http://www.atmel.com/Images/doc2521.pdf(pg. 6) , shows that Atmel recommends resistors between the ISP pins , and external SPI chips.Hope that helps .../Bingo

author: akupila
author_rank: Newbie
timestamp: June 13, 2013, 11:47:44 am
content: Thanks for the quick reply amykFair point, i had to take some shortcuts due to other design constraints. But i'll try to improve the crystal placement in Rev B.That said though when i hook up an external chip's crystal lines to this one it works fine. Having a 10cm cable in between is of course definitely worse than on the pcb.Also regarding the oscilloscope: it doesn't matter if have the probes hooked to the pins or not - the result is the same in terms of programming. If i probe the MISO, MOSI, Reset and SCK lines i see some binary data flying by on all of them (i don't have a logic analyzer). The signals are very clean from 0 to VCC (3.8v)Thanks Bingo too, I've tried setting the fuses but every time the response is the same, i cannot talk to the device at all. I think i'll try removing that external SPI chip..

author: bingo600
author_rank: Super Contributor
timestamp: June 13, 2013, 11:50:17 am
content: Quote from: akupila on June 13, 2013, 11:47:44 amAnd you are sure that the avrdude ISP speed is max 250Khz ?What programmer HW are you using , a real STK500 ?To show the full avrdude commandline , might not be a bad idea.Edit: Ohh ... I do hope the 100nF on the xVCC lines are just "invisible" on the schematic , else you have serious trouble ahead./Bingo

author: akupila
author_rank: Newbie
timestamp: June 13, 2013, 12:31:40 pm
content: Hmm, when i was going to copy the avrdude command line i realized it was set to use STK500 when i have an AVR ISP2.. d'uhI did remove the other chip too. I think it actually was the incorrect avrdude command but it's now working fine. Also the crystal is oscillating at a nice and steady 16MHz. I guess if you think it's a hardware problem it's actually in software (and the other way around)..An yeah the decoupling caps are outside of the schematicThanks for you help!

author: bingo600
author_rank: Super Contributor
timestamp: June 13, 2013, 12:48:53 pm
content: Nice that it worksBut it seems that "The Dude" have seen a ISPMKII , and could talk to it. (i missed that)QuoteSo depending on what you changed on the commandline , to get it to work.You might still have a HW prob with the SPI chip.Do you control the MAX6675 /CS in software , or have it tied to GND ?If SW controlled , i'd say you might need a 10k pullip on /CS to make sure it's inactive unless explicit driving it low.The AVR pins are tristated during the programming (RESET) , and can't drive.If it's tied to GND , you will need some series resistors .... As the chip will always be actibe on the SPI/ISP lines.And do the above sw + pullup in rev. 2./Bingo

author: akupila
author_rank: Newbie
timestamp: June 13, 2013, 01:01:59 pm
content: Seems like the chip has something to do with this too. I soldered it back and i have the same issueCode:avrdude -C/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/etc/avrdude.conf -c avrisp2 -p atmega328p -c stk500v2 -P usb -U flash:w:.build/uno/firmware.hex -vThe chip's CS line is software controlled but all the pins are directly connected to the MCU (no pullups/downs).Here's the schematic for the chip:So i guess i should put a pullup on temp_cs for rev b. Any other suggested changes?

author: akupila
author_rank: Newbie
timestamp: June 13, 2013, 01:08:16 pm
content: Ok, i tried putting a 10k pullup on temp_cs and that solves the upload problem again.

author: bingo600
author_rank: Super Contributor
timestamp: June 13, 2013, 01:09:23 pm
content: Couldn't you sneak a 10K pullup in on /CS , using wires ?That'll tell you if it's enough , or you still needs series resistors ....And maybe make your prototype workingOopzz : Too late .... But now you got a working proto./Bingo

author: akupila
author_rank: Newbie
timestamp: June 13, 2013, 01:14:23 pm
content: Hehe yep. Ghetto wiring a through hole resistor over the IC