1
四天前我已经发了一篇关于使用MM32开发板做电子指南针的帖子,那篇帖子内容不够详细。我现在把内容再细说一下。我把程序又调试修改了一些,现在把改进的程序也给大家分享一下。先来张图片看看效果。
这些照片是将传感器放在不同位置时读取的方位角度情况,由于传感器放置的不够水平精度有些小误差。
下面再说说编写程序方面的事
我使用的是MDK510版本的编译器
1.HMC5883L的底层程序是我移植厂家给的底层程序,我在I2C方面做了部分的改动。读取HMC5883L传感器的读取函数中要注意寄存器的地址和读出来的数据所存放的缓冲器大小及位置
void read_hmc5883l(void)//¶ÁÈ¡
{
IIC_Write(HMC5883L_Addr,0x00,0x14); //ÅäÖüĴæÆ÷A£º²ÉÑùƽ¾ùÊý1 Êä³öËÙÂÊ75Hz Õý³£²âÁ¿
IIC_Write(HMC5883L_Addr,0x01,0x20); //ÅäÖüĴæÆ÷B£ºÔöÒæ¿ØÖÆ
IIC_Write(HMC5883L_Addr,0x02,0x00); //ģʽ¼Ä´æÆ÷£ºÁ¬Ðø²âÁ¿Ä£Ê½
Delayms(5);
IIC_Read(HMC5883L_Addr,0x03,8);//OUT_X_L_A
IIC_Read(HMC5883L_Addr,0x04,8);//OUT_X_H_A
IIC_Read(HMC5883L_Addr,0x07,8);//OUT_Y_L_A
IIC_Read(HMC5883L_Addr,0x08,8);//OUT_Y_H_A
if(x>0x7fff)x-=0xffff;
if(y>0x7fff)y-=0xffff;
if(z>0x7fff)z-=0xffff;
x=IIC_buf[0] << 8 | IIC_buf[1]; //Combine MSB and LSB of X Data output register
y=IIC_buf[2] << 8 | IIC_buf[3]; //Combine MSB and LSB of Y Data output register
z=IIC_buf[4] << 8 | IIC_buf[5]; //Combine MSB and LSB of Z Data output register
angledot= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degrees
}
2.关于显示坐标与角度方位情况的函数操作
if((angledot < 22.5) || (angledot > 337.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"South");
}
if((angledot> 22.5) && (angledot < 67.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"South-West");
}
if((angledot > 67.5) && (angledot < 112.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"West");
}
if((angledot > 112.5) && (angledot < 157.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"North-West");
}
if((angledot > 157.5) && (angledot < 202.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"North");
}
if((angledot> 202.5) && (angledot < 247.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"NorthEast");
}
if((angledot > 247.5) && (angledot < 292.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"East");
}
if((angledot > 292.5) && (angledot < 337.5 ))
{
LCD_ShowString(170,290,200,16,16," ");
LCD_ShowString(170,290,200,16,16,"SouthEast");
}
在显示新的位置字符串时需要将显示的上一位置的字符串清除掉,我这里是让显示空格的方式来清除上次显示的字符串。
3.在方位识别方面还可以将角度分的更细致,在这里我只分了八个方位角。由于使用的是杜邦线连接的传感器,所以也会是数据受到影响。
|
|