STM32评估板与迪文屏交互,没几行代码就实现一些简单功能,也没那么难!

今日头条

1151人已加入

描述

STM32评估板与迪文屏做交互,也没那么难! 没几行C代码就可以实现一些简单的功能。跟大家分享下,详细视频和说明请参照http://bbs.elecfans.com/forum.php?mod=viewthread&tid=1626837&extra=


一、评估板使用说明

1、功能

LCD屏

注:可实现的功能:

a.通过屏上的图标来控制5LED灯的状态(LED灯的开和关来表示状态),上电5LED熄灭状态。

b. 通过评估板上的物理按键1,来控制亮屏和息屏,上电状态为亮屏状态。

c. 通过评估板上的物理按键2&3,来调节屏幕上的温度指数,上电初始值          25度,以1为步长递增递减,最小0,最大255

d. 通过评估板上的物理按键4,来控制音乐的播放和停止,上电音乐不播放。

e. 通过评估板上的物理按键5&6来调节音量的大小,上电音量值50,以10为步长递增递减,最小0,最大250

 

2、需使用的工具

a.硬件:正点原子Mini STM32开发板(MCUSTM32F103RCT6,串口TTL电平)+面包板手工焊接5LED6路按键扩展。

b. 软件:STM32CubeMX+KEIL MDK5

c.原理图:详见“硬件原理图”文件夹。

d.程序:工程文件在DGUS II工程文件夹中,下载DWIN_SET导入屏中即可。 STM32程序在“MCU_STM32F103RCT6程序”文件夹。


二、硬件及软件介绍

1、硬件原理图:详见“硬件原理图”文件夹

1)硬件接口定义:

LCD屏


2)硬件原理图:

                                              LCD屏

1STM32F103原理图

 

LCD屏

2LEDKEY原理图

2、软件程序

界面工程文件在“DGUS_II工程”文件夹中。STM32程序在“MCU_STM32F103RCT6程序”文件夹中。

 

1)界面工程

LED1图标:0X1000

LED1按键:0X1200

LED2图标:0X1001

LED2按键:0X1201

LED3图标:0X1002

LED3按键:0X1202

LED4图标:0X1003

LED4按键:0X1203

LED5图标:0X1004

LED5按键:0X1204

音量显示框地址:0X1210

温度显示框地址:0X1110

 

2MCU程序

1.main():硬件初始化,触摸屏初始化,按键扫描,接收触摸屏数据处理

int main(void)

{

uint8_t i;

HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_USART1_UART_Init();

  while (1)

  {     

         HAL_UART_Receive_IT(&huart1,&usart1_data,1);

         if(UP_POWER_FLAG == 0)//上电初始化触摸屏

         {

              UP_POWER_FLAG = 1;

              HMI_SEND_BUF[0] = TEMP;

              PLC_Data_Wr2HMI(0X1110,0,1);

              delay_ms(100);

              HMI_SEND_BUF[0] = VOLUME;

              PLC_Data_Wr2HMI(0X1210,0,1);//ÒôÁ¿ÏÔʾ

              delay_ms(100);

              WRITE_HMI(0X00A1,VOLUME,0x00,0,0,2);//ĬÈÏÒôÁ¿50

              delay_ms(100);

              for(i=0;i<5;i++)

              {

                   HMI_SEND_BUF[i] = 0;

              }

              for(i=0;i<20;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              HMI_HANDLEFLAG = 0;

         }

         if(PLAY ==1)//监测是否播放完音乐

         {

              READ_HMI(0x00a1,1);

              delay_ms(100);

              if(HMI_HANDLEFLAG)

              {

                   if(usart1_rxBuf[8] == 0)

                   {

                       PLAY = 0;

                       for(i=0;i<50;i++)

                       {

                            usart1_rxBuf[i] = 0x00;

                       }

                       usart1_counter = 0;

                   }

              }

         }

         SCAN_KEY(0);//按键扫描

         if(HMI_HANDLEFLAG)//接收触摸屏数据处理

         {

              HMI_HANDLEFLAG = 0;

              CLOSE_OPEN_IO(0x01);

              CLOSE_OPEN_IO(0x02);

              CLOSE_OPEN_IO(0x03);

              CLOSE_OPEN_IO(0x04);

              CLOSE_OPEN_IO(0x05);

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

         }            

}   

2. PLC_Data_Wr2HMI ():数据写入触摸屏变量寄存器

void PLC_Data_Wr2HMI(uint16_t HMI_start_addr,uint16_t PLC_return_data_start_addr,uint8_t len)

{

     uint8_t i;

     usart1_txBuf[0]=0x5a;

     usart1_txBuf[1]=0xa5;

     usart1_txBuf[2]=len*2+3;

     usart1_txBuf[3]=0x82;

     usart1_txBuf[4]=((uint8_t)(HMI_start_addr>>8))&0XFF;

     usart1_txBuf[5]=(uint8_t)(HMI_start_addr&0XFF);

     for(i=0;i

     {

         usart1_txBuf[i*2+6]=((HMI_SEND_BUF[i+PLC_return_data_start_addr] >>8)&0xff);

         usart1_txBuf[i*2+7]=(HMI_SEND_BUF[i+PLC_return_data_start_addr] &0xff);

     }

     HAL_UART_Transmit(&huart1, usart1_txBuf, len*2+6, 10);//串口发送

}

3. WRITE_HMI ():数据写入触摸屏寄存器

void WRITE_HMI(uint16_t addr,uint8_t data1,uint8_t data2,uint8_t data3,uint8_t data4,uint8_t len)

{

         usart1_txBuf[0]=0x5a;

         usart1_txBuf[1]=0xa5;

         usart1_txBuf[2]=len*2+3;

         usart1_txBuf[3]=0x82;

         usart1_txBuf[4]=(uint8_t)((addr>>8)&0xff);

         usart1_txBuf[5]=(uint8_t)(addr&0xff);

         usart1_txBuf[6]=data1;

         usart1_txBuf[7]=data2;

         usart1_txBuf[8]=data3;

         usart1_txBuf[9]=data4;

         HAL_UART_Transmit(&huart1, usart1_txBuf, len*2+6 , 20);

}

4. READ_HMI ():读触摸屏寄存器数据

void READ_HMI(uint16_t addr,uint8_t num)

{

         usart1_txBuf[0]=0x5a;

         usart1_txBuf[1]=0xa5;

         usart1_txBuf[2]=0x04;

         usart1_txBuf[3]=0x83;

         usart1_txBuf[4]=(uint8_t)((addr>>8)&0xff);

         usart1_txBuf[5]=(uint8_t)(addr&0xff);

         usart1_txBuf[6]=num;

 

         HAL_UART_Transmit(&huart1, usart1_txBuf, 7 , 20);

}

5). CLOSE_OPEN_IO ():开关LED

void CLOSE_OPEN_IO(uint8_t hmi_key_value)

{

     uint8_t i;

     if(usart1_rxBuf[7] == 0x00&&usart1_rxBuf[8] == hmi_key_value)//LED1

     {

         if(LED_FLAG[hmi_key_value-1] == 0)

         {

           if(hmi_key_value == 0x01)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET);//开LED1

              }

              if(hmi_key_value == 0x02)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_RESET); //开LED2

              }

              if(hmi_key_value == 0x03)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET); //开LED3

              }

              if(hmi_key_value == 0x04)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); //开LED4

              }

              if(hmi_key_value == 0x05)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); //开LED5

              }

              LED_FLAG[hmi_key_value-1] = 1;

         }

         else if(LED_FLAG[hmi_key_value-1] == 1)

         {

           if(hmi_key_value == 0x01)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_SET);//关LED1

              }

              if(hmi_key_value == 0x02)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_SET); //关LED2

              }

              if(hmi_key_value == 0x03)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET); //关LED3

              }

              if(hmi_key_value == 0x04)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); //关LED4

              }

              if(hmi_key_value == 0x05)

              {   

                   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); //关LED5

              }

              LED_FLAG[hmi_key_value-1] = 0;

         }

         for(i=0;i<9;i++)

         {

              usart1_rxBuf[i] = 0xff;

         }

         for(i=0;i<5;i++)

         {

              HMI_SEND_BUF[i] = LED_FLAG[i];

         }

         PLC_Data_Wr2HMI(0X1000,0,5);//发送LED状态到触摸屏,改变LED显示图标;

         delay_ms(50);

     }

}

6). CLOSE_OPEN_IO ():开关LED

void SCAN_KEY(uint8_t io_key_value) 

{

     uint8_t i;

         if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_10)==GPIO_PIN_RESET)//KEY1按下

         {

              delay_ms(150);//去抖

              if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_10)==GPIO_PIN_RESET)

              {

                   if(HMI_FLAG[0] == 0)

                   {

                       WRITE_HMI(0X82,0x00,0x00,0,0,1);//熄灭屏幕

                       HMI_FLAG[0] = 1;

                   }else if(HMI_FLAG[0] == 1)

                   {

                       WRITE_HMI(0X82,0X64,0x00,0,0,1);//点亮屏幕

                       HMI_FLAG[0] = 0;

                   }

              }

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

         }

         if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_11)==GPIO_PIN_RESET)//KEY2按下

         {

              delay_ms(150);//去抖

              if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_11)==GPIO_PIN_RESET)

              {

                   TEMP += 1;

                   HMI_SEND_BUF[0] = TEMP;

                   PLC_Data_Wr2HMI(0X1110,0,1);//温度+

              }

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

         }

         if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)==GPIO_PIN_RESET)//KEY3按下

         {

              delay_ms(150);//去抖

              if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)==GPIO_PIN_RESET)

              {

                   TEMP -= 1;

                   HMI_SEND_BUF[0] = TEMP;

                   PLC_Data_Wr2HMI(0X1110,0,1);//温度-

              }

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

         }

         if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13)==GPIO_PIN_RESET)//KEY4按下

         {

              delay_ms(150);//去抖

              if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13)==GPIO_PIN_RESET)

              {

                   if(PLAY == 0)

                   {            

                       WRITE_HMI(0X00A0,0X00,0x0E,0,0,1);

                       PLAY =1;//播放

                   }else if(PLAY == 1)

                   {                          

                       WRITE_HMI(0X00A0,0X00,0x00,0,0,1);

                       PLAY =0;//停止

                   }

              }

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

          }

         if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==GPIO_PIN_RESET)//KEY5

         {

              delay_ms(150);//去抖

              if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)==GPIO_PIN_RESET)

              {

                   if(VOLUME<250)

                   {

                       VOLUME += 10;

                       WRITE_HMI(0X00A1,VOLUME,0x00,0,0,2);//音量+

                       delay_ms(50);

                       HMI_SEND_BUF[0] = VOLUME;

                       PLC_Data_Wr2HMI(0X1210,0,1);//写当前音量到触摸屏

                       delay_ms(50);

                   }

              }

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

         }

         if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==GPIO_PIN_RESET)//KEY6按下

         {

              delay_ms(150);//去抖

              if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)==GPIO_PIN_RESET)

              {

                   if(VOLUME >= 10)

                   {

                       VOLUME -= 10;

                       WRITE_HMI(0X00A1,VOLUME,0x00,0,0,2);//音量-

                       delay_ms(50);

                       HMI_SEND_BUF[0] = VOLUME;

                       PLC_Data_Wr2HMI(0X1210,0,1);//写当前音量到触摸屏

                       delay_ms(50);

                   }            

              }

              for(i=0;i<50;i++)

              {

                   usart1_rxBuf[i] = 0x00;

              }

              usart1_counter = 0;

         }

 

}

 

 

7). delay_ms ():延时函数,不精确慎用

void delay_ms(uint16_t nms)

{

     uint16_t i,j;

     for(i=0;i

     {

         for(j=0;j<10000;j++)

         {

              ;

         }

     }

}

8). USART1_IRQHandler ():数据接收中断

void USART1_IRQHandler(void)

{

  HAL_UART_IRQHandler(&huart1);

     if(usart1_rxBuf[0] == 0x5a&&usart1_rxBuf[1] == 0xa5&&usart1_rxBuf[2])

     {

         if(usart1_counter >= 3&&usart1_counter<(usart1_rxBuf[2]+3))

         {

              usart1_rxBuf[usart1_counter] = usart1_data;

              usart1_counter++;

         }

         if(usart1_counter >= 3&&usart1_counter>=(usart1_rxBuf[2]+3))

         {

              HMI_HANDLEFLAG = 1;//接收完成标志

         }

     }

     if(usart1_counter < 3)

     {   

         usart1_rxBuf[usart1_counter] = usart1_data;   

         usart1_counter++;

     }

     if(HMI_HANDLEFLAG)

     {

         usart1_counter = 0;

     }

     HAL_UART_Receive_IT(&huart1,&usart1_data,1);//开接收中断

}


打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分