物联网嵌入式工程师40周[包含电子书]|之 IIC总线通信(二)
物联网嵌入式工程师40周[包含电子书]|之 IIC总线通信(二)
附上资源MP4下载地址
我用夸克网盘分享了「2025年物联网嵌入式工程师40周[包含电子书]-完结」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。
链接:https://pan.quark.cn/s/3b0a95006b2d失效访问:cowcowit.com/course/4/4
一、光环境传感器硬件原理图分析
二、IMX6ull IIC控制器介绍
The Inter IC (I2C) provides functionality of a standard I2C slave and master. The I2C is designed to be compatible with the standard NXP I2C bus protocol.
I2C提供标准I2C从站和主站的功能。I2C被设计为与标准NXP I2C总线协议兼容。
I2C is a two-wire, bidirectional serial bus that provides a simple, efficient method of data exchange, minimizing the interconnection between devices. This bus is suitable for applications requiring occasional communications over a short distance between many devices. The flexible I2C standard allows additional devices to be connected to the bus for expansion and system development.
I2C是一种双线双向串行总线,它提供了一种简单、高效的数据交换方法,最大限度地减少了设备之间的互连。这种总线适用于需要在许多设备之间进行短距离偶尔通信的应用。灵活的I2C标准允许将其他设备连接到总线以进行扩展和系统开发。
1.IIC控制器设计
2.Clocks
l Peripheral clock: This clock is used for peripheral bus register read/writes. (外围时钟:该时钟用于外围总线寄存器读/写)
l Module clock: This is the functional clock of the I2C. The serial bit clock frequency is derived from the module clock. The module clock and peripheral clocks are synchronous with each other. The minimum frequency of the module clock should be 12.8 MHz for Fast mode to achieve 400-kbps operation.(模块时钟:这是I2C的功能时钟。串行位时钟频率来自模块时钟。模块时钟和外围时钟相互同步。快速模式下模块时钟的最小频率应为12.8 MHz,以实现400 kbps操作)
3.Arbitration procedure
If multiple devices simultaneously request the bus, the bus clock is determined by a synchronization procedure in which the low period equals the longest clock-low period among the devices, and the high period equals the shortest. A data arbitration procedure determines the relative priority of competing devices.
如果多个设备同时请求总线,则总线时钟由同步过程确定,其中低周期等于设备之间最长的时钟低周期,高周期等于最短的。数据仲裁过程确定竞争设备的相对优先级。
A device loses arbitration if it sends logic high while another sends logic low; it immediately switches to Slave Receive mode and stops driving I2Cn_SDA. In this case, the transition from master to Slave mode does not generate a Stop condition. Meanwhile, hardware sets the arbitration lost bit in the I2C Status register (I2C_I2SR[IAL] to indicate loss of arbitration).
如果设备发送逻辑高电平而另一个发送逻辑低电平,则设备将失去仲裁;它会立即切换到从属接收模式并停止驱动I2Cn_SDA。在这种情况下,从主模式到从属模式的转换不会生成停止条件。同时,硬件会在I2C状态寄存器中设置仲裁丢失位(I2C_I2SR[IAL]以指示仲裁丢失)。
4.Initialization sequence
Before the interface can transfer serial data, registers must be initialized, as listed here. (在接口可以传输串行数据之前,必须初始化寄存器,如下所示)
\1. Set the data sampling rate (I2C_IFDR[IC] to obtain SCL frequency from the system bus clock(设置数据采样率(I2C_IFDR[IC],从系统总线时钟获取SCL频率).
\2. Update the address in the (I2C_IADR) to define its slave address (address can range from 0 to 0x7f). (更新(I2C_IADR)中的地址以定义其从属地址(地址范围可以从0到0x7f))
\3. Set the I2C enable bit (I2C_I2CR[IEN]) to enable the I2C bus interface system. (设置I2C使能位(I2C_I2CR[IEN])使能I2C总线接口系统)
\4. Modify the bits in the I2C_I2CR to select Master/Slave mode, Transmit/Receive mode, and Interrupt-Enable or not.(修改I2C_I2CR中的位以选择主/从模式、发送/接收模式和中断-启用与否)
5.Generation of Start
After completion of the initialization procedure, serial data can be transmitted by selecting the Master Transmit mode. On a multimaster bus system, the busy bus (I2C_I2SR[IBB]) must be tested to determine whether the serial bus is free. If the bus is free (IBB = 0), the Start signal and the first byte (the slave address) can be sent. The data written to the data register comprises the address of the desired slave and the LSB indicates the transfer direction.
在完成初始化程序后,可以通过选择主传输模式来传输串行数据。在多主总线系统上,必须测试繁忙总线(I2C_I2SR[IBB])以确定串行总线是否空闲。如果总线空闲(IBB=0),则可以发送Start信号和第一个字节(从属地址)。写入数据寄存器的数据包括所需从属的地址,LSB指示传输方向。
The free time between a Stop and the next Start condition is built into the hardware that generates the Start cycle. Depending on the relative frequencies of the system clock and the SCL period, it may be necessary to wait until the I2C is not busy after writing the calling address to the data register (I2C_I2DR), before proceeding to load data into the data register (I2C_I2DR).
停止和下一个开始条件之间的空闲时间内置在生成开始周期的硬件中。根据系统时钟和SCL周期的相对频率,在将调用地址写入数据寄存器(I2C_I2DR)后,可能需要等到I2C不忙,然后再将数据加载到数据寄存器(I2C_I2DR)中。
6.Generation of Stop
A data transfer ends when the master signals a Stop, which can occur after all data is sent. For a master receiver to terminate a data transfer, it must inform the slave transmitter by not acknowledging the last data byte. This is done by setting the transmit acknowledge bit (I2C_I2CR[TXAK]) before reading the next-to-last byte. Before the last byte is read, a Stop signal must be generated.
数据搬迁结束时,主站发出停止信号,这可能发生在所有数据发送后。对于主接收器终止数据搬迁,它必须通过不确认最后一个数据字节来通知从站发送器。这是通过在读取倒数第二个字节之前设置传输确认位(I2C_I2CR[TXAK])来完成的。在读取最后一个字节之前,必须生成停止信号。
7.Generation of Repeated Start
After the data transfer, if the master still requires the bus, it can signal another Start followed by another slave address without signaling a Stop.
数据搬迁后,如果主站仍然需要总线,它可以发出另一个开始信号,然后是另一个从站地址,而不会发出停止信号。
8.Post-transfer software response
Sending or receiving a byte sets the data transferring bit (I2C_I2SR[ICF]), which indicates one byte of communication is finished. Upon completion, the interrupt status (I2C_I2SR[IIF]) is also set. An external interrupt is generated if the interrupt enable (I2C_I2CR[IIEN]) is set. The software must first clear the interrupt status (I2C_I2SR[IIF]) in the interrupt routine.
发送或接收一个字节设置数据传输位(I2C_I2SR[ICF]),表示一个字节的通信完成。完成后,还设置中断状态(I2C_I2SR[IIF])。如果设置了中断使能(I2C_I2CR[IIEN]),则会生成外部中断。软件必须首先清除中断例程中的中断状态(I2C_I2SR[IIF])。
The data transferring bit (I2C_I2SR[ICF]) is cleared either by reading from I2C_I2DR in Receive mode or by writing to this register in Transmit mode.
数据传输位(I2C_I2SR[ICF])通过在接收模式下从I2C_I2DR读取或在传输模式下写入该寄存器来清除。
三、IIC控制器重点寄存器介绍
1.I2C Frequency Divider Register (I2Cx_IFDR)
The I2C_IFDR provides a programmable prescaler to configure the clock for bit-rate selection. The register does not get reset by a software reset.
I2C_IFDR提供了一个可编程的预分频器来配置时钟以进行比特率选择。寄存器不会被软件复位复位。
**I2C clock Frequency = (PERCLK_ROOT frequency)/(division factor corresponding to IFDR)*
2.I2C Control Register (I2Cx_I2CR)
3.I2C Status Register (I2Cx_I2SR)
4.I2C Data I/O Register (I2Cx_I2DR)
**In Master Receive mode, reading the data register allows a read to occur and initiates the next byte to be received.\In Slave mode, the same function is available after it is addressed.
在主接收模式下,读取数据寄存器允许读取发生并启动要接收的下一个字节。在从属模式下,相同的功能在寻址后可用。
四、IMX6ull IIC控制器操作
1.管脚初始化
1 void iic1_pin_init(void)
2 {
3 //SCL
4 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO28 &= ~(0xf << 0);
5 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO28 |= (0x2 << 0);
6
7 //SDA
8 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO29 &= ~(0xf << 0);
9 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO29 |= (0x2 << 0);
10
11 return;
12 }
2.控制器初始化
1 void iic_con_init(I2C_Type *base)
2 {
3 /i2c disabled/
4 base->I2CR &= ~(1 << 7);
5
6 /*
7 *I2C SCL = 100KHZ
8 *IPG CLK = 66MHZ = 66000KHZ
9 * DIV = 660
10 */
11 base->IFDR = 0x15;
12
13 /Enable I2C/
14 base->I2CR |= (1 << 7);
15
16 return;
17 }
3.产生开始信号
1 void iic_con_send_start(I2C_Type *base)
2 {
3 /Clear Interrupt,IIC start work/
4 base->I2SR &= ~(1 << 1);
5
6 /*
7 MSTA [5] Changing MSTA from 0 to 1 signals a Start
8 on the bus and selects Master mode.
9 */
10 base->I2CR &= ~(1 << 5);
11 base->I2CR |= (1 << 5);
12
13 return;
14 }
15
16 void iic_con_send_repeat_start(I2C_Type *base)
17 {
18 /Clear Interrupt,IIC start work/
19 base->I2SR &= ~(1 << 1);
20
21 /*
22 RSTA [2] 1 Generates a Repeated Start condition
23 */
24 base->I2CR |= (1 << 2);
25
26 return;
27 }
4.发送从机地址和读写位
1 void iic_con_send_slave_addr(I2C_Type *base,uint8_t slave_rw)
2 {
3 int flag;
4
5 /Master Transmit Mode/
6 base->I2CR |= (1 << 4);
7
8 /Send data/
9 base->I2DR = slave_rw;
10
11 /Wait One byte transfer is completed/
12 do{
13 flag = base->I2SR & (1 << 1);
14 }while(!flag);
15
16 /Clear Interrupt,IIC start work/
17 base->I2SR &= ~(1 << 1);
18
19 return;
20 }
5.产生结束信号
1 void iic_con_send_stop(I2C_Type *base)
2 {
3 gpt_delay_ms(10);
4
5 /Clear Interrupt,IIC start work/
6 base->I2SR &= ~(1 << 1);
7
8 /*
9 MSTA [5] Changing MSTA from 1 to 0 generates a Stop
10 and selects Slave mode
11 */
12 base->I2CR |= (1 << 5);
13 base->I2CR &= ~(1 << 5);
14
15 return;
16 }
6.读写数据
(1)写数据
1 void iic_con_send_data(I2C_Type *base,uint8_t *buf,uint32_t size)
2 {
3 int i;
4 int flag;
5
6 /Master Transmit Mode/
7 base->I2CR |= (1 << 4);
8
9 for(i = 0;i < size;i ++){
10 /Send data/
11 base->I2DR = buf[i];
12
13 /Wait One byte transfer is completed/
14 do{
15 flag = base->I2SR & (1 << 1);
16 }while(!flag);
17
18 /Clear Interrupt,IIC start work/
19 base->I2SR &= ~(1 << 1);
20 }
21
22 return;
23 }
(2)读数据
1 void iic_con_read_data(I2C_Type *base,uint8_t *buf,uint32_t size)
2 {
3 int i;
4 int flag;
5 uint8_t dumy;
6
7 /*
8 TXAK [3] 0 An acknowledge signal is sent
9 MTX [4] 0 Receive.
10 */
11 base->I2CR &= ~((1 << 4) | (1 << 3));
12
13 /Dumy data/
14 dumy = base->I2DR;
15
16 for(i = 0;i < size;i ++){
17
18 if(i == size – 1){
19 /*
20 * TXAK [3] 1 No acknowledge signal response is sent
21 */
22 base->I2CR |= (1 << 3);
23 }
24
25 /Wait One byte transfer is completed/
26 do{
27 flag = base->I2SR & (1 << 1);
28 }while(!flag);
29
30 buf[i] = base->I2DR;
31
32 /Clear Interrupt,IIC start work/
33 base->I2SR &= ~(1 << 1);
34 }
35
36 return;
37 }
7.案例代码具体实现
1 #include “imx6ull.h”
2
3 void iic1_pin_init(void)
4 {
5 //SCL
6 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO28 &= ~(0xf << 0);
7 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO28 |= (0x2 << 0);
8
9 //SDA
10 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO29 &= ~(0xf << 0);
11 IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO29 |= (0x2 << 0);
12
13 return;
14 }
15
16 void iic_con_init(I2C_Type *base)
17 {
18 /i2c disabled/
19 base->I2CR &= ~(1 << 7);
20
21 /*
22 *I2C SCL = 100KHZ
23 *IPG CLK = 66MHZ = 66000KHZ
24 * DIV = 660
25 */
26 base->IFDR = 0x15;
27
28 /Enable I2C/
29 base->I2CR |= (1 << 7);
30
31 return;
32 }
33
34 void iic_con_send_start(I2C_Type *base)
35 {
36 /Clear Interrupt,IIC start work/
37 base->I2SR &= ~(1 << 1);
38
39 /*
40 MSTA [5] Changing MSTA from 0 to 1 signals a Start
41 on the bus and selects Master mode.
42 */
43 base->I2CR &= ~(1 << 5);
44 base->I2CR |= (1 << 5);
45
46 return;
47 }
48
49 void iic_con_send_repeat_start(I2C_Type *base)
50 {
51 /Clear Interrupt,IIC start work/
52 base->I2SR &= ~(1 << 1);
53
54 /*
55 RSTA [2] 1 Generates a Repeated Start condition
56 */
57 base->I2CR |= (1 << 2);
58
59 return;
60 }
61
62 void iic_con_send_slave_addr(I2C_Type *base,uint8_t slave_rw)
63 {
64 int flag;
65
66 /Master Transmit Mode/
67 base->I2CR |= (1 << 4);
68
69 /Send data/
70 base->I2DR = slave_rw;
71
72 /Wait One byte transfer is completed/
73 do{
74 flag = base->I2SR & (1 << 1);
75 }while(!flag);
76
77 /Clear Interrupt,IIC start work/
78 base->I2SR &= ~(1 << 1);
79
80 return;
81 }
82
83 void iic_con_send_stop(I2C_Type *base)
84 {
85 gpt_delay_ms(10);
86
87 /Clear Interrupt,IIC start work/
88 base->I2SR &= ~(1 << 1);
89
90 /*
91 MSTA [5] Changing MSTA from 1 to 0 generates a Stop
92 and selects Slave mode
93 */
94 base->I2CR |= (1 << 5);
95 base->I2CR &= ~(1 << 5);
96
97 return;
98 }
99
100 void iic_con_send_data(I2C_Type *base,uint8_t *buf,uint32_t size)
101 {
102 int i;
103 int flag;
104
105 /Master Transmit Mode/
106 base->I2CR |= (1 << 4);
107
108 for(i = 0;i < size;i ++){
109 /Send data/
110 base->I2DR = buf[i];
111
112 /Wait One byte transfer is completed/
113 do{
114 flag = base->I2SR & (1 << 1);
115 }while(!flag);
116
117 /Clear Interrupt,IIC start work/
118 base->I2SR &= ~(1 << 1);
119 }
120
121 return;
122 }
123
124 void iic_con_read_data(I2C_Type *base,uint8_t *buf,uint32_t size)
125 {
126 int i;
127 int flag;
128 uint8_t dumy;
129
130 /*
131 TXAK [3] 0 An acknowledge signal is sent
132 MTX [4] 0 Receive.
133 */
134 base->I2CR &= ~((1 << 4) | (1 << 3));
135
136 /Dumy data/
137 dumy = base->I2DR;
138
139 for(i = 0;i < size;i ++){
140
141 if(i == size – 1){
142 /*
143 * TXAK [3] 1 No acknowledge signal response is sent
144 */
145 base->I2CR |= (1 << 3);
146 }
147
148 /Wait One byte transfer is completed/
149 do{
150 flag = base->I2SR & (1 << 1);
151 }while(!flag);
152
153 buf[i] = base->I2DR;
154
155 /Clear Interrupt,IIC start work/
156 base->I2SR &= ~(1 << 1);
157 }
158
159 return;
160 }
五、IMX6ull IIC控制器读取光环境传感器数据
1.初始化管脚和I2C控制器
1 void hardware_ltr_553als_wa_init(void)
2 {
3 iic1_pin_init();
4 iic_con_init(I2C1);
5 return;
6 }
2.编写传感器内部寄存器写函数接口
1 void hardware_ltr_553als_wa_write_register(uint8_t regaddr,uint8_t regval)
2 {
3 int ret;
4 uint8_t tx_buf[] = {regaddr,regval};
5
6 //1.产生start信号
7 iic_con_send_start(I2C1);
8
9 //2.发送从机地址 + write
10 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_WRITE);
11
12 //3.发送寄存器地址,发送寄存器数据
13 iic_con_send_data(I2C1,tx_buf,sizeof(tx_buf));
14
15 //4.发送stop信号
16 iic_con_send_stop(I2C1);
17
18 return;
19 }
3.编写传感器内部寄存器读函数接口
1 uint8_t hardware_ltr_553als_wa_read_register(uint8_t regaddr)
2 {
3 int ret;
4 uint8_t rx_buf[1];
5 uint8_t tx_buf[] = {regaddr};
6
7 //1.产生start信号
8 iic_con_send_start(I2C1);
9
10 //2.发送从机地址 + write
11 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_WRITE);
12
13 //3.发送寄存器地址
14 iic_con_send_data(I2C1,tx_buf,sizeof(tx_buf));
15
16 //4.重新发送开始信号
17 iic_con_send_repeat_start(I2C1);
18
19 //7.发送从机地址 + read
20 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_READ);
21
22 //8.读取从机数据
23 iic_con_read_data(I2C1,rx_buf,sizeof(rx_buf));
24
25 //9.发送stop信号
26 iic_con_send_stop(I2C1);
27
28 return rx_buf[0];
29 }
4.读取传感器ID
1 void hardware_read_ltr_553als_wa_manufac_id(void)
2 {
3 uint8_t manufac_id;
4
5 manufac_id = hardware_ltr_553als_wa_read_register(MANUFAC_ID_REG_ADDR);
6 uart_printf(“manufac ID:%#x\r\n”,manufac_id);
7
8 return;
9 }
5.读取接近传感器数据
1 void hardware_ltr_553als_wa_ps_data_read(void)
2 {
3 uint16_t data;
4 uint8_t ps_data[2];
5
6 //Active Mode
7 hardware_ltr_553als_wa_write_register(PS_CONTR_REG_ADDR,0x3);
8 gpt_delay_ms(10);
9
10 ps_data[0] = hardware_ltr_553als_wa_read_register(PS_DATA0_REG_ADDR);
11 gpt_delay_ms(10);
12
13 ps_data[1] = hardware_ltr_553als_wa_read_register(PS_DATA1_REG_ADDR);
14 gpt_delay_ms(10);
15
16 data = ((ps_data[1] & 0x7) << 8) | ps_data[0];
17 uart_printf(“ps data:%d\r\n”,data);
18
19 return;
20 }
6.读取光线传感器数据
1 void hardware_ltr_553als_wa_als_data_read(void)
2 {
3 uint8_t low;
4 uint8_t high;
5 uint16_t channel[2];
6
7 //Active Mode
8 hardware_ltr_553als_wa_write_register(ALS_CONTR_REG_ADDR,0x1);
9 gpt_delay_ms(10);
10
11 //read channel_1 data
12 low = hardware_ltr_553als_wa_read_register(ALS_DATA_CH1_REG0_ADDR);
13 gpt_delay_ms(10);
14
15 high = hardware_ltr_553als_wa_read_register(ALS_DATA_CH1_REG1_ADDR);
16 gpt_delay_ms(10);
17
18 channel[1] = (high << 8) | low;
19
20 //read channel_0 data
21
22 low = hardware_ltr_553als_wa_read_register(ALS_DATA_CH0_REG0_ADDR);
23 gpt_delay_ms(10);
24
25 high = hardware_ltr_553als_wa_read_register(ALS_DATA_CH0_REG1_ADDR);
26 gpt_delay_ms(10);
27
28 channel[0] = (high << 8) | low;
29
30 uart_printf(“channel_0 data:%d\r\n”, channel[0]);
31 uart_printf(“channel_1 data:%d\r\n”, channel[1]);
32
33 return;
34 }
7.案例代码具体实现
1 #include “imx6ull.h”
2
3 #define SLAVE_ADDRESS_WRITE 0x46
4 #define SLAVE_ADDRESS_READ 0x47
5
6 #define MANUFAC_ID_REG_ADDR 0x87
7 #define PS_CONTR_REG_ADDR 0x81
8 #define PS_DATA0_REG_ADDR 0x8D
9 #define PS_DATA1_REG_ADDR 0x8E
10 #define ALS_CONTR_REG_ADDR 0x80
11 #define ALS_DATA_CH1_REG0_ADDR 0x88
12 #define ALS_DATA_CH1_REG1_ADDR 0x89
13 #define ALS_DATA_CH0_REG0_ADDR 0x8A
14 #define ALS_DATA_CH0_REG1_ADDR 0x8B
15
16 void hardware_ltr_553als_wa_init(void)
17 {
18 iic1_pin_init();
19 iic_con_init(I2C1);
20 return;
21 }
22
23 void hardware_ltr_553als_wa_write_register(uint8_t regaddr,uint8_t regval)
24 {
25 int ret;
26 uint8_t tx_buf[] = {regaddr,regval};
27
28 //1.产生start信号
29 iic_con_send_start(I2C1);
30
31 //2.发送从机地址 + write
32 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_WRITE);
33
34 //3.发送寄存器地址
35 iic_con_send_data(I2C1,tx_buf,sizeof(tx_buf));
36
37 #if 0
38 //1.产生start信号
39 iic_con_send_start(I2C1);
40
41 //2.发送从机地址 + write
42 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_WRITE);
43
44 //3.发送寄存器地址,发送寄存器数据
45 iic_con_send_data(I2C1,tx_buf,sizeof(tx_buf));
46 #endif
47
48 //4.发送stop信号
49 iic_con_send_stop(I2C1);
50
51 return;
52 }
53
54 uint8_t hardware_ltr_553als_wa_read_register(uint8_t regaddr)
55 {
56 int ret;
57 uint8_t rx_buf[1];
58 uint8_t tx_buf[] = {regaddr};
59
60 //1.产生start信号
61 iic_con_send_start(I2C1);
62
63 //2.发送从机地址 + write
64 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_WRITE);
65
66 //3.发送寄存器地址
67 iic_con_send_data(I2C1,tx_buf,sizeof(tx_buf));
68
69 //4.重新发送开始信号
70 iic_con_send_repeat_start(I2C1);
71
72 //7.发送从机地址 + read
73 iic_con_send_slave_addr(I2C1,SLAVE_ADDRESS_READ);
74
75 //8.读取从机数据
76 iic_con_read_data(I2C1,rx_buf,sizeof(rx_buf));
77
78 //9.发送stop信号
79 iic_con_send_stop(I2C1);
80
81 return rx_buf[0];
82 }
83
84 void hardware_read_ltr_553als_wa_manufac_id(void)
85 {
86 uint8_t manufac_id;
87
88 manufac_id = hardware_ltr_553als_wa_read_register(MANUFAC_ID_REG_ADDR);
89 uart_printf(“manufac ID:%#x\r\n”,manufac_id);
90
91 return;
92 }
93
94 void hardware_ltr_553als_wa_ps_data_read(void)
95 {
96 uint16_t data;
97 uint8_t ps_data[2];
98
99 //Active Mode
100 hardware_ltr_553als_wa_write_register(PS_CONTR_REG_ADDR,0x3);
101 gpt_delay_ms(10);
102
103 ps_data[0] = hardware_ltr_553als_wa_read_register(PS_DATA0_REG_ADDR);
104 gpt_delay_ms(10);
105
106 ps_data[1] = hardware_ltr_553als_wa_read_register(PS_DATA1_REG_ADDR);
107 gpt_delay_ms(10);
108
109 data = ((ps_data[1] & 0x7) << 8) | ps_data[0];
110 uart_printf(“ps data:%d\r\n”,data);
111
112 return;
113 }
114
115 void hardware_ltr_553als_wa_als_data_read(void)
116 {
117 uint8_t low;
118 uint8_t high;
119 uint16_t channel[2];
120
121 //Active Mode
122 hardware_ltr_553als_wa_write_register(ALS_CONTR_REG_ADDR,0x1);
123 gpt_delay_ms(10);
124
125 //read channel_1 data
126 low = hardware_ltr_553als_wa_read_register(ALS_DATA_CH1_REG0_ADDR);
127 gpt_delay_ms(10);
128
129 high = hardware_ltr_553als_wa_read_register(ALS_DATA_CH1_REG1_ADDR);
130 gpt_delay_ms(10);
131
132 channel[1] = (high << 8) | low;
133
134 //read channel_0 data
135 low = hardware_ltr_553als_wa_read_register(ALS_DATA_CH0_REG0_ADDR);
136 gpt_delay_ms(10);
137
138 high = hardware_ltr_553als_wa_read_register(ALS_DATA_CH0_REG1_ADDR);
139 gpt_delay_ms(10);
140
141 channel[0] = (high << 8) | low;
142
143 uart_printf(“channel_0 data:%d\r\n”, channel[0]);
144 uart_printf(“channel_1 data:%d\r\n”, channel[1]);
145
146 return;
147 }
148
149 void hardware_test_ltr_553als_wa(void)
150 {
151 hardware_ltr_553als_wa_init();
152
153 while(1){
154 hardware_read_ltr_553als_wa_manufac_id();
155 hardware_ltr_553als_wa_ps_data_read();
156 hardware_ltr_553als_wa_als_data_read();
157 gpt_delay_sec(1);
158 }
159
160 return;
161 }
作者:小牛cow