}
//************************************** //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/=19; value-=20; //角度值基准校正 //转换为10位数据 lcd_printf(dis, value); //转换数据显示 DisplayListChar(x,y,dis,4); //启始列,行,显示数组,显示长度 }
void main() {
int
i,j,k,m,n,num0=380,num1,num2=380,num3,num4=380,num5,num6=380,num7,cun1,t1,t2; char c='A'; delay(1000); //上电延时 InitLcd(); //液晶初始化 InitMPU6050(); //初始化MPU6050 delay(500); while(1) { for(i=0;i<50000;i++) { if(key0==0) { delay(4000); if(key0==0) { c++; } } if(key1==0)
{
delay(4000); if(key1==0) { c--; } }
if(key2==0&&c=='A') {
delay(4000); if(key2==0) {
num0+=19; num1++; } }
if(key3==0&&c=='A') {
delay(4000); if(key3==0) {
num0-=19; num1--; } }
if(key2==0&&c=='B') {
delay(4000); if(key2==0) {
num2+=19; num3++; } }
if(key3==0&&c=='B') {
delay(4000); if(key3==0) {
num2-=19;
num3--; } }
if(key2==0&&c=='C') {
delay(4000); if(key2==0) {
num4+=19; num5++; } }
if(key3==0&&c=='C') {
delay(4000); if(key3==0) {
num4-=19; num5--; } }
if(key2==0&&c=='D') {
delay(4000); if(key2==0) {
num6+=19; num7++; } }
if(key3==0&&c=='D') {
delay(4000); if(key3==0) {
num6-=19; num7--; } }