*Example 1:
Using P3 port to light up 8 LEDs in sequence
#include<reg51.h> //Include the header file for microcontroller registers / Function: Delay for a period of time
void delay(void)
{
unsigned char i,j;
for(i=0;i<250;i++)
for(j=0;j<250;j++)
;
}
/ Function: Main function
void main(void)
{
while(1)
{
P3=0xfe; //First light on
delay(); //Call delay function
P3=0xfd; //Second light on
delay(); //Call delay function
P3=0xfb; //Third light on
delay(); //Call delay function
P3=0xf7; //Fourth light on
delay(); //Call delay function
P3=0xef; //Fifth light on
delay(); //Call delay function
P3=0xdf; //Sixth light on
delay(); //Call delay function
P3=0xbf; //Seventh light on
delay(); //Call delay function
P3=0x7f; //Eighth light on
delay(); //Call delay function
} / /
}
*Example 2:
Lighting up 8 LEDs in sequence by manipulating the address of P3 port
#include<reg51.h> //Include the header file for microcontroller registers
sfr x=0xb0; //The address of P3 in memory is b0H, through sfr can define all internal 8-bit special function registers of 8051 core microcontroller, operating on address x is equivalent to operating on P1 port
/
Function: Delay for a period of time
/
void delay(void)
{
unsigned char i,j;
for(i=0;i<250;i++)
for(j=0;j<250;j++)
; //Use loop to wait for several machine cycles, thus delaying for a period of time
}
/
Function: Main function
/
void main(void)
{
while(1)
{
x=0xfe; //First light on
delay(); //Call delay function
x=0xfd; //Second light on
delay(); //Call delay function
x=0xfb; //Third light on
delay(); //Call delay function
x=0xf7; //Fourth light on
delay(); //Call delay function
x=0xef; //Fifth light on
delay(); //Call delay function
x=0xdf; //Sixth light on
delay(); //Call delay function
x=0xbf; //Seventh light on
delay(); //Call delay function
x=0x7f; //Eighth light on
delay(); //Call delay function
} / /
}
*Example 3: Control light flashing time with different data types
#include<reg51.h> //Include the header file for microcontroller registers
/
Function: Delay for a period of time using integer data
/
void int_delay(void) //Delay for a longer period of time
{
unsigned int m; //Define unsigned integer variable, double-byte data, value range from 0 to 65535 for(m=0;m<36000;m++)
; //Do nothing
}
/
Function: Delay for a period of time using character data
/
void char_delay(void) //Delay for a shorter period of time
{
unsigned char i,j; //Define unsigned char variable, single-byte data, value range from 0 to 255 for(i=0;i<200;i++)
for(j=0;j<180;j++)
; //Do nothing
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
while(1)
{
for(i=0;i<3;i++)
{
P1=0xfe; //Light on P1.0
int_delay(); //Delay for a longer period of time
P1=0xff; //Turn off
int_delay(); //Delay for a longer period of time
}
for(i=0;i<3;i++)
{
P1=0xef; //Light on P1.4
char_delay(); //Delay for a longer period of time
} P1=0xff; //Turn off char_delay(); //Delay for a longer period of time } }
*Example 4: Control the first light on with microcontroller
#include<reg51.h> //Include the header file for microcontroller registers
void main(void)
{
P1=0xfe; //P1=1111 1110B, which outputs low level at P1.0
}
*Example 5: Control a light to flash with microcontroller: Understand the working frequency of microcontroller #include<reg51.h> //Include the header file for microcontroller registers
/
Function: Delay for a period of time
/
void delay(void) //Two voids mean no return value, no parameter passing {
unsigned int i; //Define unsigned integer, maximum value range 65535 for(i=0;i<20000;i++) //Do 20000 empty loops
; //Do nothing, wait for a machine cycle
}
/ Function: Main function (C language stipulates that there must be one and only one main function) / void main(void)
{
while(1) //Infinite loop
{
P1=0xfe; //P1=1111 1110B, P1.0 outputs low level
delay(); //Delay for a period of time
P1=0xff; //P1=1111 1111B, P1.0 outputs high level
delay(); //Delay for a period of time
}
}
*Example 6: Send the status of P1 port to P0, P2, P3 ports: Understand the function of I/O port pins
#include<reg51.h> //Include the header file for microcontroller registers
/ Function: Main function (C language stipulates that there must be one and only one main function) /
void main(void)
{
while(1) //Infinite loop
{
P1=0xff; // P1=1111 1111B, turn off LED
P0=P1; // Send P1 port status to P0 port
P2=P1; // Send P1 port status to P2 port
P3=P1; // Send P1 port status to P3 port
}
}
*Example 7: Display the results of addition and subtraction operations using P0 and P1 ports #include<reg51.h>
void main(void)
{
unsigned char m,n;
m=43; //Decimal number 2×16+11=43
n=60; //Decimal number 3×16+12=60
P1=m+n; //P1=103=0110 0111B, results in P1.3, P1.4, P1.7 lights up P0=n-m; //P0=17=0001 0001B, results in P0.0, P0.4 lights off }
*Example 8: Display the results of multiplication operations using P0 and P1 ports
#include<reg51.h> //Include the header file for microcontroller registers
void main(void)
{
unsigned char m,n;
unsigned int s;
m=64;
n=71;
s=m*n; //s=64*71=4544, requires 16-bit binary representation, high 8 bits sent to P1 port, low 8 bits sent to P0 port
//Since 4544=17*256+192=H3 16 16 16+H2 16 16+H1 16+H0
//Dividing both sides by 256 gives 17+192/256=H3 16+H2+(H1 16+H0)/256
//Therefore, the high 8-bit hexadecimal number H3 16+H2 must equal 17, that is, 4544 divided by 256 gives the quotient
//The low 8-bit hexadecimal number H1 16+H0 must equal 192, that is, 4544 divided by 256 gives the remainder
P1=s/256; //Send the high 8 bits to P1 port , P1=17=11H=0001 0001B, P1.0 and P1.4 lights off, others light up
P0=s%256; //Send the low 8 bits to P0 port , P0=192=c0H=1100 0000B,P3.1,P3.6,P3.7 lights off, others light up
}
*Example 9: Display the results of division operations using P1 and P0 ports
#include<reg51.h> //Include the header file for microcontroller registers
void main(void)
{
P1=36/5; //Integer result
P0=((36%5)*10)/5; //Decimal result
while(1)
; //Infinite loop to prevent the program from “running away”
}
*Example 10: Use self-incrementing operation to control the P0 port 8-bit LED flowing pattern
#include<reg51.h> //Include the header file for microcontroller registers
/
Function: Delay for a period of time
/
void delay(void)
{
unsigned int i;
for(i=0;i<20000;i++)
;
}
/ Function: Main function
/ void main(void)
{
unsigned char i;
for(i=0;i<255;i++) //Note: i cannot exceed 255
{
P0=i; //Send the value of i to P0 port
delay(); //Call delay function
}
}
*Example 11: Display the logical