tyrl800 发表于 2024-1-21 09:26
程序没问题,检查硬件
/*按键K1K2加减数字数码管显示 按键K3K4加减数码管显示
#include "STC8H.h"
#include "intrins.h"
unsigned char NixieTable[]={ 0x03,/*0*/
0x9F,/*1*/
0x25,/*2*/
0x0D,/*3*/
0x99,/*4*/
0x49,/*5*/
0x41,/*6*/
0x1F,/*7*/
0x01,/*8*/
0x09,/*9*/};
sbit k1 = P0^3 ;
sbit k2 = P5^4 ;
sbit k3 = P3^6 ;
sbit k4 = P3^7 ;
unsigned int Num,Num1;
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
i = 15;
j = 90;
do
{
while (--j);
} while (--i);
}
void DelayXms(unsigned int x) //@11.0592MHz
{
while (--x)
{
Delay1ms();
}
}
void Led_Seg(unsigned int date) //显示函数
{
unsigned int a,b,c;
a=date/100%10; //求百位
b=date/10%10; //求十位
c=date%10; //求个位
P14=1;P15=1;P33=1;
P2=NixieTable[a];//百位
P14=0;P15=1;P33=1;
DelayXms(5);
P14=1;P15=1;P33=1;
P2=NixieTable;//十位
P15=0;P33=1;P14=1;
DelayXms(5);
P14=1;P15=1;P33=1;
P2=NixieTable[c];//个位
P33=0;P14=1;P15=1;
DelayXms(5);
}
void Led_Seg1(unsigned int date) //显示函数
{
unsigned int a,b,c;
a=date/100%10; //求百位
b=date/10%10; //求十位
c=date%10; //求个位
P00=1;P01=1;P02=1;
P2=NixieTable[a];//百位
P00=0;P01=1;P02=1;
DelayXms(5);
P00=1;P01=1;P02=1;
P2=NixieTable;//十位
P00=1;P01=0;P02=1;
DelayXms(5);
P00=1;P01=1;P02=1;
P2=NixieTable[c];//个位
P00=1;P01=1;P02=0;
DelayXms(5);
}
unsigned char keyscan()
{
char x=0;
if ( k1 == 0|k2 == 0|k3 == 0|k4 == 0)
{
DelayXms(10);
if ( k1 == 0|k2 == 0|k3 == 0|k4 == 0)
{
if (k1 == 0) x=1;
else if (k2 == 0) x=2;
else if (k3 == 0) x=3;
else x=4;
while ( k1 == 0|k2 == 0|k3 == 0|k4 == 0);
}
}
return(x);
}
void Timer0_init() //1ms
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初始值
TH0 = 0xD4; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //开T0中断
EA=1;
}
void main ()
{
P0M0 = 0x00; P0M1 = 0x00;
P1M0 = 0x00; P1M1 = 0xc0;
P2M0 = 0x00; P2M1 = 0x00;
P3M0 = 0x00; P3M1 = 0x00;
k1=k2=k3=k4=1;
Num=123;Num1=456;
Timer0_init() ;
while(1)
{
unsigned char y;
y=keyscan();
if(y!=0)
{
if(y==1) Num++;
else if(y==2) Num--;
else if(y==3) Num1 ++;
else Num1--;
}
}
}
void TM1_Isr0()interrupt 1 //数码管扫描定时器0
{
unsigned int s;
if(++s>1)
{ s=0;
Led_Seg(Num);P00=1;
Led_Seg1(Num1);
}
}
看看这个四个按键分别控制两组三位数码管加减 |