MSP430F149驅(qū)動(dòng)BH1750FVI程序
/**********************************
**************寫(xiě)入數(shù)據(jù)************/
void Single_Write_BH1750(unsigned char REG_Address)
{
BH1750_Start(); //起始信號(hào)
BH1750_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)
BH1750_SendByte(REG_Address); //內(nèi)部寄存器地址,請(qǐng)參考中文pdf22頁(yè)
// BH1750_SendByte(REG_data); //內(nèi)部寄存器數(shù)據(jù),請(qǐng)參考中文pdf22頁(yè)
BH1750_Stop(); //發(fā)送停止信號(hào)
}
/**************************************
初始化BH1750,根據(jù)需要請(qǐng)參考pdf進(jìn)行修改
*************************************/
void Init_BH1750()
{
Single_Write_BH1750(0x01);
}
/***************************
*********單字節(jié)讀取*********/
/*
unsigned char Single_Read_BH1750(unsigned char REG_Address)
{
unsigned char REG_data;
BH1750_Start(); //起始信號(hào)
BH1750_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)
BH1750_SendByte(REG_Address); //發(fā)送存儲(chǔ)單元地址,從0開(kāi)始
BH1750_Start(); //起始信號(hào)
BH1750_SendByte(SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號(hào)
REG_data=BH1750_RecvByte(); //讀出寄存器數(shù)據(jù)
BH1750_SendACK(1);
BH1750_Stop(); //停止信號(hào)
return REG_data;
}
*/
/*******************************
連續(xù)讀出BH1750內(nèi)部數(shù)據(jù)
********************************/
void Multiple_Read_BH1750(void)
{
unsigned char i;
BH1750_Start(); //起始信號(hào)
BH1750_SendByte(0x47); //發(fā)送設(shè)備地址+讀信號(hào)
for (i = 0; i < 3; i++){ //連續(xù)讀取6個(gè)地址數(shù)據(jù),存儲(chǔ)中BUF
BUF[i] = BH1750_RecvByte(); //BUF[0]存儲(chǔ)0x32地址中的數(shù)據(jù)
if (i == 3) {
mnack(); //最后一個(gè)數(shù)據(jù)需要回NOACK
}
else {
BH1750_SendACK(); //回應(yīng)ACK
}
}
BH1750_Stop(); //停止信號(hào)
Delay5ms();
}
/*******************************************************************************/
/*******************************LCD1602函數(shù)************************************/
/*******************************************************************************/
void conversion(unsigned int temp_data) //數(shù)據(jù)轉(zhuǎn)換出 個(gè),十,百,千,萬(wàn)
{
wan = temp_data / 10000 + 0x30;
temp_data = temp_data % 10000;//取余運(yùn)算
qian = temp_data / 1000 + 0x30;
temp_data = temp_data % 1000;//取余運(yùn)算
bai = temp_data / 100 + 0x30;
temp_data = temp_data % 100;//取余運(yùn)算
shi = temp_data / 10 + 0x30;
temp_data = temp_data % 10;//取余運(yùn)算
ge = temp_data + 0x30;
}
void WaitForEnable(void)
{
DataDir &= 0x00;
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();
while((P4IN & Busy)!=0); //檢測(cè)忙標(biāo)志 //STA7為0 允許讀寫(xiě)操作使能則退出循環(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; //控制線端口設(shè)為輸出狀態(tài)
DataDir = 0xFF; //數(shù)據(jù)端口設(shè)為輸出狀態(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); //計(jì)算數(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); //延時(shí)200ms
InitLcd(); //初始化LCD
Init_BH1750(); //初始化BH1750
while(1)
{
Single_Write_BH1750(0x01); // power on
Single_Write_BH1750(0x10); // H- resolution mode
delay(180); //延時(shí)180ms
Multiple_Read_BH1750(); //連續(xù)讀出數(shù)據(jù),存儲(chǔ)在BUF中
inmaindispaly();
delay(1000);
}
}
評(píng)論