CC2538光照传感器实验程序 并把光照值通过串口发送 帮忙看看哪里有错
#define EXAMPLE_PIN_UART_RXD GPIO_PIN_0#define EXAMPLE_PIN_UART_TXD GPIO_PIN_1#define EXAMPLE_GPIO_UART_BASE GPIO_A_BASE
#define EXAMPLE_PIN_I2C_SCL GPIO_PIN_2
#define EXAMPLE_PIN_I2C_SDA GPIO_PIN_3
#define EXAMPLE_GPIO_I2C_BASE GPIO_B_BASE
//*****************************************************************************
//
// Number of I2C data packets to send.
//
//*****************************************************************************
#define NUM_I2C_DATA 3
#define SLAVE_ADDRESS 0x46
void
InitConsole(void)
{
//
// Map UART signals to the correct GPIO pins and configure them as
// hardware controlled.
//
IOCPinConfigPeriphOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD, IOC_MUX_OUT_SEL_UART0_TXD);
GPIOPinTypeUARTOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD);
IOCPinConfigPeriphInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD, IOC_UARTRXD_UART0);
GPIOPinTypeUARTInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD);
//
// Initialize the UART (UART0) for console I/O.
//
UARTStdioInit(0);
}
//*****************************************************************************
//
// Configure the I2C0 master and slave and connect them using loopback mode.
//
//*****************************************************************************
int
main(void)
{
// uint32_t pui32DataTx[4]={2,3,4,5};
// uint32_t pui32DataRx[NUM_I2C_DATA];
//uint32_t ui32Index;
unsigned long delay;
unsigned short dis_data_0; unsigned char i, BUF_0[8];
fp32 temp;
//
// Set the clocking to run directly from the external crystal/oscillator.
// (no ext 32k osc, no internal osc)
//
SysCtrlClockSet(false, false, SYS_CTRL_SYSDIV_16MHZ);
//
// Set IO clock to the same as system clock
//
SysCtrlIOClockSet(SYS_CTRL_SYSDIV_16MHZ); //
// The I2C peripheral must be enabled before use.
//
SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_I2C);
//
// Do reset of I2C module 执行I2C模块的复位
//
SysCtrlPeripheralReset(SYS_CTRL_PERIPH_I2C);
//
// Configure I2C pins 配置I2C引脚
//
GPIOPinTypeI2C(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL);
GPIOPinTypeI2C(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA);
//
// Configure pins as peripheral input and output 将引脚配置为外设输入和输出
//
IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL, IOC_I2CMSSCL);
IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA,
IOC_I2CMSSDA); IOCPinConfigPeriphOutput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL,
IOC_MUX_OUT_SEL_I2C_CMSSCL);
IOCPinConfigPeriphOutput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA,
IOC_MUX_OUT_SEL_I2C_CMSSDA); //
// Enable loopback mode. Loopback mode is a built in feature that is
// useful for debugging I2C operations. It internally connects the I2C
// master and slave terminals, which effectively let's you send data as
// a master and receive data as a slave.
// NOTE: For external I2C operation you will need to use external pullups
// that are stronger than the internal pullups. Refer to the datasheet for
// more information.环回模式是一个内置功能,可用于调试I2C操作。
//它在内部连接I2C主机和从机端子,这有效地让您作为主机发送数据,并作为从机接收数据。
//注:对于外部I2C操作,需要使用比内部上拉电阻更强的外部上拉电阻。
HWREG(I2CM_CR) |= I2CM_CR_LPBK;
//
// Enable and initialize the I2C master module. Use the system clock for
// the I2C module. The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps. For this example we will use a data rate of 100kbps.
//启用并初始化I2C主模块。使用I2C模块的系统时钟。最后一个参数设置I2C数据传输速率。
//如果为false,则数据速率设置为100kbps,如果为true,则数据速率将设置为400kbps。
I2CMasterInitExpClk(SysCtrlClockGet(), false);
I2CMasterEnable();
//
// Enable the I2C slave module. This module is enabled only for testing
// purposes. It does not need to be enabled for proper operation of the
// I2Cx master module.使能I2C从模块。
//
I2CSlaveEnable(); // TBD is this needed or done by I2CSlaveInit
InitConsole();
// Place the data to be sent in the data register 将要发送的数据放在数据寄存器中
I2CMasterDataPut(0x01);
while(1)
{
I2CSlaveInit(SLAVE_ADDRESS);
I2CMasterSlaveAddrSet(SLAVE_ADDRESS, false);
I2CMasterDataPut(0x01);
I2CMasterDataPut(0x10);
delay = 1800000;
while(–delay); I2CSlaveInit(SLAVE_ADDRESS+1);
I2CMasterSlaveAddrSet(SLAVE_ADDRESS+1, true);
/* // Do a dummy receive to make sure you don't get junk on the first receive.
//做一个虚拟接收,以确保你不会在第一次接收垃圾。
I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE); // Tell the master to read data.
I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE); */
for (i=0; i<3; i++) //连续读取个地址数据,存储中BUF
{
BUF_0[i] = I2CMasterDataGet();
}
dis_data_0=BUF_0[0];
dis_data_0=(dis_data_0<<8)+BUF_0[1]; //合成数据,即光照数据
temp=(fp32)dis_data_0/1.2;
UARTprintf(" Sending: '%d' . . . ",temp);
}
帮忙看看哪里有错 谢谢了
xyz549040622:
建议你单独调通一个模块,然后在合并。并且先找出问题,是发送部分的问题,还是读取传感器部分的问题。
bin shi2:
回复 xyz549040622:
谢谢你 感觉这个iic程序是把主芯片 既做为主机又做为从机了 所以 I2CMasterDataGet() I2CSlaveDataPut() I2CMasterDisable() 等iic.h中的函数可以用吗 要自己模拟写吗 还是可以用
xyz549040622:
回复 bin shi2:
我觉得只要主从模式切换过来,还是可以用的。前提是你要切换ok。
bin shi2:
#define uint32 unsigned int////*****************************************************************************#define EXAMPLE_PIN_UART_RXD GPIO_PIN_0 #define EXAMPLE_PIN_UART_TXD GPIO_PIN_1 #define EXAMPLE_GPIO_UART_BASE GPIO_A_BASE
#define EXAMPLE_PIN_I2C_SCL GPIO_PIN_2#define EXAMPLE_PIN_I2C_SDA GPIO_PIN_3#define EXAMPLE_GPIO_I2C_BASE GPIO_B_BASE
//*****************************************************************************//// Number of I2C data packets to send.////*****************************************************************************#define NUM_I2C_DATA 3
//*****************************************************************************//// Set the address for slave module. This is a 7-bit address sent in the// following format:// [A6:A5:A4:A3:A2:A1:A0:RS]//// A zero in the "RS" position of the first byte means that the master// transmits (sends) data to the selected slave, and a one in this position// means that the master receives data from the slave.////*****************************************************************************#define SLAVE_ADDRESS 0x46
//*****************************************************************************//// This function sets up UART0 to be used for a console to display information// as the example is running.////*****************************************************************************void start();voidInitConsole(void){ // // Map UART signals to the correct GPIO pins and configure them as // hardware controlled. // IOCPinConfigPeriphOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD, IOC_MUX_OUT_SEL_UART0_TXD); GPIOPinTypeUARTOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD); IOCPinConfigPeriphInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD, IOC_UARTRXD_UART0); GPIOPinTypeUARTInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD); // // Initialize the UART (UART0) for console I/O. // UARTStdioInit(0);}
//*****************************************************************************//// Configure the I2C0 master and slave and connect them using loopback mode.////*****************************************************************************intmain(void){ unsigned long delay; unsigned short dis_data_0; unsigned char i, BUF_0[8]; uint32 vLux; fp32 temp;
// // Set the clocking to run directly from the external crystal/oscillator. // (no ext 32k osc, no internal osc) // SysCtrlClockSet(false, false, SYS_CTRL_SYSDIV_16MHZ);
// // Set IO clock to the same as system clock // SysCtrlIOClockSet(SYS_CTRL_SYSDIV_16MHZ); // // The I2C peripheral must be enabled before use. // SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_I2C); // // Do reset of I2C module 执行I2C模块的复位 // SysCtrlPeripheralReset(SYS_CTRL_PERIPH_I2C);
// // Configure I2C pins 配置I2C引脚 // GPIOPinTypeI2C(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL); GPIOPinTypeI2C(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA);
// // Configure pins as peripheral input and output 将引脚配置为外设输入和输出 // IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL, IOC_I2CMSSCL); IOCPinConfigPeriphInput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA, IOC_I2CMSSDA); IOCPinConfigPeriphOutput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SCL, IOC_MUX_OUT_SEL_I2C_CMSSCL); IOCPinConfigPeriphOutput(EXAMPLE_GPIO_I2C_BASE, EXAMPLE_PIN_I2C_SDA, IOC_MUX_OUT_SEL_I2C_CMSSDA);
// // Enable and initialize the I2C master module. Use the system clock for // the I2C module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. For this example we will use a data rate of 100kbps. //启用并初始化I2C主模块。使用I2C模块的系统时钟。最后一个参数设置I2C数据传输速率。 //如果为false,则数据速率设置为100kbps,如果为true,则数据速率将设置为400kbps。 I2CMasterInitExpClk(SysCtrlClockGet(), false); I2CMasterEnable();
// // Enable the I2C slave module. This module is enabled only for testing // purposes. It does not need to be enabled for proper operation of the // I2Cx master module.使能I2C从模块。 // I2CSlaveEnable(); // TBD is this needed or done by I2CSlaveInit???
// // Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an // arbitrary 7-bit number (set in a macro above) that is sent to the // I2CMasterSlaveAddrSet function. // I2CSlaveInit(SLAVE_ADDRESS);
// // Tell the master module what address it will place on the bus when // communicating with the slave. Set the address to SLAVE_ADDRESS // (as set in the slave module). The receive parameter is set to false // which indicates the I2C Master is initiating a writes to the slave. If // true, that would indicate that the I2C Master is initiating reads from // the slave. // I2CMasterSlaveAddrSet(SLAVE_ADDRESS, false); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for I2C operation. InitConsole(); // Place the data to be sent in the data register 将要发送的数据放在数据寄存器中 // I2CMasterDataPut(0x46); //start(); I2CMasterDataPut(0x10); start(); I2CMasterDataPut(0x01); start();
delay = 1800000; while(–delay); //I2CSlaveInit(SLAVE_ADDRESS+1); I2CMasterSlaveAddrSet(SLAVE_ADDRESS, true); // I2CMasterSlaveAddrSet(SLAVE_ADDRESS, true); // Do a dummy receive to make sure you don't get junk on the first receive. //做一个虚拟接收,以确保你不会在第一次接收垃圾。 I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE);
// // Dummy acknowledge and wait for the receive request from the master.虚拟确认并等待来自主机的接收请求。 // This is done to clear any flags that should not be set. // /* while(!(I2CSlaveStatus() & I2C_SLAVE_ACT_TREQ)) { }*/ // Do a dummy receive to make sure you don't get junk on the first receive. //做一个虚拟接收,以确保你不会在第一次接收垃圾。 //I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE);
for (i=0; i<2; i++) //连续读取个地址数据,存储中BUF { BUF_0[i] = I2CMasterDataGet(); } I2CMasterDataPut(0x00); start(); // UARTprintf(" Sending: '%d' . . . ",BUF_0[0]); dis_data_0=BUF_0[0]; dis_data_0=(dis_data_0<<8)+BUF_0[1]; //合成数据,即光照数据 temp=(fp32)dis_data_0/1.2; vLux = (uint32)(temp*1.4); UARTprintf(" Sending: '%d' . . . ",vLux); //}}void start(){ I2CMasterControl(I2C_MASTER_CMD_SINGLE_SEND); // Wait until the slave has received and acknowledged the data. while(!(I2CSlaveStatus() & I2C_SLAVE_ACT_RREQ)) { }}
这是修改过的 可以实现主机到从机发送数据 串口发送也是正常的 有一个问题是不能从从机寄存器里读出数据 望解答