第一周
一、发光二极管的点亮,控制
1、led发光原理
二极管,PN结。
led点亮:开发板原理图
注:二极管采用共阳极,需要对阴极即P1口输入低电平点亮
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
void Delayms(uint time)//延迟函数
{
uint i,j;
for(i=0;i<time;i++)
{
for(j=0;j<110;j++);
}
}
void main()
{
uchar aa=0xFE;
P1=aa;
while(1)
{
P1=aa;
aa=_irol_(aa,1);//循环左移
Delayms(500);
}
}
二、数码管
1、数码管原理图
2、数码管受两个锁存器管理
分别为位锁存器和段锁存器,分别控制哪一个数码管亮和数码管的那一段亮
2.1锁存器的原理:
通过输入高电平(1)(P2^6或P2^7)来打开,输入低电平关闭并保存上一次数据。
3、数码管移位循环输出0-9
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
void DelayMS(uint x)
{
uchar i;
while(x--)
{
for(i=120;i>0;i--);
}
}
uchar code table[]={
0x3f,0x06,0x5b,
0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f
};
void main()
{
uint i=0;
uchar aa = 0xfe;
while(1)
{
wela = 1;
P0 = aa;
wela = 0;
P0=0xff;
dula = 1;
P0 = table[(i++)%10];
dula = 0;
P0 = 0xff;
aa = _crol_(aa,1);
if(aa==0xBF)//1011 1111
aa = 0xFE;//1111 1110
DelayMS(1000);
}
}
4、6个数码管分别显示1-6
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
void Delayms(uint time)
{
uint i,j;
for(i=0;i<time;i++)
{
for(j=0;j<110;j++);
}
}
void main()
{//1
while(1)
{
wela = 1;
P0 = 0xfe;//1111 1110
wela = 0;
P0=0xff;
dula = 1;
P0 = 0x06;
dula = 0;
Delayms(1);
P0=0xff;
//2
wela = 1;
P0 = 0xfd;//1111 1101
wela = 0;
P0=0xff;
dula = 1;
P0 = 0x5b;
dula = 0;
Delayms(1);
P0=0xff;
//3
wela = 1;
P0 = 0xfb;//1111 1011
wela = 0;
P0=0xff;
dula = 1;
P0 = 0x4f;
dula = 0;
Delayms(1);
P0=0xff;
//4
wela = 1;
P0 = 0xf7;//1111 0111
wela = 0;
P0=0xff;
dula = 1;
P0 = 0x66;
dula = 0;
Delayms(1);
P0=0xff;
//5
wela = 1;
P0 = 0xef;//1110 1111
wela = 0;
P0=0xff;
dula = 1;
P0 = 0x6d;
dula = 0;
Delayms(1);
P0=0xff;
//6
wela = 1;
P0 = 0xdf;//1101 1111
wela = 0;
P0=0xff;
dula = 1;
P0 = 0x7d;
dula = 0;
P0=0xff;
Delayms(1);
}
}
注:
①
②