Write PIC microcontroller programs based on PICC compilation environment

Publisher:缘到泉Latest update time:2012-02-23 Source: 现代电子技术 Keywords:PIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

At present, the PIC series of microcontrollers produced by Microchip have rapidly occupied the domestic market with its advantages of low cost, low power consumption, high performance, fast development speed and one-time user programmability, becoming the best-selling microcontroller in China. However, there are relatively few books and articles introducing its C language development tools in China, and not many people use it, which has brought many difficulties and inconveniences to programmers in the process of development.

Microchip does not have its own C language compiler for low-end and mid-range PIC microcontrollers, but many professional third-party companies provide a number of C language compilers that support PIC microcontrollers, including Hitech, CCS, IAR, Bytecraft, etc. Hitech's PICC compiler is stable and reliable, and the compiled code is highly efficient. It is widely recognized by engineers who use PIC microcontrollers for system design and development. Therefore, this article mainly introduces the basic characteristics of PIC's C language based on Hi-Tech PICC.

1. Differences and similarities between HiTech PICC and ANSI C and characteristics of HiTech PICC language

Except that PICC does not support recursive function calls, PICC basically complies with the ANSI standard. The main reason is the special stack structure of PIC microcontrollers. The stack in PIC microcontrollers is implemented by hardware, and its depth is fixed with the chip, so it is impossible to implement recursive algorithms that require a large number of stack operations. In addition, the efficiency of implementing software stacks in PIC microcontrollers is not very high. For this reason, the PICC compiler uses a "static overlay" technology to allocate fixed address space to local variables in C language functions. The machine code generated after this treatment is very efficient. When the code size exceeds 4 kB, the difference between the length of the code compiled by C language and the length of the code implemented entirely in assembly code is not very large (<10%). Of course, the premise is that you need to pay attention to the efficiency of the statements you write during the entire C code writing process. 

2Variables in PICC

The variable types in PICC are the same as those in standard C, so they will not be repeated here. In order to make the compiler generate the most efficient machine code, PICC leaves the bank problem of the data registers in the microcontroller to the programmer to manage. Therefore, when defining user variables, you must decide which bank to put these variables in. If not specifically specified, the defined variables will be located in bank0. The variables defined in other banks must be preceded by the corresponding bank number, for example:

bank1 unsigned char temp; // variable is located in bank1

The size of a bank of data registers in the mid-range PIC microcontroller is 128 bytes. Excluding the special function register area of ​​the first several bytes, the total number of variable bytes defined in a bank in the C language cannot exceed the number of available RAM bytes. If the bank capacity is exceeded, an error will be reported at the final connection. The general information is as follows:

The connector prompts that a total of 0x12C (300) bytes are ready to be placed in bank1, but the capacity of bank1 is insufficient. Although the bank location of the variable must be determined by the programmer himself, there is no need to specifically write instructions to set the bank before performing variable access operations when writing the source program. The C compiler will automatically generate assembly instructions for the corresponding bank settings based on the object being operated. In order to avoid frequent bank switching and improve code efficiency, try to locate variables that implement the same task in the same bank; when reading and writing variables in different banks, try to merge variables in the same bank together for continuous operations.

Bit variables can only be global or static. PICC will merge 8 bit variables located in the same bank into one byte and store it at a fixed address. PICC implements bit addressing for the entire data storage space. The 0th bit of the 0x000 unit is the bit address 0x0000. From then on, each byte has 8 bit addresses. If a bit variable flag1 is addressed as 0x123, then the actual storage space is located at:
Byte address = 0x123/8 = 0x24
Bit offset = 0x123%8 = 3

That is, the flag1 bit variable is located at the 3rd bit of the byte at address 0x24. When debugging the program, if you want to observe the changes of flag1, you must observe the byte at address 0x24 instead of 0x123. When PICC compiles the original code, whenever possible, the operation of ordinary variables will be implemented with the simplest bit operation instructions. Assuming that a byte variable tmp is finally located at address 0x20, then In

addition, the function can return a bit variable. In fact, this returned bit variable will be stored in the carry bit of the microcontroller and brought out of the return. [page]

3 Pointers in PICC
3.1 Pointers to RAM

When PICC compiles the original C program, the pointer operation pointing to RAM is finally implemented with FSR for indirect addressing. The range that FSR can directly and continuously address is 256 B, so a pointer can cover the storage area of ​​two banks at the same time (bank0/1 or bank2/3, one bank area is 128 B). To cover the internal data storage space of up to 512 B, the addressing area applicable to the pointer must be clearly specified when defining the pointer. For example:

Since the defined pointer has a clear bank applicable area, type matching must be implemented when assigning a value to the pointer variable, otherwise an error will occur. For example:

If such an erroneous pointer operation occurs, PICC will inform a message similar to the following when the connection is finally made:
Fixup overflow in expression (…)

3.2 Pointers to ROM constants

If a set of variables is a constant that has been defined in the ROM area, then the pointer to it can be defined as follows:

3.3 Pointer to function

Because the efficiency of implementing function pointer calls on the specific architecture of PIC microcontrollers is not high, it is recommended that you try not to use function pointers unless required by special algorithms.

4Subroutines and functions in PICC

The program space of the mid-range PIC microcontroller has the concept of paging, but you don't need to worry too much about the paging of the code when programming in C language, because the page setting when calling all functions or subroutines (if the code exceeds one page) is implemented by the instructions automatically generated by the compiler.

4.1 Function code length limit

PICC determines that the machine code generated by a function in a C source program after compilation must be placed in the same program page. The length of a program page of the mid-range series of PIC microcontrollers is 2 kB, and the code generated by any function written in C language cannot exceed 2 kB. If you really need to write a long program continuously to achieve a specific function, then you must split these continuous codes into several functions to ensure that the code compiled by each function does not exceed one page space.

4.2 Call hierarchy control

PIC microcontrollers use hardware stacks, so the function call levels are subject to certain restrictions during programming. Generally, the hardware stack depth of the mid-range microcontrollers in the PIC series is 8 levels. Programmers must control the nesting depth of subroutine calls to meet this restriction. After the final compilation and connection is successful, PICC can generate a connection positioning mapping file (*map), which contains a detailed function call nesting indicator graph "call graph". Some function calls are library functions that are automatically added when compiling the code. These function calls cannot be directly seen from the C source program, but they are clear at a glance on the nesting indicator graph. 

5. Mixed programming of C language and assembly language

Some special instructions of the microcontroller do not have a direct corresponding description in the standard C language syntax, such as the clear watchdog instruction "clrwdt" and the sleep instruction "sleep" of the PIC microcontroller; the microcontroller system emphasizes the real-time control. In order to achieve this requirement, it is sometimes necessary to use assembly instructions to implement part of the code to improve the efficiency of program operation. There are two ways to embed assembly instructions in C programs:
(1) If you only need to embed a few assembly instructions, PICC provides a function-like statement:
asm("clrwdt");
This is the most direct and easiest way to directly embed assembly instructions in the original C program.
(2) If you need to write a continuous section of assembly instructions, PICC supports another syntax description: start the assembly instruction segment with "#asm" and end it with "#endasm". For example:
[page]
5.1 Assembly instructions address global variables defined in C language

All symbols defined in C will automatically have an underscore "_" added in front of them after compilation. Therefore, if you want to address various variables defined in C language in assembly instructions, you must add the "_" symbol in front of the variable. For example, count in the above example is an unsigned global variable defined in C. In assembly language, you only need to add the "_" symbol in front of it to access it. In addition, for multi-byte global variables defined in C, for example, there is the following definition in C:

Then when accessing it in assembly, you have to access it by bytes, for example:

5.2 Assembly instructions address local variables of C functions

As mentioned above, PICC uses a "static overwrite" technique to determine a fixed address (located in bank0) for each automatic local variable (including the entry parameters when calling a function), so the embedded assembly instructions only need to use the direct addressing method of the data register to address it, so the key is to know the addressing symbols of these local variables. It is recommended that readers first write a small piece of C code, which contains the simplest local variable operation instructions, and compile this C source code into the corresponding PICC assembly instructions; check how the assembly instructions generated by the C compiler address these local variables, and use the same addressing method for the inline assembly instructions written by yourself.

Compared with assembly language, the advantages of programming in C language are unquestionable: development efficiency is greatly improved, humanized statement instructions plus modular programs are easy to manage and maintain on a daily basis, and programs are easy to port between different platforms. So since C language is used for programming, try to avoid using embedded assembly instructions or writing assembly instruction module files as a whole. For example:


the circular right shift operation of variables is very inconvenient to implement in C language. PIC microcontrollers already have corresponding shift operation assembly instructions, so it is most efficient to implement it in the form of embedded assembly. At the same time, the control of the number of shifts, in essence, the decrement judgment of the variable count1 can also be directly implemented with assembly instructions, which can save code, but it is more intuitive and easier to maintain to describe it in standard C language.

6. Notes

(1) Since all local variables will occupy the storage space of bank0, the number of bytes of variables that users can locate in bank0 will be limited. Please pay attention to this in actual use.
(2) When a non-bit variable is converted into a bit variable in a program, please note that the compiler only judges the lowest bit of the ordinary variable: if the lowest bit is 0, it is converted into bit variable 0; if the lowest bit is 1, it is converted into bit variable 1.
(3) Since the internal resources of the PIC series microcontroller are very limited, unsigned character variables should be used as much as possible to save space under the conditions allowed.
(4) PICC does not reserve address space for absolutely located variables, for example:

So please use it with caution.
(5) Try to use global variables for parameter passing. The biggest advantage of using global variables is that addressing is intuitive. You only need to add an underscore before the variable name defined in C language to address it in the assembly statement; the efficiency of using global variables for parameter passing is also higher than that of formal parameters.
(6) For multi-byte variables (such as int type, float type variables, etc.), PICC follows the Littleendian standard, that is, the low byte is placed at the low address of the storage space, and the high byte is placed at the high address. Please pay attention to this when programming.

7 Conclusion

Generally, the codes generated by C language are relatively complicated, so if you want to write high-quality and practical C language programs, you must have a detailed understanding of the microcontroller architecture and hardware resources. However, using C language to develop PIC series microcontroller system software has the advantages of high code writing efficiency, intuitive software debugging, convenient maintenance and upgrading, high code reuse rate, and convenient cross-platform code transplantation. Therefore, the application of C language programming in microcontroller system design will become more and more extensive.

References

[1]HITECH Software.PICC ANSI C compiler use's guide[EB].2002.
[2]Cai Chunjie, Xing Wu.PIC 16/17 microcontroller principle and application[M].Hefei: University of Science and Technology of China Press, 1997

Keywords:PIC Reference address:Write PIC microcontroller programs based on PICC compilation environment

Previous article:Application of PIC16C65 single chip microcomputer in scanning tunneling microscope
Next article:Application of PIC16C7X microcontroller in low power consumption identification and meter reading

Recommended ReadingLatest update time:2024-11-16 15:57

PIC microcontroller-stamping discharge monitoring
A small product that realizes the following functions: In the interval between two stampings, the stamped parts must be discharged through the stamping monitoring light curtain. If there is no discharge within the set time, the machine must be stopped to avoid damaging the mold. It can be turned off when not in use
[Microcontroller]
PIC microcontroller-stamping discharge monitoring
PIC16F877 matrix keyboard
/Purpose of the experiment: Familiarize yourself with the scanning method of the keyboard matrix //The program does not perform key debounce, nor does it consider the situation where multiple keys are pressed at the same time. //The lowest two digital tubes display the corresponding keys (e.g., if S10 is pressed, 10
[Microcontroller]
Introduction to PIC Microcontroller
There are many types of PIC microcontrollers, which are difficult for beginners to deal with and easy to confuse. The following is a simple classification, which I hope will help beginners learn: Initial 8-bit microcontroller: PIC12C5XXX/16C5X series The PIC16C5X series is the earliest s
[Microcontroller]
Dynamic display of LEDs using PIC16F877A microcontroller
list p=16F877A,R=DEC include "p16F877A.inc" ;;;;;user variables;;;;;;;; Count2 equ 23H Count3 equ 24H data_out1 equ 25H data_out2 equ 26H ;;;;;;;reset vector;;;;;;;;; org 0x00 goto mainline ;;;;;;delay program;;;;;;; delay_2ms movlw 0x05 movwf Count2 lp0 movlw 0xff movwf Count3 lp1 decfsz Count3,F goto lp1 decfsz Coun
[Microcontroller]
PIC Microcontroller Quick Start
  PIC16F616 is a 14-pin, 8-bit CMOS microcontroller. It uses a reduced instruction set with only 35 instructions. Due to the use of the Harvard bus structure with separate data bus and instruction bus, most of the instructions are single-cycle instructions except for a few instructions that are not single-cycle. This
[Microcontroller]
Design of three-phase half-controlled rectifier circuit based on PIC microcontroller and TC787 chip
Rectification circuits are widely used in DC motor speed regulation, DC voltage regulation, and other occasions. The three-phase half-controlled rectifier bridge circuit structure is a common rectifier circuit, which is easy to control and has low cost. This article introduces a three-phase half-cont
[Microcontroller]
Design of three-phase half-controlled rectifier circuit based on PIC microcontroller and TC787 chip
Intrusion Detection Device Based on PIC16F628A
Introduction: This paper introduces the principle and characteristics of a person detection device. The device is based on the PIC16F628A single-chip microcomputer, uses a pair of independent ultrasonic transducers for transmission and reception, and uses the Doppler effect to effectively detect whether someone enters
[Microcontroller]
Intrusion Detection Device Based on PIC16F628A
Program module for PIC microcontroller to access ferroelectric memory
//This subroutine is used to exchange data between the PIC series microcontroller and the ferroelectric memory FM24C04 #define bitset(var,bitno) ((var)|=(1 #define bitclr(var,bitno) ((var)&=((1 //Define the write address and read address of the ferroelectric //Use the high 256 bytes, then A2 A3 //Use the low 2
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号