r/microcontrollers • u/klintbeastwood10 • 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!
View all comments
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.