没有找出来bug,大家发现了可以提醒一下
#include <STC15F2K60S2.H>
#include <intrins.h>
#include "iic.h"
u8 a[8]={10,10,10,10,10,10,10,10};
u8 timepoint=0;
u8 rb2=0;
u8 point=0;
u8 writep=0;
u8 flag_display=0;
u8 flag_pcfad=0;
u8 flag_key=0;
u8 flag_pwm=0;
u8 pwm=0;
u8 site=0;
u8 moshi=0;
u16 time=0;
u8 jiange=4;
u8 key=0;
u8 t1=4,t2=4,t3=4,t4=4;
u8 val=0;
u8 xianshi=0;
u16 tiaodong=0;
u8 liangdu=0;
u8 key_val(void)
{
u8 ret=0;
P3=0x0f;P44=0;P42=0;
if(P30==0)ret=7;
if(P31==0)ret=6;
if(P32==0)ret=5;
if(P33==0)ret=4;
return ret;
}
u8 key_sta(void)
{
static u8 sta=0;
u8 ret=0;
val=key_val();
switch(sta)
{
case 0:if(val!=0)sta=1;break;
case 1:if(val!=0){sta=2;ret=val;}else sta=0;break;
case 2:if(val==0)sta=0;break;
default:break;
}
return ret;
}
void ctrl(u8 ctrl)
{
u8 p2data=P2;
P2=(ctrl<<5)|(P2&0x01f);
P2=p2data&0x1f;
}
void display(u8 site,u8 light)
{
P0=0xff;ctrl(7);
P0=0x01<<site;ctrl(6);
switch(light)
{
case 0:P0=0xc0;break;
case 1:P0=0xf9;break;
case 2:P0=0xa4;break;
case 3:P0=0xb0;break;
case 4:P0=0x99;break;
case 5:P0=0x92;break;
case 6:P0=0x82;break;
case 7:P0=0xf8;break;
case 8:P0=0x80;break;
case 9:P0=0x90;break;
case 10:P0=0xff;break;
case 11:P0=0xbf;break;
default:break;
}
ctrl(7);
}
void get(u8 a0,u8 a1,u8 a2,u8 a3,u8 a4,u8 a5,u8 a6,u8 a7)
{
a[0]=a0;a[1]=a1;a[2]=a2;a[3]=a3;a[4]=a4;a[5]=a5;a[6]=a6;a[7]=a7;
}
void Timer2Init(void) //1??@12.000MHz
{
AUXR &= 0xFB; //?????12T??
T2L = 0x18; //???????
T2H = 0xFC; //???????
AUXR |= 0x10; //???2????
IE2|=0x04;
EA=1;
}
void isr(void) interrupt 12
{
flag_display++;
flag_display=flag_display%8;
flag_key++;
flag_pcfad++;
tiaodong++;
if(key==7){key=0;timepoint++;timepoint=timepoint%2;}
if(timepoint==0)time++;
tiaodong=tiaodong%1600;
display(flag_display,a[flag_display]);
flag_pwm++;
flag_pwm=flag_pwm%10;
if(point==1)
{
if(moshi==0)jiange=t1;
if(moshi==1)jiange=t2;
if(moshi==2)jiange=t3;
if(moshi==3)jiange=t4;
point=0;
}
else
{
if(moshi==0)t1=jiange;
if(moshi==1)t2=jiange;
if(moshi==2)t3=jiange;
if(moshi==3)t4=jiange;
}
switch(moshi)
{
case 0:
{
if(flag_pwm<(3*liangdu))P0=((0xfe<<site)|(0x7f>>(7-site)));else P0=0xff;ctrl(4);
if(time/100>t1){time=0;site++;site=site%7;}
break;
}
case 1:
{
if(flag_pwm<(3*liangdu))P0=((0x7f>>site)|(0xfe<<(7-site)));else P0=0xff;ctrl(4);
if(time/100>t2){time=0;site++;site=site%7;}
break;
}
case 2:
{
if(flag_pwm<(3*liangdu))P0=(((0xfe<<site)|(0x7f>>(7-site)))&0x0f)|(((0x7f>>site)|(0xfe<<(7-site)))&0xf0);else P0=0xff;ctrl(4);
if(time/100>3){time=0;site++;site=site%4;}
break;
}
case 3:
{
if(flag_pwm<(3*liangdu))P0=(((0xfe<<site)|(0x7f>>(7-site)))&0x0f)|(((0x7f>>site)|(0xfe<<(7-site)))&0xf0);else P0=0xff;ctrl(4);
if(time/100>t4){time=0;if(site==0)site=site+4;site--;site=site%4;}
break;
}
default:break;
}
}
void Process(void)
{
if(flag_pcfad>100&&!writep)
{
writep=1;
if(moshi==0)atwrite(0xf1,t1);
if(moshi==1)atwrite(0xf2,t2);
if(moshi==2)atwrite(0xf3,t3);
if(moshi==3)atwrite(0xf4,t4);
}
if(flag_pcfad>200){writep=0;flag_pcfad=0;rb2=pcfad(0x43);liangdu=rb2/80;}
if(flag_key>=10){flag_key=0;key=key_sta();}
}
void main()
{
P0=0x00;ctrl(5);
P0=0xff;ctrl(4);
Timer2Init();
t1=atread(0xf1)%13;if(t1<4)t1=4;
t2=atread(0xf2)%13;if(t2<4)t2=4;
t3=atread(0xf3)%13;if(t3<4)t3=4;
t4=atread(0xf4)%13;if(t4<4)t4=4;
while(1)
{
Process();
if(key==6){key=0;xianshi++;xianshi=xianshi%3;}
switch(xianshi)
{
case 0:{get(10,10,10,10,10,10,10,10);break;}
case 1:
{
if(key==5){key=0;point=1;moshi++;moshi=moshi%4;}
if(key==4){key=0;if(moshi>0){point=1;moshi--;moshi=moshi%4;}}
if(tiaodong<=800)
{
if(jiange>=10)get(11,moshi+1,11,10,jiange/10,jiange%10,0,0);
else get(11,moshi+1,11,10,10,jiange%10,0,0);
}
else get(10,10,10,10,10,jiange%10,0,0);
break;
}
case 2:
{
if(key==5){key=0;if(jiange<12)jiange++;}
if(key==4){key=0;if(jiange>4)jiange--;}
if(tiaodong<=800)
{
if(jiange>=10)get(11,moshi+1,11,10,jiange/10,jiange%10,0,0);
else get(11,moshi+1,11,10,10,jiange%10,0,0);
}
else get(11,moshi+1,11,10,10,10,10,10);
break;
}
default:break;
}
if(xianshi==0&&val==4)get(10,10,10,10,10,10,11,liangdu);
}
}
#include <STC15F2K60S2.H>
#include "intrins.h"
#include "iic.h"
sbit sda=P2^1;
sbit scl=P2^0;
#define DELAY_TIME 5
//
static void I2C_Delay(unsigned char n)
{
do
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
}
while(n--);
}
//
void I2CStart(void)
{
sda = 1;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 0;
I2C_Delay(DELAY_TIME);
scl = 0;
}
//
void I2CStop(void)
{
sda = 0;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 1;
I2C_Delay(DELAY_TIME);
}
//
void I2CSendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++){
scl = 0;
I2C_Delay(DELAY_TIME);
if(byt & 0x80){
sda = 1;
}
else{
sda = 0;
}
I2C_Delay(DELAY_TIME);
scl = 1;
byt <<= 1;
I2C_Delay(DELAY_TIME);
}
scl = 0;
}
//
unsigned char I2CReceiveByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++){
scl = 1;
I2C_Delay(DELAY_TIME);
da <<= 1;
if(sda)
da |= 0x01;
scl = 0;
I2C_Delay(DELAY_TIME);
}
return da;
}
//
unsigned char I2CWaitAck(void)
{
unsigned char ackbit;
scl = 1;
I2C_Delay(DELAY_TIME);
ackbit = sda;
scl = 0;
I2C_Delay(DELAY_TIME);
return ackbit;
}
//
void I2CSendAck(unsigned char ackbit)
{
scl = 0;
sda = ackbit;
I2C_Delay(DELAY_TIME);
scl = 1;
I2C_Delay(DELAY_TIME);
scl = 0;
sda = 1;
I2C_Delay(DELAY_TIME);
}
u8 pcfad(u8 ctrl)
{
u8 ret;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(ctrl);
I2CWaitAck();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
ret=I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return ret;
}
u8 atread(u8 add)
{
u8 ret;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
ret=I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return ret;
}
void atwrite(u8 add,u8 dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(add);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
}
#ifndef _IIC_H
#define _IIC_H
typedef unsigned char u8;
typedef unsigned int u16;
u8 pcfad(u8 ctrl);
u8 atread(u8 add);
void atwrite(u8 add,u8 dat);
#endif
大家注意代码长度,最好不要超限