1.单片机外观图: 2.相关原理图: 3.案例的目的 显示一个电话号码,并且在电话号码开头数字出现在数码管中,下方的发光二极管开始发亮,,消失即就熄灭,这样方便了解是从哪里开始。
4.源代码
#include<STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
sbit led_sel=P2^3;
uchar uiLed = 0xff;
uchar weixuan[8]={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
uchar duanxuan[11] = { 0x06, 0x4f, 0x5b, 0x66, 0x6d, 0x06,0x07,0x3f,0x66,0x6f,0x4f };
uint wei1;
uint wei2;
uint wei3;
uint wei4;
uint wei5;
uint wei6;
uint wei7;
uint wei8;
uint ucSeg7State;
uchar ucCount;
void init()
{
P0M0=0XFF;
P0M1=0X00;
P2M0=0X0F;
P2M1=0X00;
wei1=0;
wei2=1;
wei3=2;
wei4=3;
wei5=4;
wei6=5;
wei7=6;
wei8=7;
ucSeg7State = 0;
ucCount = 0;
led_sel=1;
P0=0x80;
TMOD = 0x01;
ET0 = 1;
TH0 = ( 65535 - 1000 ) / 256;
TL0 = ( 65535 - 1000 ) % 256;
TR0 = 1;
EA = 1;
}
void T0_Process() interrupt 1
{
TH0 = ( 65535 - 1000 ) / 256;
TL0 = ( 65535 - 1000 ) % 256;
ucSeg7State++;
if( ucSeg7State == 8 )
{
ucSeg7State = 0;
ucCount++;
}
if( ucCount == 100 )
{
ucCount = 0;
wei1++;
wei2++;
wei3++;
wei4++;
wei5++;
wei6++;
wei7++;
wei8++;
}
if(3<(wei1%11))
{
if(ucCount>50)
{
led_sel=1;
P0=uiLed;
}
else{
led_sel=0;
}
}
if((wei1%11)==0)
{
if(ucCount>50)
{
led_sel=1;
P0=uiLed;
}
else{
led_sel=0;
}
}
P0 = 0;
P2 = weixuan[ucSeg7State];
switch( ucSeg7State )
{
case 0:
P0 = duanxuan[ wei1 % 11];
break;
case 1:
P0 = duanxuan[ wei2% 11];
break;
case 2:
P0 = duanxuan[ wei3% 11];
break;
case 3:
P0 = duanxuan[ wei4 % 11];
break;
case 4:
P0 = duanxuan[ wei5 % 11];
break;
case 5:
P0 = duanxuan[ wei6 % 11];
break;
case 6:
P0 = duanxuan[ wei7% 11];
break;
default:
P0 = duanxuan[ wei8 % 11];
break;
}
}
void main()
{
init();
while(1)
{
}
}
5.技巧 本项目主要是了解如何进行数码管与流水灯的结合,如果是普通实现类似功能的话,可以直接在段选的数组中设置特殊符号或者空格用来隔开开头与结尾。 比如设置段选数组里面最后两位为数码管不亮时的16进制数,利用小工具软件,可以参考 STC15系列单片机学习笔记——流水灯设计与数码管设计,里面带小工具下载链接
|