#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_i2c.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/i2c.h"
#include "utils/uartstdio.h"
#define SLAVE_ADDRESS 0x29 //0x29=00101001 01010010=0x52
#define AMBIENT_TEMPERATURE 0x05
#define DEVICE_ID_REGISTER 0x12
#define MANUFACTURE_ID_REGISTER 0x07
#define TCS34725_ENABLE 0x00
#define TCS34725_ENABLE_PON 0x01
uint16_t convertTemp(uint8_t, uint8_t);
void ConfigureUART(void);
void I2C_Init(void);
void I2C_Send(void);
uint16_t I2C_readMode(void);
uint16_t I2C_TempRead(void);
void Device_ID(uint8_t device_reg);
void Manufacture_ID(uint8_t manufacture_reg);
void Ambient_Temp(uint8_t ambient_temp_reg);
uint8_t readI2C0(uint16_t device_address, uint16_t device_register)
{
//specify that we want to communicate to device address with an intended write to bus
I2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);
//the register to be read
I2CMasterDataPut(I2C0_BASE, device_register);
//send control byte and register address byte to slave device
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
//wait for MCU to complete send transaction
while(I2CMasterBusy(I2C0_BASE));
//read from the specified slave device
I2CMasterSlaveAddrSet(I2C0_BASE, device_address, true);
//send control byte and read from the register from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
//wait while checking for MCU to complete the transaction
while(I2CMasterBusy(I2C0_BASE));
//Get the data from the MCU register and return to caller
return( I2CMasterDataGet(I2C0_BASE));
}
void writeI2C0(uint16_t device_address, uint16_t device_register, uint8_t device_data)
{
//specify that we want to communicate to device address with an intended write to bus
I2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);
//register to be read
I2CMasterDataPut(I2C0_BASE, device_register);
//send control byte and register address byte to slave device
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));
I2CMasterSlaveAddrSet(I2C0_BASE, device_address, true);
//specify data to be written to the above mentioned device_register
I2CMasterDataPut(I2C0_BASE, device_data);
//wait while checking for MCU to complete the transaction
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
//wait for MCU & device to complete transaction
while(I2CMasterBusy(I2C0_BASE));
}
int main(void)
{
//FPULazyStackingEnable();
uint8_t Device_id = 0;
//uint16_t Manufacture_id = 0;
// Set the clocking to run directly from the crystal
//SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
// Enable the GPIO Port that is used for the on-board LEDs
//SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Enable the GPIO Pins for the LEDs (PF1, PF2, PF3);
//GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
// Initialize the UART
ConfigureUART();
SysCtlDelay(10000000);
UARTprintf("Program Starting....\n\n");
SysCtlDelay(50000000);
UARTprintf("UART Initialized\n");
SysCtlDelay(50000000);
I2C_Init();
SysCtlDelay(50000000);
writeI2C0(SLAVE_ADDRESS,TCS34725_ENABLE,TCS34725_ENABLE_PON);
UARTprintf("POWER ON!\n");
SysCtlDelay(10000000);
// I2C Send data to Slave Address - Device ID register - See MCP9808 datasheet
//I2C_Send();
//Device_ID(DEVICE_ID_REGISTER);
//while(I2CMasterBusy(I2C0_BASE))
//{
//}
Device_id = readI2C0(SLAVE_ADDRESS,DEVICE_ID_REGISTER);
UARTprintf("Received Device ID from Slave: 0x%x\n\r", Device_id);
SysCtlDelay(50000000);
}
void ConfigureUART(void)
{
// Enable the GPIO Peripheral used by the UART
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
// Configure the GPIO Pins for UART mode
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Use the internal 16MHz oscillator as the UART Clock source
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
// Initialize the UART for console I/O.
UARTStdioConfig(0, 9600, 16000000);
}
void I2C_Init()
{
// Enable I2C1 peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
// Enable GPIO Port B to be used for I2C0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// Configure the pin muxing for I2C1 functions on Port B2 and B3
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
// Set write mode
UARTprintf("I2C Master init communication with Slave Address\n");
SysCtlDelay(10000);
UARTprintf("I2C Init complete!\n");
SysCtlDelay(50000000);
}
void I2C_Send()
{
// Specify Slave device address to write to
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false);
UARTprintf("Master transmit to Slave address\n\n");
}
void Device_ID(uint8_t device_reg)
{
// Send Register address on Slave device
I2CMasterDataPut(I2C0_BASE, device_reg);
UARTprintf("Writing to device id register on Slave address\n");
// Initiate send of register address from Master to Slave
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
UARTprintf("Device ID register sent to Slave address\n");
}
uint16_t I2C_readMode()
{
uint8_t UpperByte = 0;
uint8_t LowerByte = 0;
uint16_t data = 0;
// Set read mode
I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);
// Get first byte from slave and ack for more
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C0_BASE));
UpperByte = I2CMasterDataGet(I2C0_BASE);
// Get second byte from slave and nack for complete
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
while(I2CMasterBusy(I2C0_BASE));
LowerByte = I2CMasterDataGet(I2C0_BASE);
// See MCP9808 Data Sheet for each of the register information requested for
data = UpperByte<<8|LowerByte;
return data;
}
程序如上,不能正确的读出DEVICE_ID,在调试过程中发现ARBLST寄存器值为1,也就是发生了
arbitration lost的错误,请问是为什么呢
附:TCS34725颜色传感器资料