/*
============================================================================Name : ZTW_51.cAuthor : clare_liuVersion :Copyright : Your copyright noticeDescription : Hello World in C, Ansi-style============================================================================*///// Update to MPU6050 by shinetop// MCU: STC89C52// 2012.3.1// 功用: 显现加快度计和陀螺仪的10位原始数据//// GY-52 MPU3050 IIC测验程序// 运用单片机STC89C51// 晶振:11.0592M// 显现:LCD1602// 编译环境 Keil uVision2// 参阅宏晶网站24c04通讯程序// 时刻:2011年9月1日// QQ:531389319//#include#include //Keil library#include //Keil library#include typedef unsigned char uchar;typedef unsigned short ushort;typedef unsigned int uint;//// 界说51单片机端口//#define DataPort P0 //LCD1602数据端口sbit SCL = P1 ^ 0; //IIC时钟引脚界说sbit SDA = P1 ^ 1; //IIC数据引脚界说sbit LCM_RS = P2 ^ 0; //LCD1602指令端口sbit LCM_RW = P2 ^ 1; //LCD1602指令端口sbit LCM_EN = P2 ^ 2; //LCD1602指令端口sbit JDQ = P3 ^ 7; //继电器//矩阵式键盘按键值的数码管显现试验sbit P14 = P1 ^ 4; //将P14位界说为P1.4引脚sbit P15 = P1 ^ 5; //将P15位界说为P1.5引脚sbit P16 = P1 ^ 6; //将P16位界说为P1.6引脚sbit P17 = P1 ^ 7; //将P17位界说为P1.7引脚sbit P30 = P3 ^ 0; unsigned char code Tab[ ]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数字0~9的段码unsigned char keyval; //界说变量贮存按键值//// 界说MPU6050内部地址//#define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值:0x07(125Hz)#define CONFIG 0x1A //低通滤波频率,典型值:0x06(5Hz)#define GYRO_CONFIG 0x1B //陀螺仪自检及丈量规模,典型值:0x18(不自检,2000deg/s)#define ACCEL_CONFIG 0x1C //加快计自检、丈量规模及高通滤波频率,典型值:0x01(不自检,2G,5Hz)#define ACCEL_XOUT_H 0x3B#define ACCEL_XOUT_L 0x3C#define ACCEL_YOUT_H 0x3D#define ACCEL_YOUT_L 0x3E#define ACCEL_ZOUT_H 0x3F#define ACCEL_ZOUT_L 0x40#define TEMP_OUT_H 0x41#define TEMP_OUT_L 0x42#define GYRO_XOUT_H 0x43#define GYRO_XOUT_L 0x44#define GYRO_YOUT_H 0x45#define GYRO_YOUT_L 0x46#define GYRO_ZOUT_H 0x47#define GYRO_ZOUT_L 0x48#define PWR_MGMT_1 0x6B //电源办理,典型值:0x00(正常启用)#define WHO_AM_I 0x75 //IIC地址寄存器(默许数值0x68,只读)#define SlaveAddress 0xD0 //IIC写入时的地址字节数据,+1为读取////界说类型及变量///////////////////////////////////////////////////////////* **关于频率和占空比的确认,关于12M晶振,假定PWM输出频率为1KHZ,这样守时中止次数 **设定为C=10,即0.01MS中止一次,则TH0=FF,TL0=F6;我们设定中止时刻为0.01ms,这样**能够设定占空比可从1-100改变。即0.01ms*100=1ms *//** TH0和TL0是计数器0的高8位和低8位计数器,核算方法:TL0=(65536-C)%256; ** TH0=(65536-C)/256,其间C为所要计数的次数即多长时刻发生一次中止;TMOD是计数器** 作业形式挑选,0X01一共选用形式1,它有16位计数器,最大计数脉冲为65536,最长时 ** 间为1us*65536=65.536ms */#define V_TH0 0Xff#define V_TL0 0X38#define V_TMOD 0X11uchar dis[4]; //显现数字(-511至512)的字符数组int dis_data; //变量int Temperature, Temp_h, Temp_l; //温度及凹凸位数据unsigned char ZKB1, ZKB2;uchar head=2;uchar slot_a=1;uchar ch_a=3;uchar ch_a_h=1; //可调 1~2uchar slot_b=1;uchar ch_b=3;uchar ch_b_h=1; //可调 1~2uchar slot_c=1;uchar ch_c=3;uchar ch_c_h=1; //可调 1~2uchar slot_d=1;uchar ch_d=3;uchar ch_d_h=1; //可调 1~2////函数声明//void delay(unsigned int k); //延时//LCD相关函数void InitLcd(); //初始化lcd1602void lcd_printf(uchar *s, int temp_data);void WriteDataLCM(uchar dataW); //LCD数据void WriteCommandLCM(uchar CMD, uchar Attribc); //LCD指令void DisplayOneChar(uchar X, uchar Y, uchar DData); //显现一个字符void DisplayListChar(uchar X, uchar Y, uchar *DData, L); //显现字符串//MPU6050操作函数void InitMPU6050(); //初始化MPU6050void Delay5us();void I2C_Start();void I2C_Stop();void I2C_SendACK(bit ack);bit I2C_RecvACK();void I2C_SendByte(uchar dat);uchar I2C_RecvByte();void I2C_ReadPage();void I2C_WritePage();void display_ACCEL_x();void display_ACCEL_y();void display_ACCEL_z();uchar Single_ReadI2C(uchar REG_Address); //读取I2C数据void Single_WriteI2C(uchar REG_Address, uchar REG_data); //向I2C写入数据//////////////////////////////////////////////////////////////////void init_sys(void); /*体系初始化函数*/void Delay5Ms(void);///////////////////////////////////////////////////////////////////函数功用:数码管动态扫描延时/void led_delay(void) {unsigned char j;for (j = 0; j < 200; j++);}/函数功用:按键值的数码管显现子程序/void display(unsigned char k,unsigned char z) {P2 = 0xf7; //点亮数码管DS0 1111 0111P0 = Tab[z / 10]; //显现十位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;P2 = 0xef; //点亮数码管DS1 1110 1111 P0 = Tab[z % 10]; //显现个位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;P2 = 0xbf; //点亮数码管DS6 1011 1111P0 = Tab[k / 10]; //显现十位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;P2 = 0x7f; //点亮数码管DS7 0111 1111P0 = Tab[k % 10]; //显现个位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;}/函数功用:软件延时子程序/void delay20ms(void) {unsigned char i, j;for (i = 0; i < 100; i++)for (j = 0; j < 60; j++);}////整数转字符串//void lcd_printf(uchar *s, int temp_data) {if (temp_data < 0) {temp_data = -temp_data;*s = -;} else*s = ;*++s = temp_data / 100 + 0x30;temp_data = temp_data % 100; //取余运算*++s = temp_data / 10 + 0x30;temp_data = temp_data % 10; //取余运算*++s = temp_data + 0x30;}////延时 k 毫秒//void delayNms(unsigned int k) {unsigned int i, j;for (i = 0; i < k; i++) {for (j = 0; j < 125; j++);}}////LCD1602初始化//void InitLcd() {WriteCommandLCM(0x38, 1);WriteCommandLCM(0x08, 1);WriteCommandLCM(0x01, 1);WriteCommandLCM(0x06, 1);WriteCommandLCM(0x0c, 1);DisplayOneChar(0, 0, A); DisplayOneChar(0, 1, G); }////LCD1602写答应//void WaitForEnable(void) {DataPort = 0xff;LCM_RS = 0;LCM_RW = 1;_nop_();LCM_EN = 1;_nop_();_nop_();while (DataPort & 0x80);LCM_EN = 0;}////LCD1602写入指令//void WriteCommandLCM(uchar CMD, uchar Attribc) {if (Attribc)WaitForEnable();LCM_RS = 0;LCM_RW = 0;_nop_();DataPort = CMD;_nop_();LCM_EN = 1;_nop_();_nop_();LCM_EN = 0;}////LCD1602写入数据//void WriteDataLCM(uchar dataW) {WaitForEnable();LCM_RS = 1;LCM_RW = 0;_nop_();DataPort = dataW;_nop_();LCM_EN = 1;_nop_();_nop_();LCM_EN = 0;}////LCD1602写入一个字符//void DisplayOneChar(uchar X, uchar Y, uchar DData) {Y &= 1;X &= 15;if (Y)X |= 0x40;X |= 0x80;WriteCommandLCM(X, 0);WriteDataLCM(DData);}////LCD1602显现字符串//void DisplayListChar(uchar X, uchar Y, uchar *DData, L) {uchar ListLength = 0;Y &= 0x1;X &= 0xF;while (L--) {DisplayOneChar(X, Y, DData[ListLength]);ListLength++;X++;}}////延时5微秒(STC90C52RC@12M)//不同的作业环境,需求调整此函数//当改用1T的MCU时,请调整此延时函数//void Delay5us() {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}////I2C起始信号//void I2C_Start() {SDA = 1; //拉高数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 0; //发生下降沿Delay5us(); //延时SCL = 0; //拉低时钟线}////I2C中止信号//void I2C_Stop() {SDA = 0; //拉低数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 1; //发生上升沿Delay5us(); //延时}////I2C发送应对信号//进口参数:ack (0:ACK 1:NAK)//void I2C_SendACK(bit ack) {SDA = ack; //写应对信号SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}////I2C接纳应对信号//bit I2C_RecvACK() {SCL = 1; //拉高时钟线Delay5us(); //延时CY = SDA; //读应对信号SCL = 0; //拉低时钟线Delay5us(); //延时return CY;}////向I2C总线发送一个字节数据//void I2C_SendByte(uchar dat) {uchar i;for (i = 0; i < 8; i++) //8位计数器{dat <<= 1; //移出数据的最高位SDA = CY; //送数据口SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}I2C_RecvACK();}////从I2C总线接纳一个字节数据//uchar I2C_RecvByte() {uchar i;uchar dat = 0;SDA = 1; //使能内部上拉,预备读取数据,for (i = 0; i < 8; i++) //8位计数器{dat <<= 1;SCL = 1; //拉高时钟线Delay5us(); //延时dat |= SDA; //读数据SCL = 0; //拉低时钟线Delay5us(); //延时}return dat;}////向I2C设备写入一个字节数据//void Single_WriteI2C(uchar REG_Address, uchar REG_data) {I2C_Start(); //起始信号I2C_SendByte(SlaveAddress); //发送设备地址+写信号I2C_SendByte(REG_Address); //内部寄存器地址,I2C_SendByte(REG_data); //内部寄存器数据,I2C_Stop(); //发送中止信号}////从I2C设备读取一个字节数据//uchar Single_ReadI2C(uchar REG_Address) {uchar REG_data;I2C_Start(); //起始信号I2C_SendByte(SlaveAddress); //发送设备地址+写信号I2C_SendByte(REG_Address); //发送存储单元地址,从0开端I2C_Start(); //起始信号I2C_SendByte(SlaveAddress + 1); //发送设备地址+读信号REG_data = I2C_RecvByte(); //读出寄存器数据I2C_SendACK(1); //接纳应对信号I2C_Stop(); //中止信号return REG_data;}////初始化MPU6050//void InitMPU6050() {Single_WriteI2C(PWR_MGMT_1, 0x00); //免除休眠状况Single_WriteI2C(SMPLRT_DIV, 0x07);Single_WriteI2C(CONFIG, 0x06);Single_WriteI2C(GYRO_CONFIG, 0x18);Single_WriteI2C(ACCEL_CONFIG, 0x01);}////组成数据//int GetData(uchar REG_Address) {char H, L;H = Single_ReadI2C(REG_Address);L = Single_ReadI2C(REG_Address + 1);return (H << 8) + L; //组成数据}////在1602上显现10位数据//void Display10BitData(int value, uchar x, uchar y) {value /= 64; //转化为10位数据lcd_printf(dis, value); //转化数据显现DisplayListChar(x, y, dis, 4); //启始列,行,显现数组,显现长度}////显现温度//void display_temp() {Temp_h = Single_ReadI2C(TEMP_OUT_H); //读取温度Temp_l = Single_ReadI2C(TEMP_OUT_L); //读取温度Temperature = Temp_h << 8 | Temp_l; //组成温度Temperature = 35 + ((double) (Temperature + 13200)) / 280; // 核算出温度lcd_printf(dis, Temperature); //转化数据显现DisplayListChar(11, 1, dis, 4); //启始列,行,显现数组,显现位数}//延时void Delay5Ms(void) {unsigned int TempCyc = 100000;while (TempCyc--);}/*函数功用:对体系进行初始化,包含守时器初始化和变量初始化*/void init_timer_interrupt(void) /*体系初始化函数*/{/*PWM守时器T0初始化,每隔0.2ms发生一次中止,合计100次,担任发生周期为20ms的PPM信号*/TMOD = V_TMOD;TH0 = V_TH0;TL0 = V_TL0;/*keyboard 守时器T1初始化,*/TH1 = (65536 - 500) / 256; //守时器T0的高8位赋初值TL1 = (65536 - 500) % 256; //守时器T0的高8位赋初值keyval = 0x00; //按键值初始化为0/*翻开并使能中止,发动守时器T0和T1*/// EA = 1; //开总中止// ET0 = 1; //守时器T0中止答应 // TR0 = 1; //发动守时器T0// ET1 = 1; //守时器T1中止答应// TR1 = 1; //发动守时器T1// EX0=1; //答应运用外中止// IT0=1; //挑选负跳变来触发外中止// EX1=1; //答应运用外中止// IT1=1; //挑选负跳变来触发外中止}//*//主程序//*void main() {///*T0 & T1 initial///init_timer_interrupt();///*mpu6050 initial///delayNms(500); //上电延时500msInitLcd(); //液晶初始化InitMPU6050(); //初始化MPU6050delayNms(150); //上电延时150mswhile(1){Display10BitData(GetData(ACCEL_XOUT_H),2,0); //显现X轴加快度Display10BitData(GetData(ACCEL_YOUT_H),7,0); //显现Y轴加快度Display10BitData(GetData(ACCEL_ZOUT_H),12,0); //显现Z轴加快度Display10BitData(GetData(GYRO_XOUT_H),2,1); //显现X轴角速度Display10BitData(GetData(GYRO_YOUT_H),7,1); //显现Y轴角速度Display10BitData(GetData(GYRO_ZOUT_H),12,1); //显现Z轴角速度delayNms(500);}ZKB1 = 5; /*占空比初始值设定*/ZKB2 = 99; /*占空比初始值设定*/while (1) //无限循环{//keyboard eventdisplay(keyval,ZKB1); //调用按键值的数码管显现子程序led_delay(); //动态扫描延时 //hand eventif (!P1_4) //假如按了+键,添加占空比{Delay5Ms();if (!P1_4) {ZKB1++;JDQ = ~JDQ;}}if (!P1_5) //假如按了-键,削减占空比{Delay5Ms();if (!P1_5) {ZKB1--;JDQ = ~JDQ;}}/*对占空比值限制规模*/if (ZKB1 > 10)ZKB1 = 5;if (ZKB1 < 5)ZKB1 = 10;}}/函数功用:外部中止1/void EXT_INIT0_interserve(void) interrupt 0 using 0{ZKB1++;ZKB2 = 100 - ZKB1;JDQ = ~JDQ;}/函数功用:守时器T0的中止服务子程序,每隔0.2ms发生一次中止,重复100次,构成周期为20ms的周期信号(PPM信号)/void timer0_interserve(void) interrupt 1 using 1{static uchar click=0; /*中止次数计数器变量*/TH0=V_TH0; /*康复守时器初始值*/TL0=V_TL0;TR0 = 1; //发动守时器T0++click;if (click>100) click=0; /* 依据keyval值设置占空比ZKB1的巨细(1~99),输出所需求的PPM信号* */ if (click<=ZKB1) /*当小于占空比值时输出低电平,高于时是高电平,然后完成占空比的调整*/{ P1_0=1; }else{ P1_0=0; }}/函数功用:外部中止1/void EXT_INIT1_interserve(void) interrupt 2 using 2{ZKB1--;ZKB2 = 100 - ZKB1;JDQ = ~JDQ;}/函数功用:守时器T1的中止服务子程序,进行键盘扫描,判别键位,发生中止距离为500us=0.5ms/void timer1_interserve(void) interrupt 3 using 3 //守时器T1的中止编号为3,运用第3组寄存器{TR1=0; //封闭守时器T0P1=0xf0;//一切行线置为低电平“0”,一切列线置为高电平“1”if((P1&0xf0)!=0xf0)//列线中有一位为低电平“0”,阐明有键按下delay20ms();//延时一段时刻、软件消抖if((P1&0xf0)!=0xf0)//的确有键按下{P1=0xfe; //榜首行置为低电平“0”(P1.0输出低电平“0”)if(P14==0)//假如检测到接P1.4引脚的列线为低电平“0”keyval=1;//可判别是S1键被按下if(P15==0)//假如检测到接P1.5引脚的列线为低电平“0”keyval=2;//可判别是S2键被按下if(P16==0)//假如检测到接P1.6引脚的列线为低电平“0”keyval=3;//可判别是S3键被按下if(P17==0)//假如检测到接P1.7引脚的列线为低电平“0”keyval=4;//可判别是S4键被按下P1=0xfd;//第二行置为低电平“0”(P1.1输出低电平“0”)if(P14==0)//假如检测到接P1.4引脚的列线为低电平“0”keyval=5;//可判别是S5键被按下if(P15==0)//假如检测到接P1.5引脚的列线为低电平“0”keyval=6;//可判别是S6键被按下if(P16==0)//假如检测到接P1.6引脚的列线为低电平“0”keyval=7;//可判别是S7键被按下if(P17==0)//假如检测到接P1.7引脚的列线为低电平“0”keyval=8;//可判别是S8键被按下P1=0xfb;//第三行置为低电平“0”(P1.2输出低电平“0”)if(P14==0)//假如检测到接P1.4引脚的列线为低电平“0”keyval=9;//可判别是S9键被按下if(P15==0)//假如检测到接P1.5引脚的列线为低电平“0”keyval=10;//可判别是S10键被按下if(P16==0)//假如检测到接P1.6引脚的列线为低电平“0”keyval=11;//可判别是S11键被按下if(P17==0)//假如检测到接P1.7引脚的列线为低电平“0”keyval=12;//可判别是S12键被按下P1=0xf7;//第四行置为低电平“0”(P1.3输出低电平“0”)if(P14==0)//假如检测到接P1.4引脚的列线为低电平“0”keyval=13;//可判别是S13键被按下if(P15==0)//假如检测到接P1.5引脚的列线为低电平“0”keyval=14;//可判别是S14键被按下if(P16==0)//假如检测到接P1.6引脚的列线为低电平“0”keyval=15;//可判别是S15键被按下if(P17==0)//假如检测到接P1.7引脚的列线为低电平“0”keyval=16;//可判别是S16键被按下}TR1=1; //舱位守时器T0TH1=(65536-500)/256;//守时器T0的高8位赋初值TL1=(65536-500)%256;//守时器T0的高8位赋初值}