Quantcast
Channel: Zigbee 和 Thread
Viewing all articles
Browse latest Browse all 16962

帖子: RE: CC2538光照传感器实验程序 并把光照值通过串口发送 ?

$
0
0

#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();
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)
{
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))
{
}
}

这是修改过的  可以实现主机到从机发送数据  串口发送也是正常的  有一个问题是不能从从机寄存器里读出数据  望解答


Viewing all articles
Browse latest Browse all 16962

Trending Articles