1 | /* Send InitAirQuality command to start the air quality measurment
|
2 | * 1. Send Init_air_quality command to start air quality measurment */
|
3 | I2C_I2CMasterSendStart(DeviceAddress,I2C_I2C_WRITE_XFER_MODE); // Initialize a transaction for writing
|
4 | I2C_I2CMasterWriteByte(InitAirQuality_MSB); // Send MSB command
|
5 | I2C_I2CMasterWriteByte(InitAirQuality_LSB); // Send LSB command
|
6 | I2C_I2CMasterSendStop(); // I2C Stop
|
7 |
|
8 | /* 2. Send measure_air_quality command */
|
9 | I2C_I2CMasterSendStart(DeviceAddress,I2C_I2C_WRITE_XFER_MODE); // Initialize a transaction for writing
|
10 | I2C_I2CMasterWriteByte(MeasureAirQuality_MSB); // Send LSB command
|
11 | I2C_I2CMasterWriteByte(MeasureAirQuality_LSB); // Send LSB command
|
12 | I2C_I2CMasterSendStop(); // I2C Stop
|
13 | CyDelay(10); // wait 10ms
|
14 |
|
15 | /* Readout 6 bytes: 1. CO2 (MSB, LSB, CRC) 2. TVOC (MSB, LSB, CRC) */
|
16 | I2C_I2CMasterSendStart(DeviceAddress,I2C_I2C_READ_XFER_MODE); // Initialize a transaction for reading
|
17 | airQuality[0] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register MSB
|
18 | airQuality[1] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register LSB
|
19 | airQuality[2] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register CRC
|
20 | airQuality[3] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register MSB
|
21 | airQuality[4] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register LSB
|
22 | airQuality[5] = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA); // Read from register CRC
|
23 | I2C_I2CMasterSendStop(); // I2C Stop
|
24 | asm("nop");
|
25 |
|
26 | for(;;)
|
27 | {
|
28 | CyDelay(1000);
|
29 | /* Send measure_air_quality command every second */
|
30 | I2C_I2CMasterSendStart(DeviceAddress,I2C_I2C_WRITE_XFER_MODE); // Initialize a transaction for writing
|
31 | I2C_I2CMasterWriteByte(MeasureAirQuality_MSB); // Send MSB command
|
32 | I2C_I2CMasterWriteByte(MeasureAirQuality_LSB); // Send LSB command
|
33 | I2C_I2CMasterSendStop(); // I2C Stop
|
34 |
|
35 | CyDelay(10); // wait 10ms
|
36 |
|
37 | /* Readout 6 bytes: 1. CO2 (MSB, LSB, CRC) 2. TVOC (MSB, LSB, CRC) */
|
38 | I2C_I2CMasterSendStart(DeviceAddress,I2C_I2C_READ_XFER_MODE); // Initialize a transaction for reading
|
39 | airQuality[0] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register MSB
|
40 | airQuality[1] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register LSB
|
41 | airQuality[2] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register CRC
|
42 | airQuality[3] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register MSB
|
43 | airQuality[4] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); // Read from register LSB
|
44 | airQuality[5] = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA); // Read from register CRC
|
45 | I2C_I2CMasterSendStop(); // I2C Stop
|
46 | asm("nop");
|
47 | }
|