Categories: Microcontroller circuits
Number of views: 23111
Comments on the article: 0

PIC microcontrollers for beginners

 

In the modern market there are a number of families and series of microcontrollers from different manufacturers, among them AVR, STM32 and PIC can be distinguished. Each of the families has found its own scope. In this article I will tell beginners about PIC microcontrollers, namely, what it is and what you need to know to get started with them.

PIC microcontrollers for beginners

What is a PIC

PIC is the name of a series of microcontrollers manufactured by Microchip Technology Inc (USA). The name PIC comes from the Peripheral Interface Controller.

PIC microcontrollers have a RISC architecture. RISC - an abbreviated set of instructions, is also used in processors for mobile devices. There are a number of examples of its use: ARM, Atmel AVR other.

In 2016, Microchip bought Atmel, a manufacturer of AVR controllers. Therefore, the official website presents microcontrollers of the family and PIC and AVR.

Microcontroller PIC16F1xxx

Families

Among the 8-bit PIC microcontrollers, it consists of 3 families that differ in architecture (bit depth and instruction set).

  • Baseline (PIC10F2xx, PIC12F5xx, PIC16F5x, PIC16F5xx);

  • Mid-range (PIC10F3xx, PIC12F6xx, PIC12F7xx, PIC16F6xx, PIC16F7xx, PIC16F8xx, PIC16F9xx);

  • Enhanced Mid-range (PIC12F1xxx, PIC16F1xxx);

  • High-end or PIC18 (18Fxxxx, 18FxxJxx and 18FxxKxx).

Comparison of microcontroller families

The characteristics are given in the table below.

PIC Microcontroller Features

In addition to 8-bit microcontrollers, Microchip produces 16-bit:

  • PIC24F;

  • DsPIC30 / 33F for signal processing.

Representatives of the 16-bit family operate at speeds from 16 to 100 MIPS (millions of instructions per second completed). It is worth noting and features:

  • machine cycle - 2 cycles;

  • ADC resolution - 16 bits;

  • support a number of communication protocols (UART, IrDA, SPI, I2S ™, I2C, USB, CAN, LIN and SENT), PWM and more.

There is also a family of 32 bit microcontrollers - PIC32MX, the main features:

  • operate at a frequency of up to 120 MHz;

  • Perform up to 150 MIPS

  • ADC: 10-bit, 1 Msps (quantization speed), up to 48 channels.



What PIC to start with?

Beginners should start mastering PIC microcontrollers from an 8-bit line. In general, the manufacturer claims that the feature of the whole family is the easy portability of programs from one family to another and the pinout of a number of models.

One of the most popular microcontrollers in the amateur radio environment is PIC16f628A. Its technical characteristics are as follows:

  • There is a built-in clock generator. You can tune to work with a frequency of 4 or 8 MHz;

  • 18 pins, of which 16 - input / output, and 2 - power;

  • To operate at frequencies up to 20 MHz, you can connect a quartz resonator, but in this case there are not 16, but 14 legs left for input / output;

  • There is a letter F in the marking, which means that FLASH-memory with a capacity of 2048 words is used;

  • 14-bit instructions, 35 pieces;

  • 2 comparators;

  • 4 analog inputs;

  • PORTB inputs have pull-up resistors;

  • Two 8-bit timers and one 16-bit;

  • Machine cycle - 4 cycles of a quartz resonator or internal oscillator);

  • 224 bytes of RAM;

  • 128 bytes of EEPROM;

  • USART - serial port;

  • internal voltage reference;

  • powered by 3.3 to 5 V.

The reasons for its popularity are the low price and the ability to clock from an internal generator.

Which pinout of 16f628 is shown below:

Pinout at 16f628

The block internal circuitry of this microcontroller is shown below.

Microcontroller structure 16f628

What should I pay attention to in the scheme in the first place?

This microcontroller has two ports PORTA and PORTB. Each pin, each of them can be used as input and output, as well as for connecting peripherals or activating other microcontroller modules.

Consider this part of the scheme in large.

MK ports

For example, ports RB0-RB3 - can act as analog. If necessary, a clock source is connected to RA6, RA7 (quartz resonator) The outputs of the microcontroller themselves are configured in the input / output mode using the TRIS register.

There are commands for this type:

TRISA = 0; // All pins of port A are set as outputs
TRISB = 0xff; // All pins of port B are assigned as inputs
TRISA0 = 1; // So a separate pin is assigned as input (1) or output (0)
TRISA5 = 1// here the 5th output of port A is assigned as an input

In general, the operating modes, the inclusion of a WDT (watchdog timer), the choice of the clock source of the microcontroller and so on are configured using special registers - SFR, and the memory and data are stored in GFR - in simple words, this is static RAM.

In the official Datasheet, on pages 18-21 you will find 4 memory banks for special purpose registers SFR and general purpose registers GFR. Knowledge of registers is important, so print and learn the indicated pages from Datasheet.

For convenience, these tables are presented in the form of pictures below (the numbering of the registers, like everything in digital electronics, starts from 0, so the fourth number is 3).

Special Purpose Registers
Special Purpose Registers
Special Purpose Registers
Special Purpose Registers

How to connect and in what language to program?

To run this microcontroller, it is enough to apply plus to Vdd and minus to Vss. If you need a quartz resonator, then it is connected to pins 16 and 15 (OSC1 and OSC2) of the PIC16f628 microcontroller, for other controllers with a larger or smaller number of pins - look in datasheet. But this point must be indicated during programming and firmware.

Speaking of portability and coincidence of the pinout - on 16f84A - it is similar, and on many others.

Connection of a quartz resonator

A fragment of a circuit with an external resonator connected to pic16f628a:

Circuit fragment with an external resonator connected to pic16f628a

There are two main languages ​​for programming PIC microcontrollers - assembler and C, there are others, for example PICBasic, etc. You can still highlight the simplified programming language JAL (just another language).

For example, below is a program for “LED flashing” - a kind of “Hello World” for the PIC microcontroller in C.

An example of a PIC microcontroller program in C

In line 1, the PIC microcontroller library is connected, then the delay program library is connected.

In the main (void) function, the initial parameters are set at the beginning, just as we did in the Void setup () function - in articles about arduino. Then, in lines 11-16, an infinite while (1) loop is declared, during which the “LED blinking” program is executed.

In the example, the state of the port is constantly inverted, i.e. if it was at "0", then it will go to "1" and vice versa. In C for PIC there are the following command management commands:

PORTA = 0; // translates all the pins of port A to a low level (log. 0)
PORTB = 0xff; // translates all the pins of port B to a high level (log. 1)
RB5 = 1; // The fifth pin of port B is high

And it looks like the same program, but already in the JAL language, I translated into Russian comments from the developers of built-in examples in JALedit (development environment).

Sample JAL Program

There is a temptation to choose JAL, and it may seem easier to you. Of course, you can implement any projects on it, but from the point of view of benefit for you as a specialist, it is a useless language. You will achieve significantly greater results by studying the syntax and principles of programming in C (most of the languages ​​currently popular are C-like) or in Assembler - this is a low-level language that will make you understand the principle of the device and what happens in the program at any given time.


How to work

If you say quite generalized to work with any microcontrollers you need:

1. Text editor.

2. The compiler.

3. The program for downloading firmware to the microcontroller.

And I even read old textbooks, where the author, working from under DOS, wrote code, compiled and flashed it using various means. Now, for all popular operating systems, there are development environments, both highly specialized (for a specific family of microcontrollers or families from one manufacturer) and universal (either contain all the necessary tools, or they are connected as plug-ins).

For example, in a series of articles about Arduino, we looked at the Arduino IDE; in it, we wrote the code and with its help “poured” the firmware into the “stone”. For PIC microcontrollers there are such programs as:

  • MPASM - used for development in the Assembler language from Microchip;

  • MPLAB is also the Microchip IDE for PIC controllers. It consists of many blocks for testing, checking, working with code and compiling programs and downloading to the microcontroller. There is also a version of MPLAB X IDE - it features great functionality and is built on the basis of the NetBeans platform;

  • MikroC is a universal environment (not only for PIKs) for development. As the name implies, it is “sharpened” for C programming, and there are also programs like MikroBasic and MikroPascal for the corresponding languages;

  • JALedit - suitable for the JAL language that we mentioned above;

  • And a number of other lesser-known ones.

MPLAB IDE

How to flash a microcontroller?

There are a number of programmers for PIC micronotrollers. Officially considered PICkit. Their 4 versions. But you can flash and universal, for example, TL866 (it supports almost everything that a novice radio amateur might need, while it’s very cheap).

Programmers for PIC Micronotrollers

Also on the network there are a number of different programmer circuits for PICs, both for working through the COM port:

Programmer circuit

So it is via USB (in fact, also com, only through the converter on the IC MAX232).

Programmer circuit

Conclusion

PIC16 microcontrollers are suitable for simple projects, such as simple automation, voltmeters, thermometers and other small things. But this does not mean that you cannot do complex and large projects on this family, I gave an example of why they are most often used. For a general idea, I recommend watching a few videos:

In one article, it is pointless to consider topics on how to program microcontrollers, no matter which family. Since this is a very large amount of information.

See also at i.electricianexp.com:

  • Types and arrangement of AVR microcontrollers
  • Microcontroller programming for beginners
  • What are microcontrollers - purpose, device, software
  • Programmable microcontrollers in JavaScript: which one to choose, characteristics ...
  • Methods for reading and managing Arduino I / O ports

  •