求助,求助,做的数码管秒表显示是乱的
时间:10-02
整理:3721RD
点击:
照着视频做的简易数码管秒表显示是乱的,搞了我好几天都找不出原因,
#include <reg51.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
code uint16 number[] = { 0x3f,0x06,0x5b,
0x4f,0x66,0x6d,
0x7d,0x07,0x7f,
0x6f } ;
uint16 counter=0;
uint8 b[6];
sbit BCD6 = P1^0;
sbit BCD5 = P1^1;
sbit BCD4 = P1^2;
sbit BCD3 = P1^3;
sbit BCD2 = P1^4;
sbit BCD1 = P1^5;
timer0()
{
TMOD|= 0x10;
TMOD&= 0xdf;
TH0 = 0x4c;
TL0 = 0x00;
TR0 = 1;
}
inter()
{
ET0=1;
EA=1;
}
refresh()
{
static uint8 j=0;
switch(j)
{
case 0: BCD6 = 0; j++; P0 = number[b[0]]; break;
case 1: BCD5 = 0; j++; P0 = number[b[1]]; break;
case 2: BCD4 = 0; j++; P0 = number[b[2]]; break;
case 3: BCD3 = 0; j++; P0 = number[b[3]]; break;
case 4: BCD2 = 0; j++; P0 = number[b[4]]; break;
case 5: BCD1 = 0; j=0; P0 = number[b[5]]; break;
default: break;
}
}
inter_timer0() interrupt 1
{
static uint32 sec=0;
TH0=0x4c;
TL0=0x00;
counter++;
if(counter==20)
{ sec++;
counter=0;
b[0]=sec%10;
b[1]=sec/10%10;
b[2]=sec/100%10;
b[3]=sec/1000%10;
b[4]=sec/10000%10;
b[5]=sec/100000%10;
}
refresh();
}
main()
{
timer0();
inter();
while(1);
}
P0和P1(0~5)直接接共阴数码管的 单片机是stc89c52
高手给看看啦~·谢啦
第1个数码管的公共端接P2^7,第二个接P2^6,以此类推
暂停接P3^2,清零接P1^0
数码管采用动态扫描的方法接
代码不一定正确
参考《从零开始学51单片机》
#include <reg51.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
code uint16 number[] = { 0x3f,0x06,0x5b,
0x4f,0x66,0x6d,
0x7d,0x07,0x7f,
0x6f } ;
uint16 counter=0;
uint8 b[6];
sbit BCD6 = P1^0;
sbit BCD5 = P1^1;
sbit BCD4 = P1^2;
sbit BCD3 = P1^3;
sbit BCD2 = P1^4;
sbit BCD1 = P1^5;
timer0()
{
TMOD|= 0x10;
TMOD&= 0xdf;
TH0 = 0x4c;
TL0 = 0x00;
TR0 = 1;
}
inter()
{
ET0=1;
EA=1;
}
refresh()
{
static uint8 j=0;
switch(j)
{
case 0: BCD6 = 0; j++; P0 = number[b[0]]; break;
case 1: BCD5 = 0; j++; P0 = number[b[1]]; break;
case 2: BCD4 = 0; j++; P0 = number[b[2]]; break;
case 3: BCD3 = 0; j++; P0 = number[b[3]]; break;
case 4: BCD2 = 0; j++; P0 = number[b[4]]; break;
case 5: BCD1 = 0; j=0; P0 = number[b[5]]; break;
default: break;
}
}
inter_timer0() interrupt 1
{
static uint32 sec=0;
TH0=0x4c;
TL0=0x00;
counter++;
if(counter==20)
{ sec++;
counter=0;
b[0]=sec%10;
b[1]=sec/10%10;
b[2]=sec/100%10;
b[3]=sec/1000%10;
b[4]=sec/10000%10;
b[5]=sec/100000%10;
}
refresh();
}
main()
{
timer0();
inter();
while(1);
}
P0和P1(0~5)直接接共阴数码管的 单片机是stc89c52
高手给看看啦~·谢啦
case 0: BCD6 = 0; j++; P0 = number[b[0]]; break;
j++是什么意思,不要 看看
j++是 为了执行完case0后执行case1.....就是刷新完最后一个数码管后再刷新倒数第二个~我自己写出了的是六个亮成一片的,在000 000 和888 888 这两个数里来回显示
怎么把代码并在一行,看着好晕
单片机是STC89C52,你用 #include <reg51.h> 干什么
应该用#include<reg52.h>
我以为是一样的
中断应该是interrupt 3吧
现在你用的是定时器1,不是定时器0.所以应该进入中断是inter_timer0() interrupt 3
- #include <reg51.h> //包含头文件
- #define fosc 11059200L //7328000
- #define T0_50ms (65536 - 50000 * ( fosc / 12000000L)) //50ms定时参数
- sbit en1 = P2^4;
- sbit en2 = P2^5;
- sbit en3 = P2^6;
- sbit en4 = P2^7
- sbit clear = P1^0;
- unsigned char display_num,display_fen,count_in_T0; //计数值 、进入定时器的次数
- bit running;
- //定义端口寄存器
- sfr P0M0 = 0X93;
- sfr P0M1 = 0X94;
- sfr P1M0 = 0X91;
- sfr P1M1 = 0X92;
- sfr P2M0 = 0X95;
- sfr P2M1 = 0X96;
- sfr P3M0 = 0Xb1;
- sfr P3M1 = 0Xb2;
- /*
- 共阴极数码管0 - 9的字形码
- “0” 3FH “5” 6DH
- “1” 06H “6” 7DH
- “2” 5BH “7” 07H
- “3” 4FH “8” 7FH
- “4” 66H “9” 6FH
- */
- const unsigned char seg7[10] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07, 0x7f, 0x6f};
- void init_port()
- {
- //配置P0模式为开漏输出
- P0M1 = 0xff;
- P0M0 = 0xff;
- P1M1 |= 0x01; //P1.0输入
- P1M0 |= 0x00;
- clear = 1;
- }
- void init_int()
- {
- IT0 = 1;//设置INT0为下降沿触发
- EX0 = 1;//允许INT0中断
- }
- void init_timer()
- {
- TMOD = 0X01; //定时器0模式1,16bit
- TH0 = T0_50ms >> 8; //TH0 = T0_50ms / 256 hhhh hhhh llll llll
- TL0 = T0_50ms; //TL0 = T0_50ms % 256
- ET0 = 1; //允许T0中断
- }
- //定义延时函数
- void ms_delay(unsigned int t)
- {
- unsigned int i;
- for (t; t > 0; t--) //外层循环t次
- for (i = 110;i > 0; i--) //内层循环110次
- ;
- }
- void display(unsigned char c4,unsigned char c3,unsigned char c2, unsigned char c1)
- {
- P0 = seg7[c4]; //送入十位(分)的段码
- en4 = 0; //显示DS2
- ms_delay(10);
- en4 = 1; //关闭DS2
- P0 = seg7[c3]; //送入个位(分)的段码
- en3 = 0; //显示DS2
- ms_delay(10);
- en3 = 1; //关闭DS2
- P0 = seg7[c2]; //送入十位(秒)的段码
- en2 = 0; //显示DS2
- ms_delay(10);
- en2 = 1; //关闭DS2
- P0 = seg7[c1]; //送入个位(秒)的段码
- en1 = 0; //显示DS1
- ms_delay(10);
- en1 = 1; //关闭DS1
- }
- void INT0_interrupt() interrupt 0
- {
- running = ~running; //切换暂停/计时
- if (running)
- {//切换为运行
- TR0 = 1;
- }
- else
- { //切换为暂停
- TR0 = 0;
- }
- }
- void Timer0_interrupt() interrupt 1
- {
- TH0 = T0_50ms >> 8; //重装初值
- TL0 = T0_50ms;
- if ((++ count_in_T0) == 20)//count_in_T0自加到20,计时1s
- {
- count_in_T0 = 0;
- if ( (++ display_num) ==60)
- {//display_num自加1后判断是否等于60
- display_num = 0;
- if ((++ display_fen) == 60)
- {//display_fen自加1后判断是否等于60
- display_fen = 0;
- }
- }
- }
- }
- main()
- {
- unsigned char qian,bai,shi, ge; //定义千、百、十位、个位要输出的数据
- init_port();
- init_int();
- init_timer();
-
- display_num = 0;
- count_in_T0 = 0;
- running = 0;
- //把四个数码管都关闭
- en4 = 1;
- en3 = 1;
- en2 = 1;
- en1 = 1;
- EA = 1;//开总中断
- TR0 = 1;//启动T0
- while(1)
- {
- qian = display_fen /10;
- bai = display_fen % 10;
- shi = display_num / 10;
- ge = display_num % 10;
- display(qian,bai,shi,ge);
- //检测清零按键
- if ( clear == 0)
- {
- ms_delay(10);
- if ( clear == 0 )
- {//确认按下清零按钮
- if ( !running )//如果处于暂停状态
- display_num = 0;
- }
- }
- }
- }
第1个数码管的公共端接P2^7,第二个接P2^6,以此类推
暂停接P3^2,清零接P1^0
数码管采用动态扫描的方法接
代码不一定正确
参考《从零开始学51单片机》
P0口要拉电阻
写程序要边写边测试,写完后才不会出错…
来自:中国电子技术论坛 Android客户端来自: Android客户端
写程序要边写边测试,写完后才不会出错…
来自:中国电子技术论坛 Android客户端来自: Android客户端