"I. how to change from compilation to PICC
First of all, you should have the foundation of C language. The header file of C code must have include, which is a collection of many header files. The C compiler automatically loads other header files according to your chip in pic. H. This is easier to use than assembly. The loaded header file actually declares the chip registers and some functions.
static volaTIle unsigned char TMR0 @ 0x01;
staTIc volaTIle unsigned char PCL @ 0x02;
staTIc volatile unsigned char STATUS @ 0x03;
It can be seen that the registers defined in the assembly header file are similar. As follows:
TMR0 EQU 0X01;
PCL EQU 0X02;
STATUS EQU 0X03;
All define boring addresses as recognized names.
1. How to assign values?
If value is attached to TMR0, in the compilation:
MOVLW 200;
MOVWF TMR0;
Of course, make sure that the current page is 0, otherwise an error will occur.
C language:
TMR0=200;// No error on any page.
You can see that C is very straightforward. And the biggest advantage is that when operating a register, you don't have to consider the problem of the page. Everything by
C automatic completion.
2. How to perform bit operation?
Bit manipulation in assembly is easy. Easier in C. In the header file of C, every bit of all registers that may need bit operation has been
Each bit has a defined name:
For example, each I / O port of porta is defined as Ra0, ra1, ra2... RA7。 Each bit of option is defined as: PS0
PS1、PS2 、PSA 、T0SE、T0CS、INTEDG 、RBPU。 It can be directly calculated and added value.
For example:
RA0=0;
RA2=1;
In the compilation:
BCF PORTA,0;
BSF PORTA,2;
It can be seen that the two are similar, but there is no need to consider the problem of pages in C.
3. Memory allocation problem
Defining a memory in assembly is a very careful problem. Too many problems should be considered. If you don't pay attention, you will make an error. For example, 16 bit
Operation, etc. With C, you don't need to think too much. Here is an example:
16 bit division (c code):
INT X=5000;
INT Y=1000;
INT Z=X/Y;
It takes too much effort in compilation.
Give a small C code and control an LED flashing with Ra0:
#include
void main()
{
int x;
CMCON=0B111; // Drop the comparator of port a, if it has the function of comparator.
ADCON1=0B110; // Turn off the A / D function, if there is a / D function.
TRISA=0; // All RA ports are output.
loop:RA0=! RA0;
for(x=60000;-- x;){;} // delayed
goto loop;
}
Say Ra0 =! Ra0 means that pic reads - modifies - writes to the port register first. The meaning of the above sentence is Cheng Xuxian
Read Ra0, then reverse it, and finally write the calculated value back to Ra0, which realizes the flashing function.
2、 On bit operation of PICC
Since pic processor is the most efficient for bit operation, some bool variables can be placed in a bit of memory to achieve operation
The speed is fast, and the purpose of saving space to the greatest extent can be achieved. There are many options for bit operation in C.
*********************************************
For example: char X; x=x|0B00001000;
char x; x=x & 0B11011111;
The above formula is:
#Define BitSet (VaR, bitno) (VaR | = 1 #define bitclr (VaR, bitno) (VaR & = ~ (1), then the above operation is:
char x; bitset(x,4);
char x; bitclr (x,5);
*************************************************
However, the above method has the disadvantage that it is not intuitive to the meaning of each person. It is better to see the meaning of each person directly in the code,
This can improve programming efficiency and avoid errors. If we want to use the 0-2 bits of X to represent the bool values of temperature, voltage and current respectively, it can be as follows:
unsigned char x @ 0x20;
bit temperature@ (unsigned)&x*8+0;
bit voltage@ (unsigned)&x*8+1;
bit current@ (unsigned)&x*8+2;
After this definition, the bit of X has a visual name, which is no longer a boring number such as 1, 2, 3 and 4. You can modify x globally or operate on each bit:
char=255;
temperature=0;
if(voltage)。。.。。.
*****************************************************************
Another method is to use C's struct structure to define, such as:
struct cypok{
temperature:1;
voltage:1;
current:1;
none:4;
}x @ 0x20;
So you can use
x.temperature=0;
if(x.current)。。.。
Wait for the operation.
**********************************************************
The above method is very effective in some simple designs, but it is difficult for complex designs. For example, in multi-channel industrial control.
The front end needs to collect multi-channel signals respectively, and then set the multi-channel output of the control multi-channel. For example, there are 2 control channels, and the front-end signal of each channel
There are temperature, voltage and current. The rear end control includes motor, horn, relay and led. If it is implemented by assembly, it is a headache
In fact, it is very easy to implement it in C. here also involves a little memory management of C (in fact, the biggest advantage of C is memory management).
The following structure is adopted:
union cypok{
struct out{
motor:1;
relay:1;
speaker:1;
led1:1;
led2:1;
}out;
struct in{
none:5;
temperature:1;
voltage:1;
current:1;
}in;
char x;
};
union cypok an1;
union cypok an2;
What are the benefits of the above structure?
The signal paths an1 and an2 are subdivided;
The signal type of each channel is subdivided (front-end signal in or back-end signal out):
an1.in ;
an1.out;
an2.in;
an2.out;
Then, the specific meaning of each signal is subdivided, such as:
an1.in.temperature;
an1.out.motor;
an2.in.voltage;
an2.out.led2; etc.
This structure is very intuitive. It represents two signals in two memories. And can be extremely convenient to expand.
If you add more signals, you only need to add:
union cypok an3;
union cypok an4;
From the above, we can see the great benefits of using C.
3、 Delay function and loop volume optimization of PICC.
Many friends say that the delay time can not be accurately controlled in C and can not be as intuitive as assembly. In fact, it is not. Let's have a deep understanding of the delay function
Can design an ideal framework. Generally, we use for (x = 100)-- x;){;} This sentence is equivalent to x = 100; while(--x){;};
Or for (x = 0); x《100; x++){;}。
To write a delay function.
Special attention should be paid here: x = 100 does not mean that the loop will jump out after only running 100 instructions.
You can see the compiled assembly:
x=100; while(--x){;}
After compilation:
movlw 100
bcf 3,5
bcf 3,6
movwf _ delay
l2 decfsz _ delay
goto l2
return
It can be seen from the code that the total number of instructions is 303, and the formula is 8 + 3 * (x-1). Note that the cycle period is X-1 and 99. this
It is summarized in that x is a char type loop body. When x is int, it is greatly affected by the x value. It is recommended to design a char type
Loop body, and then call it with a loop body to achieve accurate long-time delay. Here is a method to accurately control the delay
Function. The assembly code of this function is the most concise and can accurately control the instruction time:
void delay(char x,char y){
char z;
do{
z=y;
do{;} while(--z);
}while(--x);
}
The instruction time is: 7 + (3 * (Y-1) + 7) * (x-1) if the call instruction, page setting and transfer parameters of function call are added
Seven instructions spent. Then: 14+ (3* (Y-1) +7) * (x-1). If the delay requirement is not particularly strict, you can use this function:
void delay(){
unsigned int d=1000;
while(--d){;}
}
This function produces a delay of 10003us in 4m crystal, that is, 10ms. If D is changed to 2000, it is 20003us, and so on. Some friends don't understand why they don't use the decrement after while (X --) to control the number of cycles according to the set X value? Now look at the assembly code that compiled it:
bcf 3,5
bcf 3,6
movlw 10
movwf _ delay
l2
decf _ delay
incfsz _ delay,w
goto l2
return
It can be seen that there is one more instruction in the loop body, which is not concise. Therefore, in PICC, it is best to use the front decrement to control the circulating body.
Let's talk about the following statement:
for(x=100;-- x;){;} And for (x = 0; x《100; x++){;}
Literally, they mean the same, but you can view the code through assembly. The latter code is long, while the former assembles a concise generation
Code. Therefore, in PICC, it is best to write the loop body in the form of the former. A good c compiler will automatically turn the incremental loop into the decrement loop. because
This is determined by the hardware characteristics of the processor. PICC is not a very intelligent c compiler, so the human brain is the first. Master some
Experience is good for writing efficient and concise code.
4、 In depth discussion of PICC bit operation
1. Use bit operation to make some flag bits, that is, bool variables. It can be simply defined as follows:
bit a,b,c;
PICC will automatically arrange a memory, and automatically arrange a bit in this memory to correspond to a, B, C. because we just use them to simply
It represents some 0 and 1 information, so we don't need to know in detail how many address bits they are. Just use them.
2. If you need to use a variable with fixed address for bit operation, you can refer to the registers defined in pic. H.
For example, use 25h memory to define 8-bit variables
static volatile unsigned char myvar @ 0x25;
static volatile bit b7 @ (unsigned)&myvar*8+7;
static volatile bit b6 @ (unsigned)&myvar*8+6;
static volatile bit b5 @ (unsigned)&myvar*8+5;
static volatile bit b4 @ (unsigned)&myvar*8+4;
static volatile bit b3 @ (unsigned)&myvar*8+3;
static volatile bit b2 @ (unsigned)&myvar*8+2;
static volatile bit b1 @ (unsigned)
Our other product: