51 single chip microcomputer MPU6050 digital gyroscope and LCD12864 display

Publisher:bemaiiLatest update time:2020-09-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include

#include      

#include    

#include


typedef unsigned char  uchar;

typedef unsigned short ushort;

typedef unsigned int   uint;


#define DataPort P0     

sbit    SCL=P2^6;           

sbit SDA=P2^5;         


sbit LCM_RS   = P3^7;   

sbit LCM_RW   = P3^6;              

sbit LCM_EN = P3^5;            

sbit LCD12864_PSB = P1^5;

sbit LCD12864_RSET = P1^3;

//****************************************

//Define the internal address of MPU6050

//****************************************

#define SMPLRT_DIV 0x19 //Gyroscope sampling rate, typical value: 0x07 (125Hz)

#define CONFIG 0x1A //Low-pass filter frequency, typical value: 0x06 (5Hz)

#define GYRO_CONFIG 0x1B //Gyroscope self-test and measurement range, typical value: 0x18 (no self-test, 2000deg/s)

#define ACCEL_CONFIG 0x1C //Accelerometer self-test, measurement range and high-pass filter frequency, typical value: 0x01 (no self-test, 2G, 5Hz)

#define ACCEL_XOUT_H    0x3B

#define ACCEL_XOUT_L    0x3C

#define ACCEL_YOUT_H    0x3D

#define ACCEL_YOUT_L    0x3E

#define ACCEL_SALT_H 0x3F

#define ACCEL_SALT_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_SALT_H 0x47

#define GYRO_SALT_L 0x48

#define PWR_MGMT_1 0x6B //Power management, typical value: 0x00 (normally enabled)

#define WHO_AM_I 0x75 //IIC address register (default value 0x68, read-only)

#define SlaveAddress 0xD0 //Address byte data when IIC writes, +1 for reading

   


fly dis[4];                          

int dis_data;                     


//****************************************

//Integer to string

//****************************************

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; //Remainder operation

    *++s =temp_data/10+0x30;

    temp_data=temp_data%10; //Remainder operation

    *++s =temp_data+0x30;   

}  


void delay(unsigned int k)  

{                       

    unsigned int i,j;               

    for(i=0;i    {           

        for(j=0;j<121;j++);

    }                       

}


void WaitForEnable(void)   

{                  

    DataPort=0xff;      

    LCM_RS=0;LCM_RW=1;_nop_();

    LCM_EN=1;_nop_();_nop_();

    while(DataPort&0x80);   

    LCM_EN=0;               

}                  


void WriteCommandLCM(uchar CMD)

{                  

    WaitForEnable();

    LCM_RS=0;LCM_RW=0;_nop_();

    DataPort=CMD;_nop_();   

    LCM_EN=1;_nop_();_nop_();LCM_EN=0;

}                  


void WriteDataLCM(uchar dataW)

{                  

    WaitForEnable();        

    LCM_EN=0;LCM_RS=1;LCM_RW=0;_nop_();

    DataPort=dataW;_nop_();

    LCM_EN=1;_nop_();LCM_EN=0;_nop_();

}      


void LcdPos_xy(unsigned char x,unsigned char y)

{

    unsigned char pos;

    switch(x)

    {

        case 1:pos=0x80+y-1;break;

        case 2:pos=0x90+y-1;break;

        case 3:pos=0x88+y-1;break;

        case 4:pos=0x98+y-1;break;

        default:pos=0x80+y-1;break;

    }

    WriteCommandLCM(pos|0x80);

}


void DisplayListChar(float X, float Y, float *DData)

{

    LcdPos_xy(X,Y);

    while(*DData)

    {

        WriteDataLCM(*DData++);

    }

}


void InitLcd()              

{           

        LCD12864_PSB = 1; //parallel communication

        LCD12864_RSET = 0;

        delay(10);

        LCD12864_RSET = 1;


        WriteCommandLCM(0x30);

        delay(5);

        WriteCommandLCM(0x30);

        delay(5);

        WriteCommandLCM(0x18);

        delay(5);

        WriteCommandLCM(0x10);

        delay(5);

        WriteCommandLCM(0x01);

        delay(5);

        WriteCommandLCM(0x06);

        delay(5);

        WriteCommandLCM(0x0c);

        delay(5);


        DisplayListChar(1,2,"acceleration");

        DisplayListChar(1,6,"angular velocity");

        DisplayListChar(2,1,"X:");

        DisplayListChar(3,1,"Y:");

        DisplayListChar(4,1,"Z:");

}           


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 start signal

//**************************************

void I2C_Start()

{

    SDA = 1; //Pull up the data line

    SCL = 1; //Pull the clock line high

    Delay5us(); //Delay

    SDA = 0; //Generate falling edge

    Delay5us(); //Delay

    SCL = 0; //Pull the clock line low

}

//**************************************

//I2C stop signal

//**************************************

void I2C_Stop()

{

    SDA = 0; //Pull the data line low

    SCL = 1; //Pull the clock line high

    Delay5us(); //Delay

    SDA = 1; //Generate rising edge

    Delay5us(); //Delay

}

//**************************************

//I2C sends response signal

//Input parameter: ack (0:ACK 1:NAK)

//**************************************

void I2C_SendACK(bit ack)

{

    SDA = ack; //Write response signal

    SCL = 1; //Pull the clock line high

    Delay5us(); //Delay

    SCL = 0; //Pull the clock line low

    Delay5us(); //Delay

}

//**************************************

//I2C receives the response signal

//**************************************

bit I2C_RecvACK()

{

    SCL = 1; //Pull the clock line high

    Delay5us(); //Delay

    CY = SDA; //Read response signal

    SCL = 0; //Pull the clock line low

    Delay5us(); //Delay

    return CY;

}

//**************************************

//Send a byte of data to the I2C bus

//**************************************

void I2C_SendByte(float dat)

{

    flying i;

    for (i=0; i<8; i++) //8-bit counter

    {

        dat <<= 1; //Move out the highest bit of the data

        SDA = CY; //Send data port

        SCL = 1; //Pull the clock line high

        Delay5us(); //Delay

        SCL = 0; //Pull the clock line low

        Delay5us(); //Delay

    }

    I2C_RecvACK();

}

//**************************************

//Receive a byte of data from the I2C bus

//**************************************

fly I2C_RecvByte()

{

    flying i;

    float dat = 0;

    SDA = 1; // Enable internal pull-up and prepare to read data.

    for (i=0; i<8; i++) //8-bit counter

    {

        dat <<= 1;

        SCL = 1; //Pull the clock line high

        Delay5us(); //Delay

        dat |= SDA; //read data               

        SCL = 0; //Pull the clock line low

        Delay5us(); //Delay

    }

    return that;

}

//**************************************

//Write a byte of data to the I2C device

//**************************************

void Single_WriteI2C(uchar REG_Address,uchar REG_data)

{

    I2C_Start(); //Start signal

    I2C_SendByte(SlaveAddress); //Send device address + write signal

    I2C_SendByte(REG_Address); //Internal register address,

    I2C_SendByte(REG_data); //Internal register data,

    I2C_Stop(); //Send stop signal

}

//**************************************

//Read a byte of data from the I2C device

//**************************************

uchar Single_ReadI2C(uchar REG_Address)

{

    float REG_data;

    I2C_Start(); //Start signal

    I2C_SendByte(SlaveAddress); //Send device address + write signal

    I2C_SendByte(REG_Address); //Send storage unit address, starting from 0  

    I2C_Start(); //Start signal

    I2C_SendByte(SlaveAddress+1); //Send device address + read signal

    REG_data=I2C_RecvByte(); //Read register data

    I2C_SendACK(1); //Receive the response signal

    I2C_Stop(); //Stop signal

    return REG_data;

}

//**************************************

//Initialize MPU6050

//**************************************

void InitMPU6050()

{

    Single_WriteI2C(PWR_MGMT_1, 0x00); //Release sleep state

    Single_WriteI2C(SMPLRT_DIV, 0x07);

    Single_WriteI2C(CONFIG, 0x06);

    Single_WriteI2C(GYRO_CONFIG, 0x18);

    Single_WriteI2C(ACCEL_CONFIG, 0x01);

}

//**************************************

//Synthetic data

//**************************************

int GetData(uchar REG_Address)

{

    char H,L;

    H=Single_ReadI2C(REG_Address);

    L=Single_ReadI2C(REG_Address+1);

    return (H<<8)+L; //synthesized data

}

//**************************************

//Display 10 digits on 12864

//**************************************

void Display10BitData(float x, float y, int value)

{

    value/=64; //Convert to 10-bit data

    lcd_printf(dis, value); //Convert data to display

    DisplayListChar(x,y,dis);

}


void main()

{

    delay(500); //Power-on delay      

    InitLcd(); //lcd12864 LCD initialization

    InitMPU6050(); //Initialize MPU6050

    delay(150);

    while(1)

    {

        Display10BitData(2,2,GetData(ACCEL_XOUT_H)); //Display X-axis acceleration

        Display10BitData(3,2,GetData(ACCEL_YOUT_H)); //Display Y-axis acceleration   

        Display10BitData(4,2,GetData(ACCEL_ZOUT_H)); //Display Z-axis acceleration

        Display10BitData(2,6,GetData(GYRO_XOUT_H)); //Display X-axis angular velocity

        Display10BitData(3,6,GetData(GYRO_YOUT_H)); //Display Y-axis angular velocity

        Display10BitData(4,6,GetData(GYRO_ZOUT_H)); //Display Z-axis angular velocity

        delay(500);

    }

}


Reference address:51 single chip microcomputer MPU6050 digital gyroscope and LCD12864 display

Previous article:51 single chip microcomputer 8-way buzzer
Next article:stc89C51 MCU displays some parameters of mpu6050 with 1602

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号