* Example 1:
Use P3 port to light up 8-bit LED
#include//Header file containing 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; //The first light is on
delay(); //Call the delay function
P3=0xfd; //The second light is on
delay(); //Call the delay function
P3=0xfb; //The third light is on
delay(); //Call the delay function
P3=0xf7; //The fourth light is on
delay(); //Call the delay function
P3=0xef; //The fifth light is on
delay(); //Call the delay function
P3=0xdf; //The sixth light is on
delay(); //Call the delay function
P3=0xbf; //The seventh light is on
delay(); //Call the delay function
P3=0x7f; //The eighth light is on
delay(); //Call the delay function
} //
}
* Example 2:
Light up the 8-bit LED by operating the P3 port address
#include //Contains the header file of the microcontroller register
sfr x=0xb0; //The address of port P3 in the memory is b0H. The 8051 core microcontroller can be defined through sfr
//All internal 8-bit special function registers, the operation of address x is the operation of port P1
/
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 a loop to wait for several machine cycles, thereby delaying for a period of time
}
/
Function: Main function
/
void main(void)
{
while(1)
{
x=0xfe; //The first light is on
delay(); //Call the delay function
x=0xfd; //The second light is on
delay(); //Call the delay function
x=0xfb; //The third light is on
delay(); //Call the delay function
x=0xf7; //The fourth light is on
delay(); //Call the delay function
x=0xef; //The fifth light is on
delay(); //Call the delay function
x=0xdf; //The sixth light is on
delay(); //Call the delay function
x=0xbf; //The seventh light is on
delay(); //Call the delay function
x=0x7f; //The eighth light is on
delay(); //Call the delay function
}
}
* Example 3: Use different data types to control the flashing time of the light
#include //Contains the header file of the microcontroller register
/
Function: Use integer data to delay for a period of time
/
void int_delay(void) //delay for a longer period of time
{
unsigned int m; //define unsigned integer variable, double-byte data, value range is 0~65535 for(m=0;m<36000;m++)
; //No operation
}
/
Function: Use character data to delay for a period of time
/
void char_delay(void) //delay for a short period of time
{
unsigned char i,j; //define unsigned character type variable, single byte data, value range 0~255 for(i=0;i<200;i++)
for(j=0;j<180;j++)
; //No operation
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
while(1)
{
for(i=0;i<3;i++)
{
P1=0xfe; //The light of P1.0 port turns on
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; //The light of P1.4 port turns on
char_delay(); //Delay for a longer period of time
} P1=0xff; //Turn off char_delay(); //Delay for a longer period of time } }
* Example 4: Use the microcontroller to control the first light
#include //Contains the header file for 51 MCU register definition
void main(void)
{
P1=0xfe; //P1=1111 1110B, that is, P1.0 outputs low level
}
*Example 5: Using a microcontroller to control a light to flash: Understanding the operating frequency of a microcontroller
#include //Contains the header file of the microcontroller register
/
Function: Delay for a period of time
/
void delay(void) //The two voids mean no return value and no parameter passing {
unsigned int i; //define an unsigned integer with a maximum value range of 65535 for(i=0;i<20000;i++) //do 20,000 empty loops
; //Do nothing, wait for one machine cycle
}
/ Function: Main function (C language requires 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 while
P1=0xff; //P1=1111 1111B, P1.0 outputs high level
delay(); //delay for a while
}
}
*Example 6: Send the status of port P1 to ports P0, P2, and P3 respectively: Understanding I/O ports
Pin Function
#include //Contains the header file of the microcontroller register
/ Function: Main function (C language stipulates that there must be and can only be one main function) /
void main(void)
{
while(1) // infinite loop
{
P1=0xff; // P1=1111 1111B, turn off the LED
P0=P1; // Send the status of P1 port to P0 port
P2=P1; // Send the status of P1 port to P2 port
P3=P1; // Send the status of P1 port to P3 port
}
}
* Example 7: Use P0 and P1 to display the results of addition and subtraction operations respectively
#include
void main(void)
{
unsigned char m,n;
m=43; // decimal number 2x16+11=43
n=60; // decimal number 3x16+12=60
P1=m+n; //P1=103=0110 0111B, the lights of P1.3, P1.4, and P1.7 are turned on. P0=nm; //P0=17=0001 0001B, the lights of P0.0 and P0.4 are turned off. }
* Example 8: Use P0 and P1 to display the multiplication result
#include //Contains the header file of the microcontroller register
void main(void)
{
unsigned char m,n;
unsigned int s;
m=64;
n=71;
s=mn; //s=64 71=4544, requires 16-bit binary representation, the upper 8 bits are sent to port P1, and the lower 8 bits are sent to port P0
//Since 4544=17 256+192=H3 16 16 16+H2 16 16+H1 16+H0
// Divide both sides by 256, we get 17+192/256=H3 16+H2+(H1 16+H0)/256
//Therefore, the high 8 bits of the hexadecimal number H3 16 + H2 must equal 17, which is the quotient of 4544 divided by 256
//The lower 8 bits of the hexadecimal number H1 16 + H0 must equal 192, which is the remainder of 4544 divided by 256
P1=s/256; //The upper 8 bits are sent to P1 port, P1=17=11H=0001 0001B, P1.0 and P1.4 ports are off, and the rest are on
P0=s%256; //The lower 8 bits are sent to P0 port, P3=192=c0H=1100 0000B, P3.1, P3.6, P3.7 ports are off, and the rest are on
}
* Example 9: Use P1 and P0 to display the result of division operation
#include //Contains the header file of the microcontroller register
void main(void)
{
P1=36/5; //Find the integer
P0=((36%5) 10)/5; //Find the decimal
while(1)
; //Infinite loop to prevent the program from "running away"
}
* Example 10: Use auto-increment operation to control the 8-bit LED flow pattern at port P0
#include //Contains the header file of the microcontroller register
/
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 port P0
delay(); //Call the delay function
}
}
*Example 11: Use P0 port to display the result of logical "AND" operation
#include //Contains the header file of the microcontroller register void main(void)
{
P0=(4>0)&&(9>0xab); //Send the result of the logic operation to port P0
while(1)
; //Set an infinite loop to prevent the program from "running away"
}
* Example 12: Use P0 port to display the result of conditional operation
#include //Contains the header file of the microcontroller register void main(void)
{
P0=(8>4)?8:4; //Send the conditional operation result to port P0, P0=8=0000 1000B while(1)
; //Set an infinite loop to prevent the program from "running away"
}
*Example 13: Use port P0 to display the result of bitwise XOR operation
#include //Contains the header file of the microcontroller register void main(void)
{
P0=0xa2^0x3c; //Send the conditional operation result to P0 port, P0=8=0000 1000B while(1)
; //Set an infinite loop to prevent the program from "running away"
}
* Example 1 4 : Use P0 to display the result of left shift operation
#include //Contains the header file of the microcontroller register void main(void)
{
P0=0x3b<<2; //Send the left shift result to P0 port, P0=1110 1100B=0xec while(1)
; //Infinite loop to prevent the program from "running away"
}
* Example 1 5 : "Universal Logic Circuit" Experiment
#include //Contains the header file of the microcontroller register
sbit F=P1^4; //Define the F bit as P1.4
sbit X=P1^5; //define the X bit as P1.5
sbit Y=P1^6; //Define the Y bit as P1.6
sbit Z=P1^7; //Define the Z bit as P1.7
void main(void)
{
while(1)
{
F=((~X)&Y)|Z; //Assign the result of the logical operation to F
;
}
}
* Example 1 6 : Use right shift operation to light up the 8-bit LED at port P1
#include //Contains the header file of the microcontroller register
/
Function: Delay for a period of time
/
void delay(void)
{
unsigned int n;
for(n=0;n<30000;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
while(1)
{
P1=0xff;
delay();
for(i=0;i<8;i++) //Set the number of loops to 8
{
P1=P1>>1; //Each time the loop is repeated, the binary bits of P1 are shifted right by 1 bit, and the high bits are filled with 0 delay(); //Call the delay function
}
}
}
* Example 1 7 : Use if statement to control the flow direction of 8-bit LED at P0 port
#include //Contains the header file of the microcontroller register
sbit S1=P1^4; //Define S1 bit as P1.4
sbit S2=P1^5; //Define S2 bit as P1.5
/
Function: Main function
/
void main(void)
{
while(1)
}
{ if(S1==0) //If button S1 is pressed, P0=0x0f; //The high four-bit LED of port P0 is on if(S2==0) //If button S2 is pressed, P0=0xf0; //The low four-bit LED of port P0 is on }
* Example 18 : Use the switch statement to control the lighting status of the 8-bit LED at port P0
#include //Contains the header file of the microcontroller register
sbit S1=P1^4; //Define S1 bit as P1.4
/
Function: Delay for a period of time
/
void delay(void)
{
unsigned int n;
for(n=0;n<10000;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
i=0; //Initialize i to 0
while(1)
{
if(S1==0) //If the S1 key is pressed
{
delay(); //delay for a while
if(S1==0) //If the S1 key is detected to be pressed again
i++; //i increases by 1
if(i==9) //If i=9, reset it to 1
i=1;
}
switch(i) //Use multiple branch selection statement
{
}
} case 1: P0=0xfe; //The first LED is on break; case 2: P0=0xfd; //The second LED is on break; case 3:P0=0xfb; //The third LED is on break; case 4:P0=0xf7; //The fourth LED is on break; case 5:P0=0xef; //The fifth LED is on break; case 6:P0=0xdf; //The sixth LED is on break; case 7:P0=0xbf; //The seventh LED is on break; case 8:P0=0x7f; //The eighth LED is on break; default: //Default value, turn off all LEDs P0=0xff; }
* Example 19 : Use the for statement to control the number of times the buzzer sounds
#include//Header file including MCU registers sbit sound=P3^7; //Define the sound bit as P3.7 / Function: Delay to form 1600Hz audio
/ void delay1600(void)
{
unsigned char n;
for(n=0;n<100;n++)
;
}
/ Function: Delay to form 800Hz audio
/ void delay800(void)
{
unsigned char n;
for(n=0;n<200;n++)
;
}
/ Function: Main function
/ void main(void)
{
unsigned int i;
while(1)
{
for(i=0;i<830;i++)
{
sound=0; //P3.7 outputs low level delay1600();
sound=1; //P3.7 outputs high level delay1600();
}
for(i=0;i<200;i++)
{
sound=0; //P3.7 outputs low level delay800();
sound=1; //P3.7 outputs high level delay800();
}
}
}
*Example 2 0 : Using the while statement to control the LED
#include //Contains the header file of the microcontroller register / Function: Delay about 60ms (3 100 200=60000μs) / void delay60ms(void)
{
unsigned char m,n;
for(m=0;m<100;m++)
for(n=0;n<200;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
while(1) // infinite loop
{
i=0; //Initialize i to 0
while(i<0xff) //When i is less than 0xff (255), execute the loop body {
P0=i; //Send i to P0 port for display
delay60ms(); //delay
i++; //i increases by 1
}
}
}
*Example 2 1 : Use do-while statement to control the 8-bit LED of port P0 to light up
#include //Contains the header file of the microcontroller register
/
Function: Delay about 60ms (3 100 200=60000μs)
/
void delay60ms(void)
{
unsigned char m,n;
for(m=0;m<100;m++)
for(n=0;n<200;n++)
;
}
/
Function: Main function
/
void main(void)
{
do
{
P0=0xfe; //The first LED is on
delay60ms();
}
P0=0xfd; //The second LED turns on delay60ms(); P0=0xfb; //The third LED turns on delay60ms(); P0=0xf7; //The fourth LED turns on delay60ms(); P0=0xef; //The fifth LED turns on delay60ms(); P0=0xdf; //The sixth LED turns on delay60ms(); delay60ms(); P0=0xbf; //The seventh LED turns on delay60ms(); P0=0x7f; //The eighth LED turns on delay60ms(); }while(1); //Infinite loop to make the 8-bit LED light up in a circular manner
* Example 2 2 : Use character array to control the 8-bit LED of port P0 to light up
#include //Contains the header file of the microcontroller register
/
Function: Delay about 60ms (3 100 200=60000μs)
/
void delay60ms(void)
{
unsigned char m,n;
for(m=0;m<100;m++)
for(n=0;n<200;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
unsigned char code Tab[ ]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //define unsigned character array
while(1)
{
for(i=0;i<8;i++)
{
P0=Tab[i]; //Quote array elements in sequence and send them to P0 port for display
delay60ms(); //Call delay function
}
}
}
*Example 2 3 : Use P0 port to display string constants
#include //Contains the header file of the microcontroller register
/
Function: Delay about 150ms (3 200 250=150 000μs=150ms
/
void delay150ms(void)
{
unsigned char m,n;
for(m=0;m<200;m++)
for(n=0;n<250;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char str[]={"Now,Temperature is :"}; //Assign the string to all elements of character type
unsigned char i;
while(1)
{
i=0; //Initialize i to 0 and start displaying from the first element
while(str[i]!='\0') //As long as the end mark '\0' is not displayed
{
P0=str[i]; //Send the i-th character to port P0 for display
delay150ms(); //Call 150ms delay function
i++; //point to the next character to be displayed
}
}
}
* Example 2 4 : Use port P0 to display the result of pointer operation
#include
void main(void)
{
unsigned char p1, p2; //define unsigned character pointer variables p1, p2 unsigned char i, j; //define unsigned character data
i=25; //Assign an initial value of 25 to i
j=15;
p1=&i; // Make the pointer variable point to i and initialize the pointer
p2=&j; // Make the pointer variable point to j and initialize the pointer
P0= p1+ p2; // p1+ p2 is equivalent to i+j, so P0=25+15=40=0x28
//Then P0=0010 1000B, the LEDs of P0.3 and P0.5 pins are off, and the others are on while(1)
; //Infinite loop to prevent the program from "running away"
}
* Example 2 5 : Use pointer array to control the 8-bit LED of P0 port to light up
#include
/
Function: Delay about 150ms (3 200 250=150 000μs=150ms
/
void delay150ms(void)
{
unsigned char m,n;
for(m=0;m<200;m++)
for(n=0;n<250;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char code Tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char p[ ]={&Tab[0],&Tab[1],&Tab[2],&Tab[3],&Tab[4],&Tab[5], &Tab[6],&Tab[7]};
unsigned char i; //define unsigned character data
while(1)
{
for(i=0;i<8;i++)
{
P0= p[i];
delay150ms();
}
}
}
* Example 2 6 : Use the pointer of the array to control the 8-bit LED of port P0 to light up
#include
/
Function: Delay about 150ms (3 200 250=150 000μs=150ms
/
void delay150ms(void)
{
unsigned char m,n;
for(m=0;m<200;m++)
for(n=0;n<250;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
unsigned char Tab[ ]={0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF, 0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE, 0xFE,0xFC,0xFB,0xF0,0xE0,0xC0,0x80,0x00, 0xE7,0xDB,0xBD,0x7E,0x3C,0x18,0x00,0x81, 0xC3,0xE7,0x7E,0xBD,0xDB,0xE7,0xBD,0xDB}; //Flowing light control code
unsigned char p; //define unsigned character pointer
p=Tab; //Store the first address of the array into pointer p
while(1)
{
for(i=0;i<32;i++) //Total 32 flow light control codes
{
P0= (p+i); // The value of (p+i) is equal to a[i]
}
delay150ms(); //Call 150ms delay function } }
* Example 2 7 : Use ports P0 and P1 to display the return value of an integer function
#include
/ Function: Calculate the sum of two unsigned integers
/ unsigned int sum(int a,int b)
{
unsigned int s;
s=a+b;
return (s);
}
/ Function: Main function
/ void main(void)
{
unsigned z;
z=sum(2008,2009);
P1=z/256; //Get the high 8 bits of z
P0=z%256; //Get the lower 8 bits of z
while(1)
;
}
* Example 28 : Use a parameter function to control the flow speed of the 8-bit LED at port P0
#include
/ Function: Delay for a period of time
/ void delay(unsigned char x)
{
unsigned char m,n;
for(m=0;m<x;m++)< span>
for(n=0;n<200;n++)
;
}
/
Function: Main function
/
void main(void)
{
unsigned char i;
unsigned char code Tab[ ]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F}; //Flowing light control code
while(1)
{
//Fast flow of water to light up the LED
for(i=0;i<8;i++) //8 running light control codes in total
{
P0=Tab[i];
delay(100); //delay about 60ms, (3 100 200=60 000μs) }
//Slow water flow lights up the LED
for(i=0;i<8;i++) //8 running light control codes in total
{
P0=Tab[i];
delay(250); //delay about 150ms, (3 250 200=150 000μs) }
}
}
丨The article is organized to spread relevant technologies, the copyright belongs to the original author丨
丨If there is any infringement, please contact us to delete丨
|