HMC5883 51串口測試程序 作者: 時間:2016-11-29 來源:網(wǎng)絡(luò) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對面交流海量資料庫查詢 收藏 /**************************************接收應(yīng)答信號**************************************/bit HMC5883_RecvACK(){SCL = 1; //拉高時鐘線Delay5us(); //延時CY = SDA; //讀應(yīng)答信號SCL = 0; //拉低時鐘線Delay5us(); //延時return CY;}/**************************************向IIC總線發(fā)送一個字節(jié)數(shù)據(jù)**************************************/void HMC5883_SendByte(BYTE dat){BYTE i;for (i=0; i<8; i++) //8位計數(shù)器{dat <<= 1; //移出數(shù)據(jù)的最高位SDA = CY; //送數(shù)據(jù)口SCL = 1; //拉高時鐘線Delay5us(); //延時SCL = 0; //拉低時鐘線Delay5us(); //延時}HMC5883_RecvACK();}/**************************************從IIC總線接收一個字節(jié)數(shù)據(jù)**************************************/BYTE HMC5883_RecvByte(){BYTE i;BYTE dat = 0;SDA = 1; //使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),for (i=0; i<8; i++) //8位計數(shù)器{dat <<= 1;SCL = 1; //拉高時鐘線Delay5us(); //延時dat |= SDA; //讀數(shù)據(jù) SCL = 0; //拉低時鐘線Delay5us(); //延時}return dat;}//***************************************************void Single_Write_HMC5883(uchar REG_Address,uchar REG_data){HMC5883_Start(); //起始信號HMC5883_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫信號HMC5883_SendByte(REG_Address); //內(nèi)部寄存器地址,請參考中文pdf HMC5883_SendByte(REG_data); //內(nèi)部寄存器數(shù)據(jù),請參考中文pdfHMC5883_Stop(); //發(fā)送停止信號}/*//********單字節(jié)讀取內(nèi)部寄存器*************************uchar Single_Read_HMC5883(uchar REG_Address){ uchar REG_data;HMC5883_Start(); //起始信號HMC5883_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫信號HMC5883_SendByte(REG_Address); //發(fā)送存儲單元地址,從0開始 HMC5883_Start(); //起始信號HMC5883_SendByte(SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號REG_data=HMC5883_RecvByte(); //讀出寄存器數(shù)據(jù)HMC5883_SendACK(1); HMC5883_Stop(); //停止信號return REG_data; }*///******************************************************////連續(xù)讀出HMC5883內(nèi)部角度數(shù)據(jù),地址范圍0x3~0x5////******************************************************void Multiple_read_HMC5883(void){ uchar i;HMC5883_Start(); //起始信號HMC5883_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫信號HMC5883_SendByte(0x03); //發(fā)送存儲單元地址,從0x3開始 HMC5883_Start(); //起始信號HMC5883_SendByte(SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號for (i=0; i<7; i++) //連續(xù)讀取6個地址數(shù)據(jù),存儲中BUF{BUF[i] = HMC5883_RecvByte(); //BUF[0]存儲0x32地址中的數(shù)據(jù)if (i == 6){HMC5883_SendACK(1); //最后一個數(shù)據(jù)需要回NOACK}else{HMC5883_SendACK(0); //回應(yīng)ACK}}HMC5883_Stop(); //停止信號Delay5ms();}//初始化HMC5883,根據(jù)需要請參考pdf進行修改****void Init_HMC5883(){Single_Write_HMC5883(0x02,0x00); //Single_Write_HMC5883(0x01,0xE0); //}//*********************************************************//主程序********//*********************************************************void main(){ // bit sign_bit;unsigned int i;int x,y,z;double angle;delay(100); init_uart();Init_HMC5883();while(1) //循環(huán){ Multiple_Read_HMC5883(); //連續(xù)讀出數(shù)據(jù),存儲在BUF中x = BUF[0] << 8 | BUF[1]; y = BUF[4] << 8 | BUF[5]; z = BUF[2] << 8 | BUF[3]; angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degreesconversion(angle); //計算數(shù)據(jù)和顯示SeriPushSend(bai);SeriPushSend(shi);SeriPushSend(ge);SeriPushSend(0x0d);SeriPushSend(0x0a);for (i=0;i<10000;i++); //延時 }} 上一頁 1 2 下一頁
評論