以下是程序,下载到板子上不能实现一秒变化一次,大概8秒才一次
#include<reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e}; //【16进制数组】
uint8 i = 0;
uint16 counter = 0;
//sbit ENLED = P1^4;
sbit SMG1 = P3^4;
sbit SMG2 = P3^5;
sbit SMG3 = P3^6;
sbit SMG4 = P3^7;
void timer1_init() //定时器初始化
{
TMOD |= 0x01;
TH1 = 0xfc;
TL1 = 0x67; //1ms
TR1 = 1;
}
void int_init() //中断初始化
{
ET1 = 1;
EA = 1;
}
main()
{
P0 = 0xff;P3 = 0xff;
timer1_init();
int_init();
while(1);
}
void refresh() //数码管动态扫描显示
{
static uint8 j = 0;
switch(j)
{
case 0: SMG1 = 0;P0 = number[i];break;
case 1: SMG2 = 0;P0 = number[i];break;
case 2: SMG3 = 0;P0 = number[i];break;
case 3: SMG4 = 0;P0 = number[i];break;
default: break;
}
if(3 == j++)
j = 0;
}
void interrupt_timer1() interrupt 3
{
TH1 = 0xfc;
TL1 = 0x67;
counter++;
if(1000 == counter)
{
i++;
if(15 == i)
{
i = 0;
}
}
refresh();
}
附上原理图
在线等 谢谢各位了~~
此帖出自
小平头技术问答
lz的代码逻辑就有问题,1秒的扫描间隔?
至少需要30Hz(或更好) * 4(com口数量)的扫描频率
数码管程序需要分层次写,将IO脚根据逻辑分组定义,这样你的上层程序会完全与硬件无关,,这句话没有理解到 能详细点吗?
谢谢细心解答~~THX!
是不会飘红,因为你设计的扫描频率是1Hz,而且不关com口这就变成同步1hz刷新了,
但又由于定时器配置错误,变成0.125Hz的同步刷新
一周热门 更多>