Principle of Microcomputer-06-Single-chip C Language Design

C language programming of single chip microcomputer

The programming of the single-chip microcomputer application system can be implemented in C language in addition to assembly language. There are many kinds of C language that can operate the hardware of 51 series single-chip computers. They are generally called C51.
Based on the standard C (ANSI C), C51 expands the corresponding data types and variables for the hardware resources of the single-chip microcomputer. C51 has the same syntax, program structure, and design methods as standard C. This chapter focuses on the extension of C51 to Standard C, and explains the programming method of C51 with a few simple examples.

Why use C51

  • C language is a general programming language with high code rate, rich data types and operators, and a good program structure. It is suitable for programming of various applications. C51 compiler.
  • The target code generated by C51 has high running speed, small storage space required, conforms to the ANSI standard of C language, the generated code follows the Intel target file format, and can be mixed with A51 assembly language or PL / M51 language target code

Program structure

全局变量说明        /*可被各函数引用*/  
main(  )                 /*主函数*/  
 { 
    局部变量说明	   /* 只在本函数引用 */
    执行语句  (包括函数调用语句); 
 }  
Fun1(形式参数表)   /*函数1*/  
形式参数说明  
 {  
    局部变量说明  
    执行语句(包括调用其他函数语句);  
 } 
  … 
Funn(形式参数表)        /*函数n*/  
形式参数说明  
{  
    局部变量说明  
    执行语句  (包括调用其他函数语句) 
 } 

type of data

Memory mode

data char var;                         //字符变量var定位在片内RAM区
char code MSG[]=″ENTER PARAMETER:″     //字符数组定位在程序存储区
unsigned long xdata array[100];       /* 无符号长型数组定位在片外RAM区,每元素占4bytes*/ 
float idata x,y,z;                     /*实型变量x,y,z,定位在片内用 间址访问的内部RAM区*/   
bit  lock;                            /*位变量Lock定位在片内RAM可位寻址区*/
unsigned int pdata dimension;         /* 无符号整型变量 dimension定位
                                                               在分页的外部RAM区 */  
 unsigned char xdata vector [10][4][4];  /* 无符号字符型三维数组,定位在片外RAM区*/ 
sfr P0=0x80;                            /*定义P0口,地址为80H*/
char bdata flags;                      /*字符变量flags定位在可位寻址内部RAM区*/  
sbit flag0=flags^0;                    /*定义flag0为flags.0


Pointer variable

The pointer is the address of the storage unit, and the variable that stores this address is called a pointer variable.



exercise

2. What are the characteristics of C51 and assembly language? How to realize the complementary advantages of the two?

Answer: Using C51 for programming, the allocation of system hardware resources is simpler than using assembly language , and the program is easier to read and modify , which is suitable for writing larger programs.

The target program generated by assembly language occupies less storage space, runs faster, has the advantages of high efficiency and strong real-time , and is suitable for writing short and efficient programs.

7. How is the interrupt function defined? What is the significance of the various options?

Answer: interrupt n is used to define the interrupt function. N is the interrupt number, which can be 0 ~ 31. The interrupt address can determine the entry address of the interrupt service program.

Interrupt numbers corresponding to commonly used interrupt sources

Interrupt source External interrupt 0 Timer 0 External interrupt 1 Timer 1 Serial port
Interrupt number 0 1 2 3 4

9. What is the difference between a general pointer and a memory-based pointer?

Answer: When the memory type of the object it points to is not specified at the time of definition, the pointer variable is regarded as a general pointer.

Generally, the pointer occupies 3 bytes : the first byte stores the memory type code of the pointer (determined by the default value of the compilation mode), and the second and third bytes store the high and low address offsets of the pointer, respectively Shift amount. ** When the storage type of the object it points to is specified at the time of definition, the pointer variable belongs to a memory-based pointer.

Memory-based pointers can efficiently access objects , the type is determined by the memory type in the C51 source code, and is determined at compile time.

12. Correct the errors in the following program.

#include<reg51.h>
main ()
{a = c;
 int a = 7,c;
 delay (10)
void delay( );
{
 char i;
 for (i = 0;i< = 255;i++);
}

Answer: This program has the following errors:

Variables must be defined before being referenced.

The fifth sentence should be followed by a semicolon after calling the function.

At the end of the Main () function, there are missing back braces.

The called function delay () follows main () and must be declared before it.

The sixth sentence of the function indicates that the semicolon after the statement should be removed.

13. Try to explain why the pointer length of xdata type uses 2 bytes.

Answer: Because xdata is an external data memory, it can have a maximum storage unit of 64KB . The xdata pointer is the address of the external data storage unit. To represent 64KB unit addresses, it must be represented by 16 bytes in 2 bytes .

64KB = 65536B = 2 ^ 16B

Published 225 original articles · Like 140 · Visit 250,000+

Guess you like

Origin blog.csdn.net/jankin6/article/details/105402750