Recovery of the PIC12f629 and PIC12f675 calibration constants. Features of EasyPIC5 operation with PIC12F629 microcontrollers Programming features of the pic12f629 microcontroller

Recovery of the PIC12f629 and PIC12f675 calibration constants. Features of EasyPIC5 operation with PIC12F629 microcontrollers Programming features of the pic12f629 microcontroller

10.02.2022
PIC controllers remain popular for low cost, low power, compact systems that require little control. These controllers allow you to replace hardware logic with flexible software that interacts with external devices through good ports.

Miniature PIC controllers are good for building serial data interface converters, for implementing the functions "reception - processing - data transmission" and simple regulators of automatic control systems.

Microchip distributes MPLAB, a free integrated program editing and debugging environment that writes binary files to PIC microcontrollers through programmers.

The interaction of MPLAB and Matlab/Simulink allows you to develop programs for PIC controllers in the Simulink environment - graphical simulation and analysis of dynamic systems. This paper discusses PIC controller programming tools: MPLAB, Matlab/Simulink and the PIC-KIT3 programmer in the following sections.

Characteristics of miniature PIC controller PIC12F629
Integrated development environment MPLAB IDE
Connecting Matlab/Simulink to MPLAB
Connecting the PIC-KIT3 programmer

Specifications of miniature PIC controller

The PIC12xxx family contains controllers in a miniature 8-pin package with an integrated clock generator. The controllers have a RISC architecture and provide the execution of most processor instructions in one machine cycle.

As an example, below are the specifications of the low cost compact 8-bit PIC12F629 controller with multi-functional ports, low power consumption and wide power supply range.

Architecture: RISC
Supply voltage VDD: 2.0V to 5.5V (< 6,5В)
Consumption:
- <1,0 мА @ 5,5В, 4МГц
- 20 µA (typ) @ 32 kHz, 2.0V
- <1,0 мкА (тип) в режиме [email protected].0V
Power dissipation: 0.8W
Multi-function I/O channels: 6/5
Maximum output current of GPIO ports: 125mA
Current through programmable internal port pull-ups: ≥50 (250) ≤400µA @ 5.0V
Controller bit: 8
Clock frequency from external generator: 20 MHz
Machine cycle time: 200 ns
Clock frequency from internal RC oscillator: 4 MHz ±1%
Machine cycle time: 1µs
FLASH program memory: 1K
Number of erase/write cycles: ≥1000
RAM data memory: 64
EEPROM data memory: 128
Number of erase/write cycles: ≥10K (-40°C ≤TA≤ +125°C)
Special purpose hardware registers: 16
Command list: 35 instructions, all commands are executed in one machine cycle,
except for jump commands executed in 2 cycles
Hardware stack: 8 levels
Timer/Counter TMR0: 8-bit with prescaler
Timer/Counter TMR1: 16-bit with prescaler

Additional features:
Power On Reset (POR)
Reset timer (PWRT) generator start waiting timer (OST
Power undervoltage reset (BOD)
Watchdog Timer WDT
Multiplexed Output -MCLR
Interrupt system for changing the signal level at the inputs
Individually programmable pull-up resistors for each input
Programmable input protection
SLEEP low power mode
Selecting the operating mode of the clock generator
ICSP In-Circuit Programming Using Two Pins
Four custom cell IDs

Maximum operating temperature for E version (extended range) from -40°C to +125°C;
Storage temperature from -65°C to +150°C.

The CMOS controller technology provides a completely static mode of operation, in which the stop of the clock generator does not lead to a loss of the logical states of the internal nodes.
The PIC12F629 microcontroller has a 6-bit GPIO I/O port. One GP3 pin of the GPIO port works only as an input, the remaining pins can be configured to work as both an input and an output. Each GPIO pin has an individual interrupt enable bit for changing the signal level at the inputs and an enable bit for the internal pull-up resistor.

Integrated development environment MPLAB IDE

MPLAB IDE - a free integrated software development environment for PIC microcontrollers includes tools for creating, editing, debugging, translating and linking programs, writing machine code to microcontrollers through programmers.

Free versions of MPLAB (including MPLAB 8.92) are stored on the Microchip website in the "DOWNLOAD ARCHIVE" section.

Create a project

An example of creating a PIC controller program project in the MPLAB environment includes the following steps.

1. Calling the project manager.

2. Select the type of PIC microcontroller.


3. Choice of compiler, for example, Microchip MPASM for assembler.


4. Selecting the path to the project directory (the Browse... button) and entering the project name.

5. Attaching files to the project in the Project Wizard → Step Four window can be omitted. This can be done later, inside the active project. The Next key opens the next window.

6. Completion of project creation (Finish key).

As a result of creating the FirstPrMPLAB project, the MPLAB interface takes the form shown in Fig. one.


Rice. one. MPLAB v8.92 environment interface and project template.

Creating a program file
The program can be created using any text editor. MPLAB has a built-in editor that provides a number of advantages, such as online lexical analysis of the source text, as a result of which reserved words, constants, comments, user-defined names are highlighted in the text.

Creating a program in MPLAB can be done in the following sequence.

1. Open the program editor: menu → File → New. Initially, the program was given the name Untitled.

2. Type or copy a program, for example, in assembler.


Rice. 2. An example of the simplest program (in assembler) for outputting signals through the controller ports GP0, GP1, GP2, GP4, GP5 at the maximum frequency.

Writing ‘1’ in the TRISIO register bit sets the corresponding output buffer to the 3rd state, in which case the GP port can only work as an input. Setting the TRISIO to zero sets the GP port to output.

Note. According to the PIC12F629 specification, the GP3 port of the microcontroller works only as an input (the corresponding bit of the TRISIO register is not reset - it is always in '1').

The TRISIO and GPIO registers are in different banks of the memory area. Bank switching is performed by bit 5 of the STATUS register.

Any assembly language program begins with the org directive and ends with the end directive. The goto Metka transition causes the program to loop through.

The program (Fig. 2) uses the following notation.

LIST Directive - Controller Type Assignment
__CONFIG directive - setting controller configuration bits
The equ directive - assigning a numeric value
Directive org 0 - start of program execution from address 0
bsf command - sets a bit of the specified register to 1
Command bсf - resets a bit of the specified register to 0
movlw command - writes a constant to the W register
movwf command - copies the contents of the W register to the specified register
goto command - provides a transition without a condition to a line with a label
The end directive - the end of the program

Setting the required microcontroller configuration
The configuration of the PIC12F629 microcontroller depends on the settings of the configuration word (2007h), which can be set in the program through the __CONFIG directive.

Directly or via the MPLAB window: menu → Configure → Configuration Bits:

Where:

Bit 2-0- FOSC2:FOSC0. Clock selection
111 - External RC generator. Connected to pin GP5. GP4 works like CLKOUT
110 - External RC generator. Connected to pin GP5. GP4 works as I/O
101 - Internal RC oscillator 4MHz. GP5 works as I/O. GP4 - like CLKOUT
100 - Internal RC oscillator 4MHz. GP5 and GP4 act as I/O
011 - EC generator. GP4 works as I/O. GP5 - like CLKIN
010 - HC generator. Resonator connects to GP4 and GP5
001 - XT generator. Resonator connects to GP4 and GP5
000 - LP generator. Resonator connects to GP4 and GP5

Bit 3- WDTE: Watchdog Timer setting
1 - WDTE enabled
0 - WDTE disabled

The watchdog timer prevents the microcontroller from freezing - it restarts the program after a certain period of time if the timer has not been reset. The timer period is set in the OPTION_REG register. Resetting the watchdog timer is called with the CLRWDT command.

Bit 4- PWRTE: Power on timer enable:
1 - PWRT disabled
0 - PWRT enabled

The timer delays the microcontroller in the reset state when VDD power is applied.

Bit 5- MCLR: GP3/-MCLR output mode selection
1 - works like -MCLR
0 - works as a GP3 I/O port

Bit 6- BODEN: Enable undervoltage reset (usually< 2.0В)
1 - BOR reset allowed
0 - BOR reset is disabled, the timer automatically turns on

When BOR reset is enabled, the PWRT timer is automatically enabled.

Bit 7- .CP: Program memory protection bit from being read by the programmer
1 Protection off
0 Protection enabled

When the protection is turned off, the entire program memory is erased.

Bit 8- .CPD: Data memory EPROM protection bit
1 Protection off
0 Protection enabled

After the protection is turned off, all information will be deleted.

Bit 11-9- Not used: Read as ‘1’.

Bit 13-12- BG1:BG0. Power Down Reset Calibration Bits
00 - lower calibration limit
11 - upper calibration limit


Adding a program to a project

An example of adding a program to a project is shown in (Fig. 3).


Rice. 3. Adding the FirstPrMPLAB.asm program to the FirstPrMPLAB.mcp project

Compilation

To create a binary file with the hex extension for microcontroller firmware, you need to compile the project. Compilation is started by the menu command → Project → Build All. Compilation results can be seen in the Output window (Fig. 1). If there are no errors in the program, then the compiler issues a message about successful compilation: BUILD SUCCEEDED, the boot HEX file can be found in the working directory:

Program debugging

Debugging the program in the MPLAB IDE environment can be performed using the MPLAB REAL ICE hardware emulator or the MPLAB SIM software simulator. The launch of the latter is performed as shown in Fig. 4.


Rice. 4. Connection to the MPLAB SIM simulator for program debugging.

After starting the debugger, the MPLAB SIM tab appears in the Output window (Fig. 1), where MPLAB displays the current debugger information. The debugger commands (Fig. 5) become active after launch.


Rice. five. Debugger commands.

Debugger commands:

Run - Continuous execution of the program until the breakpoint (Breakpoint) if one is set.
Halt - Halt the program at the current step of execution.
Animate - Animation of continuous program execution.
Step Into - Execution in steps (Calls are executed in one step).
Step Over - Execute step by step including Call commands.
Reset - Initial installation of the program. Jump pointer to the first command.
Breakpoints - Display a list of breakpoints. List processing.

When executing the program step by step, the current step is marked with an arrow (Fig. 6). Continuous program execution is stopped by a Halt command or by the program hitting a breakpoint. A breakpoint is set/cleared at the program line by double-clicking.
An example of an assembler program that changes the state of the controller ports at maximum speed is shown in Fig. 6 (right). The program sends data b'10101010' and b'01010101' to the GPIO port register. Since in the GPIO register, not all bits, but only 0,1,2,4 and 5, carry out data transfer to the controller ports, the state of the GPIO register (Fig. 6, left) differs in the values: b'00100010' and b'00010101'.


Rice. 6. The state of the special-purpose registers of the controller at the time of program execution (left) and the program being executed step by step (right).

During debugging, you can monitor the status of registers, variables, memory in the corresponding windows opened in the View section of the main menu. During debugging, you can make changes to the program code, the contents of registers, memory, change the values ​​of variables. After changing the code, you need to recompile the program. Changing the contents of registers, memory and variable values ​​(section windows View: Special Function Register, File Register, EEPROM, Watch) does not require recompilation.

The microcontroller model port input signals can be set in the Debugger → Stimulus section. The settable states of the port signals are tied to the time (tacts) of debugging.

Sometimes the results of program execution in debug mode do not correspond to the execution of the same program in a real controller, for example, the program debugger (Fig. 6) without the movlw 0x07 and movwf cmcon instructions shows that the outputs GP0 and GP1 of the GPIO register do not change - they are at zero state, the contents of the GPIO register are alternately 0x14 and 0x20. However, the controller executing the program without these instructions shows on the oscilloscope the cyclic operation of all five outputs: 0x15 and 0x22, including GP0 and GP1 (see Fig. 7).

Oscillograms of the controller executing program cycles Fig. 6 (Metka… goto Metka) are shown in Fig. 7.


Rice. 7. Oscillograms of the output GP0 (left) and GP1 (right) of the PIC12F629 microcontroller, powered by an internal 4MHz RC oscillator. The program (Fig. 6) generates maximum frequency signals at all controller outputs. For a signal period of 5.3 μs, 5 commands (6 machine cycles) are executed, the amplitude of the GP0 signal on the oscillogram is 4.6V, the controller power measured by the programmer is 4.75V.

microcontroller firmware

To write a program to the microcontroller (controller firmware), you need to connect the microcontroller to the MPLAB IDE integrated environment through the programmer. The organization of the connection is shown below in the section “Connecting the PIC-KIT3 programmer”.

Note. The PIC12F629 controller has a factory-set internal clock frequency calibration constant. If necessary, it can be read and restored using MPLAB tools using the programmer.

Commands for working with the programmer and changing its settings are located in the MPLAB Programmer menu. The type of programmer in MPLAB is selected in the section: menu → Programmer → Select Programmer.


Rice. 8. Selecting a programmer to connect to the MPLAB environment.

The firmware of the microcontroller through the programmer is launched by the command: menu → Programmer → Program. The successful firmware message is shown in Fig. nine.


Rice. nine. Starting the firmware of the microcontroller and the type of message about the successful firmware.

Note: While flashing the microcontroller, the PIC-KIT3 programmer has a yellow LED flashing.

Connecting MATLAB/SIMULINK to MPLAB

In the simulation system of dynamic systems Simulink (application for Matlab) in the graphical programming language, you can develop programs for the family of PIC controllers with ADC / DAC, counters, timers, PWM, DMA, UART, SPI, CAN, I2C interfaces, etc.

An example of a Simulink PIC controller program is shown in Fig. 10.


Rice. 10. An example of a program in a graphical programming language for a PIC controller executed in the simulation environment for dynamic systems Simulink.

The interaction of development tools and compilation of programs for PIC controllers in Simulink is shown in Fig. eleven .


Rice. eleven. Structure of tools for building an adequate PIC controller model in a graphical programming language.

To build a development environment, the following Matlab components are required:

Simulink
Real Time Workshop Embedded Coder
Real Time Workshop

And Microchip's C compiler:

C30 for PIC24, dsPIC30 and PIC33 controllers
or C32 for PIC32 series controllers

Installing Matlab Components

The site has Simulink libraries (dsPIC Toolbox) for PIC controllers and Matlab versions from R2006a to R2012a:

You must register to download the library. The programs support the operation of 100 microcontrollers from the PIC 16MC, 24F, 30F, 32MC, 33F, 56GP, 64MC, 128MC, 128GP series.
The free versions work with Simulink PIC controller models with up to 7 I/O ports.

To install dsPIC Toolbox - library of PIC controller blocks for Matlab/Simulink you need:

Download dsPIC Toolbox for the required version of Matlab.
Unpack the zip file in the folder where the Simulink blocks will be installed.
Start matlab.
Set the current Matlab directory to the folder with the unpacked file.
Open and run the install_dsPIC_R2012a.m file, for example, using the menu button or keyboard key.

The dsPIC libraries and Simulink model examples are installed in the current Matlab folder (Fig. 12). Installed blocks for modeling PIC controllers are available in the Embedded Target for Microchip dsPIC section of the Simulink library (Fig. 13).


Rice. 12. The contents of the current directory after installing install_dsPIC_R2012a.m.


Rice. 13. Blocks of the installed library "Embedded Target for Microchip dsPIC".

To jointly compile a Simulink model using Matlab and MPLAB, you must set the path to the MPLAB directory with the MplabOpenModel.m, MplabGetBuildinfo.m and getHardwareConfigs.m files in the path Matlab environment variable with the highest priority:

>>

Installing the MPLAB C Compiler

The MPLAB compilers are on the Microchip website (Download Archive → MPLAB C Compiler for PIC24 and dsPIC DSCs). To install the demo version of the C30 compiler, you need to download it from the link PIC24/dsPIC v3.25 (Fig. 14) and run the received file mplabc30-v3.25-comboUpgrade.exe.


Rice. fourteen. Versions of the C compiler (left) and its installation modes (right).

Note. The work was done with version v3.25 of the C30 compiler for PIC24/dsPIC. The check showed that the next version v3.30 does not support joint compilation of Matlab R2012a models (dsPIC Toolbox) without errors.

The installation exe file creates a new mplabc30 directory in the c:\Program Files (x86)\Microchip\ section with the following files:


Rice. 15. C30 MPLAB compiler directories.

Simulink programming sequence for PIC controllers

1. Create a working directory and copy the *.mdl examples from the example section into it (see Fig. 12).
2. Download matlab. Set it to the working directory.
3. Include in the environment variable path Matlab with the highest priority the path to MPLAB - the directory c:\Program Files (x86)\Microchip\MPLAB IDE\Tools\MATLAB\:

>> path("c:\Program Files (x86)\Microchip\MPLAB IDE\Tools\MATLAB\",path)
Note: Using the >>path command with no arguments causes the list of paths of the path variable to be displayed in the Command Window. You can remove the path from the path variable with the rmpath command, for example:

>>rmpath(" c:\Program Files\Microchip\MPLAB IDE\Tools\MATLAB\")
4. Create a Simulink model for the PIC controller using the blocks of the "Embedded Target for Microchip dsPIC" library (Fig. 13), or load a ready-made model, for example, Servo_ADC.mdl.

The type of controller for which the Simulink model is being developed is selected from the list in the Master > PIC block (Fig. 16, Fig. 10), which must be included in the model.


Rice. 16. Selecting the controller type in the Master block of the model.

5. Check the model configuration settings: Menu → Simulation → Configuration Parameters . The S-function compiler dspic.tlc must be specified in the entry line System target file of the Code Generation section (Fig. 17). Selecting dspic.tlc configures all other model configuration parameters, including the integration step and method.


Rice. 17. Selecting the S-function compiler dspic.tlc for PIC controller models in the "Main Menu → Simulation → Configuration Parameters → Code Generation" section.

6. Compile the tmp_Servo_ADC.mdl model. Compiler startup is shown in Fig. eighteen.


Rice. eighteen. Launching the Simulink compiler of the model.

As a result of successful compilation (message: ### Successful completion of build procedure for model: Servo_ADC), a HEX file is created in the current directory to flash the PIC controller and the MCP project of the MPLAB environment (Fig. 19).


Rice. 19. Model compilation results.

The launch of the model in Matlab/Simulink is performed in the model window with the button, the conditional simulation time is set in the line:


Managing the compilation of Simulink models from the MPLAB environment

Compilation of a Simulink model can be controlled by commands in the Matlab/Simulink section of the MPLAB environment, for example, in the following order.

1. Develop a PIC controller model in Matlab/Simulink. Save the model.
2. Start MPLAB.
3. Select MPLAB menu → Tools → Matlab/Simulink and the new section will appear in the menu.


4. In the Matlab/Simulink section, open a Simulink model, for example, Servo_ADC, with the command "Matlab/Simulink → Specify Simulink Model Name → Open → File name → Servo_ADC.mdl → Open". The Open command starts Matlab and opens the model.

5. Compile the model and create an MCP project with the Generate Codes or Generate Codes and Import Files commands. Translation of an MDL model into an MCP project is performed by the Matlab TLC compiler.
As a result, the MPLAB project is created:

With model scripts in C language.

6. Open the project: menu → Project → Open → Servo_ADC.mcp (Fig. 20).


Rice. twenty. MCP structure of the Servo_ADC.mdl Simulink project in the MPLAB environment.
The Simulink project of the model is ready for editing, debugging and compiling into controller machine codes using MPLAB.

Connecting the PIC-KIT3 programmer

You can find out which programmers write binary code to a specific microcontroller in the menu section → Configure → Select Device of the MPLAB 8.92 environment. For example, the PIC-KIT3 programmer does not support the PIC12C508A controller (Fig. 21, left figure), but works with the PIC12F629 controller (Fig. 21, right figure).


Rice. 21. List of programmers for microcontroller firmware.

Information about the installed PIC-KIT3 programmer driver can be requested from the device manager of Windows OS (Fig. 22).


Rice. 22. Information about the installed PIC-KIT3 programmer driver.

The diagram for connecting the PIC12F629 microcontroller to the PIC-KIT3 programmer is shown in Fig. 23.


Rice. 23. Scheme of connecting the PIC12F629 microcontroller to the PIC-KIT3 programmer.

The PGM output of the programmer is not used for flashing PIC12F629 controllers. The presence of the PGM output for different types of PIC controllers is shown in Fig. 24. It is recommended to “pull” the PGM output to the common wire (GND), through a 1K resistor.


Rice. 24. Outputs of PGM PIC controllers.

The indication of the LEDs of the Olimex PIC-KIT3 programmer is shown in the following:

Yellow - Red - Programmer status
On - Off - Connected to USB line
On - On - Interaction with MPLAB
Blinking - Permanently on - Microcontroller firmware

You should not connect the power of the VDD microcontroller (Fig. 23) to the programmer if the controller is powered from its own power supply.

When powering the microcontroller from the programmer on the VDD line, it is necessary to set the operating voltage, for example, 5V by the MPLAB program (Menu → Programmer → Settings → Power), as shown in Fig. 25.

Note. If there is no voltage on the VDD line, MPLAB IDE gives an error message: PK3Err0045: You must connect to a target device to use


Rice. 25. Setting the VDD voltage on the PIC-KIT3 programmer using the MPLAB IDE v8.92 program.

If the programmer cannot set the required voltage, for example, 5V when it is powered by USB, in which the voltage is less than 5V, MPLAB IDE issues an error message: PK3Err0035: Failed to get Device ID. In this case, you first need to measure the voltage of the programmer - read it in the menu tab → Programmer → Settings → Status, and then set the voltage (no more than the measured one) in the menu tab → Programmer → Settings → Power.


Rice. 26. Measuring (left) and setting (right) the VDD voltage of the PIC-KIT3 programmer using the MPLAB IDE v8.92 program.

An example of an MPLAB message for successfully connecting the microcontroller to the programmer using the menu command → Programmer → Reconnect is shown in Fig. 27.


Rice. 27. MPLAB message about successful connection of the microcontroller to the programmer.

It is possible to program not only a separate PIC controller, but also a controller that is part of a working device. To program the PIC controller as part of the device, it is necessary to provide for the installation of jumpers and current-limiting resistors, as shown in Fig. 28.


Rice. 28. Connecting the microcontroller as part of an electronic device to the programmer.

Conclusion

Low-bit PIC-controllers have a wide power supply range, low consumption and small size. They are programmed in low level languages. Developing programs in the Simulink graphical programming language using numerous libraries significantly reduces development and debugging time compared to programming at the assembler level. The structures developed for Simulink PIC controllers can also be used for computer simulation of dynamic systems involving controllers. However, due to code redundancy, this approach is applicable only for families of PIC controllers with sufficient resources.
Simulink Add Tags Tell to:
For some unknown reason, I constantly tried to postpone my acquaintance with microcontrollers until later. Perhaps the idea of ​​using digital technology modules created unreasonable fears in the head of a beginner who had never tried to look beyond the boundaries of analog electronics. However, at the same time, I understood very clearly what opportunities even the cheapest microcontrollers would give me. And then, one day, I still decided to understand this seemingly complex, but also very interesting topic.

It was logical to start with studying the theory and assembling some kind of someone else's project using a microcontroller (hereinafter MK). I think it’s not worth talking about the first - the Internet is already full of articles on this topic. As for the second, my choice fell on this device. Its scheme is quite simple to repeat and focusing on the assembly will also be redundant, so let's go straight to the MK firmware.

ExtraCheap programmerAfter a short search on the Internet, quite a few programmer circuits were found, but most of them were quite complex devices designed to flash several types of MK. I wanted to assemble a circuit specifically for the PIC12F629. After spending a little more time, I finally found what I was looking for - the ExtraCheap programmer from the WiiFree project.

The COM port is used for data transfer. The circuit is powered by 5 volts, which can be taken from the USB or PS / 2 ports.
A few photos of the assembled device:

To work with the programmer, it is recommended to use the IC-Prog program.

Setting up IC-Prog We download archives with the latest versions of IC-Prog Software, NT/2000 driver, Helpfile in Russian language from the official website of the program and unpack them into the same directory.
Now you need to install the programmer driver, for which we run icprog.exe (if error messages appear, we simply ignore them) and select the "Options" item in the "Settings" menu. Open the "Programming" tab and check the box next to "Verify during programming". Next, in the “Misc” section, you need to activate the “Enable NT / 2000 / XP Driver” option, save the settings by clicking on the “OK” button and restart the program.

You can change the interface language in the "Language" section. In order to specify the type of our programmer to the program, press F3, select "JDM Programmer" in the window that opens and specify the COM port to which the device is connected.

On this, the preliminary setup of the program can be considered complete.

The MKIC-Prog firmware allows you to work with a large number of MKs, but we only need PIC12F629 - select it in the drop-down list located in the upper right corner of the program.
To read the firmware from the MK, execute the "Read chip" command (icon with a green arrow or F8).

At the end of the reading process, the MK firmware in hexadecimal form will be displayed in the program code window. You should pay attention to the last memory cell at address 03F8 - the value of the OSCCAL constant is stored there, which is set by the manufacturer when calibrating the chip. Each MK has its own, so it would be nice to rewrite it somewhere (I, for example, scratch it with a needle on the back of the PIC) to facilitate the recovery process (although this is not necessary) if during the firmware this constant was accidentally overwritten.


In order to “upload” the firmware from the *.hex file to the MK, it must be opened in the program (“File”-> “Open File ...” or Ctrl + O) and execute the command “Program the chip” (icon with yellow lightning or F5). We answer "Yes" to the first question.


But the next question must be answered “No”, otherwise the OSCCAL constant, which was mentioned earlier, will be overwritten.


After that, the firmware process will begin. At the end, the program will display an informational message about its results.

On this I would like to bring the topic to an end. I hope this information will help beginners understand the basics of programming PIC microcontrollers.
Thank you for your attention. PCB File (LAY) Section:

microcontroller PIC 12F 629 differs from MicroChip in a small number of pins and a program memory capacity of 1 K words. A maximum of 6 input / output lines can be used (3 pin works only for input). Despite the very modest characteristics, in some cases the use of this microcontroller is fully justified. To work with PIC12F629 and its extended version with ADC PIC12F675, you can use the board EasyPIC5 and microPascal environment.

Setting up the EasyPIC5 board

To assemble the circuit on the PIC12F629, you need to install the MK in the appropriate slot on the board. In this case, all other controllers must be removed.

The second action should be to change the jumpers of the Socket Selection programming lines.

It is also necessary to determine how the clock frequency is generated. If you are using an external oscillator, you will need to install a crystal in the OSC2 socket. The PIC 12F 629 microcontroller can also be operated from an internal frequency generator. In this case, OSC2 jumpers are set to the I / O position, connecting the microcontroller outputs to the board elements. This completes the preparation for work.

Programming Features

PIC 12F 629 has only one I/O port called GPIO . On the board, the lines of this port are connected to the PORTA/PORTGP pin header and other elements associated with it. This allows you to develop devices and debug programs, just like for other MKs. The output of the temperature sensor DS18B20, which can be connected to the RA 5 line, is not connected to the socket for PIC 12F 629.

When you start the development environment, a project opens, where, first of all, you need to set the type of frequency generator used. The most popular option for this crystal would be to use an internal oscillator. This allows the GP4 and GP5 lines to be used for I/O. The internal oscillator is designated INTR_OSC_NOCLOCKOUT. Other configuration bits are also set, depending on the requirements of the circuit. If it is necessary to set the calibration constant, this can be done after starting the microICD programmer program.

Writing a program differs little from this action for other controllers, taking into account the features of the PIC 12F 629. Main - the name of the input / output port. In the MicroPascal IDE, its global GPIO definition, and the config register designated TRISIO. Additionally, during initialization, you need to determine the purpose of the pins GP 0 and GP 1. By default, they are the inputs of the analog comparator. When used as digital lines, the command CMCON:=7 must be executed. It configures output data as discrete I/O lines. Well, do not forget that the GP 3 output only works as an input. Otherwise, programming the PIC 12F 629 is no different from other PICmicro controllers.

Work program example

As an example, below is the source text in the language micropascal, which flashes the LED connected to the GP0 port.

Program PIC12F629Start;
const i=500;
begin // Program start
GPIO:=0;
CMCON:=7; // gp0,gp1 - discrete lines
TRISIO:= %00001000; // all lines to output except gp3
while TRUE do // main loop
begin
setbit(gpio,0);
delay_ms(i);
clearbit(gpio,0);
delay_ms(i);
end;
end.

You have no rights to post comments

Microcontrollers PIC12F629 And PIC12F675 have an internal 4 MHz oscillator, which allows them to work without an external quartz resonator or RC circuit. This frees up one or two pins to use as I/O and allows the device to have a minimal number of components in the design.

The internal oscillator must be calibrated, this is achieved by selecting a calibration value at the factory and writing it to the OSCCAL register when the device is initialized before starting the operating program. The calibration value is in the last program memory address - 0x3FF.

Problems arise if the value at address 0x3FF is accidentally erased when programming the microcontroller. After calibration, the value is unique for each individual microcontroller, there is no way to know what it was, but you can restore it by comparing it with a known frequency.

For this, this program and scheme was created. Download the code that is in the archive into the microcontroller and place it in the device described below, and within a few seconds you will receive a new calibration value for the operation of the internal oscillator with a frequency of 4 MHz and an accuracy of 1%.

How it works

A frequency reference is required to calibrate the internal oscillator. Fortunately, we don't need signal generators or other measuring equipment. In fact, an accurate signal can be obtained from the AC mains. In most parts of the world, electricity is supplied at 50 or 60 Hz (many digital clocks use this to maintain accuracy). Using almost any transformer with an output voltage of 6 to 12 volts AC, you can get an accurate source for calibrating the microcontroller's oscillator.

The frequency of the microcontroller's internal oscillator depends on changes in temperature and supply voltage. As the supply voltage increases, the frequency decreases slightly. When switch S1 is open, a voltage of 5 volts is applied through two diodes, in total about 3.4 volts is supplied to the controller. If S1 is closed, then the controller operates on 5 volts. Microchip calibrates batches at 3.5 volts and 25°C. In this circuit, it is possible to calibrate at a voltage of 3.4 Volts and 5 Volts, but it is desirable to calibrate the controller with S1 open (3.4 Volts).

S1 open - calibration occurs at 3.4 volts

S1 closed - calibration occurs at 5 volts

S1 is not a power switch

Two 1N4148 diodes provide a voltage drop, a 150R resistor ensures a stable voltage across the diodes. The 100nF filter capacitor should be placed as close as possible to the controller pins (1 and 8).

The frequency reference signal is fed into the controller using a BC548 transistor, a 10K resistor, a 1N4148 diode, and a transformer. You can use any NPN transistor, its characteristics are not critical. Any transformer can also be used, but with an output voltage ranging from 6 to 12 volts AC.

It is important to apply only an AC signal to the input of the transistor. Do not use direct current, even without a filter capacitor.

OBSERVE SAFETY INSTRUCTIONS WHEN WORKING WITH HIGH VOLTAGE!

Code for calibration

Next, you need to program the microcontroller with the code that is in the archive under the name "recal1.hex". The code will work with PIC12F629 and 12F675 as well. Download this code before starting the calibration operation.

After you have successfully calibrated the microcontroller and read the new calibration value, you can reprogram the controller using your code. This software is only needed to calculate a new calibration value, and should not be left in the microcontroller's memory afterwards. (Someone will ask, believe me!)

Calibration start

1. Install jumper J1 if the mains frequency in your area is 60 Hz and remove the jumper if the frequency is 50 Hz. Make sure that jumper J1 is set correctly, as this affects the correct calibration.

J1 removed - mains frequency 50Hz
J1 installed - mains frequency 60Hz

2. The AC signal must be present before powering up the microcontroller during calibration, it is necessary to ensure a stable signal at the proper level.

3. Insert the microcontroller into the socket, open the switch S1 and thereby apply a voltage of 5 volts.

4. When the microcontroller starts up, the LEDs will blink once.

5. If the frequency reference signal is not detected at GP2 (pin 5), the red "Error" LED will light up and the green "Completed" LED will flash. (If this happens, turn off the power to the circuit and make sure the connection is correct)

6. During the calibration process, both LEDs are off. Calibration will take less than 5 seconds.

7. If the calibration fails, the red "Error" LED will light up and the program will be stopped.

8. If the calibration is completed successfully, the green LED "Completed" will light up and a test signal with a frequency of 5 kHz will appear on GPIO1. If you have a frequency meter, you can check if the calibration is correct.

9. After calibration is complete, you can turn on/off switch S1 and monitor the test signal to see changes in signal frequency compared to supply voltage.

10. Turn off the power to the circuit, remove the microcontroller from the socket and connect it to the programmer. Next, read the microcontroller data and check the contents of the EEPROM memory

When you read the EEPROM there should be one of three pairs of values ​​at addresses 0x00 and 0x01:

If the EEPROM at addresses 0x00 and 0x01 contains 0xFF, then the program did not work correctly.

If the EEPROM addresses 0x00 and 0x01 contain 0x00, then the program could not set the calibration word correctly (the red LED "Error" was on). Make sure the frequency reference is correct, J1 is set correctly and try again.

If EEPROM address 0x00 contains the value 0x34 and address 0x01 contains 0xNN, where NN is the new calibration value, then the program has completed successfully (green "Completed" LED on) and OSCCAL has been correctly calibrated. Use the value 0xNN at address 0x01 to calibrate the memory.

Changed firmware version

Some programmers or software increase the VDD signal before enabling VPP. This allows the program to start working before the microcontroller enters the programming mode by first reading the EEPROM. If the green LED indicates the successful completion of the calibration, and after reading the EEPROM you find 0xFF in it at addresses 0x00 and 0x01, then your programmer is working in this mode.

If you have this problem, you can download a modified version of the original program, which is in the archive under the name "recal2.hex". This version does not initialize the EEPROM at startup, so calibration data is not cleared when the programmer reads data from the microcontroller. Use this firmware only when such a problem occurs.


Using a new calibration value

IMPORTANT: This circuit/program only calculates the correct calibration value and cannot save it back to the controller's memory - you must do this manually using the programmer.

If you want to check the calibration constant of the microcontroller, knowing the factory value, the calibration value restored by this circuit may differ slightly from the factory one. This is normal as there are differences in supply voltage and temperature. You can check the value of the test signal on GP1 with a frequency meter after calibration is completed, this frequency should be within 1% of 5 kHz.

If the calibration is successful, remove the microcontroller and connect it to the programmer. Read the data from the EEPROM of the controller (this is not program memory) where the value just calculated was stored. (See example below)

Address 0x00 will contain the value 0x34 and address 0x01 will contain the calibration value. It is this value that should be written to program memory at address 0x3FF. Some programmers allow you to read the calibration value and then write it back to the controller. If the programmer does not do this, then you must manually write the value 0xNN to address 0x3FF, where "NN" is the hexadecimal calibration value read from the EEPROM.

Many beginner radio amateurs at the beginning of their business are afraid to start working with a microcontroller. This is due to many things, and the main one is often the fear of how to program correctly and how to program. This article provides diagram of a simple programmer for a PIC microcontroller.We look, collect, ask on the official forum and leave feedback if you succeed

I would advise you to start your work first with general information about microcontrollers.

ExtraCheap programmer

In the Internet many different schemes of programmers.But most of them are very complex, and it is rare to see photographs that would confirm its performance.

But the right programmer was found for many requests.

The COM port is used for data transfer. The circuit is powered by 5 volts, which can be taken from the USB or PS / 2 ports.

Another photo of this device:

To work with the programmer, it is recommended to use the IC-Prog program

Setting up IC-Prog

Download from the off site the latest version of the program IC-Prog Software, NT/2000 driver, Helpfile in Russian language and unpack them into the same directory.

Now you need to install the programmer driver, for which we run icprog.exe (if error messages appear, we simply ignore them) and select the "Options" item in the "Settings" menu. Open the "Programming" tab and check the box next to "Verify during programming". Next, in the “Misc” section, you need to activate the “Enable NT / 2000 / XP Driver” option, save the settings by clicking on the “OK” button and restart the program.

You can change the interface language in the "Language" section. In order to specify the type of our programmer to the program, press F3, in the window that opens, select "JDM Programmer" and specify the COM port to which the device is connected.

On this, the preliminary setup of the program can be considered complete.

Firmware MK

IC-Prog allows you to work with a large number of MKs, but we only need PIC12F629 - select it from the drop-down list located in the upper right corner of the program.
To read the firmware from the MK, execute the "Read chip" command (icon with a green arrow or F8).

At the end of the reading process, the MK firmware in hexadecimal form will be displayed in the program code window. You should pay attention to the last memory cell at address 03F8 - the value of the OSCCAL constant is stored there , which is set by the manufacturer when calibrating the chip. Each MK has its own, so it would be nice to rewrite it somewhere (I, for example, scratch it with a needle on the back of the PIC "a) to facilitate the recovery process (although this is not necessary ) if this constant was accidentally overwritten during firmware.


In order to “upload” the firmware from the *.hex file to the MK, it must be opened in the program (“File”-> “Open File ...” or Ctrl + O) and execute the command “Program the chip” (icon with yellow lightning or F5). We answer "Yes" to the first question.


But the next question must be answered “No”, otherwise the OSCCAL constant, which was mentioned earlier, will be overwritten.


After that, the firmware process will begin. At the end, the program will display an informational message about its results.

On this I would like to bring the topic to an end. I hope this information will help beginners understand the basics of PIC programming.microcontrollers.

© 2022 hecc.ru - Computer technology news