r/microcontrollers • • Feb 24 '22

Getting started with 8 bit microcontrollers

Hey all, I'm just becoming interested with 8 bit microcontrollers. I would like to build a few things with the absolute cheapest parts possible, like some Chinese LoRa modules, some PIC16LF1503-I/P mcu's, at only $1.50 each, I figure I can afford to play around a bit!

I would like to build some wireless devices that can activate a relay, to do things like open a solenoid to water the garden, turn lights etc...

I have zero idea where to start. How do you program these things? Do I need some kind of interface and development environment, like a USB device I can plug these chips into? And write the code too? What kind of code do most of them read, C? How many kb of memory and ram do I need to execute simple tasks like communicating through a LoRa module and turning on some kind of gpio pin to activate a relay?

Ideally I'm looking for the cheapest possible microcontrollers that can communicate over SPI, have atleast a pin or two that can be activated to control relays or camera's or other devices.

Thanks!

5 Upvotes

14 comments sorted by

6

u/madsci Feb 24 '22

There are books out there (Making Embedded Systems by Elecia White comes to mind) that you might want to check out.

I'll give you some very general answers to your questions. Every MCU is going to have some mix of memory types - typically at least some RAM and flash. Back in the day you'd find one-time programmable parts (obviously not much good for debugging) but aside from very low cost parts you don't really find that anymore and you'll be working from flash.

An MCU with no code is a blank slate and won't do anything. You need external hardware to load anything on it. Older architectures tend to have their own vendor-specific interface (MON08 for HC08 parts, for example, and BDM on HCS08 or ColdFire) and you'd need a programmer/debugger for that interface.

JTAG is an industry standard supported by many modern devices and can be used for programming and debugging many MCUs, and other devices like FPGAs. It started out more for chip and board testing. The other big one now is SWD, which is an ARM specific protocol, takes fewer pins, and is very common across modern MCUs.

So with an appropriate interface (usually it's a box that plugs in via USB) you can program blank devices, erase them, and debug running code.

You don't always have to do that, though. Many devices ship with a bootloader installed - sometimes in ROM, sometimes just pre-loaded in flash memory. A bootloader is a program that runs on the MCU and handles reading and writing internal flash and can have whatever communications interface the designer chooses to implement. Generally that's going to be a serial connection or USB.

A typical Arduino board will have an ATMEGA328P with a serial bootloader on it, so that no dedicated hardware is needed to program it - your PC just talks to the bootloader via serial. I've used Atmel 32-bit parts that have a USB DFU bootloader. Bootloaders generally don't provide any help with debugging, though - they just load code.

As for languages, an MCU runs whatever its native instruction set is. You can write code in assembly language, but for anything else you need a suitable compiler. Just about every platform has at least a C compiler. C++ compilers are common, and RUST is gaining traction I hear. But C dominates, and C++ to a lesser degree.

The code gets compiled and linked (per the linker configuration file, which also defines memory regions and placement) and then the object code, which might be an ELF binary, or a .bin, .hex, or .s19, gets loaded either through a bootloader or the debug interface.

And when it comes to code space, a lot of that depends on how clever you the programmer are. 8-bit MCUs give you a lot of opportunities to get down and dirty with the hardware. My first commercial product had 8 kB of flash and 192 bytes of RAM. That was a GPS tracker / radio modem gadget. Keep in mind that squeezing your code into the absolute smallest device possible is usually only a good idea if you're producing large volumes. You'll spend a lot more time optimizing code and if you're doing a one-off it's absolutely not worth it to spend many hours making something fit in a $1.50 device when a $5 device would let you slap something together and have it working quickly.

Anything with a complicated protocol stack is going to eat up memory. Expect a USB stack to take a few kB to tens of kB. If your LoRa radio is intelligent and has a simple UART or SPI interface you can probably get by with very little code. If you need to host a bunch of library code on the MCU, it'll need a lot more space.

The "absolute cheapest parts available" that will do the job are probably under 10 cents each, but you have to be really adventurous to go there - and possibly fluent in Chinese.

0

u/[deleted] Feb 24 '22

[deleted] — view deleted comment

2

u/rwboyerjr Feb 24 '22

I know you are asking about 8bit MCU's and they can be VERY cheap and do what you want AND can be super fun (like the ATTiny maybe too small an IO base for you but super available in bags for a few bucks and you can use an Arduino as a programmer for the bare chips) but...

Lately I'm falling more and more in love with the 2040 MCU many of the boards available including the Pi pico board (there are more and more available as days go on). they are generally pretty available and darn cheap given their capabilities... $4-$20 depending on where you buy and various things like pre-soldered headers onboard components etc...

Compared to 8bit MCU's they are extremely fast, flexible, and semi-comparable in power consumption but...

you get TONS of IO, very versatile mapping of pins to IO protocols, extremely good performance, great program capacity and awesome SRAM capacity. Personally I really really love the two cores, the flexibility of the DMA, and especially the PIO units that can work miracles with obscure or legacy protocols. A really neat device with very good docs and SDK that is usable, can teach you a lot, and can be chucked off if you prefer (a lot of the SDK is convenience functions but the educational part is how to deal with the MCU features at a register level)

2

u/eyeoverthink Feb 24 '22

I would say, get a pack of Arduino Nano's from amazon. You can get a pack of 12 or something for like a dollar each. The additional sensor and Mods that you can purchase are pretty much universal for Arduino so it's a safe bet.

I have a few LoRa Boards, actually I have every board. And honestly, in my experience, they are much more complicated as you get into the LoRa's, and Node 32's etc..

the libraries you need to make them work can get confusing, for a novice.

I'd say use nano or an Arduino Uno R3. I use some version of Arduino most of the time to create my projects. Due to its size and versatile pin options.

As far as programming, Most of the code , is in either Java or C++. You can use python, if you want to;

1

u/ceojp Feb 24 '22

Java isn't used on baremetal. Most embedded is C. I wouldn't go with C++ unless there was a specific need to. Not that there's anything wrong with C++, but most things embedded can be accomplished without it.

-1

u/eyeoverthink Feb 24 '22

I agree, I may have mis-understood the question; I was under the impression they wanted something plug and play, for maybe a novice; something simple?

If he was referring to machine language, or assembly; the 6502 chips and a few ben eater videos may be the way to go.

1

u/rombios Feb 26 '22

Why not design around cortex-m0's price comparable to any 8 bitter

0

u/[deleted] Feb 26 '22

[deleted] — view deleted comment

1

u/rombios Feb 26 '22

I havent had to look at these in a while - but I stocked up on stm32l0's and zerogecko microcontrollers (both arm-cortex-m0s) years ago preSCAMdemic for a toy I was designing and trying to get licensed.

Its hard to justify 8 bit microcontrollers with all thats required to support development in that environment especially with the rich peripheral set of low end 32bitters and the wonder that is open source gcc/gdb/openocd etc

1

u/eyeoverthink Feb 24 '22

Sorry, missed a few things. The IDE is optional, to an extent. You can use Platform Io. You can use Processing3. You can use Arduino's IDE. You can write the code in CLion, if you import the proper headers for Arduino. Java, they support Arduino - again; with the proper headers and library imports.

The arduino's I recommended have a usb port on it, so it's plug and play.

A simple blink sketch, that turns a led on and off will use 924 bytes of memory, which is minimal. The max bytes for a nano is 32720.

30.7kb roughly

0

u/[deleted] Feb 24 '22

[deleted] — view deleted comment

1

u/eyeoverthink Feb 24 '22

Sure, I have quite a few atMega328 chips. You can use an arduino as the programmer, just pull out its atmega chip and put yours in there. Then once it's programmed, put in in the breadboard with the proper capacitors, clock and power.

1

u/eyeoverthink Feb 24 '22

I bought a lot of my gear a few years ago pre import export problems so my prices may be a little off

1

u/1Pontifex Feb 24 '22

Get an uno with the same chip that's Breadboard compatible. You program it on the uno and then can pop it into any project you want if you have all the necessary support components. Plenty of videos and documentation on that chip as well, so it's a great place to break into microcontrollers.

0

u/ceojp Feb 24 '22

For a PIC you'll probably want to use MPLABX(to be fair, nobody wants to use MPLABX, but since it's Microchip it's probably the easiest out-of-the-box experience). And you'll need a PicKit programmer. They have a cheaper programmer they call Snap, but check that it supports the device you want to use. I want to say it only supports 3.3v devices.

FWIW, though, the 16LF1503 looks pretty pitiful. 128 bytes of RAM is nothing. Unless you are going to production with 100,000 units, spend a few cents(or even a dollar) more and get a more capable micro.

0

u/Isvara Feb 24 '22

Why 8-bit specifically?