Comprehensive Guide to 80 Microcontroller Programming Examples

*Example 1:

Use P3 port to light up 8 LEDs in sequence

#include<reg51.h> // Include microcontroller register header file / Function: Delay for a certain 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 microcontroller register header file

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

// Manipulating address x is the same as manipulating P1 port

/

Function: Delay for a certain 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 certain 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 flash time with different data types

#include<reg51.h> // Include microcontroller register header file

/

Function: Delay for a certain 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 0~65535 for(m=0;m<36000;m++)

; // Empty operation

}

/

Function: Delay for a certain 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, value range 0~255 for(i=0;i<200;i++)

for(j=0;j<180;j++)

; // Empty operation

}

/

Function: Main function

/

void main(void)

{

unsigned char i;

while(1)

{

for(i=0;i<3;i++)

{

P1=0xfe; // Light up the light 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 light at 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: Use microcontroller to control the first light on

#include<reg51.h> // Include 51 microcontroller register definition header file

void main(void)

{

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

}

*Example 5: Use microcontroller to control a light flashing: understand the working frequency of microcontroller #include<reg51.h> // Include microcontroller register header file

/

Function: Delay for a certain period of time

/

void delay(void) // Two voids mean no return value, no parameter transmission {

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 and can only be one main function) / void main(void)

{

while(1) // Infinite loop

{

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

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

P1=0xff; // P1=1111 1111B, P1.0 outputs high level

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

}

}

*Example 6: Send the P1 port status to P0, P2, P3 ports: understand the I/O port pin function

#include<reg51.h> // Include microcontroller register header file

/ 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 LED

P0=P1; // Send the P1 port status to the P0 port

P2=P1; // Send the P1 port status to the P2 port

P3=P1; // Send the P1 port status to the P3 port

}

}

*Example 7: Use P0 and P1 to display the results of addition and subtraction operations #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 are on P0=n-m; // P0=17=0001 0001B, result P0.0, P0.4 lights are off }

*Example 8: Use P0 and P1 to display the result of multiplication

#include<reg51.h> // Include microcontroller register header file

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 number representation, the high 8 bits are sent to the P1 port, and the low 8 bits are sent to the P0 port

// Since 4544=17*256+192=H3*16+H2*16+H1*16+H0

// Dividing both sides by 256, we get 17+192/256=H3*16+H2+(H1*16+H0)/256

// Therefore, the high 8-bit hexadecimal number H3*16+H2 must be equal to 17, that is, 4544 divided by 256 is the quotient

// The low 8-bit hexadecimal number H1*16+H0 must be equal to 192, that is, 4544 divided by 256 is the remainder

P1=s/256; // Send the high 8 bits to the P1 port, P1=17=11H=0001 0001B, P1.0 and P1.4 lights are off, the rest are on

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

}

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

#include<reg51.h> // Include microcontroller register header file

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: Use auto-increment operation to control P0 port 8-bit LED flowing pattern

#include<reg51.h> // Include microcontroller register header file

/

Function: Delay for a certain 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 the P0 port

delay(); // Call delay function

}

}

*Example 11: Use P0 port to display the logical

Leave a Comment