ATmega328p, Ram, Mhz(speed), storage..etc Project Guidance

Hi all,

Intro:
So I've been searching for atmega328 details..etc months and months ago and I opened a topic
here as well about I2C so I'm a bit familiar but I'd like to know from more experienced people more
and more in deep details bit to bit about the atemga328 because I have a project and need to
know exactly with what I'm working and what's the possibilities and abilities of the chip.

Atmega328 :
Flash: 32kb
RAM: 2kb
ROM: 1kb

32pins
Voltage 1.8-5.5v
0-20Mhz
(there is more but i wrote the basic details)

So I'm planing to build a board with several chips interacting with each other but as example let's take just 3 chips, why 3 and not 2? because with 3 there is more possible ways to transfer data. Few things that I don't know but I know from actual computers is RAM ram is used to store temporary data, the data which you send/receive or process in the (OLED, liquid display, serial as out or input).

2kb = 2.000bytes = 16.000bits (1 byte = 8bits).
As we know from ASCII table we store each character in a array of 8 bits which makes one character
= 1 byte. At this point we can read/write 2kb, 2k characters per second?

Example: We got 3x 328p chips connected thru I2C, 1st chip send a 500byte array of characters, the 2nd one 1000byte of characters and the 3rd one 1000 byte array of characters.
1st one send to -> 3rd
2nd send to ->1st and
3rd send to ->1st

We got a 500bytes going out of the 1st chip and at the same time 2000byte coming at the chip,
as we know there is 2kb RAM, so the chip will "crash"?
at my point of view I think it will.

About the speed Mhz

So we got 16Mhz 16.000Hz, or 16.000 times per second, mostly know as 16.000 cycles per second.
How exactly can someone understand the processing times per second and bytes/bits ?
16.000 that means is a 16byte/bit processor?
and what's the actual difference between Mhz and bit?

The main point of this topic is to understand and use practically the chip with the right coding and
saving storage adding more and more getting even the last drop of juce. To make a compact coding
with full storage and working almost at the edge of the bits

D.60

The first, and important point, is that it will be very much easier to build a project if it can all fit in one chip. Communication between chips uses up pins and CPU cycles as well as being another opportunity for mistakes.

500 bytes is a huge amount of data for a chip with just 2048 bytes of ram. Can you not reduce the data flow to small chunks of 4, 8 or 16 bytes or even 64.

Can you organize things so one of the chips is the master and the others are the slaves and the data flow is only between master and slave and never between slave and slave. If there is slave-to-slave communication it will make debugging much more difficult - you would need to monitor all 3 chips at the same time.

16MHz is 16 million per second. You seem to think it is only 16 thousand. Very roughly an Arduino does one simple operation on a byte for each clock cycle - such as copy a byte from a memory location into a register or add two bytes.

Most of the information you are looking for will be found in the Atmel datasheet for the Atmega 328.

...R

I2C fast rate uses a clock speed of 400,000 Hz. slow clock speed of 100,000 Hz.
So a 1000 byte array, or 8000 bits of data, can be pretty easily transferred in under 1 second,
1000 bytes does use nearly 1/2 of the available 2048 bytes SRAM, if 2 are sending 1000 bytes to 1 then you will have a problem. You could change to a '1284P chip, which has 16Kbytes of SRAM, more than enough. I offer boards in several form factors,such as: Cross Roads Electronics

Well I got a project but not sure if everything can fit in a single chip
Because of the big amount of sensors, servos..etc I need a different speed for
sensor and I need the servos to act/move real time, so at least I can try to minimize
to at least 2 328chips, I need the sensors to run at different clock speed as well
for that I heard that millis function can help, never used it but ill give a try.
Another thing is that I need to save all sensors data into a sdcard so that's the big question, will the chip be capable to handle 5+ servos, multiple sensors running at different clock speed (time,sec) and save all that data?

I gave an example of what im working with but its more complicated because that's not the only stuffs I need to put. I want to put minimum 3x chips as controll, sensors and saving data, to connect them I need I2C but I need more and more ram to process and send data and I need all of them real time.

Well that's my story could you help?
Im able to use only atmega328p, I'd gladly take a offer working with other chips but right now I have only 328p and that's the only I should use.

D.60

What do you mean, you "need the sensors to run at different clock speeds"?

I2C (and SPI) sensors are the only ones that have a "clock speed", and as long as you clock the I2C or SPI bus slower than the maximum speed of the slowest member, you should be good.

I would suggest, in order to get the most out of this forum and save us all as much time as possible, that you post a list of the sensors and other devices you're planning to use. At a minimum, we need to know how you're interfacing with each sensor (serial, I2C, SPI, analog, something else).

It might make sense to offload all the servo control to one of the many dedicated servo controller boards (these are boards that you tell over serial or I2C "set this servo to that position" and it holds that position until you tell it otherwise. Some may have other more advanced features. If you've got a lot of servos to control, that may be the way to go.

If ram ends up being the limiting factor (unless you're interacting with the internet or are coding badly, it usually isn't), the 1284p is your chip.

All the AVR chips are 8-bit processors. Their being 8-bit is not particularly relevant to a sketch writer though - it comes with their being simple, small (memory-wise), low power (computation-wise) chips.

This is unrelated to their clock speed (which happens to usually be 16mhz or 8mhz - but you can make it anything up to 20mhz). mhz (MHz for the sticklers) is million cycles per second. If you look at the Arduino code for delayMicroseconds(), which does a busywait loop, you can see where they calculate out how many instructions their math takes, hence how many microseconds it takes. As proudly touted at the top of every Atmel datasheet, most instructions are executed in a single clock cycle - though what looks like a single operation in C may compile to far more than one instruction. (division, and floating point math, for example, take a lot more instructions than you'd think)

what's the actual difference between Mhz and bit?

That's like asking "what's the difference between a turnip and an adjective?"

Atmega328 :
Flash: 32kb
RAM: 2kb
ROM: 1kb

32pins
Voltage 1.8-5.5v
0-20Mhz
(there is more but i wrote the basic details)

This is simply nonsense.
The correct use of the shift key and the right units is crucial here.
Flash: 32KiB
RAM: 2KiB
ROM: 1KiB

Also "MHz"

I think also you could go to ATmega1284P at first. It's offering the biggest RAM and it works same as 328. SMD versin is just little bit larger. Here is my version of Arduino:

http://forum.arduino.cc/index.php?topic=277260.0

It works reliably up to 24MHz!

Domino60:
I need the sensors to run at different clock speed as well for that I heard that millis function can help, never used it but ill give a try.

I'm guessing you mean different sampling frequencies and, if so, millis() is probably the answer. Have a look at the demo Several Things at a Time. It is rather surprising that you have not come across it at least once on the course of nearly 600 Forum posts (at the time of this Reply) as it is a Sticky at the top of this Section.

...R

DrAzzy

What do you mean, you "need the sensors to run at different clock speeds"?

I ment time not clock speed, time/seconds, I got GPS which needs to run at 1sec because that's how it's
working on my project (let's not get in deep details about that).

I will give extra details to be easy for people to understand what im doing:
I'm working on a project which I need to control servos wireless so that means I will have a device and I will control 2nd device, on the 2nd device i will have servos, sensors, sdcard and other output components(leds..etc). I need to send data wireless to control the servos but in the same times servos will be controlled by the data on the sensors...etc
That's why 28pins will not be enough for all the things im gonna use.

Robin2

I have read your post with millis(); and it's a really nice example,
you declare 2 constant values (ex. 1000 "1.sec" and 0) and create a function which loop a
if statement which tells if the constant values are equal then do execute the function.
I remember when I learned C/C++ everything was inside the loop and after 1st semester everything was as
functions and in loop only the function name :slight_smile:
Just one question, while the loop loops :smiley: when the loop do a cycle, the loops start count (number) from 0? or it keeps going counting as a clock seconds? as in your example the loops should reset himself to 0 (kinda logic).

D.60

Just one question, while the loop loops :smiley: when the loop do a cycle, the loops start count (number) from 0?

There is no counter of the number of times the loop function executes.
There's nothing to stop you from adding one, if you so desire.

There is no counter of the number of times the loop function executes.
There's nothing to stop you from adding one, if you so desire.

I mean the real time, clock, sec/min/hour.

Ok but how exactly the millis() count the time while they are(functions_variables) in the loop?
there should be a logical explanation how exactly that works.

D.60

Domino60:
Ok but how exactly the millis() count the time while they are(functions_variables) in the loop?
there should be a logical explanation how exactly that works.

I am finding it difficult to make sense of your questions - you do not explain clearly what it is that you don't understand. And the smileys don't help.

The millis() function just returns a value which is the number of milliseconds since your Arduino was started or reset.

I have the feeling you need to spend a few hours studying the Arduino Reference section where all sorts of things are explained.

...R

The millis() value is incremented behind the scenes by the timer0 overflow interrupt - so it still happens while other stuff is going on, as long as you don't disable interrupts for longer than 1 millisecond at a time.

Arduinos don't have a "real time clock" - if you need a real clock that keeps good time over long periods of time, you get an external RTC module.

I know that arduino don't have a real time clock, it runs only by the time it starts and count the actual sec.
But I still don't understand how several functions can run with a different execution time while the void loop cycle all the time, isn't that affecting the functions? is the void loop reseting after each cycle or it keeps counting the seconds? because if it does then how the fuctions/millis() understand when the functon needs to run while there is only constant variables.

Read the bellow only if you want, it's annoying even for me to read what i wrote :smiley:

Let me give an example:

I declared a constant variable x = 1000; (1sec) and a another constant cr_y = 0; ,
pr_y = 0;

I got my led1 (as example) to make it turn on/off (blink) at 1sec (1000ms). I got my function down bellow the
loop that tells :

void loop(){
Led_1_State();
}
x = 1000;
cr_y = 0;
pr_y = 0;
void Led_1_State() {

 if (led1 == LOW) {
   if (cr_y - pr_y >= x) {
      led1 = HIGH;
      pr_y += x;
   }

So i got all that (constant) variables which i run them in a function if to get the actual state of the led.
When i declare the function in the void loop, the loop runs the function, enter in the function and start running it
a amount of time need to pass so the led to blink, but in the same time in the void loop there can be more functions, is the void loop run the functions one by one or all in the same time?

If the void loop runs all one by one (functions) there is a problem that it gonna take more time to run several
sensors and lag all the system.
If the void loop runs all the functions in the same time how the declared variables run again and again with the same looped constant value, is the loop reset itself after a cycle of loop? what if the other functions need more time to cycle, they all wait or how that happens?

" loop" is a function, not a "void".

It runs functions one at a time, because the processor had a single core without any kind of hardware threading.

how the declared variables run again

Variables do not run, so your question makes no sense.

Also " constant variable" is tautological; something is either a constant or a variable. It cannot be both.

It runs functions one at a time,

That's a problem then, if you need to run 2 functions in loop but you need to run 2 leds at different speed
you need to wait the 1st one and after that the 2nd one which makes a hole in the universe, it's not running at
constant ms you want but with a delay because of the (running once a time"functions in loop").

Variables do not run, so your question makes no sense.

what if i take a int i =0 and run it in a if (i =0; i<n; i++){} ? variables increase and decrease their values.

When i declare the function in the void loop,

I'm not sure what you're saying here.
You can put a function prototype inside another function (declare it), but you cannot define a function inside another function.

what if i take a int i =0 and run it in a if (i =0; i<n; i++){} ?

I'll repeat: you cannot run a variable.
You can access or reference a variable.

That's a problem then, if you need to run 2 functions in loop but you need to run 2 leds at different speed

It's not a problem.
Really, it's not.

Besides, LEDs, like variables, do not "run".
They illuminate or extinguish

Domino60:
what if i take a int i =0 and run it in a if (i =0; i<n; i++){} ? variables increase and decrease their values.

No, you will get syntax error.

Basic If Syntax

The structure of an if statement is as follows:

if ( statement is TRUE )
Execute this line of code