本帖最后由 lulugl 于 2025-4-19 22:23 编辑
【前言】
英飞凌PSOC 4000T是集成了第五代 CAPSENSE™ 触控技术,可以轻松实现触摸感应,这篇我将分享如何使用2按键来实现按键感应与OLED的结合,这实现图形化的展示:
【实现步骤】
1、首先新建一个基本2按键的示例程序:
2、选择2个按键的示例工程:
3、生成工程后,打开外设配置,由于原先的工程,他是做为slave来使用的,需要修改为master模式。如下图所示:
4、根据I2C外设的例程,复制一个原来我驱动OLED屏的示例中的OLED的驱动到工程中,帖子链接为:https://bbs.21ic.com/icview-3446028-1-1.html
5、最后工程为文件为如下所示:
6、在led控制函数中添加OLED显示函数:
- /*******************************************************************************
- * Function Name: led_control
- ********************************************************************************
- * Summary:
- * Control the LEDs one the expansion board to show the button status:
- * No touch - LEDs == OFF
- * Touch - Corresponding LED == ON
- *******************************************************************************/
- // 定义全局标志位
- static uint8_t led_states = 0;
- static uint8_t gui_fill_states = 0;
- void led_control()
- {
- uint8_t new_led_states = 0;
- uint8_t new_gui_fill_states = 0;
- uint8_t oled_need_refresh = 0;
- // 检查第一个按键和 LED 状态
- if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_BUTTON0_WDGT_ID, &cy_capsense_context))
- {
- new_led_states |= 0x01;
- new_gui_fill_states |= 0x01;
- }
- else
- {
- new_led_states &= ~0x01;
- new_gui_fill_states &= ~0x01;
- }
- // 检查第二个按键和 LED 状态
- if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_BUTTON1_WDGT_ID, &cy_capsense_context))
- {
- new_led_states |= 0x02;
- new_gui_fill_states |= 0x02;
- }
- else
- {
- new_led_states &= ~0x02;
- new_gui_fill_states &= ~0x02;
- }
- // 判断是否需要更新第一个矩形填充
- if ((new_gui_fill_states & 0x01) != (gui_fill_states & 0x01))
- {
- Cy_GPIO_Write(CYBSP_KEYPAD_LED1_PORT, CYBSP_KEYPAD_LED1_NUM, (new_gui_fill_states & 0x01) ? CYBSP_LED_ON : CYBSP_LED_OFF);
- GUI_FillRectangle(16, 32, 48, 48, (new_gui_fill_states & 0x01) ? 1 : 0);
- }
- // 判断是否需要更新第二个矩形填充
- if ((new_gui_fill_states & 0x02) != (gui_fill_states & 0x02))
- {
- Cy_GPIO_Write(CYBSP_KEYPAD_LED2_PORT, CYBSP_KEYPAD_LED2_NUM, (new_gui_fill_states & 0x02) ? CYBSP_LED_ON : CYBSP_LED_OFF);
- GUI_FillRectangle(64, 32, 96, 48, (new_gui_fill_states & 0x02) ? 1 : 0);
- }
- // 判断是否需要刷新 OLED
- if (new_led_states != led_states)
- {
- oled_need_refresh = 1;
- }
- // 更新 LED 状态和 GUI 填充状态
- led_states = new_led_states;
- gui_fill_states = new_gui_fill_states;
- // 只有当 LED 状态变化时才刷新 OLED
- if (oled_need_refresh)
- {
- OLED_Display();
- }
- }
【实现效果】
当我们按下按键时,在OLED屏上显示一个白色的方块:
|