错误提示如下:
程序如下:
//#include "NUC505Series.h"
//#include "PressureSensor.h"
//#define PSESNOR_ADDR 0x6D
volatile uint8_t i2c1_read = 0;
volatile uint8_t i2c1_addr = 0;
volatile uint8_t i2c1_reg = 0;
volatile uint8_t i2c1_data;
volatile uint8_t i2c1_done = 0;
uint8_t gpu8Pressure[5] = {0};
void I2C1_IRQHandler(void)
{
uint32_t u32Status;
u32Status = I2C_GET_STATUS(I2C1);
if (I2C_GET_TIMEOUT_FLAG(I2C1))
{
I2C_ClearTimeoutFlag(I2C1);
}
else
{
if (i2c1_read)
{
switch (u32Status)
{
case 0x08:
//Write SLA+W to Register I2CDAT
I2C_SET_DATA(I2C1, (i2c1_addr << 1));
I2C_SET_CONTROL_REG(I2C1, I2C_SI);
break;
case 0x18:
//SLA+W has been transmitted and ACK has been received
I2C_SET_DATA(I2C1, i2c1_reg);
I2C_SET_CONTROL_REG(I2C1, I2C_SI);
break;
case 0x20:
//SLA+W has been transmitted and NACK has been received
I2C_SET_CONTROL_REG(I2C1, I2C_STA | I2C_STO | I2C_SI);
break;
case 0x28:
I2C_SET_CONTROL_REG(I2C1, I2C_STA | I2C_SI);
break;
case 0x10:
//Repeat START has been transmitted and prepare SLA+R
I2C_SET_DATA(I2C1, (i2c1_addr << 1) | 0x01); //Write SLA+R to Register I2CDAT
I2C_SET_CONTROL_REG(I2C1, I2C_SI);
break;
case 0x40:
//SLA+R has been transmitted and ACK has been received
I2C_SET_CONTROL_REG(I2C1, I2C_SI);
break;
case 0x58:
//DATA has been received and NACK has been returned
i2c1_data = I2C_GET_DATA(I2C1);
I2C_SET_CONTROL_REG(I2C1, I2C_STO | I2C_SI);
i2c1_done = 1;
break;
}
}
else
{
switch (u32Status)
{
case 0x08: //START has been transmitted and prepare SLA+W
I2C_SET_DATA(I2C1, (i2c1_addr << 1)); //Write SLA+W to Register I2CDAT
I2C_SET_CONTROL_REG(I2C1, I2C_SI);
break;
case 0x18: // SLA+R has been transmitted and ACK has been received
I2C_SET_DATA(I2C1, i2c1_reg);
I2C_SET_CONTROL_REG(I2C1, I2C_SI);
break;
case 20:
I2C_SET_CONTROL_REG(I2C1, I2C_STA | I2C_STO | I2C_SI);
break;
case 0x28: //DATA has been received and NACK has been returned
I2C_SET_DATA(I2C1, i2c1_data);
I2C_SET_CONTROL_REG(I2C1, I2C_STO | I2C_SI);
i2c1_done = 1;
break;
}
}
}
u32Status = I2C_GET_STATUS(I2C1);
}
void I2C1_Read(uint8_t u8Addr, uint8_t u8RegAddr, uint8_t *pu8RetValue)
{
i2c1_done = 0;
i2c1_read = 1;
i2c1_addr = u8Addr;
i2c1_reg = u8RegAddr;
I2C_SET_CONTROL_REG(I2C1, I2C_STA);
while (!i2c1_done);
*pu8RetValue = i2c1_data;
}
void I2C1_Write(uint8_t u8Addr, uint8_t u8RegAddr, uint8_t u8Value)
{
i2c1_done = 0;
i2c1_read = 0;
i2c1_addr = u8Addr;
i2c1_reg = u8RegAddr;
i2c1_data = u8Value;
I2C_SET_CONTROL_REG(I2C1, I2C_STA);
while (!i2c1_done);
}
void Sample()
{
I2C1_Write(PSESNOR_ADDR, 0x30, 0x0A);
I2C1_Read(PSESNOR_ADDR, 0x30, &gpu8Pressure[0]);
I2C1_Read(PSESNOR_ADDR, 0x06, &gpu8Pressure[0]);
I2C1_Read(PSESNOR_ADDR, 0x07, &gpu8Pressure[1]);
I2C1_Read(PSESNOR_ADDR, 0x08, &gpu8Pressure[2]);
I2C1_Read(PSESNOR_ADDR, 0x09, &gpu8Pressure[3]);
I2C1_Read(PSESNOR_ADDR, 0x0A, &gpu8Pressure[4]);
}
void TMR2_IRQHandler(void)
{
// clear timer interrupt flag
TIMER_ClearIntFlag(TIMER2);
//Sample();
TIMER_GetIntFlag(TIMER2);
}
void PressureSensor_Init(void)
{
uint8_t u8A5 = 0;
I2C_Open(I2C1, 400000);
I2C_EnableInt(I2C1);
NVIC_EnableIRQ(I2C1_IRQn);
I2C1_Read(PSESNOR_ADDR, 0xA5, &u8A5);
u8A5 &= 0xFD;
I2C1_Write(PSESNOR_ADDR, 0xA5, u8A5);
while (1)
{
Sample();
msDelay(100);
}
TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 10);
TIMER_EnableInt(TIMER2);
NVIC_EnableIRQ(TMR2_IRQn);
TIMER_Start(TIMER2);
}
|