初学求教,数码管计时,这代码为什么到50就不加了,为什么没显示9
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6; //定义锁存器 端位选段
sbit LED1 = P1^0;
uchar counter,time_counter ;
uchar code leddata[]={
0x3F,//0
0x06,//1
0x5B,//2
0x4F,//3
0x4F, //3
0x66, //4
0x6D,//5
0x7D,//6
0x07,//7
0x7F,//8
0x6F,//9
0x00,//ximie
0x00//zidingyi
};
void delay(uint z)
{
uint x,y;
for(x = z;x > 0;x--)
for(y = 114;y > 0; y--);
}
void display(uchar i)
{
uchar shi,ge ;
shi = i/10; //求模
ge = i%10; //求余
we = 1;
P0 = 0xbf;//点亮第 1
we = 0;
du = 1;
P0 = leddata[shi];
du = 0;
delay(1);
we = 1;
P0 = 0x7f;//点亮第二8
we = 0;
du = 1;
P0 = leddata[ge];
du = 0;
delay(1);
}
void main()
{
TMOD = 0x10;//定时器1 工作模式1 16位模式定时器
TH1 = 0x4b;
TL1 = 0xfe;//定时50ms
TR1 = 1; //启动定时器
while(1)
{
if(TF1 == 1)
{
TF1 = 0; //手动青0
TH1 = 0x4b;
TL1 = 0xfe;//定时50ms ,因溢出后寄存器里是0,重新赋初值
counter++;
}
if(counter == 20)
{
counter = 0;
LED1 = ~LED1;
time_counter ++;
} // (即从4bfe加到ffff)花50ms溢出一次,TF1这时自动置1,当检测到TF==1后,counter计次数
if(time_counter == 60)
time_counter = 0 ;
display(time_counter);
} /* 计数的
当定时器溢出20次
就是一秒钟的时间到了 */
}
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6; //定义锁存器 端位选段
sbit LED1 = P1^0;
uchar counter,time_counter ;
uchar code leddata[]={
0x3F,//0
0x06,//1
0x5B,//2
0x4F,//3
0x4F, //3
0x66, //4
0x6D,//5
0x7D,//6
0x07,//7
0x7F,//8
0x6F,//9
0x00,//ximie
0x00//zidingyi
};
void delay(uint z)
{
uint x,y;
for(x = z;x > 0;x--)
for(y = 114;y > 0; y--);
}
void display(uchar i)
{
uchar shi,ge ;
shi = i/10; //求模
ge = i%10; //求余
we = 1;
P0 = 0xbf;//点亮第 1
we = 0;
du = 1;
P0 = leddata[shi];
du = 0;
delay(1);
we = 1;
P0 = 0x7f;//点亮第二8
we = 0;
du = 1;
P0 = leddata[ge];
du = 0;
delay(1);
}
void main()
{
TMOD = 0x10;//定时器1 工作模式1 16位模式定时器
TH1 = 0x4b;
TL1 = 0xfe;//定时50ms
TR1 = 1; //启动定时器
while(1)
{
if(TF1 == 1)
{
TF1 = 0; //手动青0
TH1 = 0x4b;
TL1 = 0xfe;//定时50ms ,因溢出后寄存器里是0,重新赋初值
counter++;
}
if(counter == 20)
{
counter = 0;
LED1 = ~LED1;
time_counter ++;
} // (即从4bfe加到ffff)花50ms溢出一次,TF1这时自动置1,当检测到TF==1后,counter计次数
if(time_counter == 60)
time_counter = 0 ;
display(time_counter);
} /* 计数的
当定时器溢出20次
就是一秒钟的时间到了 */
}
leddata数组定义有误,数字3重复定义,删掉看看
先试试静态显示可以不可以显示9
楼上正解
求余应该是 i% 你对i的求余运算好像没有 ge=i%;