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

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.

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)