1, Pointer basis
In the single chip microcomputer, the total memory size is 512M, including 216M in chip and 216M out of chip. Pointer, in bytes.
char: 1 byte
int: 2 bytes
long: 4 bytes
Define a pointer: * P
Go to the address of a variable: & P
The pointer is also a variable, but the variable stores an address, and the layout directly assigns a value to the pointer
sizeof is a constant that returns the byte size of a variable.
Example 1:
#include<reg52.h> sbit ADDR0 = P1^0; sbit ADDR1 = P1^1; sbit ADDR2 = P1^2; sbit ADDR3 = P1^3; sbit ENLED = P1^4; void ShiftLeft(unsigned char *p);//Parameter is an address void main() { unsigned int i; unsigned char buf = 0x01; ENLED = 0; ADDR3 = 1; ADDR2 = 1; ADDR1 = 1; ADDR0 = 0; while(1) { P0 = ~buf; for(i = 0;i<20000;i++); ShiftLeft(&buf);//Get the address of buf, move left one bit at a time, change the value of buf if(buf == 0) { buf = 0x01; } } } void ShiftLeft(unsigned char *p) { *p = *p<<1; }
Example two:
#include<reg52.h> bit cmdArrived = 0;//Check whether the upper computer sends the command. If it sends the command, set one unsigned char cmdIndex = 0; unsigned char *ptrTxd; unsigned char cntTxd = 0; unsigned char array1[1] = {1}; unsigned char array2[2] = {1,2}; unsigned char array3[4] = {1,2,3,4}; unsigned char array4[8] = {1,2,3,4,5,6,7,8}; void ConfigUART(unsigned int baud); void main() { EA = 1;//Enable total interrupt ConfigUART(9600); while(1) { if(cmdArrived)//Message sent by upper computer detected { cmdArrived = 0; switch(cmdIndex) { case 1: ptrTxd = array1; cntTxd = sizeof(array1); TI = 1; //Send out break; case 2: ptrTxd = array2; cntTxd = sizeof(array2); TI = 1; break; case 3: ptrTxd = array3; cntTxd = sizeof(array3); TI = 1; break; case 4: ptrTxd = array4; cntTxd = sizeof(array4); TI = 1; break; default: break; } } } } void ConfigUART(unsigned int baud) { SCON = 0x50; //Configure serial port as mode 1 TMOD &= 0x0F; //Reset the control bit of T1 TMOD |= 0x20; //Configure T1 as mode 2 TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 overload value TL1 = TH1; //Initial value equal to overload value ET1 = 0; //Disable T1 interrupt ES = 1; //Enable serial port interrupt TR1 = 1; //Start T1 } void InterruptUART() interrupt 4 { if(RI)//If reception is complete { RI = 0; cmdIndex = SBUF;//Index number cmdArrived = 1; } if(TI)//Send out { TI = 0; if(cntTxd > 0) { SBUF = *ptrTxd; cntTxd--; ptrTxd++;//Send next element } } }
2, Character array and character pointer
The string constant 'a' has a null value \ 0 more than the character constant 'a', which is one more byte.
3, 1602 liquid crystal
#include<reg52.h> #define LCD1602_DB P0 sbit LCD1602_RS = P1^0;//Data command control bit sbit LCD1602_RW = P1^1;//Read write control bit sbit LCD1602_E = P1^5;//Enabling position void InitLcd1602(); void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str); void main() { unsigned char str[] = "Kingst Studio"; InitLcd1602(); LcdShowStr(2, 0, str); LcdShowStr(0, 1, "Welcome to KST51"); while(1); } void LcdWaitReady() //Read state { unsigned char sta; LCD1602_DB = 0xFF;//High level before reading LCD1602_RS = 0;//Data pattern LCD1602_RW = 1;//Reading mode do { LCD1602_E = 1; sta = LCD1602_DB;//Pass LCD status to sta LCD1602_E = 0;//In order not to affect other peripherals, turn off the enable bit }while(sta & 0x80);//When the highest bit is 1, the LCD is busy and cannot be operated } void LcdWriteCmd(unsigned char cmd) //Write instruction { LcdWaitReady();//Determine whether reading and writing operation can be performed LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd;//Data writing LCD1602_E = 1;//Start writing LCD1602_E = 0;//Write off } void LcdWriteDat(unsigned char dat)//Writing data { LcdWaitReady(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat;//Data writing LCD1602_E = 1; LCD1602_E = 0; } void LcdSetCursor(unsigned char x,unsigned char y)//Display coordinate settings { unsigned char addr; if(y == 0)//In the first line { addr = 0x00 + x; } else//In the second row { addr = 0x40 + x; } LcdWriteCmd(addr | 0x80); } void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) { LcdSetCursor(x,y); while(*str != '\0') { LcdWriteDat(*str++); } } void InitLcd1602() //Lcd initialization { LcdWriteCmd(0x38); LcdWriteCmd(0x0C); LcdWriteCmd(0x06); LcdWriteCmd(0x01); }
Conclusion:
In this project, the function is written from top to bottom, which is divided into different modules, and the modules call each other.