Пример #1
0
int main()
{
    /*UART initiering*/
    UART_1_Start();
    UART_1_UartPutString("This is a unit test of accelerometer ADXL345\n\r\n");
    /*I2C initiering*/
    I2C_1_Start(); 
    I2C_1_I2CMasterClearStatus();
    
    /*Opsætter accellerometer til I2C*/
    if(I2C_1_I2CMasterSendStart(ACCEL_ADDRESS, I2C_1_I2C_WRITE_XFER_MODE) == I2C_1_I2C_MSTR_NO_ERROR
        && I2C_1_I2CMasterWriteByte(PWR_CTRL_REG) == I2C_1_I2C_MSTR_NO_ERROR
        && I2C_1_I2CMasterWriteByte(PWR_MODE) == I2C_1_I2C_MSTR_NO_ERROR) 
    UART_1_UartPutString("Accelerometer ADXL345 initialized.\n\r\n");
    UART_1_UartPutString("10000 reads, from reg DEVID, will now be performed.\n\rPlease wait.\n\r\n");
    
    I2C_1_I2CMasterSendStop();
    int errors = 0; //Variabel til at tælle fejl 
    int i;
    for(i = 0; i < 10000; i++) //Forløkke som løber igennem læsningen 10000 gange. (fra DEVID) 
    {
        if (I2C_1_I2CMasterSendStart(ACCEL_ADDRESS, I2C_1_I2C_WRITE_XFER_MODE) == I2C_1_I2C_MSTR_NO_ERROR) /* Tjekker om transfer er sket uden fejl */
        { 
            if(I2C_1_I2CMasterWriteByte(DEVID) == I2C_1_I2C_MSTR_NO_ERROR)
            {
                if(I2C_1_I2CMasterSendRestart(ACCEL_ADDRESS, I2C_1_I2C_READ_XFER_MODE) == I2C_1_I2C_MSTR_NO_ERROR)
                {
                    rawData = I2C_1_I2CMasterReadByte(I2C_1_I2C_NAK_DATA); //Læser og gemmer læsningen i rawData
                    I2C_1_I2CMasterSendStop();
                    
                    if(rawData != 0xE5)errors++; //Hvis der læses andet end device ID, inkrementeres fejl. 
                    
                }
                else
                {
                    I2C_1_I2CMasterSendStop(); /* Send Stop */
                    errors++;
                }
            } 
            else 
            {
                errors++;
            }
        }
        else
        {
            I2C_1_I2CMasterSendStop(); /* Send Stop */
            errors++;
        }
        
    }
    
    char errorString[32];
    sprintf(errorString,  "Finished reading.\n\rErrors: %d\n\r\n", errors); //Gemmer antal errors i en string
    UART_1_UartPutString(errorString); //Udskriver hvor mange fejl. 
    char readingString[32];
    sprintf(readingString,  "Data from register: %d\n\r\n", rawData); // Gemmer rawData i som en string
    UART_1_UartPutString(readingString); //Udskriver hvad der står på rawData i UART
    while(1);
}
// Function sends 0x00 to request data from nunchuck
// Returns  0 if an error occurs
int NunchuckRequestData()
{
    // Variable for errorhandling
    uint8 writeNunchuckError;
    
    
    I2C_1_I2CMasterClearStatus();         
    writeNunchuckError = I2C_1_I2CMasterSendStart(nunchuckUnitAddress, I2C_1_I2C_WRITE_XFER_MODE);
    if (writeNunchuckError == I2C_1_I2C_MSTR_NO_ERROR)
    {
        // Error happens while sending byte
        if(I2C_1_I2CMasterWriteByte(0x00)!= I2C_1_I2C_MSTR_NO_ERROR)
        {
            I2C_1_I2CMasterSendStop();
            return 0;
        }
        
        I2C_1_I2CMasterSendStop();
    }
    else 
    {
        I2C_1_I2CMasterSendStop();
        return 0;
    }
    
    I2C_1_I2CMasterSendStop();
    // No error occurred while sending byte
    return 1;
}
// Function reads data into buffer
// Returns 0 if an error occurs
int NunchuckReadData(uint8* buffer)
{
    // Variable for errorhandling
    uint8 readNunchuckError;
    
    I2C_1_I2CMasterClearStatus();
    readNunchuckError = I2C_1_I2CMasterSendStart(nunchuckUnitAddress, I2C_1_I2C_READ_XFER_MODE);
    if (readNunchuckError == I2C_1_I2C_MSTR_NO_ERROR)
    {
        // Reading data from Nunchuck
        uint8 XAxisValue = I2C_1_I2CMasterReadByte(I2C_1_I2C_ACK_DATA);
        uint8 YAxisValue = I2C_1_I2CMasterReadByte(I2C_1_I2C_ACK_DATA);
        uint8 XAxisAccValue = I2C_1_I2CMasterReadByte(I2C_1_I2C_ACK_DATA);
        uint8 YAxisAccValue = I2C_1_I2CMasterReadByte(I2C_1_I2C_ACK_DATA);
        uint8 ZAxisAccValue = I2C_1_I2CMasterReadByte(I2C_1_I2C_ACK_DATA);
        uint8 ButtonStates = I2C_1_I2CMasterReadByte(I2C_1_I2C_NAK_DATA);
        
        // Decoding Nunchuck values
        uint8 exactData0 = (XAxisValue ^ 0x17) + 0x17 - 15;
        uint8 exactData1 = (YAxisValue ^ 0x17) + 0x17;
        uint8 exactData2 = (ButtonStates ^0x17) + 0x17;
       
        // Inserting exactData into buffer
        // XAxisValue
        buffer[0] = exactData0;
        // YAxisValue
        buffer[1] = exactData1;
        // ButtonStates
        buffer[2] = exactData2;
        
        I2C_1_I2CMasterSendStop();
        return 1;
    }
    // Error has occurred while reading data
    else
    {
        I2C_1_I2CMasterSendStop();
        return 0;
    }
    
    I2C_1_I2CMasterSendStop();
}
// Function sends handshake to nunchuck to initiate communication
// Returns 0 if an error occurs
int NunchuckSendHandshake()
{
    // Variable for errorhandling
    uint8 transferErrorStatus;
    
    I2C_1_I2CMasterClearStatus();

    transferErrorStatus = I2C_1_I2CMasterSendStart(nunchuckUnitAddress, I2C_1_I2C_WRITE_XFER_MODE);
    if(transferErrorStatus == I2C_1_I2C_MSTR_NO_ERROR)
    {   
        // Error happened while sending byte
        if(I2C_1_I2CMasterWriteByte(0x40) != I2C_1_I2C_MSTR_NO_ERROR)
        {
            I2C_1_I2CMasterSendStop();
            return 0;
        }
        // No error occurred while sending byte
        else
        {
            // Error happens while sending byte
            if (I2C_1_I2CMasterWriteByte(0x00) != I2C_1_I2C_MSTR_NO_ERROR)
            {
                I2C_1_I2CMasterSendStop();
                return 0;
            }
            // No error occurred while sending byte
            else
            {
                I2C_1_I2CMasterSendStop();
                return 1;
            }
        }
        
        I2C_1_I2CMasterSendStop();
    }
    else
    {
        I2C_1_I2CMasterSendStop();
        return 0;
    }
}