11
\$\begingroup\$

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?

\$\endgroup\$
16
  • 16
    \$\begingroup\$ the 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. \$\endgroup\$ – Brian Drummond Oct 11 '20 at 18:03
  • 4
    \$\begingroup\$ It is programmed into memory which stores it until erased and reprogrammed. Check out how "flash memory" works. \$\endgroup\$ – Justme Oct 11 '20 at 18:04
  • 8
    \$\begingroup\$ Does 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) \$\endgroup\$ – Sim Son Oct 11 '20 at 18:09
  • 8
    \$\begingroup\$ @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! \$\endgroup\$ – Dave Tweed Oct 11 '20 at 18:47
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – jonk Oct 11 '20 at 19:29
35
\$\begingroup\$

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ But 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? \$\endgroup\$ – Justin Montego Oct 11 '20 at 18:13
  • 3
    \$\begingroup\$ The 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. \$\endgroup\$ – Dave Tweed Oct 11 '20 at 18:14
  • 13
    \$\begingroup\$ @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. \$\endgroup\$ – anrieff Oct 12 '20 at 6:14
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ – Graham Oct 12 '20 at 6:41
  • 4
    \$\begingroup\$ And you can look in the datasheet if you are curious about which numbers mean which instructions. \$\endgroup\$ – user253751 Oct 12 '20 at 11:48
10
\$\begingroup\$

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

\$\endgroup\$
7
\$\begingroup\$

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?

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
0
5
\$\begingroup\$

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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Does anyone not like in-circuit programming? Except for applications where they need those extra I/O pins? \$\endgroup\$ – user253751 Oct 12 '20 at 11:49
  • 1
    \$\begingroup\$ I rarely leave any free ! \$\endgroup\$ – Graham Stevenson Oct 12 '20 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.