关于tm4c129用i2c读取从机tcs34725设备id

爱兜风 2018-04-24 02:00:19
#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
#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,DEVIC
...全文
1181 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

27,374

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧