版主
最后登录1970-1-1
在线时间 小时
注册时间2014-11-16
|
#include <reg52.h>
#include <intrins.h>
#include <math.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P1^7;
sbit rw=P1^6;
sbit en=P1^5;
sbit pwm1=P2^0;
sbit pwm2=P2^1;
sbit pwm3=P2^2;
sbit xh=P1^3;
sbit key1=P2^3;
sbit key2=P2^4;
sbit SCL=P1^0;
sbit SDA=P1^1;
uchar x,q,numm;
uchar gg[13]={"1aÕÕÇ¿¶è£o000"};
uchar tt[16]={"èËìåoìía¸Dó|μ÷1a"};
uchar rr[11]={"èËá÷á¿£o000"};
void delayg(uint z)
{
uchar i;
for(i=z;i>0;i--)
{
_nop_();
_nop_();
_nop_();
//_nop_();
//_nop_();
//_nop_();
//_nop_();
//_nop_();
}
}
void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
#define SlaveAddress 0x46
typedef unsigned char BYTE;
typedef unsigned short WORD;
BYTE BUF[8];
int dis_data;
void delay_nms(unsigned int k)
{
uint i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
void key()
{
if(key1==0)
{
delay(100);
if(key1==0)
{
delay(5);
x++;
q++;
}
}
if(key2==0)
{
delay(100);
if(key2==0)
{
delay(5);
x--;
q--;
}
}
}
void jiance()
{
if(xh==1)
{
delay(1950);
if(xh==1)
{
x++;
q++;
}
}
}
void qudong()
{
uchar i,j;
for(i=x;i>0;i--)
{
pwm1=1;
pwm2=1;
pwm3=1;
delayms(100);
}
for(j=(10-x);j>0;j--)
{
pwm1=0;
pwm2=0;
pwm3=0;
delayg(100);
}
}
void Delay5us()
{
_nop_();
_nop_();
_nop_();
_nop_();
//_nop_();
//_nop_();
//_nop_();
//_nop_();
}
void Delay5ms()
{
uchar n=560;
while (n--);
}
void BH1750_Start()
{
SDA = 1;
SCL = 1;
Delay5us();
SDA = 0;
Delay5us();
SCL = 0;
}
void BH1750_Stop()
{
SDA = 0;
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}
void BH1750_SendACK(bit ack)
{
SDA = ack;
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
}
bit BH1750_RecvACK()
{
SCL = 1;
Delay5us();
CY = SDA;
SCL = 0;
Delay5us();
return CY;
}
void BH1750_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++)
{
dat <<= 1;
SDA = CY;
SCL = 1;
Delay5us();
SCL = 0;
Delay5us();
}
BH1750_RecvACK();
}
BYTE BH1750_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1;
for (i=0; i<8; i++)
{
dat <<= 1;
SCL = 1;
Delay5us();
dat |= SDA;
SCL = 0;
Delay5us();
}
return dat;
}
void Single_Write_BH1750(uchar REG_Address)
{
BH1750_Start();
BH1750_SendByte(SlaveAddress);
BH1750_SendByte(REG_Address);
BH1750_Stop();
}
void Multiple_read_BH1750()
{ uchar i;
BH1750_Start();
BH1750_SendByte(SlaveAddress+1);
for (i=0; i<3; i++)
{
BUF[i] = BH1750_RecvByte();
if (i == 3)
{
BH1750_SendACK(1);
}
else
{
BH1750_SendACK(0);
}
}
BH1750_Stop();
Delay5ms();
}
void Init_BH1750()
{
Single_Write_BH1750(0x01);
}
uchar huoqu()
{
float temp;
Single_Write_BH1750(0x01);
Single_Write_BH1750(0x10);
delay_nms(180);
Multiple_Read_BH1750();
dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];
temp=(float)dis_data/1.2;
return temp;
}
void change ()
{
rr[8]=q/100+0x30;
rr[9]=q/10%10+0x30;
rr[10]=q%10+0x30;
}
void change1()
{
uchar temp;
temp=(uchar)huoqu();
gg[10]=(uchar)temp/100+0x30;
gg[11]=(uchar)temp%100/10+0x30;
gg[12]=(uchar)temp%10+0x30;
}
void writecom (uchar com)
{
rs=0;
en=0;
P0=com;
delayms(1);
en=1;
delayms(1);
en=0;
}
void writedata (uchar date)
{
rs=1;
en=0;
P0=date;
delayms(1);
en=1;
delayms(1);
en=0;
}
void init12864 ()
{
rw=0;
writecom(0x30);
delayms(1);
writecom(0x01);
delayms(1);
writecom(0x06);
delayms(1);
writecom(0x0c);
delayms(1);
}
void place (uchar x,uchar y)
{
uchar pos;
if(x==0)
x=0x80;
if(x==1)
x=0x90;
if(x==2)
x=0x88;
if(x==3)
x=0x98;
pos=x+y;
writecom(pos);
}
void xianshi()
{
uchar i;
place(1,5);
for(i=10;i<13;i++)
writedata(gg[i]);
place(2,4);
for(i=8;i<11;i++)
writedata(rr[i]);
}
void tiaozheng()
{
if(x==11)
x=0;
}
void initzd()
{
TMOD=0X01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
main()
{
uchar i;
Init_BH1750();
init12864();
initzd();
place(2,0);
for(i=0;i<8;i++)
writedata(rr[i]);
place(0,0);
for(i=0;i<16;i++)
writedata(tt[i]);
place(1,0);
for(i=0;i<10;i++)
writedata(gg[i]);
while(1)
{
//xianshi();
//jiance();
//qudong();
change();
key();
tiaozheng();
qudong();
}
}
void enter0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
//xianshi();
numm++;
if(numm==4)
{
numm=0;
change1();
xianshi();
}
}
程序哪儿处理的不够好
|
|