【冉冰的成长日记006】
。。。不知道说啥了,没啥难的吧,就一个光敏电阻不太常用,自己悟吧
#include<STC15F2K60S2.h>
#include<iic.h>
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
unsigned char code LED[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc1,0x8c}; //10,u 11,p
unsigned char LEDbuff[]={ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,};
unsigned int AD1,AD3;
int VP1=250,VP3=300;
unsigned char cnt1=0;
unsigned int cnt2=0;
bit yemian_falg=0,tongdao_falg=0;
bit check_S4=1,check_S5=1,check_S6=1,check_S7=1;
void Initial();
void Delay(unsigned int dat);
void Timer0Init();
void LEDlight();
void Display();
void main()
{
Initial();
Timer0Init();
while(1)
{
LEDlight();
if(cnt1>=100)
{
AD1_Init();AD1=AD1_Get();AD1=AD1*1.9608;
AD3_Init();AD3=AD3_Get();AD3=AD3*1.9608;
cnt1=0;
}
if(cnt2>=300)
{
Display();
cnt2=0;
}
}
}
void Initial()
{
P2=(P2&0x1F)|0x80;
P0=0xFF;
P2=(P2&0x1F)|0xA0;
P0=0x00;
P2=(P2&0x1F)|0xC0;
P0=0xFF;
P2=(P2&0x1F)|0xE0;
P0=0xFF;
}
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;
ET0=1;
}
//void Delay(unsigned int dat) //@12.000MHz
//{
// unsigned char i, j;
//
// for(;dat>0;dat--)
// {
// i = 12;
// j = 169;
// do
// {
// while (--j);
// } while (--i);
// }
//}
void Display_1()
{
LEDbuff[7]=LED[11];
LEDbuff[6]=LED[1];
LEDbuff[5]=LED[10];
LEDbuff[4]=LED[10];
LEDbuff[3]=LED[10];
LEDbuff[2]=LED[AD1/100]+0x80;
LEDbuff[1]=LED[AD1/10%10];
LEDbuff[0]=LED[AD1%10];
}
void Display_2()
{
LEDbuff[7]=LED[11];
LEDbuff[6]=LED[3];
LEDbuff[5]=LED[10];
LEDbuff[4]=LED[10];
LEDbuff[3]=LED[10];
LEDbuff[2]=LED[AD3/100]+0x80;
LEDbuff[1]=LED[AD3/10%10];
LEDbuff[0]=LED[AD3%10];
}
void Display_3()
{
LEDbuff[7]=LED[12];
LEDbuff[6]=LED[1];
LEDbuff[5]=LED[10];
LEDbuff[4]=LED[10];
LEDbuff[3]=LED[10];
LEDbuff[2]=LED[VP1/100]+0x80;
LEDbuff[1]=LED[VP1/10%10];
LEDbuff[0]=LED[VP1%10];
}
void Display_4()
{
LEDbuff[7]=LED[12];
LEDbuff[6]=LED[3];
LEDbuff[5]=LED[10];
LEDbuff[4]=LED[10];
LEDbuff[3]=LED[10];
LEDbuff[2]=LED[VP3/100]+0x80;
LEDbuff[1]=LED[VP3/10%10];
LEDbuff[0]=LED[VP3%10];
}
void Display()
{
if(yemian_falg==0)
{
if(tongdao_falg==0) Display_1();
if(tongdao_falg==1) Display_2();
}
if(yemian_falg==1)
{
if(tongdao_falg==0) Display_3();
if(tongdao_falg==1) Display_4();
}
}
void KEY_S4()
{
if(S4!=check_S4)
{
if(check_S4==0)
{
tongdao_falg=~tongdao_falg;
}
check_S4=S4;
}
}
void KEY_S5()
{
if(S5!=check_S5)
{
if(check_S5==0)
{
yemian_falg=~yemian_falg;
}
check_S5=S5;
}
}
void KEY_S6()
{
if(S6!=check_S6)
{
if(check_S6==0)
{
if(yemian_falg==1)
{
if(tongdao_falg==0)
{
VP1 = VP1 + 20;
if(VP1>500) VP1=500;
}
if(tongdao_falg==1)
{
VP3 = VP3 + 20;
if(VP3>500) VP3=500;
}
}
}
check_S6=S6;
}
}
void KEY_S7()
{
if(S7!=check_S7)
{
if(check_S7==0)
{
if(yemian_falg==1)
{
if(tongdao_falg==0)
{
VP1 = VP1 - 20;
if(VP1<0) VP1=0;
}
if(tongdao_falg==1)
{
VP3 = VP3 - 20;
if(VP3<0) VP3=0;
}
}
}
check_S7=S7;
}
}
void KEY()
{
KEY_S4();
KEY_S5();
KEY_S6();
KEY_S7();
}
void LEDlight()
{
static unsigned char PLED=0x00;
if(AD1>VP1) PLED=PLED|0x01;else PLED=PLED&0xFE;
if(AD3>VP3) PLED=PLED|0x02;else PLED=PLED&0xFD;
if(tongdao_falg==0) PLED=PLED|0x04;else PLED=PLED&0xFB;
if(yemian_falg==0) PLED=PLED|0x08;else PLED=PLED&0xF7;
if(AD1>AD3) PLED=PLED|0x10;else PLED=PLED&0xEF;
P2=(P2&0x1F)|0x80; P0=~PLED;
}
void InterruptTimer0() interrupt 1
{
static unsigned char i=0;
switch(i)
{
case 0:P2=0xC0;P0=0x80;P2=0xE0;P0=LEDbuff[0];P2=0x00;i++;break;
case 1:P2=0xC0;P0=0x40;P2=0xE0;P0=LEDbuff[1];P2=0x00;i++;break;
case 2:P2=0xC0;P0=0x20;P2=0xE0;P0=LEDbuff[2];P2=0x00;i++;break;
case 3:P2=0xC0;P0=0x10;P2=0xE0;P0=LEDbuff[3];P2=0x00;i++;break;
case 4:P2=0xC0;P0=0x08;P2=0xE0;P0=LEDbuff[4];P2=0x00;i++;break;
case 5:P2=0xC0;P0=0x04;P2=0xE0;P0=LEDbuff[5];P2=0x00;i++;break;
case 6:P2=0xC0;P0=0x02;P2=0xE0;P0=LEDbuff[6];P2=0x00;i++;break;
case 7:P2=0xC0;P0=0x01;P2=0xE0;P0=LEDbuff[7];P2=0x00;i=0;break;
default:break;
}
KEY();
cnt1++;
cnt2++;
}
main.c
我都懒得解释了,就一个点亮led的地方用了点技巧,其他都很正常的操作,自己看吧,就不备注了。。。。(p.s.:其实是因为前几篇没人看,所以我懒得备注了,我自己又都明白,所以就没必要了,qaq假装哭的很小声)
#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);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
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;
}
//通过I2C总线发送数据
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;
}
//从I2C总线上接收数据
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;
}
void AD3_Init()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x43);
IIC_WaitAck();
IIC_Stop();
}
unsigned char AD3_Get()
{
unsigned char temp=0;
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
void AD1_Init()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x41);
IIC_WaitAck();
IIC_Stop();
}
unsigned char AD1_Get()
{
unsigned char temp=0;
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
iic.c 把通道3改成通道1就行了
#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);
void AD3_Init();
unsigned char AD3_Get();
void AD1_Init();
unsigned char AD1_Get();
#endif
iic.h
结束
不过该有的礼仪还是要有的,感谢各位观看