哪位单片机高手给我看看这个程序到底哪错了,初学者,纠结呀要求只显示有效位,为什么只显示第一个呢
//2012年10月4日11:42:31简易秒表#include<reg52.h>typedefunsignedcharuint8;typedefunsignedintu...
//2012年10月4日11:42:31 简易秒表
# include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit LED = P0^0;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e};//加分号语句
uint8 a[6];
uint16 counter = 0;
void timer1_init()
{
TMOD |= 0X10;//与和或符号靠近等于号
TMOD &= 0XDF;
TH1 = 0XFC;
TL1 = 0X67;
TR1 = 1;
}
void int1_init()
{
ET1 = 1;
EA = 1;
}
refresh()
{
static uint32 sec=0;
if(sec<10)
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
if(sec>=10&&sec<100)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
}
if(sec>=100&&sec<1000)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
}
if(sec>=1000&&sec<10000)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
}
if(sec>=10000&&sec<100000)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
}
if(sec>=100000)
{ ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; P0 = number[a[5]];
}
}
int main()
{
ENLED = 0;ADDR3 = 1;
timer1_init();
int1_init(); //不能加void
while(1);
}
void interrupt_timer1() interrupt 3
{
static uint32 sec = 0;
TH1 = 0XFC;
TL1 = 0X67;
counter++;
if(counter == 1000)
{
sec++;
counter = 0;
a[0] = sec%10;
a[1] = sec/10%10;
a[2] = sec/100%10;
a[3] = sec/1000%10;
a[4] = sec/10000%10;
a[5] = sec/100000%10;
}
refresh();
}
要求数码管只显示有效位,就好似到了时间他这位才亮,实现简易计时功能 展开
# include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit LED = P0^0;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e};//加分号语句
uint8 a[6];
uint16 counter = 0;
void timer1_init()
{
TMOD |= 0X10;//与和或符号靠近等于号
TMOD &= 0XDF;
TH1 = 0XFC;
TL1 = 0X67;
TR1 = 1;
}
void int1_init()
{
ET1 = 1;
EA = 1;
}
refresh()
{
static uint32 sec=0;
if(sec<10)
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
if(sec>=10&&sec<100)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
}
if(sec>=100&&sec<1000)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
}
if(sec>=1000&&sec<10000)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
}
if(sec>=10000&&sec<100000)
{
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
}
if(sec>=100000)
{ ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; P0 = number[a[5]];
}
}
int main()
{
ENLED = 0;ADDR3 = 1;
timer1_init();
int1_init(); //不能加void
while(1);
}
void interrupt_timer1() interrupt 3
{
static uint32 sec = 0;
TH1 = 0XFC;
TL1 = 0X67;
counter++;
if(counter == 1000)
{
sec++;
counter = 0;
a[0] = sec%10;
a[1] = sec/10%10;
a[2] = sec/100%10;
a[3] = sec/1000%10;
a[4] = sec/10000%10;
a[5] = sec/100000%10;
}
refresh();
}
要求数码管只显示有效位,就好似到了时间他这位才亮,实现简易计时功能 展开
4个回答
展开全部
仅供参考:
refresh()
{
if(sec < 10) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
}
else if(sec < 100) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
}
else if(sec < 1000) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
}
else if(sec < 10000) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
}
else if(sec < 100000) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
}
else {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; P0 = number[a[5]];
}
}
uint32 sec = 0; //该在最前面定义
refresh()
{
if(sec < 10) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
}
else if(sec < 100) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
}
else if(sec < 1000) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
}
else if(sec < 10000) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
}
else if(sec < 100000) {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
}
else {
ADDR0 = 0; ADDR1 = 0; ADDR2 = 0; P0 = number[a[0]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 0; P0 = number[a[1]];
ADDR0 = 0; ADDR1 = 1; ADDR2 = 0; P0 = number[a[2]];
ADDR0 = 1; ADDR1 = 1; ADDR2 = 0; P0 = number[a[3]];
ADDR0 = 0; ADDR1 = 0; ADDR2 = 1; P0 = number[a[4]];
ADDR0 = 1; ADDR1 = 0; ADDR2 = 1; P0 = number[a[5]];
}
}
uint32 sec = 0; //该在最前面定义
意法半导体(中国)投资有限公司
2020-01-15 广告
2020-01-15 广告
单片机(Microcontrollers)是一种集成电路芯片,是采用超大规模集成电路技术把具有数据处理能力的中央处理器CPU、随机存储器RAM、只读存储器ROM、多种I/O口和中断系统、定时器/计数器等功能(可能还包括显示驱动电路、脉宽调制...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
展开全部
你至少要把电路图一起发出来啊……还有你想要达到的目的……整个程序也没有注释,这样怎么帮你看……
追问
就是数码管显示的时候只要有效位,就动态显示那边搞错了,我不会改,不好意思了,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有一个很明显的错误, sec 应该设为全局变量,并且不要设为静态变量,改了再试试吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也是初学者不过看不等你的程序
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询