51 Some common code blocks in microcontroller development

Publisher:MoonlightStarLatest update time:2022-10-18 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

16 characters in the digital tube


uchar code table[]={

0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71};

Delay function (declare it before using it)


void delay(uint);

 

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}

Light up the first diode


#include

//Light up the first LED

sbit D1=P1^0;//P1 is the P1 port, P1^0 represents D1, and D1 is a variable named by itself

main()

{

D1=0;//Convert high frequency to low frequency

}

Light up multiple diodes


#include //Include the header file, right-click on "reg51.h" and open it to see the definition inside it

 

void main()

{

P1=0XAA; //The eight bits of the P1 port are operated together, the high and low levels are assigned to 10101010, and the diode corresponding to the bit of 0 is lit.

while(1);

}

 

 

#include //Include the header file, right-click on "reg51.h" and open it to see the definition inside it

 

sbit led1=P1^0; //The first LED is connected to the P1^0 port.

sbit led3=P1^2;

sbit led5=P1^4;

sbit led7=P1^6;

 

void main()

{

led1=0;    //Refer to the circuit diagram, P1^0 is low level, which can make led1 light up.

led3=0;

led5=0;

led7=0;

while(1);//Set an infinite loop here to let the program stay here to prevent it from running away.

}


Running water lamp fool-like lighting


#include

#define uint unsigned int

sbit p0=P1^0;

sbit p1=P1^1;

sbit p2=P1^2;

sbit p3=P1^3;

sbit p4=P1^4;

sbit p5=P1^5;

sbit p6=P1^6;

sbit p7=P1^7;

#define uchar unsigned char

void delayms(uint);

void main()

{

while(1)

{

//0xaa is 1, 3, 5, 7, 0x55 2, 4, 6, 8

p0=1;

p1=0;

p2=1;

p3=0;

p4=1;

p5=0;

p6=1;

p7=0;

 

delayms(500);

p0=0;

p1=1;

p2=0;

p3=1;

p4=0;

p5=1;

p6=0;

p7=1;

 

delayms(500);

}

}

 

void delayms(uint xms)

{

uint x,y;

for(x=xms;x>0;x--)

for(y=110;y>0;y--);

}


P1^(0~7) The position of the light-emitting diode on stc


Timer function definition

void init()//Initialization function

{

TMOD=0x01;//Set timer 0 to working mode 1 (0000 0001)

TH0=(65536-45872)/256//install the initial value and interrupt once every 50ms

TL0=(65536-45872)%256;

EA=1;//Enable total interrupt

ET0=01; //Enable timer 0 interrupt

}

interrupt service routine


Interrupt routine

void function name() interrupt interrupt number using working group

{

Interrupt service routine content

}

 

e.g.:

void T1_time() interrupt 3

{

TH1=(65536-10000)/256;

TL1=(65536-10000)%256;

}


Reference address:51 Some common code blocks in microcontroller development

Previous article:51 microcontroller learning - lighting up the first light-emitting diode
Next article:51 microcontroller implements thermometer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号