#include //1638he165合并程序 2018/5/26
#include <tm1638.h>
#include
#define uchar unsigned char
#define uint unsigned int
sbit SU0=P2^0; //计数脉冲识别
sbit SU1=P2^1; //计数脉冲识别
sbit SU2=P2^2;
sbit DJ1=P1^0;
sbit DJ2=P1^1;
sbit QH=P3^2; //输出端
sbit CK=P3^3; //时钟 上升沿有效
sbit PL=P3^4; //移位控制 低电平有效
uchar temp;
uchar temp1;
uchar tempH;
uchar tempL;
bit weia;
bit weib;
bit ding;
unsigned char num[8]; //各个数码管显示的值
unsigned int dingshiqi=0;
unsigned int num1,num2;
unsigned int su1_a,su1_b;
unsigned int su2_a,su2_b;
unsigned int su2,su4,su1;
unsigned char wei,wei1;
unsigned int k;
void delay1ms(uint i) //1ms延时程序
{
uchar j;
while(i--)
{
for(j=0;j<115;j++)
{
;
}
}
}
void delay(unsigned int i)
{
unsigned char j;
while(i--)
for(j=120; j>0; j--);
}
uint read_int165(void)
{
uchar i=0;
uint read_data=0;
PL=0; //置数,读入并行输入数据
_nop_();
PL=1; //移位,并口被锁存,串行转换开始
_nop_();
for(i=0;i<16;i++) //设定16位输入
{
read_data<<=1;
if(QH)
{
read_data|=QH;
}
CK=0; //下降沿
_nop_();
CK=1;
_nop_(); //上升沿
}
return read_data;
}
void init_t0()
{
TMOD = 0x02; //8位自动载定时器
TH0 = 0x06;
TL0 = 0x06;
TR0 = 1; //启动定时器
ET0=1; //允许定时器0中断
EA=1; //开总中断
}
void main(void)
{
unsigned char i;
init_t0();
init_TM1638();
for(i=0;i<8;i++)
Write_DATA(i<<1,tab[0]);
weia = 0;
weib = 0;
ding = 0; ///初始化
{
if(SU2==0) //如果是按键S1按下
{
delay(10); //延时20ms,软件消抖
if(SU2==0) //如果是按键S1按下
DJ1=0;
while(!SU2);
}
}
{
uint temp=0; //初始化
uchar tempH=1;
uchar tempL=1;
CK=0;
}
while(1)
{
{
temp=read_int165();
tempH=(uchar)(temp>>8); //获取高八位,存在tempH
tempL=(uchar)temp; //获取低八位存在tempL
P2=tempH; //接收的字节高八位显示在P2
P1=tempL; //接收的低八位显示在P1
}
{
i=Read_key();
switch(i)
{
case 0: //1--1
{
while(Read_key()==i); //等待按键释放
su1_a = 0;
su2_a = 0;
wei=0;
weia=1;
ding=0;
}break;
case 1:
{
while(Read_key()==i); //等待按键释放
weia=0;
wei++;
if(wei>=3)wei = 0;
}break;
case 2:
{
while(Read_key()==i); //等待按键释放
if(wei==1)
su1_b++;
if(su1_b>5500) su1_b=0;
if(wei==2)
su2_b++;
if(su2_b>5500) su2_b=0;
}break;
case 3:
{
while(Read_key()==i); //等待按键释放
if(wei==1)
{
if(su1_b>0)su1_b--;
}
if(wei==2)
{
if(su2_b>0)su2_b--;
}
}break;
case 4:
{
while(Read_key()==i); //等待按键释放
ding=~ding;
}break;
case 5:{
while(Read_key()==i);
}break;
case 6:{
while(Read_key()==i);
}
break;
case 7:{
while(Read_key()==i);
}break;
}
if(wei==0) //脉冲输入计数
{
if((ding==0)&&(weia==1))
{
if(SU0 ==0)
{
delay(1);
if(SU0==0)
{
while(!SU0);
su1_a++;
}
}
if(SU1 ==0)
{
delay(1);
if(SU1==0)
{
while(!SU1);
su2_a++;
}
}
if(su1_a==su1_b) su1_a = 0;
if(su2_a==su2_b)
{
ding = 1;
}
}
Write_DATA(3*2,tab[su1_a]);
Write_DATA(2*2,tab[su1_a0/10]);
Write_DATA(1*2,tab[su1_a00/100]);
Write_DATA(0*2,tab[su1_a000/1000]);
Write_DATA(7*2,tab[su2_a]);
Write_DATA(6*2,tab[su2_a0/10]);
Write_DATA(5*2,tab[su2_a00/100]);
Write_DATA(4*2,tab[su2_a000/1000]);
}
if(wei==1) //左边数码管设置
{
k++;
if(k>40)k = 0;
if(k>10)
{
Write_DATA(3*2,tab[su1_b]);
Write_DATA(2*2,tab[su1_b0/10]);
Write_DATA(1*2,tab[su1_b00/100]);
Write_DATA(0*2,tab[su1_b000/1000]);
}
else
{
Write_DATA(0*2,tab[20]);
Write_DATA(1*2,tab[20]);
Write_DATA(2*2,tab[20]);
Write_DATA(3*2,tab[20]);
}
}
if(wei==2) //右边数码管设置
{
Write_DATA(3*2,tab[su1_a]);
Write_DATA(2*2,tab[su1_a0/10]);
Write_DATA(1*2,tab[su1_a00/100]);
Write_DATA(0*2,tab[su1_a000/1000]);
k++;
if(k>40)k = 0;
if(k>10)
{
Write_DATA(7*2,tab[su2_b]);
Write_DATA(6*2,tab[su2_b0/10]);
Write_DATA(5*2,tab[su2_b00/100]);
Write_DATA(4*2,tab[su2_b000/1000]);
}
else
{
Write_DATA(4*2,tab[20]);
Write_DATA(5*2,tab[20]);
Write_DATA(6*2,tab[20]);
Write_DATA(7*2,tab[20]);
}
}
}
}
}
void time0() interrupt 1 //使用的是定时器T0
{
dingshiqi++;
if(dingshiqi>=3686)
{
dingshiqi = 0;
}
}
2022-07-28 19:34:00
52KB
tm1638
1