本次测试的是I2C外设,测试使用了ROHM的传感器,光照度传感器RPR-0521RS,气压传感器BM1383AGLV,地磁传感器BM1422AGMV。这几个传感器都是I2C接口,可以一起接I2C测试。
如图:使用arduino接口插在开发板。I2C使用的是I2C0,对应管脚为PB8:SCL,PB9:SDA。
这几个传感器前面都用过,直接拿以前的代码过来用,只是把I2C接口的初始化对应上,再就是I2C的读写函数。
开始粗心了,初始化没有把I2C0的时钟使能,就一直I2C没反应。后面加上就好了,测试很顺利。
- void i2c_gpio_init( void )
- {
- #ifdef I2C_USE_GPIO
-
- #else
- gpio_init_t a;
- /* Initialize scl pin */
- a.mode = GPIO_MODE_OUTPUT;
- a.odos = GPIO_OPEN_DRAIN;
- a.pupd = GPIO_PUSH_UP;
- a.nodrv = GPIO_OUT_DRIVE_6;
- a.podrv = GPIO_OUT_DRIVE_6;
- a.flt = GPIO_FILTER_DISABLE;
- a.type = GPIO_TYPE_TTL;
- a.func = GPIO_FUNC_5;
- ald_gpio_init(I2C_SCL_Port, I2C_SCL_PIN, &a);
- /* Initialize sda pin */
- a.mode = GPIO_MODE_OUTPUT;
- a.odos = GPIO_OPEN_DRAIN;
- a.pupd = GPIO_PUSH_UP;
- a.nodrv = GPIO_OUT_DRIVE_6;
- a.podrv = GPIO_OUT_DRIVE_6;
- a.flt = GPIO_FILTER_DISABLE;
- a.type = GPIO_TYPE_TTL;
- a.func = GPIO_FUNC_5;
- ald_gpio_init(I2C_SDA_Port, I2C_SDA_PIN, &a);
- /* Initialize I2C struct */
- memset(&h_i2c, 0, sizeof(h_i2c));
-
- /* Enable i2c interrupt */
- // ald_mcu_irq_config(I2C1_EV_IRQn, 3, 3, ENABLE);
- // ald_mcu_irq_config(I2C1_ERR_IRQn, 3, 3, ENABLE);
- /* Initialize I2C */
- h_i2c.perh = I2C0;
- h_i2c.init.clk_speed = 100000;
- h_i2c.init.module = I2C_MODULE_MASTER;
- h_i2c.init.dual_addr = I2C_DUALADDR_ENABLE;
- h_i2c.init.own_addr1 = 0xA0;
- h_i2c.init.addr_mode = I2C_ADDR_7BIT;
- h_i2c.init.general_call = I2C_GENERALCALL_ENABLE;
- h_i2c.init.no_stretch = I2C_NOSTRETCH_ENABLE;
- ald_i2c_reset(&h_i2c);
- ald_i2c_init(&h_i2c);
- #endif
- }
- int main()
- {
- uint32_t ms,ledms;
- uint32_t key_val,key_old,key_xor;
- uint8_t utbuff[6];
-
- float press; float temp;
- uint16_t ps; float als;
-
- /* Initialize ALD */
- ald_cmu_init();
- /* Configure system clock */
- ald_cmu_pll1_config(CMU_PLL1_INPUT_HOSC_3, CMU_PLL1_OUTPUT_96M);
- ald_cmu_clock_config(CMU_CLOCK_PLL1, 96000000);
- /* Enable peripheral clock */
- ald_cmu_perh_clock_config(CMU_PERH_GPIO, ENABLE);
- ald_cmu_perh_clock_config(CMU_PERH_UART0, ENABLE);
- ald_cmu_perh_clock_config(CMU_PERH_I2C0, ENABLE);
-
- uart_stdio_init();
- memset(uart_buf, 0x0, 64);
- light_init();
- key_init();
- lcd_gpio_init();
- i2c_gpio_init();
-
- printf_e("System start...\n");
- light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_ON);
- ald_delay_ms(1000);
- light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_OFF);
- ald_delay_ms(1000);
-
- if(BM1422AGMV_Init() == 0)
- {
- printf_e("BM1422AGMV_Init OK\r\n");
- }
- if(BM1383AGLV_Init() == 0)
- {
- printf_e("BM1383AGLV_Init OK\r\n");
- }
- if(RPR0521RS_Init() == 0)
- {
- printf_e("RPR0521RS_Init OK\r\n");
- }
-
- ms = ald_get_tick()+10;
- ledms = ald_get_tick()+1000;
- key_old = 0;
- while (1)
- {
- //按键扫描
- if(ms < ald_get_tick())
- {
- ms = ald_get_tick()+10;
-
- key_val = 0;
- if(ald_gpio_read_pin(KEY_UP_PORT, KEY_UP_PIN) == 0) key_val |= 0x01;
- if(ald_gpio_read_pin(KEY_DOWN_PORT, KEY_DOWN_PIN) == 0) key_val |= 0x02;
- if(ald_gpio_read_pin(KEY_LEFT_PORT, KEY_LEFT_PIN) == 0) key_val |= 0x04;
- if(ald_gpio_read_pin(KEY_RIGHT_PORT, KEY_RIGHT_PIN) == 0) key_val |= 0x08;
- if(ald_gpio_read_pin(KEY_CENTER_PORT, KEY_CENTER_PIN) == 0) key_val |= 0x10;
-
- key_xor = key_old ^ key_val;
- if(key_xor & 0x01)
- {
- if(key_val &0x01)
- {
- printf_e("key up press +\n");
- }else
- {
- printf_e("key up relese-\n");
- }
- }
-
- if(key_xor & 0x02)
- {
- if(key_val &0x02)
- {
- printf_e("key down press +\n");
- }else
- {
- printf_e("key down relese-\n");
- }
- }
- if(key_xor & 0x04)
- {
- if(key_val &0x04)
- {
- printf_e("key left press +\n");
- }else
- {
- printf_e("key left relese-\n");
- }
- }
- if(key_xor & 0x08)
- {
- if(key_val &0x08)
- {
- printf_e("key right press +\n");
- }else
- {
- printf_e("key right relese-\n");
- }
- }
- if(key_xor & 0x10)
- {
- if(key_val &0x10)
- {
- printf_e("key center press +\n");
- }else
- {
- printf_e("key center relese-\n");
- }
- }
- key_old = key_val;
- }
-
- if(ledms < ald_get_tick())
- {
- ledms = ald_get_tick()+1000;
- light_ctrl(LIGHT_IDX_1, LIGHT_CTRL_TOGGLE);
- light_ctrl(LIGHT_IDX_2, LIGHT_CTRL_TOGGLE);
- printf_e("Count: %d\n", __cnt++);
-
- BM1422AGMV_get_rawdata(utbuff);
- printf_e("BM1422AGMV:xx=%u\t\tyy=%u\t\tzz=%u\r\n",(uint16_t)(utbuff[1]<<8)|utbuff[0],
- (uint16_t)(utbuff[3]<<8)|utbuff[2],
- (uint16_t)(utbuff[5]<<8)|utbuff[4]);
- RPR0521RS_get_psalsval(&ps,&als);
- printf_e("RPR0521RS:Lx=%0.2f\t\tPs=%d\n",als,ps);
-
- BM1383AGLV_get_val(&press,&temp);
- printf_e("BM1383AGLV:P=%0.2f\t\tTem=%0.2f\n",press,temp);
- }
- }
- }
下面就是读取的数据,3个传感器的数值在串口上显示如下。
主要代码如下:
|