The frequency the ATmega chip is running at, is hardcoded into the bootloader (ATmegaBOOT.c). While it is a configuration parameter in the Makefile, putting a bootloader on an Arduino-like board that does not run at 16.0MHz requires adjusting the Makefile and rebuilding the bootloader.
The best place to get Makefile and bootloader source for the ATMega8 is Arduino Starter Kit kaufen [verschiedene Ausführungen]
The ATmega8's Makefile defines DEFS= -DF_CPU=16000000 -DBAUD_RATE=19200 which needs to be modified to DEFS= -DF_CPU=1000000 -DBAUD_RATE=19200
The last thing that needs to be done is adjusting the Arduino IDE preferences file: ~/Library/Arduino/preferences.txt on the Mac and C:\Documents and Settings<USERNAME>\Application Data\Arduino\preferences.txt on Windows:
The build.f_cpu preference needs to be set to 1000000L
What about OSCCAL (oscillator calibration) register? Is it any way to set that register from an arduino sketch? or from the bootloader?
Or is the internal rc oscillator is good enough even without setting the OSCCAL? I was planning to use 9600 serial comms and it would be good to know if OSCCAL is needed and how to set it up.
The adc prescaler is set to 128, resulting in 16 MHz / 128 = 125 KHz on the Arduino. A 1MHz clock will result in 7,8kHz for the ADC...
And about OSCCAL: if you run the Arduino at 5V at 25 degree Celsius, then it should work with 9600baud. I tested it on attiny84 chips and only a few of them make problems. But if temperature changes or voltage drops, the internal clock might run too fast (or too slow) and then falling back to 4800baud should solve the problem.
Just to give you some numbers:
I changed the fuse to internal oscillator @8MHz without divider and clock output on pin 14. According to my scope the actual output is 8.1 MHz.
I reflashed the bootloader using the new board entry, but when I try to upload my program the board is not recognized... if I put a 16mhz crystal back in it programs.... what am I missing?
If your main goal is to save power and be able to run at lower voltages then what you are looking for is ”CLKPR – Clock Prescale Register” which determines System Clock Prescaler for dynamic frequency scaling. It is initialized during reset with value of CKDIV8 fuses, but you can change it anytime during runtime. No need to avoid external oscillator or reprogram fuses every time.
Here is the excerpt from the Atmel datasheet:
The ATmega48P/88P/168P/328P has a system clock prescaler, and the system clock can be divided by setting the ”CLKPR – Clock Prescale Register” on page 377. This feature can be used to decrease the system clock frequency and the power consumption when the requirement for processing power is low. This can be used with all clock source options, and it will affect the clock frequency of the CPU and all synchronous peripherals.
CLKPR = (1<<CLKPCE);
CLKPR = 0011b; // Divide by 8
Of course, as it was pointed out earlier, any code that assumes CPU speed will be affected.
You can start up at medium speed, measure your supply voltage, and then either drop down to a lower speed if you need to conserve power or ramp up to a higher speed if you are connected to a host and power is not an issue.
I am not an expert on Arduino bootloader, but I believe you can configure it for default communication and then drop your speed when your sketch starts running.
yeah, I just think I'm doing it wrong. I think all the fuses are reset when you flash the bootloader, so maybe I'm just makeing changes in the incorrect order.
but somehow I seem to be meseeing it up, it never runs without a resonator in. I have a pretty simple device created, I just need it to run at lower voltages, and the time to be reasonably correct. I guess I'm stumped for now.
Hi! New to the boards... came here in seek of a way to run an arduino pro 3.3v (from sparkfun) on 2 AAs. This is important because I'm actually just stealing the power rail from a wireless xbox controller. The two batteries only give me 2.6-2.9 volts, which is only enough to boot the micro on a good day.
I tried running
CLKPR = (1<<CLKPCE);
CLKPR = B00000011;
in my setup loop but it still would not power on with 2 AAs. Are there additional steps or tricks I can try?
we were discussing changing the clock frequency in software.
you can't change the voltage in this way.
here are a couple of options:
buy an arduino that will run on 3.3V (e.g. arduino pro mini)
build a circuit like the Adafruit minty boost that takes 2 x AA and gives you 5V. This is useful if you also have other parts needing 5V anyway. I did it myself very recently.
Someone had mentioned earlier in this thread that voltage was linked to clock frequency, so thats what I was going off.
I did find a 3.3 volt dc-dc converter circuit that is now in the mail. That should work for sure. I was hoping to keep power consumption as efficient as possible by using extremely low voltage and not needing the loss in a dc-dc conversion, but I'll explore saving power with watchdog/sleep instead.
Someone had mentioned earlier in this thread that voltage was linked to clock frequency, so thats what I was going off.
Couldn't spot that statement just now, but... looking at the datasheet for the ATmega328P it does say that it will run at:
1.8-5.5V for a clock of 0-4MHz,
2.7-5.5V for clock 0-10MHz and
4.5-5.5V for clock 0-20MHz
So running at 4MHz in principle should mean you can go down to 1.8V which would give you decent battery life on AAs I guess.
Trouble is that if you're using the clock tweaks described previously then you'll still need to boot up at full clock speed first. Hmmm!
(You would probably need to connect Vcc on the pro direct to your supply as well, rather than going through U2, whatever that is - presumably a regulator.)
Sounds like it would be easier to reprogram a 4MHz bootloader into the 328, really.
Hmm, interesting. I don't have any special hardware to burn a bootloader so I guess this project will have to go on hold. Something I'll circle back to as I am interested in revising this project to be low power.
I've got a stand alone battery prowered 328 project that runs off 3 AAs - it "works ok" but reading here is sounds like I'm on the edge of it running successfully at 16MHz.
I was considering dropping the crystal down to 8MHz to give me a wider power margin - Comms is not an issue as I'm programming the 328 in an Arduino then taking it out and dropping it in my PCB.
But it sounds like just changing the crystal won't work either and it will require a software tweak too - is that correct?
As I understand it, clock speed is hard-coded into the bootloader, so changing the crystal isn't enough to change the speed. You would need to program a new bootloader in as well.
Given what we've been discussing about clock dividers above, it would seem that changing the crystal isn't actually necessary - a bootloader that sets the clock divider would suffice.