直接复制粘贴然后运行
然后打开stc烧录到开发板上面就能用 程序哪里不懂的话问我,我闲的蛋疼!
#include <STC15F2K60S2.H>
#include <intrins.h>
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char jiemian=0; //定义界面
int moshi=1; //定义模式
unsigned char jiange=4; //定义间隔
unsigned char a=0,tiao=0,shan=0;
unsigned char b=0,app=0,shan1=0;
unsigned char shuju;
unsigned char d=0;
int c=0;
unsigned char tiaozhuan;
unsigned char pwm=0;
unsigned char AD();
#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_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
ET0=1;
}
void tm0_isr() interrupt 1
{
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
a++;
if(a==160)
{
a=0;
if(tiao==1)
{
if(shan==0)
{
wu=jiange/10;liu=jiange%10;
er=11;
shan=1;
}
else
if(shan==1)
{
wu=jiange/10;liu=jiange%10;
er=moshi;
shan=0;
}
}
if(tiao==2)
{
if(shan==0)
{
er=moshi;
wu=11;liu=11;
shan=1;
}
else
if(shan==1)
{
er=moshi;
wu=jiange/10;liu=jiange%10;
shan=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=tab[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 anjian()
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
while(!P30);
}
if(d==0)
{
EA=1;
ET1=1;
d=1;
}
else
if(d==1)
{
EA=0;
ET1=0;
d=0;
b=0;
pwm=0;
}
}
if(P31==0)
{
Delayms(30);
if(P31==0)
{
while(!P31);
}
if(jiemian==0)
{
jiemian=1;
}
else
if(jiemian==1)
{
jiemian=2;
}
else
if(jiemian==2)
{
jiemian=0;
}
if(tiao==0)
{
tiao=1;
}
else
if(tiao==1)
{
tiao=2;
}
else
if(tiao==2)
{
tiao=0;
}
}
if(P32==0)
{
Delayms(35);
if(P32==0)
{
while(!P32);
}
if(jiemian==1)
{
moshi=moshi+1;
if(moshi==5)
{
moshi=1;
}
}
if(jiemian==2)
{
jiange=jiange+1;
if(jiange==13)
{
jiange=4;
}
}
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
while(!P33)
{
if(jiemian==0)
{
shuju=AD()/64+1;
yi=11;er=11;san=11;si=11;wu=11;liu=11;qi=10;ba=shuju;
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}
if(jiemian==1)
{
moshi=moshi-1;
if(moshi==0)
{
moshi=1;
}
}
if(jiemian==2)
{
jiange=jiange-1;
if(jiange==3)
{
jiange=4;
}
}
if(jiemian==0)
{
}
}
}
}
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;
}
unsigned char EEAD(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xA1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void EEDA(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
void Timer1Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0x28; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
}
void tm1_isr() interrupt 3
{
TL1 = 0x00; //设置定时初值
TH1 = 0x28; //设置定时初值
b++;
pwm++;
if(b==jiange*20)
{
b=0;
if(moshi==1)
{
P2=0X80;
tiaozhuan=~((0X01)<<app);
shan1=1;
app++;
if(app==8)
{
app=0;
}
}
if(moshi==2)
{
P2=0X80;
tiaozhuan=~((0X80)>>app);
shan1=1;
app++;
if(app==8)
{
app=0;
}
}
if(moshi==3)
{
P2=0X80;
tiaozhuan=(~((0X80)>>app))&(~((0x01)<<app));
shan1=1;
app++;
if(app==4)
{
app=0;
}
}
if(moshi==4)
{
P2=0X80;
tiaozhuan=(~((0X08)>>app))&(~((0x10)<<app));
shan1=1;
app++;
if(app==4)
{
app=0;
}
}
}
if(pwm==6)
{
pwm=0;
}
if(0<=pwm<=shuju)
{
P2=0X80;
P0=tiaozhuan;
}
if(shuju<pwm)
{
P2=0X80;
P0=0XFF;
}
}
void main()
{
chushihua();
Timer0Init();
Timer1Init();
moshi=EEAD(0X00);Delayms(2);
jiange=EEAD(0X01);Delayms(2);
while(1)
{
shuju=AD()/64+1;
anjian();
if(jiemian==0)
{
yi=11;er=11;san=11;si=11;wu=11;liu=11;qi=11;ba=11;
}
if(jiemian==1)
{
yi=10;san=10;si=11;qi=0;ba=0;
}
if(jiemian==2)
{
yi=10;san=10;si=11;qi=0;ba=0;
}
EEDA(0X00,moshi);Delayms(2);
EEDA(0X01,jiange);Delayms(2);
shumaguan1(yi,er);
shumaguan2(san,si);
shumaguan3(wu,liu);
shumaguan4(qi,ba);
}
}