Please click above for free follow…
*Example 1:
Use P3 port to light up 8 LEDs in sequence
#include<reg51.h>//Include the header file for MCU 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:
Light up 8 LEDs in sequence by manipulating the P3 port address
#include<reg51.h>//Include the header file for MCU 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 MCU, 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++)
;
}
/ 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 LED flashing time with different data types
#include<reg51.h>//Include the header file for MCU registers
/ Function: Delay for a longer period of time
void int_delay(void) // Delay for a longer period of time
{
unsigned int m; // Define unsigned int variable, two-byte data, value range 0~65535 for(m=0;m<36000;m++)
;
}
/ Function: Delay for a shorter period of time
void char_delay(void) // Delay for a shorter period of time
{
unsigned char i,j; // Define unsigned char variable, one-byte data, value range 0~255 for(i=0;i<200;i++)
for(j=0;j<180;j++)
;
}
/ 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 shorter period of time
}
P1=0xff; // Turn off
char_delay(); // Delay for a shorter period of time
}
}
*Example 4:Control the first light using the MCU
#include<reg51.h>//Include the header file for 51 MCU registers
void main(void)
{
P1=0xfe; // P1=1111 1110B, P1.0 outputs low level
}
*Example 5:Control an LED to blink with the MCU: Understand the working frequency of the MCU #include<reg51.h>//Include the header file for MCU registers
/ Function: Delay for a period of time
void delay(void) // Two voids mean no return value and no parameters passed {
unsigned int i; // Define unsigned integer, maximum value range 65535 for(i=0;i<20000;i++) // Do 20000 empty loops
;
}
/ Function: Main function (C language requires there must be 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 P1 port status to P0, P2, P3 ports: Understand the function of I/O port pins
#include<reg51.h>//Include the header file for MCU registers
/ Function: Main function (C language requires there must be 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 addition and subtraction results on P0 and P1 ports #include
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, result P1.3, P1.4, P1.7 lights up P0=n-m; // P0=17=0001 0001B, result P0.0, P0.4 lights off }
*Example 8:Display multiplication results on P0 and P1 ports
#include<reg51.h>//Include the header file for MCU 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
P1=s/256; // High 8 bits sent to P1 port, P1=17=11H=0001 0001B, P1.0 and P1.4 lights off, others on
P0=s%256; // Low 8 bits sent to P0 port, P3=192=c0H=1100 0000B,P3.1,P3.6,P3.7 lights off, others on
}
*Example 9:Display division results on P1 and P0 ports
#include<reg51.h>//Include the header file for MCU registers
void main(void)
{
P1=36/5; // Calculate integer
P0=((36%5)*10)/5; // Calculate decimal
while(1)
; // Infinite loop to prevent program from running away
}
*Example 10:Control P0 port 8-bit LED flowing patterns with auto-increment operation
#include<reg51.h>//Include the header file for MCU 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 that the value of i cannot exceed 255
{
P0=i; // Send the value of i to P0 port
delay(); // Call delay function
}
}
*Example 11:Display logical