TI中文支持网
TI专业的中文技术问题搜集分享网站

求助,关于TM4C1294控制颜色传感器TCS34725的问题

#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_ADDRESS0x29
#define AMBIENT_TEMPERATURE0x05
#define DEVICE_ID_REGISTER0x12
#define MANUFACTURE_ID_REGISTER0x07
#define TCS34725_ENABLE0x00
#define TCS34725_ENABLE_PON0x01

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 busI2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);//the register to be readI2CMasterDataPut(I2C0_BASE, device_register);//send control byte and register address byte to slave deviceI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);//wait for MCU to complete send transactionwhile(I2CMasterBusy(I2C0_BASE));//read from the specified slave deviceI2CMasterSlaveAddrSet(I2C0_BASE, device_address, true);//send control byte and read from the register from the MCUI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);//wait while checking for MCU to complete the transactionwhile(I2CMasterBusy(I2C0_BASE));//Get the data from the MCU register and return to callerreturn( 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 busI2CMasterSlaveAddrSet(I2C0_BASE, device_address, false);//register to be readI2CMasterDataPut(I2C0_BASE, device_register);//send control byte and register address byte to slave deviceI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);//wait for MCU to finish transactionwhile(I2CMasterBusy(I2C0_BASE));I2CMasterSlaveAddrSet(I2C0_BASE, device_address, true);//specify data to be written to the above mentioned device_registerI2CMasterDataPut(I2C0_BASE, device_data);//wait while checking for MCU to complete the transactionI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);//wait for MCU & device to complete transactionwhile(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 UARTConfigureUART();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 UARTSysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);// Configure the GPIO Pins for UART modeGPIOPinConfigure(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 sourceUARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);// Initialize the UART for console I/O.UARTStdioConfig(0, 9600, 16000000);
}

void I2C_Init()
{// Enable I2C1 peripheralSysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);// Enable GPIO Port B to be used for I2C0SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);GPIOPinConfigure(GPIO_PB2_I2C0SCL);GPIOPinConfigure(GPIO_PB3_I2C0SDA);// Configure the pin muxing for I2C1 functions on Port B2 and B3GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);// Set write modeUARTprintf("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 toI2CMasterSlaveAddrSet(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 deviceI2CMasterDataPut(I2C0_BASE, device_reg);UARTprintf("Writing to device id register on Slave address\n");// Initiate send of register address from Master to SlaveI2CMasterControl(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 modeI2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, true);// Get first byte from slave and ack for moreI2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);while(I2CMasterBusy(I2C0_BASE));UpperByte = I2CMasterDataGet(I2C0_BASE);// Get second byte from slave and nack for completeI2CMasterControl(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 fordata = UpperByte<<8|LowerByte;return data;
}
代码如上,按照我的预期,他应该可以读到设备的ID号0x44才对,可是他总是返回0x0.是我哪里没写对吗,还是?求指教,谢谢!
TCS34725的数据手册也上传了,寄存器地址什么的可以看数据手册,感觉寄存器地址也可能出问题?因为我DEBUG的时候那个SLAVEADRESS的值也不是我预期的
。。。。求赐教

TCS34725.pdf

user5305058:

另外这段代码我是从其他地方改的 所以有些地方可能没用,跟着MAIN函数的思路走就行

xyz549040622:

回复 user5305058:

你I2c加上拉了吗?I2c是个很简单的东西,只要配置正确(这点也不是问题,很多例程可以参考),发送指令正确,就可以收到回复的。你先用IO模拟试试,是否可以正确读到。

user5305058:

回复 xyz549040622:

I2C确实没上拉,但是我之前看了好多代码他们也没上拉啊。难道是他们在外部上拉的?另外就是

I2C的上拉该如何配置(有没有具体的函数什么的,我找了半天只找到

void GPIOPadConfigSet(uint32_t ui32Port, uint8_t ui8Pins,uint32_t ui32Strength, uint32_t ui32PinType)

这个函数)?大二狗一枚,对这些都不是很懂。另外是不是用模拟的I2C就能克服这个问题了?

xyz549040622:

回复 user5305058:

I2C协议的标准规定了,I2C必须要加外部上拉的,就是要外部上拉。模拟I2C也要上拉的。

user5305058:

回复 xyz549040622:

那我用的那个什么gpiopad啥函数那个是不是不行?因为我看他说的是什么轻微上拉

tm4c1294有内部上拉函数吗?我查网上说有的板子有内部上拉电阻,使能一下就行

还有就是上拉该如何上拉?我昨天用那个pad什么的函数将两个引脚都上拉了,结果还是失败了

xyz549040622:

回复 user5305058:

有内部上拉,实际测试过有的芯片用内部上拉是可以的,有的芯片不可以。让你加外部上拉,只是排除一种可能而已。你用内部上拉,走IO模拟看看,你抓数据波形了吗?

赞(0)
未经允许不得转载:TI中文支持网 » 求助,关于TM4C1294控制颜色传感器TCS34725的问题
分享到: 更多 (0)