main.c
#include"stc15f2k60s2.h"
#include"iic.h"
#include"intrins.h"
unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,~0x40,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt;
unsigned char mode=0,Lmode=1,liangmode=0;
unsigned int gap1=400,gap2=400,gap3=400,gap4=400;
unsigned char Rb2,ADtt;
unsigned int ttt;
bit xieyiciflag;
bit startflag,yiflag;
unsigned char PWM,PWMtt,PWMmode;
float aaa;
void display();
void keyscan();
void xieyici();
void Delay2ms()
{
unsigned char i, j;
i = 24;
j = 85;
do
{
while (--j);
} while (--i);
}
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x20;
TH1 = 0xD1;
TF1 = 0;
TR1 = 1;
ET1=1;
EA=1;
}
void Timer1() interrupt 3
{
unsigned char P0_kepper=0,P2_kepper=0;
P0_kepper=P0; P2_kepper=P2; P2=P2&0x1f;
display();
keytt++; if(keytt==10) {keytt=0; keyscan();}
ADtt++;
if(Lmode==1 && startflag==1) {ttt++; if(ttt==gap1) {yiflag=1;ttt=0;}}
else if(Lmode==2 && startflag==1){ttt++; if(ttt==gap2) {yiflag=1;ttt=0;}}
P0=P0_kepper; P2=P2_kepper;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x50;
TH0 = 0xFB;
TF0 = 0;
TR0 = 1;
ET0=1;
}
void Timer0() interrupt 1
{
if(startflag==1)
{
PWMtt++;
if(PWMtt<=PWM) PWMmode=1;
else if(PWMtt>PWM&&PWMtt<100) PWMmode=2;
else if(PWMtt==100) {PWMtt=0; PWMmode=3;}
}
}
void display()
{
P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
if(smgcom==8) smgcom=0;
}
void keyscan()
{
unsigned char dat,trg,cnt;
dat=P3^0xff;
trg=dat&(dat^cnt);
cnt=dat;
if(trg&0x01) startflag=~startflag;
else if(trg&0x02)
{
mode++;
if(mode==3)
{
mode=0;
xieyiciflag=1;
}
}
else if(trg&0x04)
{
if(mode==1){ttt=0; yiflag=0; Lmode++; if(Lmode==5) Lmode=4;}
else if(mode==2)
{
if(Lmode==1) {gap1=gap1+100; if(gap1==1300) gap1=1200;}
else if(Lmode==2){gap2=gap2+100; if(gap2==1300) gap2=1200;}
else if(Lmode==3){gap3=gap3+100; if(gap3==1300) gap3=1200;}
else if(Lmode==4){gap4=gap4+100; if(gap4==1300) gap4=1200;}
}
}
else if(trg&0x08)
{
if(mode==1){ttt=0; yiflag=0; Lmode--; if(Lmode==0) Lmode=1;}
else if(mode==2)
{
if(Lmode==1) {gap1=gap1-100; if(gap1==300) gap1=400;}
else if(Lmode==2){gap2=gap2-100; if(gap2==300) gap2=400;}
else if(Lmode==3){gap3=gap3-100; if(gap3==300) gap3=400;}
else if(Lmode==4){gap4=gap4-100; if(gap4==300) gap4=400;}
}
}
if(mode==0 && dat&0x08) mode=6;
else if(mode==6 && dat==0) mode=0;
}
void duyici()
{
gap1=10*EEPROM_read(0x01); Delay2ms();
gap2=10*EEPROM_read(0x02); Delay2ms();
gap3=10*EEPROM_read(0x03); Delay2ms();
gap4=10*EEPROM_read(0x04); Delay2ms();
}
void xieyici()
{
if(xieyiciflag==1)
{
EEPROM_write(0x01,gap1/10); Delay2ms();
EEPROM_write(0x02,gap2/10); Delay2ms();
EEPROM_write(0x03,gap3/10); Delay2ms();
EEPROM_write(0x04,gap4/10); Delay2ms();
xieyiciflag=0;
}
}
void ADC()
{
if(ADtt==200)
{
ADtt=0;
Rb2=AD_read(0x03);
}
if(Rb2<=64) {liangmode=1; PWM=25;}
else if(Rb2>64 && Rb2<=128) {liangmode=2; PWM=50;}
else if(Rb2>128 && Rb2<=192) {liangmode=3; PWM=75;}
else if(Rb2>192 && Rb2<=255) {liangmode=4; PWM=100;}
}
void xianshi()
{
if(mode==0){smgbuffer[0]=smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=smgbuffer[6]=smgbuffer[7]=11;}
else if(mode==1){smgbuffer[0]=smgbuffer[2]=10;smgbuffer[1]=Lmode;smgbuffer[3]=11;}
if(mode==1 || mode==2)
{
if(Lmode==1)
{
if(gap1>=1000){smgbuffer[4]=gap1/1000;smgbuffer[5]=gap1%1000/100;smgbuffer[6]=gap1%1000%100/10;smgbuffer[7]=gap1%10;}
else if(gap1<1000){smgbuffer[4]=11;smgbuffer[5]=gap1/100;smgbuffer[6]=gap1%100/10;smgbuffer[7]=gap1%10;}
}
else if(Lmode==2)
{
if(gap2>=1000){smgbuffer[4]=gap2/1000;smgbuffer[5]=gap2%1000/100;smgbuffer[6]=gap2%1000%100/10;smgbuffer[7]=gap2%10;}
else if(gap2<1000){smgbuffer[4]=11;smgbuffer[5]=gap2/100;smgbuffer[6]=gap2%100/10;smgbuffer[7]=gap2%10;}
}
else if(Lmode==3)
{
if(gap3>=1000){smgbuffer[4]=gap3/1000;smgbuffer[5]=gap3%1000/100;smgbuffer[6]=gap3%1000%100/10;smgbuffer[7]=gap3%10;}
else if(gap3<1000){smgbuffer[4]=11;smgbuffer[5]=gap3/100;smgbuffer[6]=gap3%100/10;smgbuffer[7]=gap3%10;}
}
else if(Lmode==4)
{
if(gap4>=1000){smgbuffer[4]=gap4/1000;smgbuffer[5]=gap4%1000/100;smgbuffer[6]=gap4%1000%100/10;smgbuffer[7]=gap4%10;}
else if(gap4<1000){smgbuffer[4]=11;smgbuffer[5]=gap4/100;smgbuffer[6]=gap4%100/10;smgbuffer[7]=gap4%10;}
}
}
else if(mode==6)
{
smgbuffer[0]=smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=11;
smgbuffer[6]=10;
smgbuffer[7]=liangmode;
}
}
void LED()
{
if(startflag==1)
{
if(Lmode==1) {if(yiflag==1){P0=_crol_(0xfe,aaa++); P2=P2&0x1f|0x80; P2=P2&0x1f; yiflag=0;}}
else if(Lmode==2){if(yiflag==1){P0=_cror_(0x7f,aaa++); P2=P2&0x1f|0x80; P2=P2&0x1f; yiflag=0;}}
}
else if(startflag==0)
{
P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
}
}
void yidong()
{
if(PWMmode==1||PWMmode==3)
{
if(Lmode==1) {P0=_crol_(0xfe,aaa);P2=P2&0x1f|0x80;P2=P2&0x1f;}
else if(Lmode==2){P0=_cror_(0x7f,aaa);P2=P2&0x1f|0x80;P2=P2&0x1f;}
}
else if(PWMmode==2){P0=0xff;P2=P2&0x1f|0x80;P2=P2&0x1f;}
}
void main()
{
P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
duyici();
Timer0Init();
Timer1Init();
while(1)
{
xieyici();
ADC();
yidong();
LED();
xianshi();
}
}
iic.c
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
unsigned char AD_read(unsigned char add)
{
unsigned char AD_temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
AD_temp=IIC_RecByte();
IIC_Stop();
return AD_temp;
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char EEPROM_temp;
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xA1);
IIC_WaitAck();
EEPROM_temp=IIC_RecByte();
IIC_Stop();
return EEPROM_temp;
}
void EEPROM_write(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();
}