这个贴子的代码,非常适合小白第一次使用迪文屏,用于单片机通信 主要写了stm32和迪文原生态8283指令数据接口框架,进行通信。 用了开机密码做个实验演示 屏幕用的是DMG80480C043_01WTC
视频效果如下: https://v.youku.com/v_show/id_XNjE4MjA4MDY0MA==.html
迪文,用的是串口2,不需要编写屏幕代码,稍微搞下GUI界面就行了 单片机用的是STM32F103VET6
先打开comm.c 这个文件,这个文件代码定义了,操作迪文屏幕内存代码
//主要是写屏幕内存 u8 DWIN_WriteLen(u16 addr,u8 *data,u8 len) { u8ret=1; EventBits_tEventValue; u16waitTick=20; xEventGroupClearBits(EventGroupHandler,EVENTBIT_DwinGetData); DWIN_RECSTA=0;
DWIN_SendByte(0x5a); DWIN_SendByte(0xa5); DWIN_SendByte(len*2+2+1); DWIN_SendByte(0x82); DWIN_SendByte(addr>>8); DWIN_SendByte(addr&0xff);
for(inti=0;i<len;i++) { DWIN_SendByte(data[i*2]); DWIN_SendByte(data[i*2+1]); } wait_ReadWriteBit|=0x01; if(waitTick) { ret=0; EventValue=xEventGroupWaitBits((EventGroupHandle_t )EventGroupHandler, (EventBits_t )EVENTBIT_DwinGetData, (BaseType_t )pdFALSE, (BaseType_t )pdFALSE, (TickType_t )waitTick); if(EventValue&EVENTBIT_DwinGetData) { if(DWIN_RecBuff[0]==0x5a && DWIN_RecBuff[1]==0xa5 && DWIN_RecBuff[3]==0x82 && DWIN_RecBuff[4]==0x4f && DWIN_RecBuff[5]==0x4b) { xEventGroupClearBits(EventGroupHandler,EVENTBIT_DwinGetData); ret=1; } DWIN_RECSTA=0; } } wait_ReadWriteBit&=~0x01;
returnret; }
//主要是写屏幕内存,单字 u8 DWIN_WriteOne(u16 addr,u16 data) { u8da[2];
da[0]=data>>8; da[1]=data&0xff;
returnDWIN_WriteLen(addr,da,1); }
//主要是读屏幕内存 u8 DWIN_ReadLen(u16 addr,u8 *data,u8 len) { u8ret=1; EventBits_tEventValue; u16waitTick=40; xEventGroupClearBits(EventGroupHandler,EVENTBIT_DwinGetData); DWIN_RECSTA=0;
DWIN_SendByte(0x5a); DWIN_SendByte(0xa5); DWIN_SendByte(4); DWIN_SendByte(0x83); DWIN_SendByte(addr>>8); DWIN_SendByte(addr&0xff); DWIN_SendByte(len);
wait_ReadWriteBit|=0x02; if(waitTick) { ret=0; EventValue=xEventGroupWaitBits((EventGroupHandle_t )EventGroupHandler, (EventBits_t )EVENTBIT_DwinGetData, (BaseType_t )pdFALSE, (BaseType_t )pdFALSE, (TickType_t )waitTick); if(EventValue&EVENTBIT_DwinGetData) { if(DWIN_RecBuff[0]==0x5a&& DWIN_RecBuff[1]==0xa5 && DWIN_RecBuff[3]==0x83 && DWIN_RecBuff[4]==(addr>>8)&& DWIN_RecBuff[5]==(addr&0xff)&& DWIN_RecBuff[6]==len) { memcpy(data,&DWIN_RecBuff[7],len*2); ret=1; xEventGroupClearBits(EventGroupHandler,EVENTBIT_DwinGetData); } DWIN_RECSTA=0; } } wait_ReadWriteBit&=~0x02;
returnret; }
//清除屏幕内存 void Clear_DWIN_Memory(u8 len,u16 addr) { DWIN_SendByte(0x5a); DWIN_SendByte(0xa5); DWIN_SendByte(len*2+2+1); DWIN_SendByte(0x82); DWIN_SendByte(addr>>8); DWIN_SendByte(addr&0xff);
for(inti=0;i<len;i++) { DWIN_SendByte(0); DWIN_SendByte(0); } }
//用于显示文本的内容 void DWIN_DispString(u16 addr,char *data) { u8sendstr[256]; u8len=0; memset(sendstr,0,sizeof(sendstr));
strcpy((char*)sendstr,data); len=strlen(data); if(len/2) { len/=2; len++; } else { len/=2; }
DWIN_WriteLen(addr,sendstr,len); } //用于切换界面 void DMT_ChagePage(u16 page) { u8Charr[]={0x5a,0x01,page>>8,page&0xff}; DWIN_WriteLen(0x84,Charr,2); }
//用于处理接收到的数据 void DWIN_GetDataHandle(void) { EventBits_tEventValue; if(wait_ReadWriteBit) return;
EventValue=xEventGroupWaitBits((EventGroupHandle_t )EventGroupHandler, (EventBits_t )EVENTBIT_DwinGetData, (BaseType_t )pdTRUE, (BaseType_t )pdFALSE, (TickType_t )0);
if(EventValue&EVENTBIT_DwinGetData) { if(DWIN_RecBuff[0]==0x5a&& DWIN_RecBuff[1]==0xa5 && DWIN_RecBuff[3]==0x83) { DWIN_RECHandle(DWIN_RecBuff[4]<<8|DWIN_RecBuff[5], &DWIN_RecBuff[7],DWIN_RecBuff[6]); } DWIN_RECSTA=0; } }
再看到实际的页面dwin_data_handle.c 这个文件,主要是处理接收的屏幕数据 typedef enum { addr_inputWinScreat=0x1039, }touch_cmd_addr;
#define WinScreat "12345678"
void ScreatCmp(u16 addr,u8 *data,u16 len) { charsctbuf[20];
data+=2; memset(sctbuf,0,sizeof(sctbuf)); for(inti=0;i<(len-1)*2;i++) { if(data==0xff&& data[i+1]==0xff) break; sctbuf=data; }
if(strlen(WinScreat)==strlen(sctbuf)&& !memcmp(WinScreat,sctbuf,strlen(sctbuf))) { DMT_ChagePage(1); } else { DWIN_DispString(0x1007,"密码错误\r\n密码:12345678"); } }
//len是字长度 void DWIN_RECHandle(u16 addr,u8 *data,u16len) { switch(addr) { caseaddr_inputWinScreat: ScreatCmp(addr,data,len); break;
} }
其它,关于代码如何去实现的,大家可以下载代码,参考下。
|