HMC5883 51串口測(cè)試程序 作者: 時(shí)間:2016-11-29 來(lái)源:網(wǎng)絡(luò) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對(duì)面交流海量資料庫(kù)查詢 收藏 /**************************************接收應(yīng)答信號(hào)**************************************/bit HMC5883_RecvACK(){SCL = 1; //拉高時(shí)鐘線Delay5us(); //延時(shí)CY = SDA; //讀應(yīng)答信號(hào)SCL = 0; //拉低時(shí)鐘線Delay5us(); //延時(shí)return CY;}/**************************************向IIC總線發(fā)送一個(gè)字節(jié)數(shù)據(jù)**************************************/void HMC5883_SendByte(BYTE dat){BYTE i;for (i=0; i<8; i++) //8位計(jì)數(shù)器{dat <<= 1; //移出數(shù)據(jù)的最高位SDA = CY; //送數(shù)據(jù)口SCL = 1; //拉高時(shí)鐘線Delay5us(); //延時(shí)SCL = 0; //拉低時(shí)鐘線Delay5us(); //延時(shí)}HMC5883_RecvACK();}/**************************************從IIC總線接收一個(gè)字節(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位計(jì)數(shù)器{dat <<= 1;SCL = 1; //拉高時(shí)鐘線Delay5us(); //延時(shí)dat |= SDA; //讀數(shù)據(jù) SCL = 0; //拉低時(shí)鐘線Delay5us(); //延時(shí)}return dat;}//***************************************************void Single_Write_HMC5883(uchar REG_Address,uchar REG_data){HMC5883_Start(); //起始信號(hào)HMC5883_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)HMC5883_SendByte(REG_Address); //內(nèi)部寄存器地址,請(qǐng)參考中文pdf HMC5883_SendByte(REG_data); //內(nèi)部寄存器數(shù)據(jù),請(qǐng)參考中文pdfHMC5883_Stop(); //發(fā)送停止信號(hào)}/*//********單字節(jié)讀取內(nèi)部寄存器*************************uchar Single_Read_HMC5883(uchar REG_Address){ uchar REG_data;HMC5883_Start(); //起始信號(hào)HMC5883_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)HMC5883_SendByte(REG_Address); //發(fā)送存儲(chǔ)單元地址,從0開(kāi)始 HMC5883_Start(); //起始信號(hào)HMC5883_SendByte(SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號(hào)REG_data=HMC5883_RecvByte(); //讀出寄存器數(shù)據(jù)HMC5883_SendACK(1); HMC5883_Stop(); //停止信號(hào)return REG_data; }*///******************************************************////連續(xù)讀出HMC5883內(nèi)部角度數(shù)據(jù),地址范圍0x3~0x5////******************************************************void Multiple_read_HMC5883(void){ uchar i;HMC5883_Start(); //起始信號(hào)HMC5883_SendByte(SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)HMC5883_SendByte(0x03); //發(fā)送存儲(chǔ)單元地址,從0x3開(kāi)始 HMC5883_Start(); //起始信號(hào)HMC5883_SendByte(SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號(hào)for (i=0; i<7; i++) //連續(xù)讀取6個(gè)地址數(shù)據(jù),存儲(chǔ)中BUF{BUF[i] = HMC5883_RecvByte(); //BUF[0]存儲(chǔ)0x32地址中的數(shù)據(jù)if (i == 6){HMC5883_SendACK(1); //最后一個(gè)數(shù)據(jù)需要回NOACK}else{HMC5883_SendACK(0); //回應(yīng)ACK}}HMC5883_Stop(); //停止信號(hào)Delay5ms();}//初始化HMC5883,根據(jù)需要請(qǐng)參考pdf進(jìn)行修改****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ù),存儲(chǔ)在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); //計(jì)算數(shù)據(jù)和顯示SeriPushSend(bai);SeriPushSend(shi);SeriPushSend(ge);SeriPushSend(0x0d);SeriPushSend(0x0a);for (i=0;i<10000;i++); //延時(shí) }} 上一頁(yè) 1 2 下一頁(yè)
評(píng)論