2019-10-15 20:20发布
小陀螺爱炒蛋 发表于 2016-11-7 12:15 ISR中不要做延时之类的操作,越短越好~如果希望达到一个舒服的应用程序开发环境,可以上OS,在ISR中触发后 ...
最多设置5个标签!
/***********************************************
function name: iic_delay()
function:
input:
output:
describe:
***********************************************/
static void iic_delay()
{
delay_us(2);
}
/***********************************************
function name: I2CStart()
function:
input:
output:
describe:
***********************************************/
void I2CStart()
{
if(BH1750_SDIO()==0x00)
{
BH1750_SDIO_Output_Set();
}
iic_delay();
BH1750_SCK=positive;
BH1750_SDO=positive;
iic_delay();
BH1750_SDO=negative;
}
/***********************************************
function name: I2CStop()
function:
input:
output:
describe:
***********************************************/
void I2CStop(void)
{
if(BH1750_SDIO()==0x00)
{
BH1750_SDIO_Output_Set();
}
iic_delay();
BH1750_SCK=positive;
BH1750_SDO=negative;
iic_delay();
BH1750_SDO=positive;
}
/***********************************************
function name: write_BH1750()
function:
input:
output:
describe:
***********************************************/
uchar write_BH1750(uchar data)
{
uchar i;
if(BH1750_SDIO()==0x00)
{
BH1750_SDIO_Output_Set();
}
BH1750_SCK=negative;
iic_delay();
for(i=0;i<8;i++)
{
if(data&0x80)
BH1750_SDO=positive;
else
BH1750_SDO=negative;
BH1750_SCK=positive;
iic_delay();
BH1750_SCK=negative;
iic_delay();
data<<=1;
}
if(BH1750_SDIO())
{
BH1750_SDIO_Input_Set();
}
BH1750_SCK=positive;
for(i=1;i<100;i++)
{
if(BH1750_SDI==0)
{
i=0;
break;
}
}
BH1750_SCK=negative;
return i;
}
/***********************************************
function name: read_BH1750()
function:
input:
output:
describe:
***********************************************/
uchar read_BH1750(uchar ack)
{
uchar i,data=0;
if(BH1750_SDIO()!=0)
{
BH1750_SDIO_Input_Set();
}
BH1750_SCK=negative;
iic_delay();
for(i=0;i<8;i++)
{
BH1750_SCK=positive;
data<<=1;
iic_delay();
data|=BH1750_SDI;
BH1750_SCK=negative;
iic_delay();
}
if(BH1750_SDIO()==0x00)
{
BH1750_SDIO_Output_Set();
}
iic_delay();
BH1750_SDO=ack;
BH1750_SCK=positive;
iic_delay();
BH1750_SCK=negative;
return data;
}
/***********************************************
function name: init_BH1750
function:
input:
output:
describe:
***********************************************/
// void init_BH1750()
// {
// I2CStart();
// write_BH1750(SLAVE_ADDRESS);
// write_BH1750(0x01);
// I2CStop();
// I2CStart();
// write_BH1750(SLAVE_ADDRESS);
// write_BH1750(0x01);
// I2CStop();
// }
/***********************************************
function name: getBH1750Sensor
function:
input:
output:
describe:
***********************************************/
uchar getBH1750Sensor(u16 *lightSensorVal,u16 adcTimes,u16 abortVal,u16 delay)
{
u16 tmp=0;
u16 v0,v1;
static u16 cnt=0;
static u16 adcTimeCnt=0;
static u16 *adc=NULL;
while(1)
{
if(cnt<delay)
{
++cnt;
}
else if(cnt==delay)
{
if(adc)
{
free(adc);
adc=NULL;
}
I2CStart(); //init BH1750
if(write_BH1750(SLAVE_ADDRESS))
{
adcTimeCnt=0;
break;
}
if(write_BH1750(0x01))
{
adcTimeCnt=0;
break;
}
I2CStop();
I2CStart(); //open BH1750 power
if(write_BH1750(SLAVE_ADDRESS))
{
adcTimeCnt=0;
break;
}
if(write_BH1750(0x01))
{
adcTimeCnt=0;
break;
}
I2CStop();
I2CStart();//startup BH1750 convert
if(write_BH1750(SLAVE_ADDRESS))
{
adcTimeCnt=0;
break;
}
if(write_BH1750(0x10))
{
adcTimeCnt=0;
break;
}
I2CStop();
if(adc==NULL)
{
adc=(u16*)calloc(adcTimes,sizeof(u16));
}
if(adc)
{
++cnt;
}
}
else
{
++cnt;
if(cnt>=delay+25)//(delay+25)*节拍时间到
{
I2CStart();
if(write_BH1750(SLAVE_ADDRESS+1))
{
cnt=delay;
adcTimeCnt=0;
break;
}
v0=read_BH1750(0);
v1=read_BH1750(1);
I2CStop();
*(adc+adcTimeCnt)=(((v0<<8)|v1)/1.2f);
if(*(adc+adcTimeCnt)>5000)
*(adc+adcTimeCnt)=5000;
cnt=0;
adcTimeCnt++;
if(adcTimeCnt==adcTimes)
{
for(adcTimeCnt=0;adcTimeCnt<adcTimes-1;adcTimeCnt++)//排序
{
for(cnt=adcTimeCnt+1;cnt<adcTimes;cnt++)
{
if(*(adc+adcTimeCnt)>*(adc+cnt))//升序排列
{
tmp=*(adc+adcTimeCnt);
*(adc+adcTimeCnt)=*(adc+cnt);
*(adc+cnt)=tmp;
}
}
}
tmp=0;
for(adcTimeCnt=abortVal;adcTimeCnt<adcTimes-abortVal;adcTimeCnt++)
tmp+=*(adc+adcTimeCnt);
*lightSensorVal=tmp/(adcTimes-2*abortVal);
adcTimeCnt=0;
cnt=0;
if(adc)
{
free(adc);
adc=NULL;
}
tmp = 1;
}
}
}
break;
}
return tmp;
}
////////////////////
void TaskDelay()
{
if(Ticker)//5ms节拍到,扫描任务,任务中最多有些几个us或者几十个us级别的delay,
{
if(fTask1Start)
{
backupKeyKeep=scan_key();//扫描按键
}
if(fTask2Start)
{
if(getBH1750Sensor(&lightSensorVal,7,2,4))//采样完成
{
if(lightSensorVal>500)
lightSensorVal=500;
if(((lightSensorVal>PWMVal*5u)&&((lightSensorVal-PWMVal*5u)<10))||(((lightSensorVal<PWMVal*5u))&&((PWMVal*5u-lightSensorVal)<10)))
lightSensorVal=PWMVal*5u;
}
}
Ticker=0;
}
}
/*
*/
void main(void)
{
while(1)
{
TaskDelay();
}
你可以用一个定时器做个节拍,做几个任务,每个任务是独立的,每个节拍每个任务都能扫描到,给你一个BH1750的例程给你看看,希望对你有所帮助。
嗯嗯,好热心,感动,一开始还不了解,现在已经找到解决方法了,没有用OS,还不会用OS,采用的是方法是设置标志变量,外部中断改变这个变量的方法
一周热门 更多>