单片机学习 1月8日
数码管静态显示
0x3f , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d ,
0 1 2 3 4 5
0x7d , 0x07 , 0x7f , 0x6f , 0x77 , 0x7c ,
6 7 8 9 A B
0x39 , 0x5e , 0x79 , 0x71 , 0x00
C D E F 不显示
位选
sbit weal = P2^7;
低电流通过时工作
段选
sbit dula = P2^6;
共阴极 高电流通过时工作
例程
#include<reg52.h>
sbit dula = P2^6; //申明U1锁存器锁存端
sbit wela = P2^7; //申明U2锁存器锁存端
void main()
{
wela = 1; //打开锁存器
P0 = 0xfc; //1111 1100 低电流通过
wela = 0; //关闭锁存器
dula = 1;
P0 = 0x6; //1100 0000 段选 共阴极 高电流通过
dula = 0;
while(1); //工作停止
}
//数码管 第1,2个显示1
#include<reg52.h>
sbit du = P2^6;
sbit we = P2^7;
void main()
{
we = 1;
P0 = 0x15; //0001 0101
we = 0;
du = 1;
P0 = 0x7f; //0011 1111
du = 0;
while(1);
}
//数码管 1 3 5 位显示8
#include<reg52.h>
sbit du = P2^6;
sbit we = P2^7;
void main()
{
we = 1;
P0 = 0xea; //1110 1010
we = 0;
du = 1;
P0 = 0x7f; //0011 1111
du = 0;
while(1);
}
//数码管 1 3 5 位显示8
数组实现方式
数组
uchar code table[]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
#include<reg52.h>
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit dula = P2^6;
sbit wela = P2^7;
void main()
{
wela = 1;
P0 = 0xfa; // 1111 1010
wela = 0;
dula = 1;
P0 = table[6];
dula = 0;
while(1);
}
//数码管 1 3 位实现6
数码管动态显示
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0x3f,0x06,0x5b,
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit dula = P2^6;
sbit weal = P2^7;
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void main()
{
P0 = 0xff; //1111 1111
weal = 1;
P0 = 0xfe; //1111 1110
weal = 0;
dula = 1;
P0 = table[0];
delay(1);
P0 = 0xff; //1111 1111
weal = 1;
P0 = 0xfd; //1111 1101
weal = 0;
dula = 1;
P0 = table[1];
delay(1);
P0 = 0xff; //1111 1111
weal = 1;
P0 = 0xfb; //1111 1011
weal = 0;
dula = 1;
P0 = table[2];
delay(1);
P0 = 0xff; //1111 1111
weal = 1;
P0 = 0xf7; //1111 0111
weal = 0;
dula = 1;
P0 = table[3];
delay(1);
P0 = 0xff; //1111 1111
weal = 1;
P0 = 0xef; //1110 1111
weal = 0;
dula = 1;
P0 = table[4];
delay(1);
P0 = 0xff; //1111 1111
weal = 1;
P0 = 0xdf; //1101 1111
weal = 0;
dula = 1;
P0 = table[5];
delay(1);
}
//数码管同时显示 0 1 2 3 4 5