Used to Arduino's - How to program a processor for product? - Page 1
Forum section: electronics / beginners

author: smiba
author_rank: Contributor
timestamp: October 18, 2017, 01:08:58 am
content: Hi!I was thinking about posting this in the microprocessor subforum, but since its such a beginner question I decided this was a better place. If it's not please move itI've finished most of the design of my product and its all working alright, but I would love to have some more control of some of the chips and settings. Right now I can set some basic I2C commands on the bootup of the device which is enough to make it work. But I can't dynamically change settings, it can run a set of predefined commands but not apply logic to them.Also having a processor with a couple of analogue inputs would allow me to check the voltage of the power lines before I allow the system to continue bootingI'm not a programming superstar, but I have enough experience to make this work on an Arduino. What would you recommend for me to use for a production product? What tools are there available? Is there a way to work with just an atmega chip while keeping a friendly interface to do the programming with?Thanks!

author: TK
author_rank: Super Contributor
timestamp: October 18, 2017, 01:26:41 am
content: AVR MCUs can be programmed using C and you will have total control. All the peripherals are accessible as registers, so basically you are setting/resetting a bunch of bits on different registers. By peripherals I mean GPIO ports, i2c, SPI, UART and so on. ATMEL/Microchip offers free IDE for AVR programming.Depending on your application, AVR MCU can be limiting and you should look for cortex-M MCUs from a lot of vendors like ST Micro, TI, NXP and many others. All of them offer free IDE.You can start with the Teensy (3.2, 3.5, 3.6...) that has cortex-M MCU and can be programmed with Arduino IDE, then you can transition to pure cortex-M code.

author: Brumby
author_rank: Unknown
timestamp: October 18, 2017, 01:58:03 am
content: While the true embedded fan-boys will wince at the idea, there's nothing wrong with implementing an Arduino program on a production platform unless you have efficiency and/or timing constraints that preclude it. So long as the board you want to use is in the board library and you program within the capabilities of the board, it should be fine.However, if you want to spin your own board, then things get significantly more "interesting"...

author: Mr. Scram
author_rank: Super Contributor
timestamp: October 18, 2017, 02:05:11 am
content: Yes, it is actually not that hard to use an AVR chip on its own. You only need to take care of a few things and it will happily do its work. Most AVR chips aren't fussy about the conditions either, so there's quite a bit of room for error. You can make it as simple or complex as you want. Adding a JTAG connector is probably the easiest way to program the chip in your own circuit. The Arduino board has one*, so you can see how that works and figure out the wiring before you make your own attempt.If you build a board with a JTAG connector, an oscillator circuit consisting of just one crystal and two capacitors, one or two decoupling capacitors and a linear regulator, you basically have an Arduino without the USB capabilities. That sounds like quit a list, but is actually a small number of very doable tasks. The best part is that you get to use the IDE you already know, and the Arduino can be used for testing and as an example. When you get more comfortable with how things work, you can migrate to Atmel Studio for some proper low level control and efficiency. AVR chips are simple enough to understand, but complex enough to do some very interesting things.One thing to note is that the choice of microcontroller in a production product depends on many things, amongst which your experience with various microcontroller families, cost, power consumption, processing power, licensing and more. However, that tends to be less critical in smaller production runs. If you don't launch a product into a mainstream market with razor thin margins, any chip that gets the job done and doesn't have some prohibitive property, like excessive cost, should do fine.*Technically the Arduino board has two, but only one for the ATmega328P.

author: hermit
author_rank: Frequent Contributor
timestamp: October 18, 2017, 02:21:36 am
content: Agreed. The ease of popping DIP version of the chip on the board is a great starting point for learning micro-controllers.This guy posts a lot on the Arduino forum and this page will give you lots of what you are looking for.link

author: TK
author_rank: Super Contributor
timestamp: October 18, 2017, 10:39:23 am
content: Quote from: smiba on October 18, 2017, 01:08:58 amThis is something that the AVR MCU does. Even if you have analog inputs, you cannot use it unless the power is already at the level that the MCU needs, so there is no need to program this feature. You need to take care of when the MCU loses the power, which is controlled by the BROWN OUT detection settings (something similar to a grace shutdown). Maybe it is already done inside the Arduino Core, but if you do pure AVR coding, you need to control BROWN OUT by yourself.

author: smiba
author_rank: Contributor
timestamp: October 18, 2017, 10:54:46 am
content: Thanks! I'll look into AVR chips and Atmel studio!I'll see what chips are available that suit my needs.I currently have 3.3V and 1.0V voltages on the board for digital ICs with 8mV ripple or less at very high frequency in the Mhz. Could I simply use these to connect the processor? Not sure how sensitive these chips are to rippleQuote from: TK on October 18, 2017, 10:39:23 amThere are about 12 different LDO and buck converters on the board and 6 different voltages. Some of them like the main 3.3V are indeed useless to check because the chip itself powers from it, but analogue circuitry will function just fine with their voltages being off... Only not in the way that's expected.

author: TK
author_rank: Super Contributor
timestamp: October 18, 2017, 10:59:47 am
content: Quote from: smiba on October 18, 2017, 10:54:46 am3.3V with 8mV ripple should be fine, but you are limited how fast the AVR MCU can run. It is rated 20MHz @ 5V, I think it goes down to 8MHz for 3.3V. I am not sure if the AVR MCU is 5V tolerant when run @ 3.3V

author: Fire Doger
author_rank: Regular Contributor
timestamp: October 18, 2017, 11:55:24 am
content: Atmega328 doesn't have JTAG, only ISP.2.7V-5.5V can go up to 10Mhz (page 2 Datasheet)1.8V-5.5V up to 4MhzIts ok if you want to use Arduino compiler, at the end everything becomes Assembly instructions.It's not the best solution for many reasons.But If its gonna work or not its up to you, if you protect your code its up to you, if you use watchdog and Brown Out its up to you.atmega328P is just like any other MCU, you can tweak every register from arduino IDE.In conclusion, to make a robust product you will have to get your hands dirty, I would suggest to start with an arduino compatible chip and then start digging into registers.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: October 18, 2017, 12:17:59 pm
content: Quote from: smiba on October 18, 2017, 01:08:58 amEven in the Arduino environment you can drop down to the hardware level by addressing the registers directly so if the 328P meets your design requirement that is no excuse to change. Arduino or more specifically the "wiring" implementation does introduce some features you may wish to work around or disable... specifically if your code is in loop{} you will find work gets done outside loop (specifically Serial I/O)... you can disable that by never leaving loop. Additionally you will find Timer0 is working and interrupting in the background to run the millis() behaviour... if you don't need millis() you can disable Timer0 or just leave it as is. You will also find some functions are painfully slow (e.g. digitalWrite) but you can avoid these by writing to the PORT registers directly. (It's even possible to write down to the assembler level within the IDE if you need to).Finally, when you get to the product stage you will want to avoid the Arduino Bootloader and program direct to the chip, setting code protection bits as you go to stop the bad guys reverse engineering your killer product.Good luck!

author: TK
author_rank: Super Contributor
timestamp: October 18, 2017, 12:47:04 pm
content: It is true you can reach hardware level with Arduino IDE, but it can change key settings like timers used by delay(). I use direct port manipulation when I need high speed pin access.

author: TK
author_rank: Super Contributor
timestamp: October 18, 2017, 12:48:52 pm
content: Quote from: Fire Doger on October 18, 2017, 11:55:24 amThis is the information I got from page 2 of the datasheet:Speed Grade:0-4MHz@1.8-5.5V0-10MHz@2.7-5.5.V0-20MHz@4.5-5.5V

author: Fire Doger
author_rank: Regular Contributor
timestamp: October 19, 2017, 02:47:06 am
content: Quote from: TK on October 18, 2017, 12:48:52 pmExactly! Higher clock requires higher voltage. Ofc on 5V everything will work. But if for example you want 10Mhz you must have at least 2.7V. And vice versa if you have 2.7V you can go only up to 10Mhz.Also keep in mind that if you change the clock then you have to reconfigure every (clock dependent) peripheral and library to work on the new clock to keep the same timing. Arduino runs on 16Mhz and everything is configured for 16Mhz (both hardware and libraries).Wiring is made to hide this from user so a clean project on Atmel studio maybe is better solution rather than digging in arduino's config files.Its up to you and what your project needs to decide if you want to go on arduino or Atmel and 3.3V or add 5V.Check out if there is any 3.3V arduino variant with proper configs (plug n play) and what clock its using etc...

author: TK
author_rank: Super Contributor
timestamp: October 19, 2017, 10:49:49 am
content: Quote from: Fire Doger on October 19, 2017, 02:47:06 amThe Seeeduino board has a user selectable 5V - 3.3V switch and it runs the ATMEGA328P @ 16MHz even when 3.3V is selected, and it seems to be running reliably. I have been using it for years and it is excellent for testing all the sensors that require 3.3V logic.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: October 19, 2017, 11:43:50 am
content: The datasheet for the ATmega328P is quite explicit.... to be in the "Safe Operating Area" you need to be at 4.5V or above to operate at 16MHz. If you are operating at 3.3V and are not hugely timing accuracy dependent then you could just use the 8MHz internal oscillator. (You can actually still use a 16MHz XTAL and use CKDIV8 to operate safely at 2MHz if that was your sort of thing). CKSEL fuse bits are used for the different modes (http://www.instructables.com/id/How-to-change-fuse-bits-of-AVR-Atmega328p-8bit-mic/shows someone tinkering). Changing a bootloader to a different frequency is well-doable (necessary if you don't want to confuse your UART and timer rates!)

author: Mr. Scram
author_rank: Super Contributor
timestamp: October 19, 2017, 12:37:22 pm
content: Quote from: NivagSwerdna on October 19, 2017, 11:43:50 amNote that the accuracy of the internal oscillator is dreadful. Even if you're aiming to something very foregiving when it comes to timings, using an external crystal isn't a bad idea.

author: james_s
author_rank: Super Contributor
timestamp: October 20, 2017, 06:14:40 pm
content: I use Bascom AVR for programming AVR micrcontrollers. If I were starting out from scratch today though I would probably just go with C. Especially if you're used to Arduino, you already almost know C.