瑞萨R5F100LE读取mpu6050数据 在1602显示的时候显示不正确
时间:10-02
整理:3721RD
点击:
最近在学习瑞萨R5F100LE,想把51的那个6050程序移植到瑞萨上,在IIC上,我用是直接配置,用瑞萨的IIC通信功能然后在读取6050的数据。可是在显示到1602的时候。数据读取不完整,不能完整的显示出所有读取出的数据。
代码如下:求大神帮忙看看啊!
主函数:
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_serial.h"
#include "r_cg_timer.h"
/* Start user code for include. Do not edit comment generated here */
#include "mpu6050.h"
#include "r_1602.h"
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"
/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
unsigned char add;
unsigned char tx_buf[20];
unsigned char rx_buf[20];
unsigned char rx_len,tx_len;
unsigned int cnt;
//unsigned char dis[4];
/* End user code. Do not edit comment generated here */
void R_MAIN_UserInit(void);
/***********************************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
//R_TAU0_Channel0_Start();
delay(10);
lcd_init();
R_MPU6050_UserInit();
delay(10);
while (1U)
{
int data;
//SCL = P60( 17 ),SDA = P61( 18 )
Display10BitData(Single_ReadI2C(ACCEL_XOUT_H),2,0); //显示X轴加速度
Display10BitData(Single_ReadI2C(ACCEL_YOUT_H),7,0); //显示Y轴加速度
Display10BitData(Single_ReadI2C(ACCEL_ZOUT_H),12,0); //显示Z轴加速度
Display10BitData(Single_ReadI2C(GYRO_XOUT_H),2,1); //显示X轴角速度
Display10BitData(Single_ReadI2C(GYRO_YOUT_H),7,1); //显示Y轴角速度
Display10BitData(Single_ReadI2C(GYRO_ZOUT_H),12,1); //显示Z轴角速度*/
delay(10);
}
/* End user code. Do not edit comment generated here */
}
/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description : This function adds user code before implementing main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
/* Start user code. Do not edit comment generated here */
EI();
/* End user code. Do not edit comment generated here */
}
/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
mpu6050.h
#ifndef _6050_H
#define _6050_H
#include "r_cg_macrodriver.h"
#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为读取
//向I2C设备写入一个字节数据
void Single_WriteI2C(unsigned char REG_Address,unsigned char REG_data)
{
unsigned char buf[2];
unsigned int cnt;
buf[0] = REG_Address; //内部寄存器地址
buf[1] = REG_data; //内部寄存器数据
R_IICA0_Master_Send(SlaveAddress,buf,2,200);
cnt = 10000;
while(cnt>0) cnt--;
}
//从I2C设备读取一个字数据
int Single_ReadI2C(unsigned char REG_Address)
{
unsigned char buf[2];
unsigned int cnt;
buf[0] = REG_Address; //内部寄存器地址
R_IICA0_Master_Send(SlaveAddress,buf,1,200);
cnt = 10000;
while(cnt>0) cnt--;
R_IICA0_Master_Receive(SlaveAddress+1,buf,2,200);
cnt = 10000;
while(cnt>0) cnt--;
return (buf[0]<<8)+buf[1];
}
//初始化MPU6050
void R_MPU6050_UserInit(void)
{
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(unsigned char REG_Address)
{
char H,L;
H=Single_ReadI2C(REG_Address);
L=Single_ReadI2C(REG_Address+1);
return (H<<8)+L; //合成数据
}
#endif
1602.c
#include "r_cg_macrodriver.h"
#include "r_1602.h"
/* Includes LCD defines */
/*******************************************************************************
* Function name : DisplayDelay
* Description : Delay routine for LCD display.
* Argument : (uint32_t) units
* Return value : none
*******************************************************************************/
void DisplayDelay(uint32_t units)
{
uint32_t counter = units * DELAY_TIMING;
while(counter--)
{
/* Delay Loop */
NOP();
}
}
/*******************************************************************************
End of function DisplayDelay
*******************************************************************************/
void delay(uint32_t ms)
{
uint32_t i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
DisplayDelay(8);
}
}
}
bit lcd_busy()
{
uint8_t result;
LCD_RS = 0u;
LCD_RW = 1u;
LCD_EN = 1u;
DisplayDelay(8);
result = (bit)(P7&0x80);
LCD_EN = 0u;
return result;
}
void lcd_wcmd(uint8_t cmd)
{
while(lcd_busy() & 0x80);
LCD_RS = 0u;
LCD_RW = 0u;
LCD_EN = 0u;
DisplayDelay(4);
P7 = cmd;
DisplayDelay(8);
LCD_EN = 1u;
DisplayDelay(8);
LCD_EN = 0u;
}
void lcd_wdat(uint8_t dat)
{
while(lcd_busy() & 0x80);
LCD_RS = 1u;
LCD_RW = 0u;
LCD_EN = 0u;
P7 = dat;
DisplayDelay(8);
LCD_EN = 1u;
DisplayDelay(8);
LCD_EN = 0u;
}
void lcd_pos(uint8_t pos)
{
lcd_wcmd(pos|0x80);
}
void lcd_init()
{
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c);
delay(5);
lcd_wcmd(0x06);
delay(5);
lcd_wcmd(0x01);
delay(5);
DisplayOneChar(0,0,'A');
DisplayOneChar(0,1,'G');
}
void flash()
{
delay(100);
lcd_wcmd(0x08);
delay(50);
lcd_wcmd(0x0c);
delay(50);
lcd_wcmd(0x08);
delay(50);
lcd_wcmd(0x0c);
delay(50);
}
//****************************************
//整数转字符串
//****************************************
void lcd_printf(unsigned char *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;
}
/*void Display10BitData(int value,unsigned char x,unsigned char y)
{
unsigned char dis[4];
int dis_data;
value/=64; //转换为10位数据
lcd_printf(dis, value); //转换数据显示
DisplayListChar(X,Y,dis); //启始列,行,显示数组,显示长度
}*/
void Display10BitData(int value,unsigned char x,unsigned char y)
{
unsigned char dis[4];
value/=64; //转换为10位数据
lcd_printf(dis, value); //转换数据显示
DisplayListChar(x,y,dis); //启始列,行,显示数组,显示长度
}
void LCD_Write_String(unsigned char X,unsigned char Y,unsigned char *s)
{
if (Y == 0)
{
lcd_wcmd(0x80 + X); //表示第一行
}
else
{
lcd_wcmd(0xC0 + X); //表示第二行
}
while (*s)
{
lcd_wdat( *s);
s ++;
}
}
void DisplayListChar(unsigned char X,unsigned char Y,unsigned char *DData)
{
int L;
unsigned char ListLength = 0;
L = 4;
Y&=0x1;
X&=0xF;
while (L--)
{
DisplayOneChar(X,Y,DData[ListLength] );
ListLength++;
X++;
}
}
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else
address = 0xc0 + x;
lcd_wcmd( address );
}
//****************************************
//LCD1602写入一个字符
//****************************************
void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
lcd_wdat(DData);
}
void WriteCommandLCM(unsigned char CMD,unsigned char Attribc)
{
if(Attribc)
while(lcd_busy() & 0x80);
LCD_RS = 0u;
LCD_RW = 0u;
DisplayDelay(8);
P7 = CMD;
DisplayDelay(8);
LCD_EN = 1u;
DisplayDelay(8);
LCD_EN = 0u;
}
下面是51读取显示的
代码如下:求大神帮忙看看啊!
主函数:
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_serial.h"
#include "r_cg_timer.h"
/* Start user code for include. Do not edit comment generated here */
#include "mpu6050.h"
#include "r_1602.h"
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"
/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
unsigned char add;
unsigned char tx_buf[20];
unsigned char rx_buf[20];
unsigned char rx_len,tx_len;
unsigned int cnt;
//unsigned char dis[4];
/* End user code. Do not edit comment generated here */
void R_MAIN_UserInit(void);
/***********************************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
//R_TAU0_Channel0_Start();
delay(10);
lcd_init();
R_MPU6050_UserInit();
delay(10);
while (1U)
{
int data;
//SCL = P60( 17 ),SDA = P61( 18 )
Display10BitData(Single_ReadI2C(ACCEL_XOUT_H),2,0); //显示X轴加速度
Display10BitData(Single_ReadI2C(ACCEL_YOUT_H),7,0); //显示Y轴加速度
Display10BitData(Single_ReadI2C(ACCEL_ZOUT_H),12,0); //显示Z轴加速度
Display10BitData(Single_ReadI2C(GYRO_XOUT_H),2,1); //显示X轴角速度
Display10BitData(Single_ReadI2C(GYRO_YOUT_H),7,1); //显示Y轴角速度
Display10BitData(Single_ReadI2C(GYRO_ZOUT_H),12,1); //显示Z轴角速度*/
delay(10);
}
/* End user code. Do not edit comment generated here */
}
/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description : This function adds user code before implementing main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
/* Start user code. Do not edit comment generated here */
EI();
/* End user code. Do not edit comment generated here */
}
/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
mpu6050.h
#ifndef _6050_H
#define _6050_H
#include "r_cg_macrodriver.h"
#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为读取
//向I2C设备写入一个字节数据
void Single_WriteI2C(unsigned char REG_Address,unsigned char REG_data)
{
unsigned char buf[2];
unsigned int cnt;
buf[0] = REG_Address; //内部寄存器地址
buf[1] = REG_data; //内部寄存器数据
R_IICA0_Master_Send(SlaveAddress,buf,2,200);
cnt = 10000;
while(cnt>0) cnt--;
}
//从I2C设备读取一个字数据
int Single_ReadI2C(unsigned char REG_Address)
{
unsigned char buf[2];
unsigned int cnt;
buf[0] = REG_Address; //内部寄存器地址
R_IICA0_Master_Send(SlaveAddress,buf,1,200);
cnt = 10000;
while(cnt>0) cnt--;
R_IICA0_Master_Receive(SlaveAddress+1,buf,2,200);
cnt = 10000;
while(cnt>0) cnt--;
return (buf[0]<<8)+buf[1];
}
//初始化MPU6050
void R_MPU6050_UserInit(void)
{
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(unsigned char REG_Address)
{
char H,L;
H=Single_ReadI2C(REG_Address);
L=Single_ReadI2C(REG_Address+1);
return (H<<8)+L; //合成数据
}
#endif
1602.c
#include "r_cg_macrodriver.h"
#include "r_1602.h"
/* Includes LCD defines */
/*******************************************************************************
* Function name : DisplayDelay
* Description : Delay routine for LCD display.
* Argument : (uint32_t) units
* Return value : none
*******************************************************************************/
void DisplayDelay(uint32_t units)
{
uint32_t counter = units * DELAY_TIMING;
while(counter--)
{
/* Delay Loop */
NOP();
}
}
/*******************************************************************************
End of function DisplayDelay
*******************************************************************************/
void delay(uint32_t ms)
{
uint32_t i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
DisplayDelay(8);
}
}
}
bit lcd_busy()
{
uint8_t result;
LCD_RS = 0u;
LCD_RW = 1u;
LCD_EN = 1u;
DisplayDelay(8);
result = (bit)(P7&0x80);
LCD_EN = 0u;
return result;
}
void lcd_wcmd(uint8_t cmd)
{
while(lcd_busy() & 0x80);
LCD_RS = 0u;
LCD_RW = 0u;
LCD_EN = 0u;
DisplayDelay(4);
P7 = cmd;
DisplayDelay(8);
LCD_EN = 1u;
DisplayDelay(8);
LCD_EN = 0u;
}
void lcd_wdat(uint8_t dat)
{
while(lcd_busy() & 0x80);
LCD_RS = 1u;
LCD_RW = 0u;
LCD_EN = 0u;
P7 = dat;
DisplayDelay(8);
LCD_EN = 1u;
DisplayDelay(8);
LCD_EN = 0u;
}
void lcd_pos(uint8_t pos)
{
lcd_wcmd(pos|0x80);
}
void lcd_init()
{
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c);
delay(5);
lcd_wcmd(0x06);
delay(5);
lcd_wcmd(0x01);
delay(5);
DisplayOneChar(0,0,'A');
DisplayOneChar(0,1,'G');
}
void flash()
{
delay(100);
lcd_wcmd(0x08);
delay(50);
lcd_wcmd(0x0c);
delay(50);
lcd_wcmd(0x08);
delay(50);
lcd_wcmd(0x0c);
delay(50);
}
//****************************************
//整数转字符串
//****************************************
void lcd_printf(unsigned char *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;
}
/*void Display10BitData(int value,unsigned char x,unsigned char y)
{
unsigned char dis[4];
int dis_data;
value/=64; //转换为10位数据
lcd_printf(dis, value); //转换数据显示
DisplayListChar(X,Y,dis); //启始列,行,显示数组,显示长度
}*/
void Display10BitData(int value,unsigned char x,unsigned char y)
{
unsigned char dis[4];
value/=64; //转换为10位数据
lcd_printf(dis, value); //转换数据显示
DisplayListChar(x,y,dis); //启始列,行,显示数组,显示长度
}
void LCD_Write_String(unsigned char X,unsigned char Y,unsigned char *s)
{
if (Y == 0)
{
lcd_wcmd(0x80 + X); //表示第一行
}
else
{
lcd_wcmd(0xC0 + X); //表示第二行
}
while (*s)
{
lcd_wdat( *s);
s ++;
}
}
void DisplayListChar(unsigned char X,unsigned char Y,unsigned char *DData)
{
int L;
unsigned char ListLength = 0;
L = 4;
Y&=0x1;
X&=0xF;
while (L--)
{
DisplayOneChar(X,Y,DData[ListLength] );
ListLength++;
X++;
}
}
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else
address = 0xc0 + x;
lcd_wcmd( address );
}
//****************************************
//LCD1602写入一个字符
//****************************************
void DisplayOneChar(unsigned char X,unsigned char Y,unsigned char DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
lcd_wdat(DData);
}
void WriteCommandLCM(unsigned char CMD,unsigned char Attribc)
{
if(Attribc)
while(lcd_busy() & 0x80);
LCD_RS = 0u;
LCD_RW = 0u;
DisplayDelay(8);
P7 = CMD;
DisplayDelay(8);
LCD_EN = 1u;
DisplayDelay(8);
LCD_EN = 0u;
}
下面是51读取显示的