学习内容:
-
10s倒计时 -
动态显示 -
LCD1602
10s倒计时
数码管引脚排列http://29cx.com/shuma/6010.html74LS47译码器:tips:译码器应与数码管寻求匹配,如共阳就配低电平有效的译码器。
74LS47译码器https://wenku.baidu.com/view/d1a54ca4c47da26925c52cc58bd63186bceb922c.html位选和段选:位选选数码管,段选输出数码段。tips:先段选后位选,会重影。
位选和段选https://wenku.baidu.com/view/8725fc2e753231126edb6f1aff00bed5b8f37343.html
#include<reg52.h>
#define uint unsigned int
void main()
{
char ctime;
for(ctime = 9;ctime > 0;ctime--)
{
P0 = ctime;
delay();
}
while(1);
}
void delay()
{
uint i,j;
for(i = 1000;i > 0;i--)
for(j = 115;j > 0;j--);
}
//tips:自动转换进制
then
#include<reg52.h>
#define uchar unsigned char
uchar code led[] = {0x81,0xf3,0x49,0x61,0x33,0x25,0x05,0xf1,0x01,0x21};//段选
ucahr code ledw[] = {0xf7,0xfb,0xfd,0xfe};//位选
uchar utime[4];
void delay(uchar xms);
void main()
{
int ctime;
uchar b,c;
for(ctime = 999;ctime > 0;ctime--)
{
utime[0] = ctime%10;
utime[1] = ctime/10%10;
utime[2] = ctime/100%10;
for(c = 0;c < 33;c++)
{
for(b = 0;b <= 2;b++)//类似动态扫描
{
P0 = led[utime[b]];
P1 = ledw[b];
delay(10);
}
}
}while(1);
}
void delay(uchar xms)
{
uchar i,j;
for(i = 0;i < xms;i++)
for(j = 0;j < 115;j++);
}
动态显示(利用发光管余晖和人眼视觉暂留作用)
tips:明确共阳还是共阴,段选要一一对应,不然乱码。动态显示依靠快速位选端。
#include<reg52.h>
#define uchar unsigned char
uchar code led[] = {0xf3,0x49,0x61,0x33};
uchar code ledw[] = {0xfe,0xfd,0xfb,0xf7};
void delay(uchar xms);
void main()
{
while(1)
{
uchar b;
for(b = 0;b < 4;b++)
{
P0 = led[b];
P1 = ledw[b];
delay(xms);
}
}
}
void delay(uchar xms)//略去不写
LCD1602
lcd1602https://blog.csdn.net/aaaaaaaa123345/article/details/124212417
#include<reg52.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden = P2^0;//enable
sbit lcdrs = P2^1;//choose data
void delay(uint xms);
void write_com(uchar com)//write com
{
lcdrs = 0;
P0 = com;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
void write_data(uchar data)//write data
{
lcdrs = 1;
P0 = data;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
void LCDloc(char x,char y)//set position
{
if(x == 1)
{
write_come(0x80+y);
}
else
{
write_come(0xc0+y);
}
}
char putchar(char c)
{
write_data(c);
return(c);
}
void init()//start
{
lcden = 0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void main()
{
int ctime;
init();
for(ctime = 999;ctime > 0;ctime--)
{
LCDloc(1,2);
printf("%d",ctime);
delay(1000);
}
while(1);
}
void delay(uint xms)
{
uint i,j;
for(i = 0;i < xms;i++)
for(j = 0; j < 115;j++);
}
|