直接复制粘贴然后运行
然后打开stc烧录到开发板上面就能用 程序哪里不懂的话问我,我闲的蛋疼!
由于没有万用表就没查DA输出的电压
所以就不知道DA输出写的对不对,不过其他应该都没问题。
电压测量那边还搞了数据做比较,嫌碍事的滴滴我,我告诉你说咋关了作比较的数据。
我好闲呀!我已经快一个月没跟女生说过话了!
#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc1,0x8e,0xff};
unsigned char tac[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x98,0x00,0x10,0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
unsigned char yi,er,san,si,wu,liu,qi,ba;
int shuju;
unsigned char shuju1;
unsigned char jiemian=0;
unsigned char add=1,DBA=2;
unsigned char LED=0;
bit qidong=0;
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void Delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j--);
}
void chushihua()
{
P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
P2=0XC0;P0=0X00;P2=0XFF;P0=0XFF;
}
void shumaguan1(unsigned char yi,unsigned char er)
{
P2=0XC0;
P0=0X01;
P2=0XFF;
P0=tab[yi];
Delayms(1);
P2=0XC0;
P0=0X02;
P2=0XFF;
P0=tab[er];
Delayms(1);
}
void shumaguan2(unsigned char san,unsigned char si)
{
P2=0XC0;
P0=0X04;
P2=0XFF;
P0=tab[san];
Delayms(1);
P2=0XC0;
P0=0X08;
P2=0XFF;
P0=tab[si];
Delayms(1);
}
void shumaguan3(unsigned char wu,unsigned char liu)
{
P2=0XC0;
P0=0X10;
P2=0XFF;
P0=tab[wu];
Delayms(1);
P2=0XC0;
P0=0X20;
P2=0XFF;
P0=tac[liu];
Delayms(1);
}
void shumaguan4(unsigned char qi,unsigned char ba)
{
P2=0XC0;
P0=0X40;
P2=0XFF;
P0=tab[qi];
Delayms(1);
P2=0XC0;
P0=0X80;
P2=0XFF;
P0=tab[ba];
Delayms(1);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
unsigned char AD()
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void DA(unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void anjian()
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
while(!P30);
}
if(jiemian==0)
{
jiemian=1;
}
else
if(jiemian==1)
{
jiemian=0;
}
}
if(P31==0)
{
Delayms(5);
if(P31==0)
{
while(!P31);
}
if(add==0)
{
DBA=2;
add=1;
}
else
if(add==1)
{
DBA=AD();
add=0;
}
}
if(P32==0)
{
Delayms(5);
if(P32==0)
{
while(!P32);
}
if(LED==0)
{
LED=1;
}
else
if(LED==1)
{
LED=0;
}
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
while(!P33);
}
if(qidong==0)
{
qidong=1;
}
else
if(qidong==1)
{
qidong=0;
}
}
}
int fan=0,zheng=0;
int t_flag=0;
int zheng_now=0;
int fan_now=0;
void Timer0Init(void) //5微秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xC4; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
}
void tm0_isr() interrupt 1
{
if(P34==0)
{
fan++;
if(t_flag==0)
{
t_flag=1;
zheng_now=zheng;
zheng=0;
}
}
else
if(P34==1)
{
zheng++;
if(t_flag==1)
{
t_flag=0;
fan_now=fan;
fan=0;
}
}
}
int intr=0;
int Time,freq;
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x20; //设置定时初值
TH1 = 0xD1; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA=1;
ET1=1;
}
void tm1_isr() interrupt 3
{
intr++;
if(intr==969)
{
TR0 = 1;ET0 = 1 ;
}
else if(intr==999)
{
intr=0;
TR0 = 0;ET0 = 0 ;
Time=(fan_now+zheng_now)*5;
freq=1000000/Time;
}
}
void main()
{
chushihua();
Timer0Init();
Timer1Init();
while(1)
{
anjian();
if(jiemian==1) //测试频率
{
if(LED==0)
{
if(freq<1000)
{
P00=1;
P01=0;
P02=1;
P03=1;
P04=add;
P05=1;
P06=1;
P07=1;
}
if((1000<freq)&&(freq<5000))
{
P00=1;
P01=0;
P02=1;
P03=0;
P04=add;
P05=1;
P06=1;
P07=1;
}
P2=0X80;
}
yi=11;er=12;san=12;si=freq/10000;wu=freq%10000/1000;liu=freq%1000/100+10;qi=freq%100/10;ba=freq%10;
}
if(jiemian==0) //测试电压
{
if(LED==0)
{
shuju1=AD();
if(shuju1<=76)
{
P00=0;
P01=1;
P02=1;
P03=1;
P04=add;
P05=1;
P06=1;
P07=1;
}
if((76<shuju1)&&(shuju1<=128))
{
P00=0;
P01=1;
P02=0;
P03=1;
P04=add;
P05=1;
P06=1;
P07=1;
}
if((128<=shuju1)&&(shuju1<=179))
{
P00=0;
P01=1;
P02=1;
P03=1;
P04=1;
P05=1;
P06=1;
P07=1;
}
if((179<=shuju1)&&(shuju1<=255))
{
P00=0;
P01=1;
P02=0;
P03=1;
P04=1;
P05=1;
P06=1;
P07=1;
}
P2=0X80;
}
if(LED==1)
{
P0=0XFF;
P2=0X80;
}
shuju=AD()*100/51;
yi=10;er=shuju1/100;san=shuju1%100/10;si=shuju1%10;wu=12;liu=shuju/100;qi=shuju%100/10;ba=shuju%10;
}
while(qidong)
{
anjian();
yi=12;er=12;san=12;si=12;wu=12;liu=20;qi=12;ba=12;
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
DA(DBA);
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}