void Single_Write_BH1750(unsigned char REG_Address)
{
BH1750_Start();//起始信號
BH1750_SendByte(SlaveAddress);//發(fā)送設備地址+寫信號
BH1750_SendByte(REG_Address);//內(nèi)部寄存器地址,請參考中文pdf22頁
//BH1750_SendByte(REG_data);//內(nèi)部寄存器數(shù)據(jù),請參考中文pdf22頁
BH1750_Stop();//發(fā)送停止信號
}
void Init_BH1750()
{
Single_Write_BH1750(0x01);
}本文引用地址:http://m.butianyuan.cn/article/201611/322554.htmvoid Multiple_Read_BH1750(void)
{
unsigned char i;
BH1750_Start();//起始信號
BH1750_SendByte(0x47);//發(fā)送設備地址+讀信號
for (i = 0; i < 3; i++){//連續(xù)讀取6個地址數(shù)據(jù),存儲中BUF
BUF[i] = BH1750_RecvByte();//BUF[0]存儲0x32地址中的數(shù)據(jù)
if (i == 3) {
mnack();//最后一個數(shù)據(jù)需要回NOACK
}
else {
BH1750_SendACK();//回應ACK
}
}
BH1750_Stop();//停止信號
Delay5ms();
}
void conversion(unsigned int temp_data)//數(shù)據(jù)轉(zhuǎn)換出 個,十,百,千,萬
{
wan = temp_data / 10000 + 0x30;
temp_data = temp_data % 10000;//取余運算
qian = temp_data / 1000 + 0x30;
temp_data = temp_data % 1000;//取余運算
bai = temp_data / 100 + 0x30;
temp_data = temp_data % 100;//取余運算
shi = temp_data / 10 + 0x30;
temp_data = temp_data % 10;//取余運算
ge = temp_data + 0x30;
}
void WaitForEnable(void)
{
DataDir &= 0x00;
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();
while((P4IN & Busy)!=0);//檢測忙標志//STA7為0 允許讀寫操作使能則退出循環(huán)
CLR_EN;
DataDir |= 0xFF;//將P4口切換為輸出狀態(tài)
}
void WriteCommandLCM(unsigned char CMD,uchar Attribc)
{
if(Attribc)
WaitForEnable();
CLR_RS;
CLR_RW;
_NOP();
DataPort = CMD;
_NOP();
SET_EN;
_NOP();
_NOP();
CLR_EN;
}
void WriteDataLCM(unsigned char dataW)
{
WaitForEnable();
SET_RS;
CLR_RW;
_NOP();
DataPort = dataW;
_NOP();
SET_EN;
_NOP();
_NOP();
CLR_EN;
}
void InitLcd()
{
CtrlDir |= 0x07;//控制線端口設為輸出狀態(tài)
DataDir= 0xFF;//數(shù)據(jù)端口設為輸出狀態(tài)
WriteCommandLCM(0x38,0);
delay(15);
WriteCommandLCM(0x08,1);
delay(1);
WriteCommandLCM(0x01,1);
delay(1);
WriteCommandLCM(0x06,1);
delay(1);
WriteCommandLCM(0x0c,1);
delay(1);
}
void DisplayOneChar(unsigned char X,unsigned char DData)
{
X |= 0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
void inmaindispaly()
{
uint temp;
dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];//合成數(shù)據(jù)
temp=dis_data;
conversion(temp);//計算數(shù)據(jù)和顯示
DisplayOneChar(0,L);
DisplayOneChar(1,i);
DisplayOneChar(2,g);
DisplayOneChar(3,h);
DisplayOneChar(4,t);
DisplayOneChar(5,:);
DisplayOneChar(7,wan); //顯示數(shù)據(jù)
DisplayOneChar(8,qian);
DisplayOneChar(9,bai);
DisplayOneChar(10,shi);
DisplayOneChar(11,ge);
DisplayOneChar(13,l); ////顯示數(shù)單位
DisplayOneChar(14,x);
}
int main( void )
{
P4DIR=0XFF;
P5DIR|=0X07;
P3DIR|=0XC0;
WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer to prevent time out reset
delay(20);//延時200ms
InitLcd();//初始化LCD
Init_BH1750();//初始化BH1750
while(1)
{
Single_Write_BH1750(0x01);// power on
Single_Write_BH1750(0x10);// H- resolution mode
delay(180);//延時180ms
Multiple_Read_BH1750();//連續(xù)讀出數(shù)據(jù),存儲在BUF中
inmaindispaly();
delay(1000);
}
}
評論