This is something thats really bugged me since I've graduated. We programmed PIC16f877a micrcontrollers in class but never got into the how it works, just the make it work. So let's say I write a program that makes LEDS flash in a specific order and send this program to the microcontroller through this special adapter to the right pins. What do you know, the LED's begin to flash as instructed. Now, I disconnect the power and remove the PIC from the breadboard. I put the PIC back in its correct spot, plug in the power, and the LED's still flash as previously programmed without having to reprogram it. How is this happening? How does this microcontroller essentially "remember" what I told it to do?
-
16the F in 16F877 refers to Flash memory, which is electrically erasable and reprogrammable. Older 16C57 used CMOS EPROM which was UV erasable and reprogrammable (unless you bought the cheaper package without a window : bit difficult to erase that one!) Older PICs still, before Microchip bought the design, were mask programmed at the factory to your design. – Oct 11 '20 at 18:03
-
4It is programmed into memory which stores it until erased and reprogrammed. Check out how "flash memory" works. – Justme Oct 11 '20 at 18:04
-
8Does your PC "forget" data you stored e.g. in your file system? It doesn't because there is some kind of non-volatile memory involved (typically flash memory like in a usb stick) – Sim Son Oct 11 '20 at 18:09
-
1Related: https://electronics.stackexchange.com/questions/483519/how-does-rom-work/483521#483521 Potentially of interest: https://electronics.stackexchange.com/questions/474419/what-happens-at-hardware-level-when-we-feed-a-code/474424#474424 – DKNguyen Oct 11 '20 at 18:15
-
1Another meaning for Flash ...flash memory. Sent serially from upload – Tony Stewart EE75 Oct 11 '20 at 18:31
-
1No one has yet mentioned my absolute favorite memory -- core. Or my absolute most hated memory type -- mercury delay line tubes (magnetic bubble memory does have a bad memory for me, though.) All of which I've experienced. – jonk Oct 11 '20 at 18:34
-
8@jonk: There's a reason -- the PIC has none of those types of memory, so they're wildly off-topic! That said, I have a 16k x 18-bit core memory board from an old DEC minicomputer that I would love to try to get working some day! – Dave Tweed Oct 11 '20 at 18:47
-
1there is a course, called "computer architecture" which is an essential subject to be passed, before moving to real microcontroller courses. that's the responsibility of a college or school. your question, and many similar things could be answered with that. – Tirdad Sadri Nejad Oct 11 '20 at 18:58
-
1you could simulate a single byte of non-volatile memory with 8 birthday balloons ... inflate them and tape them on the wall all in a row ... what you see is a representation of all `1` bits, a byte containing a value of `11111111` ... deflate some of the ballons to introduce `0` bits into the byte ... a similar thing is done inside the PIC, but using electric charges instead of air ... the same way that some of the balloons lose their air, the memory in the PIC will also lose their data bits, a process that takes decades – jsotola Oct 11 '20 at 19:02
-
3@DaveTweed Those magnetic cores are unique and very, very difficult to find being made today. In fact, I don't know if ANYONE makes them, anymore. There is a certain and very precise type of core hysteresis required for them to work well. If for any reason you decide to free that thing up, let me know!!! I will make it work. You've got 300k cores there! I've got 5 lbs of #43 wire. Might be enough! Or if you need detailed info, just ask. Also, the MSP430 has some FRAM versions. So there is some fun to be had with newer MCUs. Just it's not "core." It's kind of a crappy version. Not RAD-hard. – jonk Oct 11 '20 at 19:29
-
3Does this answer your question? [How does ROM work?](https://electronics.stackexchange.com/questions/483519/how-does-rom-work) – Dmitry Grigoryev Oct 12 '20 at 07:42
-
1the same way your phone remembers when you power it off and then on again. – old_timer Oct 12 '20 at 13:19
-
1@jonk Do you think you could write a self-answered Q&A pair (or pairs) on [retrocomputing.se] about it? – wizzwizz4 Oct 12 '20 at 21:04
-
1@jonk: [CuriousMarc](https://www.youtube.com/results?search_query=curiousmarc+core+memory) has several good videos showing many of the intricacies of driving and sensing core memory correctly. As you say, the board that I have has 295k cores on it -- they are VERY tiny! -- and a bunch of identical ICs that appear to be row/column drivers. I can't find any data on the chips, so I need to do some serious reverse-engineering... – Dave Tweed Oct 13 '20 at 00:03
-
@DaveTweed Thanks, Dave. I'll review the videos! The concept isn't that difficult, though. So you don't need to reverse engineer anything. Just forward engineer it. A little testing will be needed, obviously. But it's not rocket science. – jonk Oct 13 '20 at 02:24
-
The `f` in 16f877 stands for Flash memory which is the same memory as used by SD cards and SSD (SATA, NVME, PCIE etc) – slebetman Oct 13 '20 at 05:01
5 Answers
The PIC includes a type of nonvolatile memory called "Flash EEPROM". When you "send your program to the microcontroller", the special adapter is in fact programming your code into this memory. The physical memory is a special kind of transistor with a floating gate that can store a charge more or less indefinitely, even with no power applied. In most devices, "charged" = 0, "no charge" = 1.
Whenever the PIC is reset or powered-up, it begins executing your code, fetching it directly from this memory.
On the PIC, this memory is integrated into the same chip that holds the CPU and the rest of the logic (peripherals, etc.). The same kind of memory is used to hold the BIOS in your desktop/laptop PC, but it's a separate chip from the CPU. The first few screens you see when you switch on your PC come from the BIOS, which then loads your operating system from hard drive or SSD and runs that.

- 168,369
- 17
- 228
- 393
-
1But how exactly does that work? This "memory" is essentially just 1s and 0s. How can it hold this infinite charge and I guess "remember" where and what pins to send the output signals to? – Justin Montego Oct 11 '20 at 18:13
-
3The data is 1s and 0s. The physical memory is a special kind of transistor with a floating gate that can store a charge more or less indefinitely, even with no power applied. In most devices, "charged" = 0, "no charge" = 1. See the Wikipedia article for details. – Dave Tweed Oct 11 '20 at 18:14
-
13@JustinMontego, if I understand your confusion correctly, you seem to think that the PIC outputs charge to the LEDs directly, from the energy you put in it during programming. It doesn't work like that. The PIC needs external energy to operate, and its program sort of only diverts that energy to its peripheral pins as instructed by your program. You can see that - if you lower the voltage you supply to the PIC, the LEDs will become dimmer. – anrieff Oct 12 '20 at 06:14
-
4@JustinMontego As for remembering what pins to do what with, those "ones and zeros" form instructions to the processor. 8 bits are put together to make a number from 0 to 255. For the processor, different values for that number means different instructions, like "add two values together", "go to this instruction", "compare against this value" or whatever. The processor circuit, how all the transistors are wired inside it, sets the meaning of those values and what the processor does with things. – Graham Oct 12 '20 at 06:41
-
4And you can look in the datasheet if you are curious about which numbers mean which instructions. – user253751 Oct 12 '20 at 11:48
-
1I think the use of the term "flash EEPROM" here is misleading. The more common term these days is simply "flash" or "flash memory". Technically, USB flash drives and SSDs are also "flash EEPROM". Now, microcontrollers these days do indeed contain flash memory but they also often contain a small block, maybe a few hundred bytes, of more traditional byte-programmable EEPROM. But the flash is where the programs are stored. The distinction between these types of memory is that flash memory is higher density but must be reprogrammed in large erase blocks at a time. – tom r. Oct 13 '20 at 07:57
There are quite a few parts to make the magic of programming a microcontroller work.
Your development toolchain (compiler, assembler, linker etc) translates your program from source code to a series of machine instructions which will be stored on the microcontroller.
The micro controller normally stores the program in flash memory (UV erasable and one-time programmable microcontrollers also exist). This is a special type of memory that is designed to retain data even when the power is turned off. These rely on a structure known as a "floating gate mosfet". Essentially each mosfet in the memory array has a tiny capacitor in series with it's gate and the charge on this capacitor changes the behavior of the transistor.
The floating gate is surrounded by insulating material and therefore under normal conditions charge can neither enter or leave the floating gate. This allows the floating gate to retain charge and hence information even when the circuit is powered off.
However when sufficiently high voltages are applied, electrons can cross the insulating layer. This allows charge to be added to or removed from the floating gate. Generally the mechanism for charging the gates (known as "programming") can be done on an individual bit basis, but the method for removing charge (known as "erasing) can only be done for a whole block of memory at a time.
When you connect your programming adapter and tell the software to program the micro-controller, it puts the microcontroller into a special "program mode". The details of this vary between different microcontrollers, but on your PIC16f877a it is normally done by applying 13V to the Vpp/MCLR line. On older flash chips Vpp would actually supply the power needed for programming, but on your PIC it is just a signal line with whatever voltages are needed to program the flash array generated internally. Once in program mode the processor stops executing normal instructions and instead responds to special commands sent over the programming interface. On PICs this is a synchronous serial interface with a unidirectional clock line PGC and a bidirectional data line PGD.
Once in programming mode mode, the programming software will send a series of serial commands through the programming adapter to write your program to flash. Two different approches are possible, in one approach "erase and program" commands are used to erase and reprogram one block at a time. In the other approach the whole chip is erased with a "bulk erase" command, then "program only" commands can be used to program the new data. In both cases before issuing an "erase and program" or "program only" command the data must first be loaded into staging registers. You can find the gory details in http://ww1.microchip.com/downloads/en/devicedoc/39589b.pdf

- 21,158
- 1
- 38
- 76
First things first...do you understand how a one-time programmable fusible memory works? Like if I had a billion little wires and each one I choose to connect to either +V for a logic HI (ONE )and GND for a logic LO (ZERO), do you understand how that stores a program in itself of ones and zeroes? You could take a voltmeter and for each wire you could measure whether it is connected to +V or GND to figure out whether the bit at that location is a HI or LO. A microcontroller does the same thing.
Everything else is a fancy, more flexible version of this concept: How does ROM work?
This tends to lead to the question: "How does a microcontroller take the program of ones and zeroes and do stuff with it What happens at hardware level when we feed a code?

- 54,733
- 4
- 67
- 153
Program code is stored in microcontrollers ROM memory (in this case flash). Stored data in such memory is retained after power-off. Your microcontroller has 14KB of flash memory, there code resides after programming.
This memory is based on MOSFET structures, which retains data after power is disconnected. For more information on how flash memory works I suggest you to read wiki article.

- 792
- 4
- 6
Depending on the vintage of the microcontroller, it's equipped with either UV erasable EPROM in a ceramic package with glass window, 'one time programmable' EPROM in plastic packaging (for production) or Flash memory (typical of most current devices). Some can even be bought with mask programmed ROM for high qty manufacture.
Some programmers support various microcontrollers or the device manufacturer may have some dedicated EVB or whatever. Serial programming via SPI, USB, serial port (old school) or parallel port is also supported for some chips.
I rather like the 'in circuit programming' myself.

- 978
- 2
- 7
-
3Does *anyone* not like in-circuit programming? Except for applications where they need those extra I/O pins? – user253751 Oct 12 '20 at 11:49
-
1