- #include <reg52.h>
- #include <math.h>
- #include <stdio.h>
- #include <intrins.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- /*12864液晶引脚定义*/
- sbit CS=P3^3;
- sbit SID=P3^4;
- sbit SCLK=P3^5;
- sbit SCL=P1^0; //IIC时钟引脚定义
- sbit SDA=P1^1; //IIC数据引脚定义
- #define SlaveAddress 0x3c //定义器件在IIC总线中的从地址
- uchar Rec_Data[6];
- void Send_byte(unsigned char bbyte);
- void Delaynms(unsigned int di);
- /*延时函数*/
- void Delay(uint t)
- {
- while(t--)
- {}
- }
- /********************************************
- **LCD显示函数
- ********************************************/
- /*写入一个数据或指令*/
- void Send_DATA(uchar ddata,uchar start)
- {
- unsigned char start_data,Hdata,Ldata;
- if(start==0)
- start_data=0xf8;//写指令
- else
- start_data=0xfa; //写数据
- SCLK=0;
- CS=1;
- Hdata=ddata&0xf0; //取高四位
- Ldata=(ddata<<4)&0xf0; //取低四位
- Send_byte(start_data); //发送起始信号
- Delaynms(5); //延时是必须的
- Send_byte(Hdata); //发送高四位
- Delaynms(1); //延时是必须的
- Send_byte(Ldata); //发送低四位
- Delaynms(1); //延时是必须的
- CS=0;
- Delay(20);
- }
- void Delaynms(unsigned int di) //??
- {
- unsigned int da,db;
- for(da=0;da<di;da++)
- for(db=0;db<10;db++);
- }
- void Send_byte(unsigned char bbyte) //发送一个字节
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- SID=bbyte&0x80; //取出最高位
- SCLK=1;
- SCLK=0;
- bbyte<<=1; //左移
- }
- }
- void LCD_set_xy( unsigned char x, unsigned char y )
- { //设置LCD显示的起始位置,X为行,Y为列
- unsigned char address;
- switch(x)
- {
- case 0: address = 0x80 + y; break;
- case 1: address = 0x80 + y; break;
- case 2: address = 0x90 + y; break;
- case 3: address = 0x88 + y; break;
- case 4: address = 0x98 + y; break;
- default:address = 0x80 + y; break;
- }
- Send_DATA(address,0);
- }
- /*12864液晶初始化*/
- void LCD_Init(void)
- {
- Delay(60000);
- Send_DATA(0x30,0);
- Send_DATA(0x04,0);
- Send_DATA(0x0c,0);
- Send_DATA(0x01,0);
- Delay(3000);
- }
- /*显示字符串*/
- void Dis_str(uchar Addr,uchar *str)
- {
- Send_DATA(Addr,0);
- Delay(10);
- while(*str!='\0')
- {
- Send_DATA(*str,1);
- str++;
- Delay(10);
- }
- }
- /*起始信号*/
- void IIC_Start(void)
- {
- SDA=1;
- SCL=1;
- Delay(5);
- SDA=0;
- Delay(5);
- SCL=0;
- }
- /*停止信号*/
- void IIC_Stop(void)
- {
- SDA=0;
- SCL=1;
- Delay(5);
- SDA=1;
- Delay(5);
- }
- /*发送应答信号*/
- void IIC_SendAck(bit Ack)
- {
- SDA=Ack;//ack (0:ACK 1:NACK)
- SCL=1;
- Delay(5);
- SCL=0;
- Delay(5);
- }
- /*接收应答信号*/
- bit IIC_RecAck(void)
- {
- SCL=1;
- Delay(5);
- CY=SDA;
- SCL=0;
- Delay(5);
- return CY;
- }
- /*向IIC总线发送一个字节数据*/
- void HMC5883_Send_Byte(uchar Dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- Dat<<=1;
- SDA=CY;
- SCL=1;
- Delay(5);
- SCL=0;
- Delay(5);
- }
- IIC_RecAck();
- }
- /*从IIC总线接收一个字节数据*/
- uchar HMC5883_Rec_Byte(void)
- {
- uchar i,Dat=0;
- SDA=1;
- for(i=0;i<8;i++)
- {
- Dat<<=1;
- SCL=1;
- Delay(5);
- Dat |=SDA;
- SCL=0;
- Delay(5);
- }
- return Dat;
- }
- /*单字节写HMC5833*/
- void Single_Write_HMC5883(uchar Address,uchar Dat)
- {
- IIC_Start();
- HMC5883_Send_Byte(SlaveAddress);
- HMC5883_Send_Byte(Address);
- HMC5883_Send_Byte(Dat);
- IIC_Stop();
- }
- /*单字节读HMC5833*/
- /*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;
- }*/
- /*多字节读HMC5833*/
- void Multiple_Read_HMC5883(void)
- {
- uchar i; //连续读出HMC5883内部角度数据,地址范围0x3~0x5
- IIC_Start();
- HMC5883_Send_Byte(SlaveAddress);
- HMC5883_Send_Byte(0x03);//发送存储单元地址,从0x03开始
- IIC_Start();
- HMC5883_Send_Byte(SlaveAddress+1);
- for(i=0;i<6;i++) //连续读取6个地址数据,存储在Rec_Data
- {
- Rec_Data[i]=HMC5883_Rec_Byte();
- if(i==5)
- IIC_SendAck(1); //最后一个数据需要回NOACK
- else
- IIC_SendAck(0); //回应ACK
- }
- IIC_Stop();
- Delay(100);
- }
- //初始化HMC5883,根据需要请参考pdf进行修改****
- void HMC5883_Init(void)
- {
- Single_Write_HMC5883(0x02,0x00);
- }
- /*主函数*/
- void main(void)
- {
- int X,Y,Z;
- double Angle;
- uint Acr;
- LCD_Init();//LCD12864液晶初始化
- HMC5883_Init();//HMC5883初始化
- while(1)
- {
- Multiple_Read_HMC5883();//连续读出数据,存储在Rec_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;
- Acr=(uint)Angle;
- LCD_set_xy(2,0);
- Send_DATA(Acr%10000/1000+0x30,1);
- Send_DATA(Acr%1000/100+0x30,1);
- Send_DATA(Acr%100/10+0x30,1);
- Send_DATA('.',1);
- Send_DATA(Acr%10+0x30,1);
- Delay(50000);
- }
- }
复制代码 上面是参考网上程序改的,好像不行,显示屏一直显示045.0,显示屏用的是12864.不知道是程序问题还是模块的问题。主控用的是AT89S52。模块是GY271-HMC5883L.
|