51单片机学习
作者:互联网
1.点亮LED
#include "reg52.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
#define led P2
void delay(u16 i)
{
while(i--);
}
void main()
{
u8 i=0;
led=0xfe;
delay(50000);
while(1)
{
for(i=0;i<3;i++)
{
led=_crol_(led,2);
delay(50000);
}
for(i=0;i<3;i++)
{
led=_cror_(led,2);
delay(50000);
}
}
}
2.继电器(接触器的触头比较大,承载能力强,通过它来实现弱电到强电的控制,控制对象是电器)
#include "reg52.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit relay=P1^4;//
void delay(u16 i)
{
while(i--);
}
void main()
{
relay=0;
while(1)
{
}
}
3.静态数码管。(主要是了解共阳共阴数码管数码的表示方法)共阴取反就是供阳~。
#include "reg52.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
u8 smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,ox77,0x7c,0x39,0x5e,0x79,0x71};;
void main()
{
P0=~smgduan[0];
while(1)
{
}
}
4.动态数码管。(38编译器
#include "reg52.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
u8 smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit LSA=P2^2; //38ÒëÂëÆ÷
sbit LSB=P2^3;
sbit LSC=P2^4;
void delay(u16 i)
{
while(i--);
}
void DigDisplay1()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i)
{
case 7:
LSA=0;LSB=0;LSC=0;P0=smgduan[4];break;
case 6:
LSA=1;LSB=0;LSC=0;P0=smgduan[1];break;
case 5:
LSA=0;LSB=1;LSC=0;P0=smgduan[3];break;
case 4:
LSA=1;LSB=1;LSC=0;P0=smgduan[1];break;
case 3:
LSA=0;LSB=0;LSC=1;P0=smgduan[0];break;
case 2:
LSA=1;LSB=0;LSC=1;P0=smgduan[2];break;
case 1:
LSA=0;LSB=1;LSC=1;P0=smgduan[5];break;
case 0:
LSA=1;LSB=1;LSC=1;P0=smgduan[0];break;
}
delay(50000);
P0=0x00;//ÇåÁã
}
}
void DigDisplay2()
{
u8 i;
for(i=0;i<8;i++)
{
switch(i)
{
case 7:
LSA=0;LSB=0;LSC=0;P0=smgduan[4];break;
case 6:
LSA=1;LSB=0;LSC=0;P0=smgduan[1];break;
case 5:
LSA=0;LSB=1;LSC=0;P0=smgduan[3];break;
case 4:
LSA=1;LSB=1;LSC=0;P0=smgduan[1];break;
case 3:
LSA=0;LSB=0;LSC=1;P0=smgduan[0];break;
case 2:
LSA=1;LSB=0;LSC=1;P0=smgduan[2];break;
case 1:
LSA=0;LSB=1;LSC=1;P0=smgduan[5];break;
case 0:
LSA=1;LSB=1;LSC=1;P0=smgduan[0];break;
}
delay(100);
P0=0x00;//ÇåÁã
}
}
void main()
{
DigDisplay1();
while(1)
{
DigDisplay2();
}
}
的用处)
标签:typedef,u8,void,51,unsigned,学习,单片机,include,u16 来源: https://blog.csdn.net/jzh686/article/details/120463787