51单片机学习笔记(一)

点亮第一个LED灯

#include "reg52.h"

sbit led=P2^0;      //将单片机的P2.0端口定义为led

void main()
{
	while(1)
	{
		led=0; //P2.0端口设置为低电平
	}
}

LED闪烁实验

#include "reg52.h"

typedef unsigned int u16;         //对数据类型进行声明定义
typedef unsigned char u8;

sbit led=P2^0;

//延时函数
void delay(u16 i)
{
	while(i--);
}

void main()
{
	while(1)
	{
		led=0;
		delay(50000);
		led=1;
		delay(50000);
	}
}

LED流水灯实验

#include "reg52.h"
#include //要用到左右移函数

typedef unsigned int u16;
typedef unsigned char u8;

#define led P2

void delay(u16 i)
{
    while(i--);
}

void main()
{
    u8 i;
    led=0xfe;
    delay(50000);
    while(1)
    {
        for(i=0;i<8;i++)
        {
           P2=~(0x01<

静态数码管显示

#include "reg52.h"

typedef unsigned int u16;
typedef unsigned char u8;

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;

u8 LED[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
           0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};  //共阴极数码管码

void main()
{
    LSA=0;
    LSB=0;
    LSC=0;  //控制38译码器的Y0输出低电平
    P0=LED[0];
    while(1);
}

51单片机学习笔记(一)_第1张图片

 

你可能感兴趣的:(51单片机)