首页资源分类嵌入式处理器MSP430 > hmc5883l测试程序

hmc5883l测试程序

已有 445023个资源

下载专区

上传者其他资源

    文档信息举报收藏

    标    签:hmc5883l测试程序串口显示

    分    享:

    文档简介

    用于hmc5883l测试地磁场强度,利用MSP430控制,串口调试显示。

    文档预览

    #include typedef unsigned char uchar; typedef unsigned int uint; #define HMC5883L_DIR P1DIR #define HMC5883L_IN P1IN #define HMC5883L_OUT P1OUT #define SCL BIT6 //IIC閺冨爼鎸撳鏇″壖鐎规矮绠� #define SDA BIT7 //IIC閺佺増宓佸鏇″壖鐎规矮绠� //DRDY 娑撳秵甯� #define SCL_H (HMC5883L_DIR|=SCL,HMC5883L_OUT|=SCL) #define SCL_L (HMC5883L_DIR|=SCL,HMC5883L_OUT&=~SCL) #define SDA_H (HMC5883L_DIR|=SDA,HMC5883L_OUT|=SDA) #define SDA_L (HMC5883L_DIR|=SDA,HMC5883L_OUT&=~SDA) #define SlaveAddress 0x3c //鐎规矮绠熼崳銊ゆ閸︹問IC閹崵鍤庢稉顓犳畱娴犲骸婀撮崸锟�uchar Rec_Data[6]; int x,y,z; double Angle; uint Acr; uchar Rec_Data[6]; /*瀵よ埖妞傞崙鑺ユ殶*/ void Delay(uint t) { while(t--); } /*鐠у嘲顬婃穱鈥冲娇*/ void IIC_Start(void) { SDA_H; SCL_H; Delay(50); SDA_L; Delay(50); SCL_L; } /*閸嬫粍顒涙穱鈥冲娇*/ void IIC_Stop(void) { SDA_L; SCL_H; Delay(50); SDA_H; Delay(50); } /*閸欐垿锟芥惔鏃傜摕娣団�褰*/ void IIC_SendAck(char Ack) { if(Ack) //ack (0:ACK 1:NACK) SDA_H; else SDA_L; SCL_H; Delay(50); SCL_L; Delay(50); } char IIC_RecAck(void) { char CY; HMC5883L_DIR&=~SDA; SCL_H; Delay(50); if(HMC5883L_DIR&SDA) CY=1; else CY=0; SCL_L; Delay(50); return CY; } /*閸氭厲IC閹崵鍤庨崣鎴︼拷娑擄拷閲滅�妤勫Ν閺佺増宓*/ void HMC5883_Send_Byte(uchar Dat) { uchar i; for(i=0;i<8;i++) { if(Dat&BIT7) SDA_H; else SDA_L; SCL_H; Delay(50); SCL_L; Delay(50); Dat<<=1; } IIC_RecAck(); } /*娴犲丢IC閹崵鍤庨幒銉︽暪娑擄拷閲滅�妤勫Ν閺佺増宓*/ uchar HMC5883_Rec_Byte(void) { uchar i,Dat=0; SDA_H; HMC5883L_DIR&=~SDA; for(i=0;i<8;i++) { Dat<<=1; SCL_H; Delay(50); if((HMC5883L_IN&SDA)==SDA) Dat|=BIT0; SCL_L; Delay(50); } return Dat; } /*閸楁洖鐡ч懞鍌氬晸HMC5883*/ void Single_Write_HMC5883(uchar Address,uchar Dat) { IIC_Start(); HMC5883_Send_Byte(SlaveAddress); HMC5883_Send_Byte(Address); HMC5883_Send_Byte(Dat); IIC_Stop(); } /*閸楁洖鐡ч懞鍌濐嚢HMC5883*/ uchar Single_Read_HMC5883(uchar Addr) { uchar Value; IIC_Start(); HMC5883_Send_Byte(SlaveAddress); HMC5883_Send_Byte(Addr); IIC_Start(); HMC5883_Send_Byte(SlaveAddress+1); Value=HMC5883_Rec_Byte(); IIC_SendAck(1); IIC_Stop(); return Value; } /*婢舵艾鐡ч懞鍌濐嚢HMC5883*/ void Multiple_Read_HMC5883(void) { uchar i; //鏉╃偟鐢荤拠璇插毉HMC5883閸愬懘鍎寸憴鎺戝閺佺増宓侀敍灞芥勾閸э拷瀵栭崶锟絰3~0x5 IIC_Start(); HMC5883_Send_Byte(SlaveAddress); HMC5883_Send_Byte(0x03);//閸欐垿锟界�妯哄亶閸楁洖鍘撻崷鏉挎絻閿涘奔绮�x03瀵拷顬� IIC_Start(); HMC5883_Send_Byte(SlaveAddress+1); for(i=0;i<6;i++) //鏉╃偟鐢荤拠璇插絿6娑擃亜婀撮崸锟芥殶閹诡噯绱濈�妯哄亶閸︹墽ec_Data { Rec_Data[i]=HMC5883_Rec_Byte(); if(i==5) IIC_SendAck(1); //閺堬拷鎮楁稉锟介嚋閺佺増宓侀棁锟筋渽閸ユ慷OACK else IIC_SendAck(0); //閸ョ偛绨睞CK } IIC_Stop(); Delay(100); } //閸掓繂顬婇崠鏈扢C5883閿涘本鐗撮幑顕�付鐟曚浇顕崣鍌濓拷pdf鏉╂稖顢戞穱顔芥暭**** void HMC5883_Init(void) { Single_Write_HMC5883(0x00,0x60); Single_Write_HMC5883(0x02,0x00); //HMC5883_Send_Byte(0x00); } /********************閼惧嘲褰囨稉澶庨叡閺佺増宓佹穱鈩冧紖*******************/ /********************0鎼达拷x濮濓絽顕崡锟�**********************/ void get_angle() { Multiple_Read_HMC5883();//鏉╃偟鐢荤拠璇插毉閺佺増宓侀敍灞界摠閸屻劌婀猂ec_Data[]娑擄拷 x=Rec_Data[0]<<8 | Rec_Data[1];//Combine MSB and LSB of X Data output register z=Rec_Data[2]<<8 | Rec_Data[3];//Combine MSB and LSB of Z Data output register y=Rec_Data[4]<<8 | Rec_Data[5];//Combine MSB and LSB of Y Data output register Angle= atan2((double)y,(double)x)*(180/3.14159265)+180;//閸楁洑缍呴敍姘愁瀾鎼达拷(0~360) //Angle*=10; Delay(50000); } void Uart() { P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2; UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 104; // 1MHz/8=115200 UCA0BR1 = 0; // 1MHz 115200 UCA0MCTL = UCBRS2 + UCBRS0 ; // 娉㈢壒鐜囪皟鏁存帶鍒跺瘎瀛樺櫒(鍐欒繖涓氨鍙互) UCA0CTL1 &= ~UCSWRST; } void Hex2Dec(uint Hex_val,uchar *ptr) { ptr[0] = Hex_val / 1000; ptr[1] = (Hex_val - ptr[0]*1000)/100; ptr[2] = (Hex_val - ptr[0]*1000 - ptr[1]*100)/10; ptr[3] = Hex_val - ptr[0]*1000 - ptr[1]*100- ptr[2]*10; ptr[4] ='\n'; //ptr[4] = (Hex_val - ptr[0]*10000 - ptr[1]*1000- ptr[2]*100- ptr[3]*10); } /*********************濞村鐦粙瀣碍**********************/ uchar temp[5]; void main(void) { int i,s,tcnt,k; WDTCTL = WDTPW + WDTHOLD; // Stop WDT HMC5883_Init(); //HMC5883閸掓繂顬婇崠锟� BCSCTL1 = CALBC1_1MHZ; //鐠佸墽鐤�DCO 妫版垹宸兼稉锟�MHz DCOCTL = CALDCO_1MHZ; Uart(); do { Multiple_Read_HMC5883();//鏉╃偟鐢荤拠璇插毉閺佺増宓侀敍灞界摠閸屻劌婀猂ec_Data[]娑擄拷 x=Rec_Data[0]<<8 | Rec_Data[1];//Combine MSB and LSB of X Data output register z=Rec_Data[2]<<8 | Rec_Data[3];//Combine MSB and LSB of Z Data output register y=Rec_Data[4]<<8 | Rec_Data[5];//Combine MSB and LSB of Y Data output register // x=x&0x0FFF; //z=z&0x0FFF; //s=x&0x8000; //Hex2Dec(x,temp); /****************** if(s==1) { temp[5]='-'; x=x&0x7fff; x=~x+1; } else temp[5]='+'; ******************/ //Hex2Dec(x,temp); //Hex2Dec(z,temp); for(i=0;i<4;i++) { temp[i]+=0x30; } tcnt=0; for(k=0;tcnt<30000;k++) { tcnt++; } for(i=0;i<5;i++) { while(UCA0STAT&BUSY); //鍒ゆ柇鍙戦�鏄惁瀹屾垚 //UCA0TXBUF =temp[i]; UCA0TXBUF =Rec_Data[i]; } } while(1); }

    Top_arrow
    回到顶部
    EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高员外,我们会尽快处理。