在说动态显示之前,我们有时候写中断的时候会看到有程序在写中断的时候是这样写的
后面的using 1其实是使用一些特殊的寄存器,其实会给我们自动配置,所以在一般情况下是不需要写这个的。
我们先说一个案例,让每一个数码管依次显示不同的数
我们先把段选和位选的条件都列出来,制成一个数组,看看位选有没有什么规律
由此我们可以看见,这是1一直在左移,那么,我们可以用操作符来解决这个事情,顺便让8个数码管,显示1-8的数字,注意,这和我们之前的全部开启是不一样的,之前是数码管都显示一样的操作,现在是每个数码管依次打开,并且亮不同的数字。下面是程序:
这里用到的 都是我之前说过的。
这里我尝试使用了while( 判断),while里面用判断,结果不行,所以我们老老实实用if判断。
代码如下:
这里没有新东西,就是给大家看一下效果。
#include <STC15F2K60S2.H>
#define uchar unsigned char
uchar times = 0;
uchar numd = 0;
uchar numw = 0;
unsigned int code listd[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
unsigned int code listw[]={0x01,0x02,0x04,0x08,
0x10,0x20,0x40,0x80};
void time0() interrupt 1
{
TH0 = (65535-10000)/256;
TL0 = (65535-10000)%256;
times +=1;
}
void main(void)
{
TMOD = 0xf0;
TH0 = (65535 - 10000)/256;
TL0 = (65535 - 10000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(times ==1)
{
times = 0;
P2 =((P2&0x1f)|0xc0);
P0 = listw[numw];
P2 &= 0x1f;
numw += 1;
if(numw==8)
{
numw=0;
}
numd += 1;
if(numd==10)
{
numd = 1;
}
P2 =((P2&0x1f)|0xe0);
P0 = listd[numd];
P2 &= 0x1f;
}
}
}
数码管动态2:分离百位数
我们封装函数,然后调用,会使得主程序更简洁。
分离百位数就是一个取模,取商的思想,%n,就可以得到0~(n-1)的数。
#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar times = 0;
uchar tempw = 0x01;
uint digit = 0;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
void delay(uint k)
{
uint i = 0;
uint j = 0;
for(i=700;i>0;i--)
{
for(j=0;j<k;j++)
{
;
}
}
}
void time0() interrupt 1
{
TH0 = (65536-5000)/256;
TL0 = (65536-5000)%256;
times +=1;
}
void init_time0()
{
TMOD = 0xf0;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void display(uint hundred ,uint ten,uint single)
{
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
P2 =((P2&0x1f)|0xe0);
P0 = list[hundred];
P2 &= 0x1f;
tempw <<= 1;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
P2 =((P2&0x1f)|0xe0);
P0 = list[ten];
P2 &= 0x1f;
tempw <<= 1;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
P2 =((P2&0x1f)|0xe0);
P0 = list[single];
P2 &= 0x1f;
}
void main(void)
{
init_time0();
display(digit/100,digit%100/10,digit%10);
while(1)
{
if(times ==20)
{
digit += 1;
display(digit/100,digit%100/10,digit%10);
times = 0;
}
}
}
代码敲出来是没有问题的,出了没有调用delay函数的警告。
展示一下!!!
视频点这里观看
我们发现根本不是我们想要的结果
我们可以观察到是这么个显示结果,首先,这个数字的确是加1显示的,0-1-2,但是这个程序只加到了2,说明就是有问题的,而且,这个数码管显示的位置,不是我们所期待的。
下面,我们先从位这个地方来研究是哪个部分出错。
我们发现第一步显示就是有问题的,那我们考虑一下是不是程序太快,单片机搞不过来。那我们延时一下看看。
#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar times = 0;
uchar tempw = 0x01;
uint digit = 0;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
void delay(uint k)
{
uint i = 0;
uint j = 0;
for(i=700;i>0;i--)
{
for(j=0;j<k;j++)
{
;
}
}
}
void time0() interrupt 1
{
TH0 = (65536-5000)/256;
TL0 = (65536-5000)%256;
times +=1;
}
void init_time0()
{
TMOD = 0xf0;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void display(uint hundred ,uint ten,uint single)
{
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
P2 =((P2&0x1f)|0xe0);
P0 = list[hundred];
P2 &= 0x1f;
delay(5);
tempw <<= 1;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
P2 =((P2&0x1f)|0xe0);
P0 = list[ten];
P2 &= 0x1f;
delay(5);
tempw <<= 1;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
P2 =((P2&0x1f)|0xe0);
P0 = list[single];
P2 &= 0x1f;
delay(5);
}
void main(void)
{
init_time0();
display(digit/100,digit%100/10,digit%10);
while(1)
{
if(times ==20)
{
digit += 1;
display(digit/100,digit%100/10,digit%10);
times = 0;
}
}
}
我们改了一改,发现出现变化了。
视频点击这里观看
我们看到其实有一点点往我们想要的方向发展了,但是,还不够完美,这说明,其实我们的程序,还是因为太快了,导致了一些问题,所以,我们还是要抓紧研究这个延时的调用。
太快了,可能会导致赋值的残留,那么,我们需要给他们都加上一些延时程序。
#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar times = 0;
uchar tempw = 0x01;
uint digit = 0;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
void delay(uint k)
{
uint i = 0;
uint j = 0;
for(i=700;i>0;i--)
{
for(j=0;j<k;j++)
{
;
}
}
}
void time0() interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
times +=1;
}
void init_time0()
{
TMOD = 0xf0;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void display(uint hundred ,uint ten,uint single)
{
tempw = 0x01;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
//delay(10);
P2 =((P2&0x1f)|0xe0);
P0 = list[hundred];
P2 &= 0x1f;
delay(50);
tempw <<= 1;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
// delay(10);
P2 =((P2&0x1f)|0xe0);
P0 = list[ten];
P2 &= 0x1f;
delay(50);
tempw <<= 1;
P2 =((P2&0x1f)|0xc0);
P0 = tempw;
P2 &= 0x1f;
// delay(10);
P2 =((P2&0x1f)|0xe0);
P0 = list[single];
P2 &= 0x1f;
delay(50);
}
void main(void)
{
display(digit/100,digit%100/10,digit%10);
init_time0();
while(1)
{
if(times == 20)
{
digit += 1;
display(digit/100,digit%100/10,digit%10);
times = 0;
}
}
}
展示一下!!!!!
视频点击这里观看
简单的数码管动态我们已经完结了,我们下期再见。
数码管表白
现在我们设想一个程序:
首先,我们让流水灯一个一个流,然后双向奔赴,到一定程度,全闪。就像感情,只有经过细水长流,双向奔赴,才能开花结果。
接下来,我们让数码管显示520 1314.YU,I CAN By YOUR SIDE FOREVER ,I LOVE YOU 。
我们先让它们一个一个显示出来,最终一句话就全体闪烁一下,最终到I LOVE YOU 的部分,我们就一直闪烁,和LED一起闪烁。
那么,我们开始逐步分析吧:
数字部分是肯定能显示出来的,所以我们研究字母部分就行了
这是大概的情况,虽然我们是沉迷于技术学习的孩子,但是,我们心里时时刻刻都想着对象,你们说是吧!赶紧自己设计一段话,设计好排版,考虑在目前这只有8个数码管的小板子上,表达对你女朋友的爱意吧!当然,你还可以考虑加入蜂鸣器的版本,就是让蜂鸣器唱歌,众所周知,振动是发声的来源,所以,蜂鸣器不同的振动频率和延时效果可以让单片机唱歌,阴乐不分高低!但我不会!
好嘞,开始编想程序吧!
我们可以考虑把字母的码子,先搞出来:
好嘞!字母搞出来了!
现在开始把需要的2个定时器都设定好,主程序框架也设定好:
这是我们用到的中断
先 主框架:
#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
uchar t0,t1;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
void time0() interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
t0 +=1;
}
void time1() interrupt 3
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
t1 +=1;
}
void delay(uint k)
{
uint i = 0;
uint j = 0;
for(i=700;i>0;i--)
{
for(j=0;j<k;j++)
{
;
}
}
}
void initAll()
{
TMOD = 0x00;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
TH1 = (65536 - 50000)/256;
TL1 = (65536 - 50000)%256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
}
void display()
{
}
void main(void)
{
initAll();
while(1)
{
}
}
接下来我们完善整个函数,根据自己想要的情形
我觉得520 1314 太土了,就没用了,加了别的
#include <STC15F2K60S2.H>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar t0,t1,wei,duan,flag1;
uchar oled1 = 0xfe;
uchar oled2 = 0x7f;
unsigned int code list[]={0xc0,0xf9,0x24,0x30,
0x19,0x12,0x02,0xf8,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
void time0() interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
t0 +=1;
}
void time1() interrupt 3
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
t1 +=1;
}
void delay(uint k)
{
uint i = 0;
uint j = 0;
for(i=700;i>0;i--)
{
for(j=0;j<k;j++)
{
;
}
}
}
void InitAll()
{
TMOD = 0x00;
TH0 = (65536 - 50000)/256;
TL0 = (65536 - 50000)%256;
TH1 = (65536 - 50000)/256;
TL1 = (65536 - 50000)%256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
}
//void DisplayLed1()
//{
// again:
// if(flag1 != 8)
// {
// flag1 += 1;
// P2=((P2&0x1f)|0x80);
// P0=oled1;
// P2 &= 0x1f;
// oled1 = _crol_(oled1,1);
// t0 = 0;
// goto again;
// }
// else
// {
// P2=((P2&0x1f)|0x80);
// P0=0x00;
// P2 &= 0x1f;
// delay(100);
// P2=((P2&0x1f)|0x80);
// P0=0xff;
// P2 &= 0x1f;
// delay(100);
// P2=((P2&0x1f)|0x80);
// P0=0x00;
// P2 &= 0x1f;
// }
//}
//void DisplayLed2()
//{
// oled2 = 0x7f;
// P2=((P2&0x1f)|0x80);
// P0=oled2;
// P2 &= 0x1f;
// oled2 = _cror_(oled2,1);
continue....
//}
//void DisplaySmg1()
//{
// wei = 0x01,
5
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[5];
// P2 &= 0x1f;
// delay(500);
2
// wei<<=1;
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[2];
// P2 &= 0x1f;
// delay(500);
0
// wei<<=1;
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[0];
// P2 &= 0x1f;
// delay(500);
1
// wei<<=2;
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[1];
// P2 &= 0x1f;
// delay(500);
3
// wei<<=1;
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[3];
// P2 &= 0x1f;
// delay(500);
1
// wei<<=1;
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[1];
// P2 &= 0x1f;
// delay(500);
4
// wei<<=1;
// P2=((P2&0x1f)|0xc0);
// P0=wei;
// P2 &= 0x1f;
// P2=((P2&0x1f)|0xe0);
// P0=list[4];
// P2 &= 0x1f;
// delay(500);
//}
void DisplaySmg2()
{
wei = 0x01,
//y
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x91;
P2 &= 0x1f;
delay(500);
//u
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc1;
P2 &= 0x1f;
delay(500);
//i
wei<<=2;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xcf;
P2 &= 0x1f;
delay(500);
//c
wei<<=2;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x46;
P2 &= 0x1f;
delay(500);
//a
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x08;
P2 &= 0x1f;
delay(500);
//n
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc8;
P2 &= 0x1f;
delay(500);
}
void DisplaySmg3()
{
wei = 0x02,
//b
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x03;
P2 &= 0x1f;
delay(500);
//e
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x06;
P2 &= 0x1f;
delay(500);
//b
wei<<=2;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x03;
P2 &= 0x1f;
delay(500);
//y
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x91;
P2 &= 0x1f;
delay(500);
}
void DisplaySmg4()
{
wei = 0x01,
//y
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x91;
P2 &= 0x1f;
delay(500);
//0
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc0;
P2 &= 0x1f;
delay(500);
//u
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc1;
P2 &= 0x1f;
delay(500);
//r
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xce;
P2 &= 0x1f;
delay(500);
//s
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x12;
P2 &= 0x1f;
delay(500);
//i
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xcf;
P2 &= 0x1f;
delay(500);
//d
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x21;
P2 &= 0x1f;
delay(500);
//e
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x06;
P2 &= 0x1f;
delay(500);
}
void DisplaySmg5()
{
wei = 0x01;
//i
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xcf;
P2 &= 0x1f;
delay(500);
//l
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc7;
P2 &= 0x1f;
delay(500);
//0
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc0;
P2 &= 0x1f;
delay(500);
//v
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc1;
P2 &= 0x1f;
delay(500);
//e
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x06;
P2 &= 0x1f;
delay(500);
//y
wei <<= 1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x91;
P2 &= 0x1f;
delay(500);
//0
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc0;
P2 &= 0x1f;
delay(500);
//u
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0xc1;
P2 &= 0x1f;
delay(500);
}
void DisplaySmg6()
{
wei = 0x01,
//b
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x03;
P2 &= 0x1f;
delay(500);
//e
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x06;
P2 &= 0x1f;
delay(500);
//H
wei<<=2;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x89;
P2 &= 0x1f;
delay(500);
//A
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x08;
P2 &= 0x1f;
delay(500);
//P
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x8C;
P2 &= 0x1f;
delay(500);
//P
wei<<=1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x8C;
P2 &= 0x1f;
delay(500);
//y
wei <<= 1;
P2=((P2&0x1f)|0xc0);
P0=wei;
P2 &= 0x1f;
P2=((P2&0x1f)|0xe0);
P0=0x91;
P2 &= 0x1f;
delay(500);
}
void main(void)
{
P2=((P2&0x1f)|0xa0);
P0=0;
P2 &= 0x1f;
InitAll();
DisplaySmg2();
delay(500);
DisplaySmg3();
delay(500);
DisplaySmg4();
delay(500);
DisplaySmg5();
delay(500);
DisplaySmg6();
}
你们可以改我的代码。可以考虑一下时间的管理,因为我失败了,直接就上了,没用定时器了。
视频是我对象一个人的私有浪漫,就不放在博客里面了。
大家可以自己弄出来,发给对象。