用STM32評估板與迪文屏做交互,也沒那么難! 沒幾行C代碼就可以實現(xiàn)一些簡單的功能。跟大家分享下,詳細(xì)視頻和說明請參照http://bbs.elecfans.com/forum.php?mod=viewthread&tid=1626837&extra=
一、評估板使用說明
1、功能
注:可實現(xiàn)的功能:
a.通過屏上的圖標(biāo)來控制5路LED燈的狀態(tài)(LED燈的開和關(guān)來表示狀態(tài)),上電5路LED熄滅狀態(tài)。
b. 通過評估板上的物理按鍵1,來控制亮屏和息屏,上電狀態(tài)為亮屏狀態(tài)。
c. 通過評估板上的物理按鍵2&3,來調(diào)節(jié)屏幕上的溫度指數(shù),上電初始值????? ????25度,以1為步長遞增遞減,最小0,最大255。
d. 通過評估板上的物理按鍵4,來控制音樂的播放和停止,上電音樂不播放。
e. 通過評估板上的物理按鍵5&6來調(diào)節(jié)音量的大小,上電音量值50,以10為步長遞增遞減,最小0,最大250。
?
2、需使用的工具
a.硬件:正點原子Mini STM32開發(fā)板(MCU為STM32F103RCT6,串口TTL電平)+面包板手工焊接5路LED和6路按鍵擴展。
b. 軟件:STM32CubeMX+KEIL MDK5。
c.原理圖:詳見“硬件原理圖”文件夾。
d.程序:工程文件在DGUS II工程文件夾中,下載DWIN_SET導(dǎo)入屏中即可。 STM32程序在“MCU_STM32F103RCT6程序”文件夾。
二、硬件及軟件介紹
1、硬件原理圖:詳見“硬件原理圖”文件夾
(1)硬件接口定義:
(2)硬件原理圖:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
圖1:STM32F103原理圖
?
圖2:LED及KEY原理圖
2、軟件程序
界面工程文件在“DGUS_II工程”文件夾中。STM32程序在“MCU_STM32F103RCT6程序”文件夾中。
?
(1)界面工程
LED1圖標(biāo):0X1000
LED1按鍵:0X1200
LED2圖標(biāo):0X1001
LED2按鍵:0X1201
LED3圖標(biāo):0X1002
LED3按鍵:0X1202
LED4圖標(biāo):0X1003
LED4按鍵:0X1203
LED5圖標(biāo):0X1004
LED5按鍵:0X1204
音量顯示框地址:0X1210
溫度顯示框地址:0X1110
?
(2)MCU程序
1).main():硬件初始化,觸摸屏初始化,按鍵掃描,接收觸摸屏數(shù)據(jù)處理
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)//監(jiān)測是否播放完音樂
???????? {
????????????? 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)//接收觸摸屏數(shù)據(jù)處理
???????? {
????????????? 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 ():數(shù)據(jù)寫入觸摸屏變量寄存器
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);//串口發(fā)送
}
3). WRITE_HMI ():數(shù)據(jù)寫入觸摸屏寄存器
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 ():讀觸摸屏寄存器數(shù)據(jù)
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 ():開關(guān)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);//關(guān)LED1
????????????? }
????????????? if(hmi_key_value == 0x02)
????????????? {???
?????????????????? HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_SET); //關(guān)LED2
????????????? }
????????????? if(hmi_key_value == 0x03)
????????????? {???
?????????????????? HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET); //關(guān)LED3
????????????? }
????????????? if(hmi_key_value == 0x04)
????????????? {???
?????????????????? HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); //關(guān)LED4
????????????? }
????????????? if(hmi_key_value == 0x05)
????????????? {???
?????????????????? HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); //關(guān)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);//發(fā)送LED狀態(tài)到觸摸屏,改變LED顯示圖標(biāo);
???????? delay_ms(50);
???? }
}
6). CLOSE_OPEN_IO ():開關(guān)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);//寫當(dāng)前音量到觸摸屏
?????????????????????? 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);//寫當(dāng)前音量到觸摸屏
?????????????????????? delay_ms(50);
?????????????????? }????????????
????????????? }
????????????? for(i=0;i<50;i++)
????????????? {
?????????????????? usart1_rxBuf[i] = 0x00;
????????????? }
????????????? usart1_counter = 0;
???????? }
?
}
?
?
7). delay_ms ():延時函數(shù),不精確慎用
void delay_ms(uint16_t nms)
{
???? uint16_t i,j;
???? for(i=0;i
???? {
???????? for(j=0;j<10000;j++)
???????? {
????????????? ;
???????? }
???? }
}
8). USART1_IRQHandler ():數(shù)據(jù)接收中斷
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;//接收完成標(biāo)志
???????? }
???? }
???? 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);//開接收中斷
}
評論
查看更多