135 lines
4.1 KiB
C
135 lines
4.1 KiB
C
|
|
#include "M451Series.h"
|
|
#include <stdio.h>
|
|
#include "hal.h"
|
|
#define LCD_I2C_PORT I2C0
|
|
|
|
int MPUWriteReg(char ad,char RegAddr, char pucDATD_AA)
|
|
{
|
|
I2C_START(LCD_I2C_PORT); //Start
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
//LCD_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag
|
|
|
|
I2C_SET_DATA(LCD_I2C_PORT, ad); //send slave address
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
//LCD_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag
|
|
|
|
I2C_SET_DATA(LCD_I2C_PORT, RegAddr); //send index
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
|
|
//LCD_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag
|
|
|
|
I2C_SET_DATA(LCD_I2C_PORT, pucDATD_AA); //send Data
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
//LCD_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag
|
|
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI|I2C_CTL_STO);//Stop
|
|
}
|
|
|
|
|
|
char MPUReadReg(char ad,int unAddr/*, int unLength*/){
|
|
|
|
uint8_t tmp;
|
|
I2C_START(LCD_I2C_PORT); //Start
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
//LCD_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag
|
|
|
|
I2C_SET_DATA(LCD_I2C_PORT,ad ); //send slave address+W
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
|
|
I2C_SET_DATA(LCD_I2C_PORT, unAddr); //send index
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
|
|
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_STA | I2C_CTL_SI); //Start
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
|
|
|
|
I2C_SET_DATA(LCD_I2C_PORT, (ad+1)); //send slave address+R
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI);
|
|
I2C_WAIT_READY(LCD_I2C_PORT);
|
|
|
|
tmp = I2C_GET_DATA(LCD_I2C_PORT); //read data
|
|
|
|
I2C_SET_CONTROL_REG(LCD_I2C_PORT, I2C_CTL_SI|I2C_CTL_STO);//Stop
|
|
return tmp;
|
|
}
|
|
|
|
int MPUReadBuf(char slaveAddress,int unAddr, char *pucDATD_AA, int unLength)
|
|
{
|
|
I2C_START(I2C0); //启动
|
|
//启动
|
|
I2C_WAIT_READY(I2C0);
|
|
if(I2C_GET_STATUS(I2C0) != 0x08)
|
|
{
|
|
printf("I2CD_STArt fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
|
|
return FALSE;
|
|
}
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
|
|
|
|
//进入读写控制操作
|
|
I2C_SET_DATA(I2C0,slaveAddress);
|
|
I2C_WAIT_READY(I2C0);
|
|
if (I2C_GET_STATUS(I2C0) != 0x18)
|
|
{
|
|
printf("I2CD_STArt fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
|
|
return FALSE;
|
|
}
|
|
//写入读地址
|
|
I2C_SET_DATA(I2C0,unAddr);
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
|
|
I2C_WAIT_READY(I2C0);
|
|
if (I2C_GET_STATUS(I2C0)!= 0x28)
|
|
{
|
|
printf("I2C write reg addr fail\r\n");
|
|
return FALSE;
|
|
}
|
|
// 重新启动
|
|
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_STA);
|
|
I2C_WAIT_READY(I2C0);
|
|
if (I2C_GET_STATUS(I2C0) != 0x10)
|
|
{
|
|
printf("I2C repeated D_STArt fail\r\n");
|
|
return FALSE;
|
|
}
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
|
|
|
|
//进入读操作
|
|
I2C_SET_DATA(I2C0,slaveAddress | 1);
|
|
I2C_WAIT_READY(I2C0);
|
|
if (I2C_GET_STATUS(I2C0) != 0x40)
|
|
{
|
|
printf("I2C write control fail\r\n");
|
|
return -1;
|
|
}
|
|
//读取数据
|
|
for(int i=0;i<unLength;i++)
|
|
{
|
|
if(i==unLength-1)
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI);
|
|
else
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI | I2C_CTL_SI_AA);
|
|
I2C_WAIT_READY(I2C0);
|
|
if ((I2C_GET_STATUS(I2C0) != 0x50))
|
|
{
|
|
printf("I2C read fail\r\n");
|
|
return FALSE;
|
|
}
|
|
pucDATD_AA[i] = I2C_GET_DATA(I2C0);
|
|
}
|
|
I2C_SET_CONTROL_REG(I2C0, I2C_CTL_STO);
|
|
I2C_WAIT_READY(I2C0);
|
|
|
|
|
|
// I2C_WAIT_READY(I2C0);
|
|
} |