Go Down

Topic: Clock problem when programming ATMega328P over ICSP pins (Read 929 times) previous topic - next topic

miguelwon

I'm following this example from Adafruit that shows how to program an AVR such as ATMega328P from a Raspberry Pi, using the AVR's ICSP pins.

I run all the steps and it is working fine except a single issue with the clock. I'm testing the traditional blink example from Arduino IDE, that blinks an LED with intervals of 1 second. The problem is that instead of 1 second, my LED is blinking every 15 seconds! Any idea of what might be causing such a problem?

I using Raspberry 4 with Raspbian Buster and an ATMega328P. This is the code I'm compiling with Arduino IDE:

Code: [Select]
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

cattledog

My guess is that the 15 seconds is really 16, and you have changed the fuse settings so the processor is not running of a 16Mhz external clock source, but is running on some internal 1MHz.

Use avrdude to see what your current fuses are. The tutorial gave you a command which gives the fuse settings.

You want to see this
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D6
avrdude: safemode: efuse reads as 5

If that's not what you have, you will have to reburn the fuse settings.

Robin2

I use the "burn bootloader" option in the Arduino IDE to set the clock fuses. You only have to do it once with a new chip so it's not too much trouble.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

miguelwon

Thanks.

Yes, avrdude final output is not exactly that but:

avrdude: verifying ...
avrdude: 1052 bytes of flash verified

avrdude: safemode: lfuse reads as 62
avrdude: safemode: hfuse reads as DE
avrdude: safemode: efuse reads as FD
avrdude: safemode: Fuses OK (E:FD, H:DE, L:62)

avrdude done.  Thank you.



How do I reset the fuses? I can't use the "Burn the bootloader" option, because I just have a Raspi Pi and a ATMega328P chip (with the 16MHz crystal and two capacitors)

miguelwon

Ok, I just checked how I reset the fuses. For the present case:

-U efuse:w:0x05:m -U hfuse:w:0xD6:m -U lfuse:w:0xFF:m

Howerver, I have another issue. When I run avrdude:

Code: [Select]
sudo avrdude -p atmega328p -C avrdude.conf  -c linuxgpio -v  -U efuse:w:0x05:m -U hfuse:w:0xD6:m -U lfuse:w:0xFF:m


it says:

avrdude: safemode: efuse changed! Was 5, and is now fd
Would you like this fuse to be changed back? [y/n]


and if I confirm (y), it freezes.

Robin2

I can't use the "Burn the bootloader" option, because I just have a Raspi Pi and a ATMega328P chip (with the 16MHz crystal and two capacitors)
How are you connecting the Atmega 328 to the RPi ?

I have an 8Mhz Atmega 328 on stripboard that I use for uploading programs using ICSP to various chips. The Atmega 328 has the standard ArduinoISP program on it.  Sometimes it is useful to load one or other of Nick Gammon's diagnostic programs to the Atmega 328.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

miguelwon

I have Raspberry Pi pins MOSI, MISO, SCK  connected directly to MOSI, MISO, SCK atmega328p pins, and GPIO 12 to RESET.

I then have a 16MHz crystal and two 22uF capacitors connected to the atmega328p chip, like this setup:


https://www.arduino.cc/en/Main/Standalone


except for the power supply section because I'm using Raspi Pi 5V pin,.


Robin2

I have Raspberry Pi pins MOSI, MISO, SCK  connected directly to MOSI, MISO, SCK atmega328p pins, and GPIO 12 to RESET.
I have no experience with RPIs so I can't comment on that.

Do you have a second Atmega 328 that you could set up for 8MHz and use as an ISP programmer with the ArduinoISP program?

I used this Arduino to Breadboard Tutorial

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

miguelwon

Ok, I think I have it working properly now. According to this post "0xFD is now the same as 0x05 used to be." and so that part is ok. For the other two I just reseted the fuses with:


sudo avrdude -p atmega328p -C avrdude.conf  -c linuxgpio -v -e -U hfuse:w:0xD6:m -U lfuse:w:0xFF:m


followed by avrdude with the .hex blinking file and it started working correctly.

CrossRoads

0xFD = 11111101
0x05 = 00000101

The upper 5 fuses in that byte are not used and read back as 1s.

"I then have a 16MHz crystal and two 22uF capacitors"
22pF perhaps? The crystal won't oscillate with 22uF.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

miguelwon

Ok, thanks for clarifying that.

Yes, 22pF! My mistake.

Go Up