other.c
#include
void Delay2ms(void)
{
unsigned char i, j;
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
unsigned char seg_tran(int number)
{
unsigned char date;
switch(number)
{
case 0 : date = 0xc0 ; break;
case 1 : date = 0xf9 ; break;
case 2 : date = 0xa4 ; break;
case 3 : date = 0xb0 ; break;
case 4 : date = 0x99 ; break;
case 5 : date = 0x92 ; break;
case 6 : date = 0x82 ; break;
case 7 : date = 0xf8 ; break;
case 8 : date = 0x80 ; break;
case 9 : date = 0x90 ; break;
case 10 : date = 0xbf ; break;//-
case 11 : date = 0xc6 ; break;//c
case 12 : date = 0x8c ; break;//p
case 13 : date = 0x86 ; break;//e
case 14 : date = 0xc7 ; break;//L
case 15 : date = 0xc8 ; break;//a
case 16 : date = 0xff ; break;//kong
}
return date;
}
void seg_disp(int adr,int number)
{
unsigned char seg_adr[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
P2 = P2&0x1f|0xe0;
P2 = P2&0x1f;
P0 = seg_adr[adr];
P2 = P2&0x1f|0xc0;
P2 = P2&0x1f;
P0 = seg_tran(number);
P2 = P2&0x1f|0xe0;
P2 = P2&0x1f;
}
unsigned char key_scan(void)
{
unsigned char key_mark;
P3 = 0x0f;
if(P30 == 0) key_mark = 7;
else if(P31 == 0) key_mark = 6;
else if(P32 == 0) key_mark = 5;
else if(P33 == 0) key_mark = 4;
else key_mark = 0;
return key_mark;
}
void close_perl(void)
{
P0 = 0xff;
P2 = P2&0x1f|0x80;
P2 = P2&0x1f;
P0 = 0x00;
P2 = P2&0x1f|0xa0;
P2 = P2&0x1f;
}
void Timer0Init(void) //10毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xF0; //设置定时初值
TH0 = 0xD8; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}
void Timer1Init(void) //10毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xF0; //设置定时初值
TH1 = 0xD8; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
void Delay10us(void) //@11.0592MHz
{
unsigned char i;
_nop_();
i = 25;
while (--i);
}
void Delay50us(void) //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 1;
j = 134;
do
{
while (--j);
} while (--i);
}
void Delay250us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 3;
j = 173;
do
{
while (--j);
} while (--i);
}
other.h
#ifndef __OTHER_H_
#define __OTHER_H_
#include
#include
void Delay2ms(void);
unsigned char seg_tran(int number);
void seg_disp(int adr,int number);
unsigned char key_scan(void);
void close_perl(void);
void Timer0Init(void);
void Timer1Init(void);
void Delay10us(void);
void Delay50us(void);
void Delay250us(void);
#endif
iic.c
unsigned char adc_pcf(void)
{
unsigned char date;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x43);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
date = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
return date;
}
void ee_write(unsigned char adress,unsigned char date)
{
EA = 0;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(adress);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
EA = 1;
}
unsigned char ee_read(unsigned char adress)
{
unsigned char date;
EA = 0;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(adress);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
date = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();
EA = 1;
return date;
}
iic.h
#ifndef __IIC_H_
#define __IIC_H_
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
//void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char adc_pcf(void);
void ee_write(unsigned char adress,unsigned char date);
unsigned char ee_read(unsigned char adress);
#endif
main.c
#include
#include
#define led1 0x01
#define led2 0x02
#define led3 0x04
#define led4 0x08
#define led5 0x10
#define led6 0x20
#define led7 0x40
#define led8 0x80
unsigned char key_old;
unsigned char adc;
float adc_f;
unsigned int adc_100;
unsigned char led_time = 40;
int led_stateA = 0;//流转组别
unsigned char led_stateB = 0;//每个组别的状态
unsigned char timer0_up;
unsigned char timer1_up;
unsigned char State1[8] = {led1,led2,led3,led4,led5,led6,led7,led8};
unsigned char State2[8] = {led8,led7,led6,led5,led4,led3,led2,led1};
unsigned char State3[4] = {led1|led8,led2|led7,led3|led6,led4|led5};
unsigned char State4[4] = {led4|led5,led3|led6,led2|led7,led1|led8};
unsigned char change_mode;
unsigned char light_mode;
bit seg_bit;
bit led_start;
void led_pro(void)
{
P0 = 0xff;
if(led_stateA == 0)
{
P0 = (P0|0xff)&(~State1[led_stateB]);
P2 = P2&0x1f|0x80;
P2 = P2&0x1f;
if(led_stateB == 8)
{
led_stateB = 0;
led_stateA++;
}
}
else if(led_stateA == 1)
{
P0 = (P0|0xff)&(~State2[led_stateB]);
P2 = P2&0x1f|0x80;
P2 = P2&0x1f;
if(led_stateB == 8)
{
led_stateB = 0;
led_stateA++;
}
}
else if(led_stateA == 2)
{
P0 = (P0|0xff)&(~State3[led_stateB]);
P2 = P2&0x1f|0x80;
P2 = P2&0x1f;
if(led_stateB == 4)
{
led_stateB = 0;
led_stateA++;
}
}
else if(led_stateA == 3)
{
P0 = (P0|0xff)&(~State4[led_stateB]);
P2 = P2&0x1f|0x80;
P2 = P2&0x1f;
if(led_stateB == 4)
{
led_stateB = 0;
led_stateA++;
}
}
switch(light_mode)
{
case 1:
Delay250us();
break;
case 2:
Delay50us();
break;
case 3:
Delay10us();
break;
case 4:
break;
}
close_perl();
switch(light_mode)
{
case 1:
Delay250us();
break;
case 2:
Delay50us();
break;
case 3:
Delay10us();
break;
case 4:
break;
}
if(led_stateA == 4)
{
led_stateA = 0;
}
}
void seg_pro(void)
{
adc = adc_pcf();
adc_f = (float)adc/255*5;
adc_100 = adc_f*100;
ee_write(0x01,led_time);
if(adc < 64) light_mode = 1;
else if((adc >= 64)&&(adc < 128)) light_mode = 2;
else if((adc >= 128)&&(adc < 192)) light_mode = 3;
else if(adc > 192) light_mode = 4;
switch(change_mode)
{
case 0:
seg_disp(0,16);Delay2ms();
seg_disp(1,16);Delay2ms();
seg_disp(2,16);Delay2ms();
seg_disp(3,16);Delay2ms();
seg_disp(4,16);Delay2ms();
seg_disp(5,16);Delay2ms();
seg_disp(6,16);Delay2ms();
seg_disp(7,16);Delay2ms();
break;
case 1:
seg_disp(0,10);Delay2ms();
if(seg_bit == 0)
{
seg_disp(1,led_stateA+1);Delay2ms();
}
else
{
seg_disp(1,16);Delay2ms();
}
seg_disp(2,10);Delay2ms();
if(led_time >= 100) seg_disp(4,led_time/100);Delay2ms();
seg_disp(5,led_time%100/10);Delay2ms();
seg_disp(6,0);Delay2ms();
seg_disp(7,0);Delay2ms();
break;
case 2:
seg_disp(0,10);Delay2ms();
seg_disp(1,led_stateA+1);Delay2ms();
seg_disp(2,10);Delay2ms();
if(seg_bit == 0)
{
if(led_time > 100) seg_disp(4,led_time/100);Delay2ms();
seg_disp(5,led_time%100/10);Delay2ms();
seg_disp(6,0);Delay2ms();
seg_disp(7,0);Delay2ms();
}
else
{
seg_disp(4,16);Delay2ms();
seg_disp(5,16);Delay2ms();
seg_disp(6,16);Delay2ms();
seg_disp(7,16);Delay2ms();
}
break;
}
while((key_scan() == 4)&&(change_mode == 0))
{
adc = adc_pcf();
if(adc < 64) light_mode = 1;
else if((adc >= 64)&&(adc < 128)) light_mode = 2;
else if((adc >= 128)&&(adc < 192)) light_mode = 3;
else if(adc > 192) light_mode = 4;
seg_disp(6,10);Delay2ms();
seg_disp(7,light_mode);Delay2ms();
led_pro();
}
}
void key_pro(void)
{
unsigned char key_vol,key_down;
key_vol = key_scan();
key_down = key_vol&(key_vol^key_old);
key_old = key_vol;
switch(key_down)
{
case 7:
ET0 = ~ET0;
led_start = 1;
break;
case 6:
change_mode ++;
if(change_mode == 3)
{
change_mode = 0;
}
led_start = 1;
break;
case 5:
switch(change_mode)
{
case 1:
led_stateA ++;
if(led_stateA == 4) led_stateA = 0;
break;
case 2:
led_time = led_time + 10;
if(led_time > 120) led_time = 40;
break;
}
led_start = 1;
break;
case 4:
switch(change_mode)
{
case 1:
led_stateA --;
if(led_stateA < 0) led_stateA = 3;
break;
case 2:
led_time = led_time - 10;
if(led_time < 40) led_time = 120;
break;
}
led_start = 1;
break;
}
}
void main(void)
{
Timer0Init();
close_perl();
led_time = ee_read(0x01);
Timer1Init();
while(1)
{
key_pro();
if(led_start == 1)
{
seg_pro();
led_pro();
}
}
}
void timer0(void) interrupt 1
{
if(led_start == 1)
{
timer0_up++;
if(timer0_up == led_time)
{
timer0_up = 0;
led_stateB ++;
}
}
}
void timer1(void) interrupt 3
{
timer1_up ++;
if(timer1_up == 80)
{
timer1_up = 0;
seg_bit = ~seg_bit;
}
}