V
V
Vadimm10012018-07-14 16:57:19
Programming
Vadimm1001, 2018-07-14 16:57:19

How to tune MK AVR to 1MHz without a quartz resonator?

Added a line
#define F_CPU 1000000
to the code, added a line
#ifndef F_CPU 1000000UL
to the delay.h file (compiled in Amel studio 7).
A ds18b20 sensor is connected to the MK, everything works in the proteus, but in a real MK, 0 is simply displayed on the display. Judging by the flickering of the display in a real MK, the frequency on it is not at all 1 MHz, but much more

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Makarenya, 2018-07-15
@Vadimm1001

If you were dealing with ARMs, you could adjust the frequency on the fly. Whatever he wanted, he put it right on the spot through the CMSIS API. But here you have an AVR, and the frequency is set through fuses. And through the fuses, the controller turns into a brick, so much so that without a high-voltage programmer (which is still a rarity), the MK cannot be revived back. And setting the frequency is one of those things whose wrong value can (almost) kill the MC.
Therefore, the first point - never put / uncheck boxes where you are not sure!
Here's a picture of the fuse setup in Atmel Studio.
When you get to this page, click Read to make sure the info on the screen is correct.
The bottom one is named SUT_CKSEL, and one above it is CKDIV8.
To start with SUT_CKSEL - you must have one of the INTOSC_8MHZ options. And it doesn't matter which one. All these KCK, CK and MS indicate the delay time between the power supply and the start of the MK, they are only needed for the case of operation from half-dead batteries without a power controller. They also fail to "speed up loading", we are talking about values ​​less than 0.1 seconds.
If your SUT_CKSEL value already starts with INTSOC_8MHZ, then don't touch anything there. You will be able to kill MK, but you still won’t do better for your task.
Now check for the CKDIV8 checkbox. And this is what you need. It needs to be installed. If it costs, then these very 8 MHz from the internal generator are divided by 8. Total - 1 MHz, then what you need!
Although, as far as I remember, this jackdaw is stitched at the factory, and you don’t need to climb into fuses at all.
If, after clicking on Read, you see that SUT_CKSEL is already showing INTOSC_8MHZ..., and CKDIV8 is already checked, then don't click anywhere else and just leave this page. I will not tire of repeating - any rash manipulations here will kill your MK. The checkboxes are especially dangerous:
RSTDISBL - set it and without an HVPP programmer you will not be able to do anything with the MK
SPIEN - but you can’t remove it in any way, otherwise the result will be no less deplorable.
Well, choosing any EXTXOSC_... clock source will cause your MK to become a brick before you connect this clock source. We chose a ceramic resonator - run to ChipDip for one and a couple of capacitors to at least put the fuses back!
And now about what you have already done: by
writing:
#define F_CPU 1000000
or
#ifndef F_CPU 1000000UL
in reality - completely equal lines, you thereby indicated to all libraries (like the same delay.h) that the MK will operate at a frequency of 1 MHz. The fact is that the delay(...) method or any other methods tied to real time or real time intervals without this line do not know anything about this time itself. They can only count the number of MK cycles. And when you specify that you need a pause of 1s, then based on the frequency entry (based on F_CPU), the delay.h library calculates the number of cycles that you need to hang inside the function in order to get a delay of 1s (in your case, of course, we are talking about a million cycles).
If this setting (F_CPU) is not performed, or if it does not reflect the real frequency of the MK, then all the waiting functions and others will work somewhat strangely - that is, wait either more or less than the requested time.
Well, after that - there are 1001 different reasons why 0 is displayed on the display. Including the display may be inoperative.
And one more thing - the ds18b20 sensor works according to the single wire protocol, the hardware implementation on the AVR of this protocol simply does not exist, and it is not really needed, given the extremely low speed of the protocol itself. And it should not be so important for software libraries what frequency the MK has. Therefore, it seems very strange to me your desire to get a frequency of exactly 1 MHz to interact with this sensor.

N
n12eq3, 2018-07-14
@n12eq3

for atmel, the clock source or its speed when working from the built-in generator is not set either by defaults or by firmware at all.
you need to change and flash fuse bits. atmel studio has a corresponding tab in the programming window.

A
Alex Other planet, 2018-07-14
@BadElectrician

For micro atmega8, for example, the clock frequency of the internal oscillator of 1 MHz is set by default, but in general it is set during firmware using special fusers. It depends on what device to flash ...

A
Alexander Komarchuk, 2018-07-15
@AlexanderKomarchouk

You were right about fuse bits. Use the fuse calculator.
Here are a few links for you to choose from:

  • Engbedded Atmel AVR® Fuse Calculator
  • javascript based AVR fuse calculator
  • AVR Fuse Calculator
  • AVR Fuse Calculator
  • FUSE Calculator
  • For Android, there is also an AVR Fuse Calculator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question