⬆️ ⬇️

Programming microcontrollers PIC16 / PIC18 in C. The second article. Basic information about the platform. First program

If everything worked out well in the first article, we received a basic programming environment for continuing the training.



We go further.



As I said earlier, for now I will explain on the basis of the MIC PIC16F628A .

Be sure to download the documentation for it. I recommend to search - alldatasheet.com

DataSheet - the main document in the development based on the MK.

I recommend to print the main tables and sections for ease of reference.

')

Open LH.



The most important information about the crystal:

- maximum operating frequency - 20 MHz;

- 2048x14 bits flash ROM;

- 224 bytes of static RAM;

- 128 bytes of non-volatile ROM;

- 16 available leads;

- transceiver module;

- 3 timers.



This crystal is a representative of the so-called Mid-range of the MK PIC family.



Briefly talk about what must be understood .







The data memory of the device is divided into 4 banks .

Banks contain special purpose registers (SFR) and general purpose registers (GPR).

SFR - used to control the device and its modules.

Registers of general assignment are presented in the form of static RAM, where we can store our variables.

Special registers occupy 32 initial positions in each bank.

The LH on pages 18-21 shows all special purpose registers. Print them - useful and more than once.



This is quite a voluminous topic, and you can’t skip it at all.

On the other hand, it is tedious and uninteresting.

Try to re-force yourself and read about the organization of memory and special-purpose registers in LH and Shpak (mentioned in the first article).



I / O ports .



This device has two ports: PORTA and PORTB.

Each port pin can be used directly as a simple pin or as an output of other modules of the MC.

At the very beginning of the LH, you should have noticed that each output besides the main name, for example RB1, contains another name RX and DT.

This is where the RX is a secondary output function - like a transceiver input.

But for now we will not touch the peripheral modules of the device.



Each port pin can function as an input or as an output.

For example, if we need to light the LED, then the output to which it is connected, we configure as an output, and set the appropriate level on it.

But if we need to connect a button to the port and read the fact of pressing, then here it is necessary to configure this pin as an input.

The input / output configuration is carried out using TRIS registers.

If the corresponding bit in the TRIS register is written to 1, then this output will become an input, if 0 - an output (apparently due to the similarity of 1 and Input, and 0 and Output)

For example:

TRISA = 0; // -

TRISB = 0xff; // B -

TRISA5 = 1; // 5 - .


Register names can be viewed in the folder “HT-PICC \ include” for the corresponding controller.



To establish a certain level on the output, we use the PORT register.

For example:

PORTA = 0; //

PORTB = 0xff; // B

RB5 = 1; // B



So. We reach the moment when it is necessary to deviate from the proper C. language.

Probably I will write here a small example. Compile Let's launch it in Proteus, and I'll write about the base language C in the next article.



Let's write a small program. Blink LED.

We use the previously created project, or create a new one, according to the scheme from the first article.



First, let's include the header file.



#include <pic.h>


We do not select a specific model, but indicate pic.h. If you open it, we will see a script for selecting a specific file based on the selected device when creating a project.



Next we need to initialize our ports.

We create the main function of the program:

void main(void)

{

while(1);

}


We write at its beginning:

TRISA = 0; // -

PORTA = 0; //

TRISB = 0; // B -

PORTB = 0; //


Let's go to the "HT-PICC \ samples \ delay folder".

Copy delay.c and delay.h from there and paste them into the project folder.



Add a line at the beginning:

#include "delay.c"

We will use the DelayMs (x) function to create a pause between switching the LED.



Let's say the LED is connected to RB0.

To switch it, a logical exclusive “OR” will be used:

RB0 ^= 1;


As a result, the code takes the form:



#include <pic.h>

#include "delay.c"



void main(void)

{

TRISA = 0;

PORTA = 0;

TRISB = 0;

PORTB = 0;



while(1)

{

DelayMs(250); //

DelayMs(250);

RB0 ^= 1; //

}

}


image



The firmware is ready.

Now configure the configuration bits.

Click Configure -> Configuration bits .

Uncheck the “Configuration Bits set in code” checkbox , since we did not set anything in the code.



We see a few points.

We expose as on the screen.

Oscillator - HS - a high-frequency quartz resonator will be used as a clock generator.

WatchDog Timer - Off, if enabled, the microcontroller will be reset periodically to avoid any freezes. We do not need this opportunity yet .

Power Up Timer - Enabled, MK will be in a reset state until the supply voltage reaches the required threshold level.

Brown Out Detect - On, reset the MC, if there was a drop in the supply voltage below a certain level.

Low Voltage Program - Disabled, prohibit the use of low-voltage in-circuit programming MK. It already depends on your programmer.

Data EE Read Protect - Off, enable data reading and EEPROM of the MK memory.

Code Protect - Off, disable the code protection in the MK. If set to On, then it will be impossible to read the program from the MC. We do not need such an opportunity yet.



image



Press F10 again.

We are closing.



Run the program in Proteus.

Launch Proteus ISIS.

Being in the Component mode section, click Pick from libraries and, using the search, add components to the form:

- PIC16F628A;

- LED-RED;

- RES;



Double click on each of them and set the parameters.

For MK - choose the firmware file * .hex from the folder of our project, and set the frequency to 4 MHz.

For LED-RED choose Model type - Digital.

For a resistor, choose a resistance of 300 ohms.

Add to the Ground form in the Terminals mode section and connect as in the screenshot.

image

We press Play - the LED should blink.



In the next article I’ll walk tightly through C.

Following it will be an article on the periphery of the controller and code examples for it.

And behind it I plan to talk about USB and PIC 18.

Here's a plan for now :-)

Source: https://habr.com/ru/post/75312/



All Articles