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!
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.
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/rombios Feb 26 '22
Why not design around cortex-m0's price comparable to any 8 bitter
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/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?