?? avr_gy-27.c
字號:
/*****************************************
* 基于AVR單片機(jī)GY-27模塊通信程序 *
* HMC5883L+ADXL345 通信程序 *
* 功 能:IIC通信讀取數(shù)據(jù)并顯示 *
* 時鐘頻率:內(nèi)部1M *
* 設(shè) 計:廣運(yùn)電子 *
* 修改日期:2011年4月20日 *
* 編譯環(huán)境:ICC-AVR7.14 *
* 實驗環(huán)境:ATmega16+1602 *
* 使用端口:PC0,PC1,PC6,PC7,PA4~PA7 *
* 參 考:莫錦攀實驗程序24c02讀取實驗 *
*****************************************/
#include <iom16v.h>
#include "I2C.h"
#include "1602.h"
#include "delay.h"
#include "math.h"
#include "stdio.h"
void conversion(unsigned int i);
unsigned char display[5]={0,0,0,0,0};//顯示數(shù)據(jù)
/*********************************************
數(shù)據(jù)轉(zhuǎn)換,十六進(jìn)制數(shù)據(jù)轉(zhuǎn)換成10進(jìn)制
輸入十六進(jìn)制范圍:0x0000-0x270f(0-9999)
結(jié)果分成個十百千位,以ascii存入顯示區(qū)
**********************************************/
void conversion(unsigned int i)
{
display[0]=i/10000+0x30 ;
i=i%10000; //取余運(yùn)算
display[1]=i/1000+0x30 ;
i=i%1000; //取余運(yùn)算
display[2]=i/100+0x30 ;
i=i%100; //取余運(yùn)算
display[3]=i/10+0x30 ;
i=i%10; //取余運(yùn)算
display[4]=i+0x30;
}
//*******************************
//顯示角度
void display_angle(void)
{ float temp;
int x,y;
double angle;
x=I2C_Read(0x03);
x=(x<<8)+I2C_Read(0x04);
y=I2C_Read(0x07);
y=(y<<8)+I2C_Read(0x08);
angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degrees
angle*=10;
conversion(angle); //轉(zhuǎn)換出顯示需要的數(shù)據(jù)
LCD_write_char(0,0,'A'); //第0行,第0列 顯示A
LCD_write_char(1,0,'n'); //
LCD_write_char(2,0,'g'); //
LCD_write_char(3,0,'l'); //
LCD_write_char(4,0,'e'); //
LCD_write_char(5,0,':');
LCD_write_char(6,0,display[1]);
LCD_write_char(7,0,display[2]);
LCD_write_char(8,0,display[3]);
LCD_write_char(9,0,'.');
LCD_write_char(10,0,display[4]);
LCD_write_char(11,0,0xdf);
}
/*******************************
主程序
*******************************/
void main(void)
{
unsigned char i;
delay_nms(50); //lcd上電延時
LCD_init(); //lcd初始化
while(1){ //循環(huán)
I2C_Write(0x02,0x00); //模式寄存器寫0
delay_nms(50);
display_angle(); //顯示角度
delay_nms(50);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -