stc89C51 MCU displays some parameters of mpu6050 with 1602

Publisher:自在逍遥Latest update time:2020-09-11 Source: 51heiKeywords:stc89C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The microcontroller source program is as follows:


#include         

#include //Keil library  

#include //Keil library        

#include

#include "lcd1602.h"


typedef unsigned short ushort;

typedef unsigned int uint;


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

#define DataPort P0 //LCD1602 data port

sbit SCL=P2^1; //IIC clock pin definition

sbit SDA=P2^0; //IIC data pin definition

sbit LCM_RS=P3^5; //LCD1602 command port               

               

sbit LCM_EN=P3^4; //LCD1602 command port

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

//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) 0x09 0x11 0x19 4G 8G 16G

#define ACCEL_XOUT_H 0x3B

#define ACCEL_XOUT_L 0x3C // Acceleration X-axis output

#define ACCEL_YOUT_H 0x3D

#define ACCEL_YOUT_L 0x3E //Acceleration Y axis output

#define ACCEL_ZOUT_H 0x3F

#define ACCEL_ZOUT_L 0x40 // Acceleration Z axis output

#define TEMP_OUT_H 0x41

#define TEMP_OUT_L 0x42

#define GYRO_XOUT_H 0x43

#define GYRO_XOUT_L 0x44 // Angular velocity X-axis output        

#define GYRO_YOUT_H 0x45

#define GYRO_YOUT_L 0x46 // Angular velocity Y axis output

#define GYRO_ZOUT_H 0x47

#define GYRO_ZOUT_L 0x48 // Angular velocity Z axis output

#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 is for reading MPU I2C device address, slave address

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

//Define types and variables

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

uchar dis[6]; //Character array showing numbers (-511 to 512)

int dis_data; //variable

//int Temperature,Temp_h,Temp_l; //Temperature and high and low bit data

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

// Function declaration

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

void delay(unsigned int k); //delay                                                

//void lcd_printf(uchar *s,int temp_data);


//MPU6050 operation function

void InitMPU6050(); //Initialize MPU6050

void 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); //Read I2C data

void Single_WriteI2C(uchar REG_Address,uchar REG_data); //Write data to I2C

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

//Integer to string integer split and display on LCD screen

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

void lcd_printf(uchar *s,int temp_data)

{

        if(temp_data<0)

        {

                temp_data=-temp_data;

                *s='-';

        }

        else *s=' ';


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

        temp_data=temp_data%10000; //Remainder operation


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

        temp_data=temp_data%1000; //Remainder operation


        *++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 SeriPushSend(uchar send_data)

{

    SBUF=send_data; // Send data

        while(!TI);TI=0; //Wait for data to be sent

}

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

//Delay

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

void delay(unsigned int k)        

{                                                

        unsigned int i,j;                                

        for(i=0;i        {                        

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

        }                                                

}


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

//Delay 5 microseconds (STC90C52RC@12M) //TX-1C Tcy=1us

//Different working environments require adjustment of this function

//When using 1T MCU, please adjust this delay function

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

void Delay5us()

{

        _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(uchar dat)

{

    uchar 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

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

uchar I2C_RecvByte()

{

    uchar i;

    uchar 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 dat;

}

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

//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 a specific I2C device address + write signal (not plus 1)

    I2C_SendByte(REG_Address); //Internal register address,

    I2C_SendByte(REG_data); //Internal register data,

    I2C_Stop(); //Send stop signal

}

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

//Read a byte of data (8 bits) from the I2C device

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

uchar Single_ReadI2C(uchar REG_Address)

{

        uchar REG_data;

        I2C_Start(); //Start signal

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

        I2C_SendByte(REG_Address); //Send the storage unit address, starting from 0 to find the specific address in the slave (MPU6050 specific internal storage addressing)

        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); // Power management, typical value: 0x00 (normally enabled) Release sleep state

        Single_WriteI2C(SMPLRT_DIV, 0x07); //Gyroscope sampling rate, typical value: 0x07 (125Hz)

        Single_WriteI2C(CONFIG, 0x06); //Low-pass filter frequency, typical value: 0x06 (5Hz)

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

        Single_WriteI2C(ACCEL_CONFIG, 0x01); //Accelerometer self-test, measurement range and high-pass filter frequency, typical value: 0x01 (no self-test, 2G, 5Hz) 0x09 0x11 0x19 4G 8G 16G


……………………


Keywords:stc89C51 Reference address:stc89C51 MCU displays some parameters of mpu6050 with 1602

Previous article:51 single chip microcomputer MPU6050 digital gyroscope and LCD12864 display
Next article:Speed ​​measurement based on 51 single chip PWM speed regulation digital tube display

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号