level 12
#include "STC8.h"
sbit hold=P3^2;
unsigned char qumo[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned long int fh=0;
unsigned char tri=1;
unsigned int ms=0;
void dsmg(unsigned char zifu)
{
P0=qumo[zifu];
}
/*
void delay1s() //?? 0us
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
}
*/
void counter0on()//用16位自动重装载模式不正常,不进中断
{
TMOD|=0x06;
TH0=0x00;
TL0=0x00;
TR0=1;
ET0=1;
EA=1;
}
void timer1on()
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD|= 0x00; //xxxx 0000 16位自动重装载模式
TL1 = 0xB0; //设置定时初值
TH1 = 0x3C; //设置定时初值
TF1 = 0; //清除TF1标志
EA = 1;
ET1 = 1;
TR1 = 1;
}
void ex0on()//外部中断0接按键用于保存数据
{
IT0=1;
EX0=1;
EA=1;
}
void delay1ms(void) //@36MHz
{
unsigned char i, j;
i = 36;
j = 1;
do
{
while (--j&&tri);
} while (--i&&tri);
}
void scansmg(unsigned char dws[6])
{
unsigned char sanp=0x80,sani=0,t=6;
P2=~sanp;
while(t--&&tri)
{
dsmg(dws[sani]);
delay1ms();
P0=0x00;
sani++;
P2=~(sanp>>sani);
}
}
void chaifen(unsigned long int dws,unsigned char dwssz[6])
{
unsigned char i=0;
unsigned long int chai=100000;
unsigned char dwst=0;
while(dwst<6)
{
dwssz[dwst]=dws/chai;
dws=dws%chai;
chai=chai/10;
dwst++;
}
while(!dwssz[i]&&i<5)//消无意义0
{
dwssz[i]=16;
i++;
}
}
/*
void xiao0(unsigned char dwssz[8])//消0集成到拆分里了
{
unsigned char i=0;
while(!dwssz[i]&&i<7)
{
dwssz[i]=16;
i++;
}
}
*/
void Sys_clk()
{
P_SW2 = 0x80;
XOSCCR = 0x80; //选择有源晶振 1000 0000
while (!(XOSCCR & 1)); //等待时钟稳定
CLKDIV = 0x00; //时钟不分频
CKSEL = 0x01; //选择外部晶振 0000 0001
P_SW2 = 0x00;
}
void main()
{
unsigned char dwssz[6];
unsigned long int dws=888888;
unsigned long int result;
Sys_clk();
P0M0=0xFF;
P0M1=0x00;
counter0on();
timer1on();
P3=0xff;
chaifen(dws,dwssz);
ex0on();
while(1)
{
scansmg(dwssz);
// P1=TL0;//调试用,用二进制显示数值
if(ms>=60)
{
ms=0;
result=(TL0+256*fh);
fh=0;//做频率计启用这行
TL0=0;//同上
chaifen(result,dwssz);
// TL0++;//用于测试计时精度
tri=1;
}
}
}
void counter0() interrupt 1
{
fh++;
}
void timer1() interrupt 3
{
//TH1 = 0x3C;//16位自动重装载模式不需要这两行了
//TL1 = 0xB0;
ms++;
if(ms>=60)
{
tri=0;
}
}
void Int0() interrupt 0//保持数据
{
EA=0;
P0=0x00;
delay1ms();
delay1ms();
delay1ms();
if(!hold)
{
TR0=!TR0;
TR1=!TR1;
}
while(!hold);
delay1ms();
delay1ms();
delay1ms();
EA=1;
}
2019年10月07日 10点10分

