The usage of array in C language of single chip microcomputer

Publisher:gamma13Latest update time:2011-10-11 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

An array is an ordered collection of data elements of the same type. An array is represented by an array name, and the data in the array is uniquely identified by a specific subscript. The purpose of introducing an array is to use a continuous memory space to store multiple data of the same type to solve the storage problem of a batch of related data. Like ordinary variables, arrays must be defined before they can be used. Arrays play an important role in the C51 language, so it is necessary to have a deep understanding of arrays. The following is a detailed introduction to arrays.

(1) One-dimensional array

A one-dimensional array is the simplest array, used to store data of the same type. The data is stored linearly and continuously.

Use the following routine to illustrate array creation and data operations:

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the creation of arrays and data operations.

-------------------------------------------------- ---

*/

unsigned char array[10]; //define an array with 10 elements

void main()

{

unsigned char i;

for(i=0;i<10;i++)

{

array[i]=i; //Use subscript to call the element in the array

}

/*

---------------------------------------

array |9|8|7|6|5|4|3|2|1|0| [9]~[0]

---------------------------------------

*/

while(1);

}

The array name is used to identify the array. In fact, it is the first address of the array, that is, a pointer. However, the address it represents is fixed and cannot be changed. As mentioned in the previous chapters, array[2] is equivalent to *(array+2), but array++ cannot be used because array is a constant.

The array in the above program is created statically. The following example illustrates the dynamic creation of the array.

#include

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the dynamic creation of an array.

-------------------------------------------------- ---

*/

unsigned char *parray;

void main()

{

unsigned char i;

parray=(unsigned char *)malloc(10); //dynamically create an array

for(i=0;i<10;i++)

{

parray[i]=i; //Assign values ​​to the array

}

free(parray); //release the array

while(1);

}

A string is an important special case of an array. The data of each unit is of character type (char), and the last unit is '' (0x00), which is used to indicate the end of the string. The C51 function library provides functions specifically for processing strings, as illustrated by the following routine:

#include

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the string

-------------------------------------------------- ---

*/

char s[]={'y','a','h','o','o',''};

//Define a string and initialize it, ending with ''

void main()

{

char s_temp[10];

strcpy(s_temp,s); //strcpy is located in the string.h header file and implements character copying

//s is a constant, not s++

strcpy(s_temp,"yahoo");//Equivalent to the above statement

while(1);

}[page]

The following are some flexible uses of strings, hoping to help readers gain a deeper understanding of strings:

#include

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the flexible use of strings

-------------------------------------------------- ---

*/

/*

-------------------------------------------------- ---

This function extracts the nth substring from string s, where substrings are separated by ','

Returns a pointer to the substring

-------------------------------------------------- ---

*/

char *get_sub_string(char *s,unsigned char n)

{

int i;int d=0;int fore=0;

int len=strlen(s);

for(i=0;i

{

if(s[i]==',')

{

s[i]='';

d++;

if(d==n)

{

return s+fore;

}

else

{

fore=i+1;

}

}

}

return NULL;

}

void main()

{

unsigned char c;

char string[20];

c="yahoo"[2]; //c='h'

/*As mentioned earlier, a string is represented by the first address of the string.

The string "yahoo" is actually its first address, so it can be done like this

Take a character from it: "yahoo"[2]*/

strcpy(string,"123,234,345,456");

strcpy(string,get_sub_string(string,2));

while(1);

}

(2) Two-dimensional array

An array whose elements can be determined by two subscripts is called a two-dimensional array. The general form of its definition is:

Type specifier array name [constant expression 1] [constant expression 2]

For example: int array[6][4];

A two-dimensional array array is defined with 6 rows and 4 columns, totaling 24 elements.

The constant expression 1 and constant expression 2 in the two square brackets specify the number of rows and columns of the array, thereby determining the number of elements in the array. The row subscript starts from 0 and the maximum is 5, with a total of 6 rows; the column subscript also starts from 0 and the maximum is 3, with a total of 4 columns. There are a total of 6X4=24 elements in the array, which are expressed as follows:

array[0][0]

array[0][1]

array[0][2]

array[0][3]

array[1][0]

array[1][1]

array[1][2]

array[1][3]

array[2][0]

array[2][1]

array[2][2]

array[2][3]

array[3][0]

array[3][1]

array[3][2]

array[3][3]

array[4][0]

array[4][1]

array[4][2]

array[4][3]

array[5][0]

array[5][1]

array[5][2]

array[5][3]

In actual use, the above two-dimensional array can be regarded as a matrix with 6 rows and 4 columns, which is a flat two-dimensional structure. So how does the compiler use one-dimensional storage space to allocate continuous storage units to such a two-dimensional structure? C51 uses the row storage method, that is, the 0th row elements are stored in the memory first, and then the 1st row, 2nd row, ... elements are stored. In each row, the 0th column is stored first, followed by the 1st column, 2nd column, ... elements.

#include

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the use of two-dimensional arrays

-------------------------------------------------- ---

*/

void main()

{

unsigned char arrays[3][3]={{1,2,3},{2,3,4},{3,4,5}};

//Define a 3-row 3-column 2D array. The rest is stored in memory in a 1D manner.

//You can know this by using the following method

unsigned char test;

test=((unsigned char *)arrays)[6];//test=3;

//Convert the first address of the two-dimensional data into a one-dimensional array and access it as a one-dimensional array

while(1);

}

In addition to one-dimensional arrays and two-dimensional arrays, you can actually define arrays of any dimension. Multidimensional arrays are used to represent quantities that can only be determined by multiple subscripts.

For example: int arrays[3][3][3]

Represents array arrays as a three-dimensional array, corresponding to the three-dimensional storage model.

In fact, the memory in the microcontroller is one-dimensional, that is, all data is stored sequentially, so no matter how many dimensions the array has, the compiler abstracts the array to the actual one-dimensional array mapping stored in the microcontroller.

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the three-dimensional array

-------------------------------------------------- ---

*/

void main()

{

unsigned char test;

unsigned char arrays[2][2][2]={{{1,2},{2,3}},{{3,4},{4,5}}};

test=arrays[1][1][0]; //test=4

test=((unsigned char *)arrays)[7]; //test=5

while(1);

}[page]

(3) Structure array

Multiple structure variables can also form a structure array, and the definition method is exactly the same as that of defining a structure variable.

As an example:

#include

/*

-------------------------------------------------- ---

This program is used to illustrate the structure array

-------------------------------------------------- ---

*/

typedef struct

{

int a,b,c,d;

} Stru;

void main()

{

Stru stru[10]; //define structure array

unsigned char i=0;

for(;i<10;i++)

{

stru[i].a=i;

stru[i].b=i;

stru[i].c=i;

stru[i].d=i;

}

while(1);

}

Keywords:MCU Reference address:The usage of array in C language of single chip microcomputer

Previous article:Design of household smoke alarm based on MSP43O microcontroller
Next article:MN101EF32D single chip microcomputer realizes oscillometric blood pressure measurement

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

51 MCU timer/counter mode control word
We have learned that the timer/counter in the microcontroller can have multiple uses, so how can I make them work for the purpose I need? This requires setting it through the control word of the timer/counter. There are two special function registers related to timing/counting in the microcontroller, which are TMO
[Microcontroller]
51 MCU timer/counter mode control word
Design of intelligent car system based on single chip microcomputer
The simple intelligent electric car designed this time uses the AT89S52 single-chip microcomputer as the detection and control core of the car; the metal sensor TL-Q5MC is used to detect the iron sheets sensed on the road, and the feedback signal is sent to the single-chip microcomputer, so that the single-chip microc
[Microcontroller]
Design of intelligent car system based on single chip microcomputer
51 MCU I2C detailed explanation and program source code
I2C is a serial data communication protocol invented by Philips, which uses only two signal lines: SerialClock (SCL) and SerialData (SDA). I2C is a bus structure, with 1 Master and 1 or more Slaves. Each Slave device is distinguished by a 7-bit address, followed by a read/write bit, indicating read (=1) or write (=0),
[Microcontroller]
51 single chip microcomputer stepper motor experiment
1. Stepper Motor Picture 2. Introduction to stepper motor The stepper motor is an open-loop control element that converts electrical pulse signals into angular displacement or linear displacement. In the case of non-overload, the motor's speed and stop position only depend on the frequency and number of pulses of
[Microcontroller]
51 single chip microcomputer stepper motor experiment
A simple dialing keyboard based on 51 single chip microcomputer and 8-bit digital tube
We know that when dialing, each time a digit is dialed, the previously dialed digit must be moved one position to the left. We can use a one-bit array to achieve position movement by moving the numbers in the array and dynamically scanning the 8-bit digital tube. code show as below: #include "reg52.h" //This file
[Microcontroller]
Digital power following technology based on ATMEGA8 microcontroller
introduction The five major problems on Earth at present, including energy imbalance, dictatorship of oil-producing countries, uncontrolled climate change, energy shortage, and disappearance of biodiversity, are all caused by the extensive use and reliance on traditional energy by humans. Moreover, the five maj
[Microcontroller]
Digital power following technology based on ATMEGA8 microcontroller
Using the MCU's serial port to drive 74LS164
  The 8051 microcontroller serial interface is a programmable full-duplex serial communication interface. It can be used as an asynchronous communication method (UART) to connect to external devices that transmit information serially, or for full-duplex 8051 multi-machine systems using standard asynchronous communicat
[Microcontroller]
Using the MCU's serial port to drive 74LS164
Implementation of command batch processing in serial communication between PC and microcontroller
    Abstract: This article introduces the implementation ideas, methods, communication protocols and C51 program platform of batch processing commands for communication between PC and MCS-51 microcontroller. It provides a new method for the design of serial communication program between PC and microcontroller.   
[Industrial Control]
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号