同时进行光敏电阻和滑动变阻器采样时,两者的值是反着的。
解决方法:直接将两个值交换即可。
#include <STC15F2K60S2.H>
#include "iic.h"
code unsigned char SMG[] = { ~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F};
unsigned int v,light;
void Delay_ms(unsigned int t) //@12.000MHz
{
while(t--)
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
void Select_Hc573(char n)
{
switch(n)
{
case 4:P2 = P2 & 0x1f | 0x80;break;
case 5:P2 = P2 & 0x1f | 0xa0;break;
case 6:P2 = P2 & 0x1f | 0xc0;break;
case 7:P2 = P2 & 0x1f | 0xe0;break;
}
P2 = P2 & 0x1f;
}
void Sys_Init()
{
P0 = 0x00;
Select_Hc573(5);
P0 = 0xff;
Select_Hc573(4);
}
void Select_Bit(unsigned char pos,dat)
{
P0 = 0x01 << pos;
Select_Hc573(6);
P0 = dat;
Select_Hc573(7);
Delay_ms(1);
P0 = 0xff;
Select_Hc573(7);
}
unsigned int Read_Light()
{
unsigned int temp;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x01);
I2CWaitAck();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
temp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return temp;
}
unsigned int Read_V()
{
unsigned int temp;
I2CStart();
I2CSendByte(0x90);
I2CWaitAck();
I2CSendByte(0x03);
I2CWaitAck();
I2CStart();
I2CSendByte(0x91);
I2CWaitAck();
temp = I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return temp;
}
void Display()
{
//L
Select_Bit(0,~0x38);
Select_Bit(1,SMG[light / 100] - 0x80);
Select_Bit(2,SMG[light / 10 % 10]);
Select_Bit(3,SMG[light % 10]);
//V
Select_Bit(4,~0x3e);
Select_Bit(5,SMG[v / 100] - 0x80);
Select_Bit(6,SMG[v / 10 % 10]);
Select_Bit(7,SMG[v % 10]);
}
void main()
{
Sys_Init();
while(1)
{
v = Read_V() / 51.0 * 100;
light = Read_Light() / 51.0 * 100;
// 解决方法,交换两个变量即可
// light = Read_V() / 51.0 * 100;
// v = Read_Light() / 51.0 * 100;
Display();
}
}