我自己写的程序如下,但是三轴数据在lcd上直接显示都是0,明明磁场角度都显示好了。
#include <reg52.h>
#include <intrins.h>
#include <stdlib.h>
#include <math.h>
#include "LCD_Head.h"
uchar code Num[]={"0123456789"};
/*******************************************************************/
/* 延时函数 */
/*******************************************************************/
void delayms(int ms) //毫秒级延迟
{
while(ms--)
{
uchar i;
for(i=0;i<150;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void delayNOP() //最小延时函数,4条 NOP 指令
{
_nop_();
_nop_();
_nop_();
_nop_();
}
/************** 忙检查 ***************/
void WaitForEnable(void)
{
DataPort=0xFF;
LCM_RS=0;
LCM_RW=1;
delayNOP();
LCM_EN=1;
delayNOP();
while(DataPort&0x80);
LCM_EN=0;
}
/************** 写命令 ***************/
/**** Attribc=0 不要检忙 *************
***** Attribc=1 需要检忙 *************/
void WriteCommandLCM(uchar CMD,bit Attribc)
{
if(Attribc)
WaitForEnable();
LCM_RS=0;
LCM_RW=0;
delayNOP();
DataPort=CMD;
delayNOP();
LCM_EN=1;
delayNOP();
LCM_EN=0;
}
/************** 写数据 ***************/
void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;
LCM_RW=0;
delayNOP();
DataPort=dataW;
delayNOP();
LCM_EN=1;
delayNOP();
LCM_EN=0;
}
/************** 读数据 **************/
/************************************/
/*从lcd 读取数据到 DataPort */
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。 */
uchar ReadDataLCM()
{
uchar r_dat;
WaitForEnable();
DataPort = 0xFF;
LCM_RS = 1;
LCM_RW = 1;
LCM_EN = 1;
r_dat = DataPort;
LCM_EN = 0;
return(r_dat);
}
/********** 初始化液晶 *************/
void InitLcd()
{
// LCM_PSB = 1; //并口方式
// LCM_RST = 0; //液晶复位
// delayms(2);
// LCM_RST = 1;
delayms(2);
WriteCommandLCM(0x34,1); //扩充指令操作
WriteCommandLCM(0x30,1); //基本指令操作
WriteCommandLCM(0x0C,1); //显示开,关光标
WriteCommandLCM(0x01,1); //清除LCD 的显示内容
}
/********** 初始化HMC5883 ***********/
void Init_HMC5883()
{
Single_Write_HMC5883(0x02,0x00); //选择模式寄存器,选为连续测量模式
}
/********** 清屏函数 ***********/
void ClearScreen()
{
uchar x,y,z;
uchar Temp=0x00;
WriteCommandLCM(0x34,1);
WriteCommandLCM(0x36,1); //扩充指令 绘图显示
for(z=0;z<9;z+=8)
for(y=0;y<0x20;y++)
for(x=0;x<8;x++)
{
WriteCommandLCM(y+0x80,0); //行地址
WriteCommandLCM(x+0x80+z,0); //列地址 WriteDataLCM(Temp); //写数据 D15-D8
WriteDataLCM(Temp); //写数据 D7-D0
}
}
/************ 数字显示函数, 参数入口:DisplayNumber(地址,数字) ************/
void DisplayNumber(uchar address,double number)
{
uchar qian,bai,shi,ge;
int number2;
number*=10; //
number2=(int)number; //校验得出角度差为 58°
// number2=(int)number+580;
// if(number2>3600) number2-=3600;
// else number2=number2;
// delayNOP();
qian=number2/1000;
bai=(number2%1000)/100;
shi=(number2%100)/10;
ge=number2%10;
WriteCommandLCM(address,1);
delayms(2);
WriteDataLCM(' ');
delayms(2);
WriteDataLCM(Num[qian]);
delayms(2);
WriteDataLCM(Num[bai]);
delayms(2);
WriteDataLCM(Num[shi]);
delayms(2);
WriteDataLCM('.');
delayms(2);
WriteDataLCM(Num[ge]);
delayms(2);
DisplayListChar(address+3,2,"°");
delayms(2);
}
/************ 数字显示函数2, 参数入口:DisplayNumber(地址,数字) ************/
void DisplayNumber2(uchar address,double number)
{
uchar wan,qian,bai,shi,ge;
int number2;
number*=10; //
number2=(int)number; //校验得出角度差为 58°
// number2=(int)number+580;
// if(number2>3600) number2-=3600;
// else number2=number2;
// delayNOP();
//wan=number2/10000;
qian=number2/1000;
bai=(number2%1000)/100;
shi=(number2%100)/10;
ge=number2%10;
WriteCommandLCM(address,1);
delayms(2);
WriteDataLCM(' ');
delayms(2);
WriteDataLCM(Num[qian]);
delayms(2);
WriteDataLCM(Num[bai]);
delayms(2);
WriteDataLCM(Num[shi]);
delayms(2);
WriteDataLCM('.');
delayms(2);
WriteDataLCM(Num[ge]);
delayms(2);
DisplayListChar(address+3,2,"mG");
delayms(2);
}
/***** 汉字/字符数组显示函数, 参数入口:DisplayListChar(地址,显示宽度,汉字/
字符数组) *****/
void DisplayListChar(uchar address,uchar L,uchar Str[])
{
uchar i;
WriteCommandLCM(address,1);
for(i=0;i<L;i++)
{
WriteDataLCM(Str);
}
} //例如:DisplayListChar(0x80,4,"");
/*********************************************************
* *
* 图形显示 *
* *
*********************************************************/
void PhotoDisplay(uchar *img)
{
uchar i,j;
WriteCommandLCM(0x34,1); //写数据时,关闭图形显示
for(i=0;i<32;i++)
{
WriteCommandLCM(0x80+i,1); //先写入水平坐标值
WriteCommandLCM(0x80,1); //写入垂直坐标值
for(j=0;j<16;j++) //再写入两个 8位元的数据
WriteDataLCM(*img++);
delayms(1);
}
for(i=0;i<32;i++)
{
WriteCommandLCM(0x80+i,1);
WriteCommandLCM(0x88,1);
for(j=0;j<16;j++)
WriteDataLCM(*img++);
delayms(1);
}
WriteCommandLCM(0x36,1); //写完数据,开图形显示
}
/***********************************************************
函数名: DrawPoint
函数说明:画点
传入参数:打点位置(x0,y0);color=1,点亮;color=0,擦除
传出参数:无
返回值: 无
**********************************************************/
void DrawPoint(uchar x,uchar y,bit color)
{
uchar row,collum,cbite;
uchar tempH,tempL;
WriteCommandLCM(0x34,1); //打开扩充指令集
WriteCommandLCM(0x36,1); //打开图形显示
collum=x>>4;
cbite=x&0x0f;
if(y<32)
row=y;
else
{
row=y-32;
collum+=8;
}
WriteCommandLCM(0x80+row,1); //先写 Y 坐标
WriteCommandLCM(0x80+collum,1); //再写 X 坐标
ReadDataLCM();
tempH = ReadDataLCM(); //先读出高字节
tempL = ReadDataLCM(); //再读出低字节
WriteCommandLCM(0x80+row,1); //先写 Y 坐标
WriteCommandLCM(0x80+collum,1); //再写 X 坐标
if (color)
{
if(cbite<8)
{
tempH|=(1<<(7-cbite));
//tempL=(1<<(7-cbite));
}
else
{
//tempH=(1<<(15-cbite));
tempL|=(1<<(15-cbite));
}
}
else
{
if(cbite<8)
{
tempH&=~(1<<(7-cbite));
//tempL=(1<<(7-cbite));
}
else
{
//tempH=(1<<(15-cbite));
tempL&=~(1<<(15-cbite));
}
}
WriteDataLCM(tempH);
WriteDataLCM(tempL);
WriteCommandLCM(0x30,1);
}
/***********************************************************
函数名: Line
函数说明:画直线
传入参数:直线起始位置(x0,y0);终点(x1,y1)
传出参数:无
返回值: 无
**********************************************************/
void Line(uchar x0,uchar y0, uchar x1,uchar y1,bit color)
{
int dx; // 直线x 轴差值变量
int dy; // 直线y 轴差值变量
char dx_sym; // x 轴增长方向,为-1 时减值方向,为1 时增值方向
char dy_sym; // y 轴增长方向,为-1 时减值方向,为1 时增值方向
int dx_x2; // dx*2 值变量,用于加快运算速度
int dy_x2; // dy*2 值变量,用于加快运算速度
int di; // 决策变量
if (x0 == x1) // 画垂直线
{
if (y0 > y1)
{
dx = y0;
y0 = y1;
y1 = dx;
}
for (dx = y0; dx < y1+1; dx++)
{
DrawPoint(x0, dx, color);
}
}
if (y0 == y1) // 画水平线
{
if(x0 > x1)
{
dy = x0;
x0 = x1;
x1 = dy;
}
for (dy = x0; dy< x1+1; dy++)
{
DrawPoint(dy, y0,color);
}
}
/****************************画斜线******************************/
dx = x1-x0; // 求取两点之间的差值
dy = y1-y0;
if (dx > 0) // 判断x 轴方向
{
dx_sym = 1; // dx>0,设置dx_sym=1
}
else
{
if (dx < 0)
{
dx_sym = -1; // dx<0,设置dx_sym=-1
}
}
if (dy> 0) // 判断y 轴方向
{ dy_sym = 1; // dy>0,设置dy_sym=1
}
else
{
if (dy< 0)
{
dy_sym = -1; // dy<0,设置dy_sym=-1
}
}
dx = dx_sym * dx; // 将dx、dy 取绝对值
dy = dy_sym * dy;
dx_x2 = dx*2; // 计算2 倍的dx 及 dy 值
dy_x2 = dy*2;
/***** 使用Bresenham 法进行画直线 *****/
if (dx >= dy) // 对于dx>=dy,则使用x 轴为基准
{
di = dy_x2 - dx;
while(x0!=x1)
{
DrawPoint(x0, y0,color);
x0 += dx_sym;
if (di < 0)
{
di += dy_x2; // 计算出下一步的决策值
}
else
{
di += dy_x2 - dx_x2;
y0 += dy_sym;
}
}
DrawPoint(x0, y0,color); // 显示最后一点
}
else // 对于dx<dy,则使用 y 轴为基准
{
di = dx_x2 - dy;
while(y0!=y1)
{
DrawPoint(x0, y0,color);
y0 += dy_sym;
if (di < 0)
{
di += dx_x2;
} else
{
di += dx_x2 - dy_x2;
x0 += dx_sym;
}
}
DrawPoint(x0, y0,color); // 显示最后一点
}
}
/*******************************************
函数名称:voidDrawHand(uchar x0,uchar y0,uchar length,int int_degree,bit color)
功 能:画指针
参 数:(x0,y0)圆盘圆心坐标
length 指针长度
int_degree 顺时针相对正北的角度 取整数
color 指针颜色
返回值 :无
********************************************/
// void DrawHand(uchar x0,uchar y0,uchar length,int int_degree,bit color)
//角度校验前函数定义
// {
// uchar x,y;
// x=length * cos(int_degree * 3.14159265 / 180 - 3.14159265 / 2) + x0;
// y=length * sin(int_degree * 3.14159265 / 180 - 3.14159265 / 2) + y0;
// Line(x0,y0,x,y,color);
// }
void DrawHand(uchar x0,uchar y0,uchar length,int int_degree,bit color)
{
uchar x,y;
// int_degree+=58;
x=length * cos(int_degree * 3.14159265 / 180 - 3.14159265 / 2) + x0;
y=length * sin(int_degree * 3.14159265 / 180 - 3.14159265 / 2) + y0;
Line(x0,y0,x,y,color);
}
/**************************************
起始信号
**************************************/
void HMC5883_Start()
{
SDA = 1; //拉高数据线
SCL = 1; //拉高时钟线
delayNOP(); //延时
SDA = 0; //产生下降沿
delayNOP(); //延时
SCL = 0; //拉低时钟线
}
/**************************************
停止信号
**************************************/
void HMC5883_Stop()
{
SDA = 0; //拉低数据线
SCL = 1; //拉高时钟线
delayNOP(); //延时
SDA = 1; //产生上升沿
delayNOP(); //延时
}
/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void HMC5883_SendACK(bit ack)
{
SDA = ack; //写应答信号
SCL = 1; //拉高时钟线
delayNOP(); //延时
SCL = 0; //拉低时钟线
delayNOP(); //延时
}
/**************************************
接收应答信号
**************************************/
bit HMC5883_RecvACK()
{
SCL = 1; //拉高时钟线
delayNOP(); //延时
CY = SDA; //读应答信号
SCL = 0; //拉低时钟线
delayNOP(); //延时
return CY;
}
/**************************************
向IIC总线发送一个字节数据
**************************************/
void HMC5883_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8位计数器
{
dat<<= 1; //移出数据的最高位
SDA = CY; //送数据口
SCL = 1; //拉高时钟线
delayNOP(); //延时
SCL = 0; //拉低时钟线
delayNOP(); //延时
}
HMC5883_RecvACK();
}
/**************************************
从IIC总线接收一个字节数据
**************************************/
BYTE HMC5883_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1; //使能内部上拉,准备读取数据,
for (i=0; i<8; i++) //8位计数器
{
dat<<= 1;
SCL = 1; //拉高时钟线
delayNOP(); //延时
dat |= SDA; //读数据
SCL = 0; //拉低时钟线
delayNOP(); //延时
}
return dat;
}
/**************** 单字节写入数据 **********************/
void Single_Write_HMC5883(uchar REG_Address,uchar REG_data)
{
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(REG_Address); //内部寄存器(模式寄存器)地址,请参考中文pdf
HMC5883_SendByte(REG_data); //内部寄存器数据(即要发送到模式寄存器的数//据),请参考中文pdf
HMC5883_Stop(); //发送停止信号
}
/*********** 单字节读取内部寄存器 ***************/
uchar Single_Read_HMC5883(uchar REG_Address)
{
uchar REG_data;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号始
HMC5883_SendByte(REG_Address); //发送存储单元地址,从 0 开
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
REG_data=HMC5883_RecvByte(); //读出寄存器数据
HMC5883_SendACK(1);
HMC5883_Stop(); //停止信号
return REG_data;
}
//******************************************************
//
//连续读出HMC5883内部角度数据,地址范围 0x3~0x8 共六个地址数据
//
//******************************************************
void Multiple_read_HMC5883(void)
{
uchar i;
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress); //发送设备地址+写信号
HMC5883_SendByte(0x03); //发送存储单元地址,从 0x3 开始
HMC5883_Start(); //起始信号
HMC5883_SendByte(SlaveAddress+1); //发送设备地址+读信号
for (i=0; i<6; i++) //连续读取6个地址数据,存储中 BUF
{
BUF = HMC5883_RecvByte(); //BUF[0]存储数据
if (i == 5)
{
HMC5883_SendACK(1); //最后一个数据需要回NOACK
}
else
{
HMC5883_SendACK(0); //回应ACK
}
}
HMC5883_Stop(); //停止信号
delayms(5);
}
//3.主程序(指南针指针图形、方向、角度的显示):
/*********************************************************
******** 文件名:LCD_Main.c ********************************/
/******** 作用:主函数,图形、方向、角度的显示 **********/
//*********************************************************
#include <reg52.h>
#include <intrins.h>
#include <stdlib.h>
#include <math.h>
#define x0 31
#define y0 31 //表盘圆心坐标
#define R 19 //指针长度
BYTE BUF[8]; //接收数据缓存区
/******************* 初始化界面镶嵌字模 ********************/
uchar code img_ChuShiHua[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x02,0x08,0xFF,0x80,0x00,0xC2,0x10,0x82,0x78,0x42,0x01,0x03,0xFE,0x7F,0xC0,
0x80,
0x0F,0xFE,0x08,0x1E,0x31,0xFF,0x10,0x82,0x48,0x42,0x1F,0xF0,0x04,0x4A,0x43,
0xF8,
0x02,0x08,0x08,0x10,0x51,0x11,0x1F,0xEF,0x48,0xF7,0x91,0x10,0x18,0x4A,0x42,
0xC8,
0x03,0xF8,0x08,0x10,0x11,0xFF,0x10,0x02,0x48,0x94,0x9F,0xF0,0x20,0x7F,0xC7,
0xFC,
0x02,0x09,0xFF,0xDC,0x11,0x11,0x10,0x07,0x48,0x9C,0x91,0x17,0xFF,0x08,0x02,
0x88,
0x0F,0xFE,0x08,0x02,0x11,0xFF,0x1F,0x8E,0xC8,0xF2,0x91,0x10,0x20,0x1F,0x82,
0x48,
0x02,0x48,0x08,0x02,0x10,0x10,0x10,0x8A,0x48,0x91,0x9F,0xF0,0x20,0x60,0x87,
0xF8,
0x0D,0xF6,0x08,0x12,0x13,0xFF,0xB0,0x82,0xCA,0x90,0x91,0x08,0x20,0x9B,0x05,
0x28,
0x00,0x40,0x08,0x1C,0x7C,0x10,0x20,0x82,0x8A,0xF0,0x81,0x08,0x20,0x0C,0x05,
0x28,
0x07,0xFC,0x38,0x00,0x00,0x10,0x40,0x83,0x0E,0x97,0x81,0xF0,0xE0,0x70,0x0F,
0xFC,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x00,0x00,
0x00,
0x00,0x00,0x00,0x14,0x08,0x1F,0xF1,0x18,0x02,0x08,0xF9,0x04,0x28,0x00,0x00,
0x00,
0x00,0x00,0x00,0x28,0xFF,0x80,0x22,0x7F,0x0D,0x04,0x88,0x84,0x14,0x00,0x00,
0x00,
0x00,0x00,0x00,0x28,0x88,0x80,0xC4,0xC1,0x30,0x83,0x0C,0x04,0x14,0x00,0x00,0x00,
0x00,0x00,0x00,0x50,0xFF,0x81,0x07,0x3E,0xFF,0xFC,0x01,0xBF,0x8A,0x00,0x00,
0x00,
0x00,0x00,0x00,0x50,0x88,0xBF,0xFA,0x00,0x00,0x05,0xF8,0x84,0x0A,0x00,0x00,
0x00,
0x00,0x00,0x00,0x50,0x88,0x81,0x02,0x7F,0x3F,0xC5,0x08,0x84,0x0A,0x00,0x00,
0x00,
0x00,0x00,0x00,0x28,0xFF,0x81,0x07,0x08,0x20,0x44,0x90,0x84,0x14,0x00,0x00,
0x00,
0x00,0x00,0x00,0x28,0x88,0x41,0x04,0x2A,0x20,0x46,0x60,0xA4,0x14,0x00,0x00,
0x00,
0x00,0x00,0x00,0x14,0x08,0x41,0x01,0xC9,0x3F,0xCD,0xB0,0xC4,0x28,0x00,0x00,
0x00,
0x00,0x00,0x00,0x14,0x0F,0x87,0x06,0x38,0x20,0x43,0x0C,0x84,0x28,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x01,0x08,0x21,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x3E,0x47,0xFE,0xFF,0xC0,0x4F,0xC5,0xFC,0xBF,0x80,0x72,0x00,0x00,0x90,
0x00,
0x00,0x06,0x43,0x9C,0x73,0x80,0x20,0x44,0x94,0x92,0x80,0x5F,0xE0,0x00,0x88,
0x00,
0x00,0x0C,0x47,0xFE,0xFF,0xC0,0x14,0x4E,0xFB,0xDF,0xC0,0x54,0x00,0x0F,0xFE,
0x00,
0x00,0x08,0xD1,0x08,0x21,0x00,0x04,0x4A,0x41,0x48,0x00,0x65,0x00,0x00,0xA0,
0x00,
0x00,0x0C,0xD7,0xFE,0xFF,0xC0,0x07,0xEB,0xFF,0x7F,0xC0,0x67,0xE0,0x00,0xA4,
0x00,
0x00,0x39,0x5B,0xFF,0x7F,0xE0,0x10,0x2A,0x41,0x48,0x00,0x51,0x00,0x01,0x28,
0x00,
0x00,0x09,0x48,0x02,0x00,0x40,0x20,0x2C,0x7D,0x8F,0x80,0x75,0x40,0x01,0x28,
0x00,
0x00,0x0A,0x4B,0xFE,0x7F,0xC0,0x3F,0xA4,0xC4,0x98,0x80,0x45,0x20,0x03,0x32,
0x00,
0x00,0x08,0x42,0xA0,0x54,0x00,0x40,0x2B,0x39,0x67,0x00,0x49,0x20,0x06,0x62,0x00,
0x00,0x39,0xC6,0xA7,0x54,0xE0,0x41,0xF0,0xCE,0x19,0xC0,0x47,0x00,0x0D,0xBC,
0x00,
0x00,0x00,0x04,0xFD,0x5F,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00
};
/******************* 圆盘字模 **************************/
uchar code img_YuanPan[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x0C,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x0A,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x09,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xE3,0x8E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x03,0x03,0x81,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x0C,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x30,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x40,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x80,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x02,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x04,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x04,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x08,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x08,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x40,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x41,0x40,0x00,0x00,0x00,0x00,0x05,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x49,0x40,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x49,0x70,0x00,0x00,0x00,0x00,0x1D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x6B,0x70,0x00,0x00,0x00,0x00,0x1D,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x36,0x70,0x00,0x00,0x00,0x00,0x1D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x14,0x40,0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x14,0x40,0x00,0x00,0x00,0x00,0x05,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x40,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x20,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x08,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x04,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x04,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x02,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x80,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x40,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x30,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x0C,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x03,0x03,0x81,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0xE3,0x8E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x08,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,
0x00,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00
};
/******************** 主函数 *****************************
*********************************************************/
void main()
{
int x,y,z;
int degree,degree_past,times; //考虑到指针精度不会小于 1° 于是用整型变量
//degree_past与degree的比较决定指针与显示方向的字符是否需要变化
double angle;
delayms(40); //等待上电稳定
InitLcd(); //LCD12864初始化
delayms(5);
Init_HMC5883(); //HMC5883L初始化
delayms(5);
// PhotoDisplay(img_ChuShiHua); //镶嵌图片
// delayms(3000);
ClearScreen(); //清屏
PhotoDisplay(img_YuanPan);
delayms(20);
// angle=-58;
// degree_past=-58;
DrawHand(x0,y0,R,degree_past,1);
delayms(20);
// DisplayNumber(0x94,-58);
delayms(10);
// DisplayListChar(0x8D,1,"N");
delayms(500); //图形显示初始化
while(1)
{
Multiple_Read_HMC5883(); //连续读出数据,存储在 BUF中
x=BUF[0] << 8 | BUF[1]; //Combine MSB and LSB of X Data output register
z=BUF[2] << 8 | BUF[3]; //Combine MSB and LSB of Z Data output register
y=BUF[4] << 8 | BUF[5]; //Combine MSB and LSB of Y Data output register
angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // 度数(0~360)
DisplayNumber(0x84,angle); //度数显示的精度是0.1° 所以用double型数据 -angle
delayms(10);
DisplayNumber2(0x94,x);//显示x的强度
delayms(10);
DisplayNumber2(0x8C,y);//显示y的强度
delayms(10);
DisplayNumber2(0x9c,z);//显示z的强度
delayms(5);
degree=(int)angle;
if(( degree >= ( degree_past + 3 ) )||( degree <= degree_past - 3 ))
//检验angle 是否发生了3°以上的变化
{
DrawHand(x0,y0,R,degree_past,0); //擦除原显示指针
delayms(10);
DrawHand(x0,y0,R,degree,1); //更新角度信息后更新指针显示
delayms(10);
degree_past=degree;
delayNOP();
DisplayListChar(0x8D,2," "); //擦除原显示方向
delayms(5);
//更新显示方位
// angle+=58; //
// if(angle>360) angle-=360; //
// else angle=angle; //
// delayNOP(); // 这四
//句调整显示方向的角度 *** 校验角度后进行的修改 ***
times=(angle*10)/225;
delayNOP();
/* if((times < 1) || (times >= 15 )) //0°为正北方
{
DisplayListChar(0x8D,1,"N");
}
else if((times >= 1) && (times < 3 ))
{
DisplayListChar(0x8D,2,"NE");
}
else if((times >= 3) && (times < 5 ))
{
DisplayListChar(0x8D,1,"E");
}
else if((times >= 5) && (times < 7 ))
{
DisplayListChar(0x8D,2,"SE");
}
else if((times >= 7) && (times < 9 ))
{
DisplayListChar(0x8D,1,"S");
}
else if((times >= 9) && (times < 11 ))
{
DisplayListChar(0x8D,2,"SW");
}
else if((times >= 11) && (times < 13 ))
{
DisplayListChar(0x8D,2,"W");
}
else
{
DisplayListChar(0x8D,2,"NW");
} */
}
delayms(1000);
}
}
|