#include <stc15.h>
#include <intrins.h>
#include "iic.h"
unsigned char a[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x7f,0xbf,0xff,0x8e,0xc1,0x7f};//0~9/./-/灭/F/U
unsigned char b[8]={0};
unsigned int count_t=0,count_f=0,NE=0;
unsigned int dat;
unsigned char n=0,m=0;
void BUZZ()//初始化
{
P2=(P2&0x1f)|0x80;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2&=0x1f;
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2&=0x1f;
}
void SMGxuanze()//数码管 位选 段选
{
static int i=0;
P2=(P2&0x1f)|0xc0;
P0=0x01<<i;
P2&=0x1f;
if(i==5&&n==0)
{
P2=(P2&0x1f)|0xe0;
P0=a[b[i]]&0x7f;
P2&=0x1f;
}
else
{
P2=(P2&0x1f)|0xe0;
P0=a[b[i]];
P2&=0x1f;
}
i=(i+1)%8;
}
void NE555()//显示NE数字
{
b[0]=13;
b[1]=12;
b[2]=12;
if(NE > 9999)
b[3]=NE/10000;else {b[3]=12;}
if(NE > 999)
b[4]=NE/1000%10;else {b[4]=12;}
if(NE > 99)
b[5]=NE/100%10;else {b[5]=12;}
if(NE > 9)
b[6]=NE/10%10;else {b[6]=12;}
b[7]=NE%10;
}
void Read_AIN3()
{
IIC_Start(); //IIC总线起始信号
IIC_SendByte(0x90); //PCF8591的写设备地址
IIC_WaitAck(); //等待从机应答
IIC_SendByte(0x03); //写入PCF8591的控制字节
IIC_WaitAck(); //等待从机应答
IIC_Stop(); //IIC总线停止信号
IIC_Start(); //IIC总线起始信号
IIC_SendByte(0x91); //PCF8591的读设备地址
IIC_WaitAck(); //等待从机应答
dat = IIC_RecByte(); //读取PCF8591通道3的数据
IIC_SendAck(1); //产生非应答信号
IIC_Stop(); //IIC总线停止信号
dat=dat/0.51;
}
void PCF8591xainshi()//电压显示
{
b[0]=14;
b[1]=12;
b[2]=12;
b[3]=12;
b[4]=12;
b[5]=dat/100%10&15;
b[6]=dat/10%10;
b[7]=dat%10;
}
void Delay30ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 2;
j = 67;
k = 183;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void LED_PCF8591()//电压的SMG
{
if(dat>=150&&dat<250||dat>=350)
{
P2=(P2&0x1f)|0x80;
P0=0xfa;
}
else
{
P2=(P2&0x1f)|0x80;
P0=0xfe;
}
}
void LED_NE555()//频率的SMG
{
if(NE>=1000&&NE<5000||NE>=10000)
{
P2=(P2&0x1f)|0x80;
P0=0xf5;
}
else
{
P2=(P2&0x1f)|0x80;
P0=0xfd;
}
}
void Keys()//按键
{
if(P33==0)
{
Delay30ms();
if(P33==0)
{
n++;
n=n%2;
}
}while(P33==0);
}
void Timer()
{
TMOD=0x16;//定时器1用方式1,定时;定时器0用方式2,计数
TH0=0XFF;
TL0=0XFF;
TH1=(65535-1000)/256;//定时一次1毫秒
TL1=(65535-1000)%256;
TR0=1;
TR1=1;
ET0=1;
ET1=1;
EA=1;
}
void ServiceTimer0() interrupt 1
{
count_t++;//实现计数功能
}
void ServiceTimer2() interrupt 3
{
if(n==0)
{
Read_AIN3();
SMGxuanze();
PCF8591xainshi();
LED_PCF8591();
}
else if(n==1)
{
SMGxuanze();
NE555();
LED_NE555();
}
TH1=(65535-1000)/256;//定时一次1毫秒
TL1=(65535-1000)%256;
count_f++;
if(count_f%1000==0)//实现定时1秒
{
NE=count_t;//记录技术
count_t=0;//重置计数
}
}
void main()
{
BUZZ();
Timer();
while(1)
{
Keys();
}
}