Microcontroller Programming Examples Collection (1-30)

*Example 1:

Use 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 LED on

delay(); // Call delay function

P3=0xfd; // Second LED on

delay(); // Call delay function

P3=0xfb; // Third LED on

delay(); // Call delay function

P3=0xf7; // Fourth LED on

delay(); // Call delay function

P3=0xef; // Fifth LED on

delay(); // Call delay function

P3=0xdf; // Sixth LED on

delay(); // Call delay function

P3=0xbf; // Seventh LED on

delay(); // Call delay function

P3=0x7f; // Eighth LED 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 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 microcontroller, manipulating address x is also manipulating 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 LED on

delay(); // Call delay function

x=0xfd; // Second LED on

delay(); // Call delay function

x=0xfb; // Third LED on

delay(); // Call delay function

x=0xf7; // Fourth LED on

delay(); // Call delay function

x=0xef; // Fifth LED on

delay(); // Call delay function

x=0xdf; // Sixth LED on

delay(); // Call delay function

x=0xbf; // Seventh LED on

delay(); // Call delay function

x=0x7f; // Eighth LED on

delay(); // Call delay function

}

}

*Example 3:Control LED 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, range 0~65535 for(m=0;m<36000;m++)

;

}

/

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 character variable, single-byte data, 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 up the LED at 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 up the LED at 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:Use microcontroller to control the first LED to light up

#include<reg51.h> // Include the header file for 51 microcontroller registers

void main(void)

{

P1=0xfe; // P1=1111 1110B, that is, P1.0 outputs low level

}

*Example 5:Use microcontroller to control an LED to flash: Understand the working frequency of the microcontroller #include<reg51.h> // Include the header file for microcontroller registers

/

Function: Delay for a period of time

/

void delay(void) // The 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

;

}

/ Function: Main function (C language stipulates that 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 state 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 only one main function) /

void main(void)

{

while(1) // Infinite loop

{

P1=0xff; // P1=1111 1111B, turn off LED

P0=P1; // Send the state of P1 port to P0 port

P2=P1; // Send the state of P1 port to P2 port

P3=P1; // Send the state of P1 port to P3 port

}

}

*Example 7:Use P0 and P1 ports to display the results of addition and subtraction #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, 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:Use P0 and P1 ports to display the results of multiplication

#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^2+H2*16^1+H1*16^0

// Dividing both sides by 256 gives 17+192/256=H3*16^2+H2+(H1*16^1+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

P0=s%256; // Send the low 8 bits to P0 port , P3=192=c0H=1100 0000B,P3.1,P3.6,P3.7 lights off

}

*Example 9:Use P1 and P0 ports to display the results of division

#include<reg51.h> // Include the header file for microcontroller registers

void main(void)

{

P1=36/5; // Calculate integer

P0=((36%5)*10)/5; // Calculate decimal

while(1)

; // Infinite loop to prevent the program from “running away”

}

*Example 10:Use self-increment operation to control the 8 LEDs on P0 port to light up in sequence

#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 that the value of i must not exceed 255

{

P0=i; // Send the value of i to P0 port

delay(); // Call delay function

}

}

*Example 11:Use P0 port to display the result of logical “AND” operation #include<reg51.h> // Include the header file for microcontroller registers void main(void)

{

P0=(4>0)&&(9>0xab);// Send the logical operation result to P0 port

while(1)

; // Set infinite loop to prevent the program from “running away”

}

*Example 12:Use P0 port to display the result of conditional operation

#include<reg51.h> // Include the header file for microcontroller registers void main(void)

{

P0=(8>4)?8:4;// Send the conditional operation result to P0 port, P0=8=0000 1000B while(1)

; // Set infinite loop to prevent the program from “running away”

}

*Example 13:Use P0 port to display the result of bitwise “XOR” operation #include<reg51.h> // Include the header file for microcontroller registers void main(void)

{

P0=0xa2^0x3c;// Send the conditional operation result to P0 port, P0=8=0000 1000B while(1)

; // Set infinite loop to prevent the program from “running away”

}

*Example 16:Use P0 to display the result of left shift operation

#include<reg51.h> // Include the header file for microcontroller registers void main(void)

{

P0=0x3b<<2;// Send the left shift operation result to P0 port, P0=1110 1100B=0xec while(1)

; // Infinite loop to prevent the program from “running away”

}

*Example 17:“Universal Logic Circuit” experiment

#include<reg51.h> // Include the header file for microcontroller registers

sbit F=P1^4; // Define bit F as P1.4

sbit X=P1^5; // Define bit X as P1.5

sbit Y=P1^6; // Define bit Y as P1.6

sbit Z=P1^7; // Define bit Z as P1.7

void main(void)

{

while(1)

{

F=((~X)&Y)|Z; // Assign the logical operation result to F

;

}

}

*Example 18:Use right shift operation to light up 8 LEDs on P1 port in sequence #include<reg51.h> // Include the header file for microcontroller registers

/

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 loop count to 8

{

P1=P1>>1; // Each loop right shifts each bit of P1, high bit fills with 0 delay(); // Call delay function

}

}

}

*Example 19:Use if statement to control the direction of 8 LEDs on P0 port #include<reg51.h> // Include the header file for microcontroller registers

sbit S1=P1^4; // Define bit S1 as P1.4

sbit S2=P1^5; // Define bit S2 as P1.5

/

Function: Main function

/

void main(void)

{

while(1)

{ if(S1==0) // If button S1 is pressed P0=0x0f; // Light up the high four LEDs on P0 if(S2==0) // If button S2 is pressed P0=0xf0; // Light up the low four LEDs on P0 }

*Example 20:Use switch statement to control the lighting state of 8 LEDs on P0 port #include<reg51.h> // Include the header file for microcontroller registers

sbit S1=P1^4; // Define bit S1 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 S1 key is pressed

{

delay(); // Delay for a period of time

if(S1==0) // If S1 key is pressed again

i++; // Increment i by 1

if(i==9) // If i=9, reset it to 1

i=1;

}

switch(i) // Use multi-branch selection statement

{

}

} case 1: P0=0xfe; // First LED on break; case 2: P0=0xfd; // Second LED on break; case 3:P0=0xfb; // Third LED on break; case 4:P0=0xf7; // Fourth LED on break; case 5:P0=0xef; // Fifth LED on break; case 6:P0=0xdf; // Sixth LED on break; case 7:P0=0xbf; // Seventh LED on break; case 8:P0=0x7f; // Eighth LED on break; default: // Default value, turn off all LEDs P0=0xff; }

*Example 21:Use for statement to control the number of beeps from the buzzer

#include<reg51.h> // Include the header file for microcontroller registers sbit sound=P3^7; // Define bit sound 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 22:Use while statement to control LED

#include<reg51.h> // Include the header file for microcontroller registers / Function: Delay for 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) // Execute loop body while i is less than 0xff (255) {

P0=i; // Send i to P0 port for display

delay60ms(); // Delay

i++; // Increment i

}

}

}

*Example 23:Use do-while statement to control the 8 LEDs on P0 port to light up in sequence #include<reg51.h> // Include the header file for microcontroller registers

/

Function: Delay for 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; // First LED on

delay60ms();

}

P0=0xfd; // Second LED on delay60ms(); P0=0xfb; // Third LED on delay60ms(); P0=0xf7; // Fourth LED on delay60ms(); P0=0xef; // Fifth LED on delay60ms(); P0=0xdf; // Sixth LED on delay60ms(); delay60ms(); P0=0xbf; // Seventh LED on delay60ms(); P0=0x7f; // Eighth LED on delay60ms(); }while(1); // Infinite loop, making the 8 LEDs light up in sequence

*Example 24:Use character array to control the 8 LEDs on P0 port to light up in sequence #include<reg51.h> // Include the header file for microcontroller registers

/

Function: Delay for 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];// Sequentially reference array elements and send them to P0 port for display

delay60ms();// Call delay function

}

}

}

*Example 25:Use P0 port to display string constant

#include<reg51.h> // Include the header file for microcontroller registers

/

Function: Delay for 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[]={

Leave a Comment