本帖最后由 jinglixixi 于 2022-3-8 12:59 编辑
利用迪文智能屏的触摸操作可进行参数的设置,其设置的参数又可以借助串行通讯来输出参数值。 若接收该参数,则可以对外设加以控制。 为此,这里设计了一个通过触摸操作来控制LED的功能,其 电路连接如图1所示,也就是将智能屏的TX2和GND引脚分别同GD32L233 开发板的RX和GND相连接,以构成串行通讯通道。
图1电路连接 这样就可以在接收设置参数后,按参数值来控制开发板上的4个LED灯的状态。 在使用串口调试助手的情况下,可观察到触摸操作所发出的数据,见图2所示。
图2数据输出效果 依照智能屏的指令结构可知,所设置的参数为最后2个字节的内容,以“5A A5 06 83 10 70 01 00 01”为例,“00 01”就是参数值。 为此在开发板一侧,可编写一个程序来接收智能屏所发出的数据并提取出参数值。然后在主程序中按参数值的不同控制LED产生不同的效果。 主程序的内容为:
- int main(void)
- {
- uint16_t n;
- led_init();
- systick_config();
- nvic_irq_enable(USART0_IRQn, 0);
- playm();
- gd_eval_com_init(EVAL_COM);
- usart_interrupt_disable(USART0, USART_INT_TBE);
- while(1)
- {
- rx_counter=0;
- usart_interrupt_enable(EVAL_COM, USART_INT_RBNE);
- while(rx_counter < nbr_data_to_read) ;
- n=tx_buffer[6] = rx_buffer[8];
- if(n==1)
- {
- playm();
- gd_eval_led_on(LED1);
- }
- if(n==2)
- {
- playm();
- gd_eval_led_on(LED2);
- }
- if(n==3)
- {
- playm();
- gd_eval_led_on(LED3);
- }
- if(n==4)
- {
- playm();
- gd_eval_led_on(LED4);
- }
- if(n==5)
- {
- playm();
- }
- if(n==6)
- {
- gd_eval_led_on(LED1);
- gd_eval_led_on(LED2);
- gd_eval_led_on(LED3);
- gd_eval_led_on(LED4);
- }
- delay_1ms(200);
- rx_counter=0;
- usart_interrupt_enable(USART0, USART_INT_RBNE);
- delay_1ms(200);
- }
- }
复制代码
经程序的编译下载,即可提供触摸操作来产生图3至图6所示的控制效果。
图3点亮全部LED
图4第1个LED
图5第3个LED
图6熄灭全部LED
在此基础上,若在开发板一侧再添加上一个W2812的彩色灯环,则可以产生出缤纷的色彩效果以营造环境的灯光氛围。 由于W2812是一种单总线的器件,所以它只需一个GPIO引脚就可完成其控制,其信号引脚是与PB10来连接,其连接关系如图7所示。
图7器件连接 实现W2812控制的主程序为:
- int main(void)
- {
- uint16_t n;
- led_init();
- systick_config();
- nvic_irq_enable(USART0_IRQn, 0);
- playm();
- gd_eval_com_init(EVAL_COM);
- usart_interrupt_disable(USART0, USART_INT_TBE);
- WS2812B_Init();
- while(1)
- {
- rx_counter=0;
- usart_interrupt_enable(EVAL_COM, USART_INT_RBNE);
- while(rx_counter < nbr_data_to_read);
- n=tx_buffer[6] = rx_buffer[8];
- if(n==1)
- {
- WS2812B_DisplayAllRed();
- }
- if(n==2)
- {
- WS2812B_DisplayAllGreen();
- }
- if(n==3)
- {
- WS2812B_DisplayAllBlue();
- }
- if(n==4)
- {
- WS2812B_Displayyellow();
- }
- if(n==5)
- {
- WS2812B_DisplayMAGENTA();
- }
- if(n==6)
- {
- WS2812B_DisplayGBLUE();
- }
- if(n==7)
- {
- WS2812B_DisplayAll();
- }
- delay_1ms(200);
- rx_counter=0;
- usart_interrupt_enable(USART0, USART_INT_RBNE);
- delay_1ms(200);
- }
- }
复制代码
经程序的编译下载,其控制效果如图8和图9所示。 |