硬件准备
ADSP-EDU-BF533:BF533开发板
AD-HP530ICE:ADI DSP仿真器
软件准备
Visual DSP++软件
硬件链接
触摸屏的硬件设计原理图
功能介绍
代码实现了读取触摸屏坐标,并将触摸屏坐标换算为液晶屏的显示坐标,将光标数据叠加到背景数据上,然后显示出来,再次触动触摸屏,光标数据会重新刷新计算叠加,不会再背景上留下上次光标的拖影,实现数据实时叠加运算功能。
代码使用说明
代码主要实现了光标叠加功能,当有触摸屏触发,会计算出触发坐标,然后将背景图像送入显示内存,然后再将光标数据叠加到内存上,这样保证每次刷新光标,背景上都不会有残留的光标数据。
tem_x = (tem_x-0xb0)*100/800;
tem_y = (tem_y-0x150) *100/1288;
memcpy(DisplayBuffer_565,TempBuffer_img,261120);
Mouse(tem_x,tem_y,DisplayBuffer_565);
将 X 和 Y 的坐标换算成液晶屏显示坐标,通过 memcpy 函数将背景数据从存放的数组 TempBuffer_img 中拷贝到显示数组 DisplayBuffer_565 中,拷贝数据大小为 261120 字节。再将光标叠加到显示区域 DisplayBuffer_565 中。
代码调试步骤
- 编译并运行代码
- 用手指在液晶屏上划动
- 观察液晶屏显示
- 液晶屏上会看到有个光标会随着手指划动,跟随显示。
程序源码
#include <cdefBF533.h>
void SPIinit(void)
{
*pSPI_BAUD=10;
*pSPI_FLG |=FLS2;
*pSPI_CTL = 0x1001|CPHA| CPOL;
*pSPI_CTL = (*pSPI_CTL | SPE);
}
unsigned char spi_byte_rw(unsigned char value)
{
unsigned char incoming=0;
while(!(*pSPI_STAT & SPIF));
*pSPI_TDBR = value;
while(*pSPI_STAT & RXS)
incoming = *pSPI_RDBR;
return(incoming);
}
int read_y(void)
{
int y_dat = 0;
int temp = 0;
delay(0xff);
*pSPI_FLG &= ~FLG2;
delay(0xff);
spi_byte_rw(0x90);
delay(0xff);
spi_byte_rw(0x00);
y_dat = (spi_byte_rw(0x00)&0xff)<<1;
temp = (spi_byte_rw(0x00)&0xff)<<1;
y_dat = (y_dat<<8)|temp;
y_dat >>=4;
*pSPI_FLG |= FLG2;
delay(0xff);
return y_dat;
}
int read_x(void)
{
int x_dat = 0;
int temp = 0;
delay(0xff);
*pSPI_FLG &= ~FLG2;
delay(0xff);
spi_byte_rw(0xd4);
delay(0xff);
spi_byte_rw(0x00);
x_dat = (spi_byte_rw(0x00)&0xff)<<1;
temp = (spi_byte_rw(0x00)&0xff)<<1;
x_dat = (x_dat<<8)|temp;
x_dat>>=4;
*pSPI_FLG |= FLG2;
delay(0xff);
return x_dat;
}
#include <cdefBF533.h>
section(“sdram0_bank1”) unsigned char DisplayBuffer[272][1440];
section(“sdram0_bank1”) unsigned char DisplayBuffer_565[272][960];
section(“sdram0_bank1”) unsigned char Inputdata[391734];
section(“sdram0_bank2”) unsigned char TempBuffer_img[272][960]=
{
#include"1.dat"
};
static unsigned char mouse[]=
{
0xE0,0x07,0x80,0x01,0x80,0x01,0x00,0x00,0x00,0x00,0x03,0xC0,0x06,0x60,0x05,0xA0,
0x05,0xA0,0x06,0x60,0x03,0xC0,0x00,0x00,0x00,0x00,0x80,0x01,0x80,0x01,0xE0,0x07
};
void InitDMA(void)
{
int addr;
addr = &DisplayBuffer_565;
addr -= 1920;
*pDMA0_START_ADDR = addr;
*pDMA0_X_COUNT = 480;
*pDMA0_X_MODIFY = 2;
*pDMA0_Y_COUNT = 286;
*pDMA0_Y_MODIFY = 2;
*pDMA0_CONFIG = 0x1034;
}
void InitPPI(void)
{
*pPPI_CONTROL = 0x781e;
*pPPI_DELAY = 0;
*pPPI_COUNT = 479;
*pPPI_FRAME = 286;
}
void InitTimer(void)
{
*pTIMER1_PERIOD = 525;
*pTIMER1_WIDTH = 41;
*pTIMER1_CONFIG = 0x00a9;
*pTIMER2_PERIOD = 150150;
*pTIMER2_WIDTH = 5250;
*pTIMER2_CONFIG = 0x00a9;
}
void PPI_TMR_DMA_Enable(void)
{
*pDMA0_CONFIG |= 0x1;
asm(“ssync;”);
InitTimer();
*pPPI_CONTROL |= 0x1;
asm(“ssync;”);
*pTIMER_ENABLE|= 0x0006;
asm(“ssync;”);
}
void PPI_TMR_DMAR_Disable(void)
{
*pDMA0_CONFIG &= (~0x1);
*pPPI_CONTROL &= (~0x1);
}
void bgrtorgb24(void)
{
int i,j;
int a,b,c;
for(i=0;i<272;i++)
{
for(j=0;j<1440;j++)
{
TempBuffer_img[i][j] = Inputdata[i1440+j+54];
}
}
for(i=0;i<272;i++)
{
for(j=0;j<480;j++)
{
a = TempBuffer_img[i][j3];
b = TempBuffer_img[i][j3+1];
c = TempBuffer_img[i][j3+2];
TempBuffer_img[i][j3] = c;
TempBuffer_img[i][j3+1] = b;
TempBuffer_img[i][j*3+2] = a;
}
}
for(i=0;i<272;i++)
{
for(j=0;j<1440;j++)
{
DisplayBuffer[i][j] = (TempBuffer_img[271-i][j]);
}
}
}
void color_bar(void)
{
int i,j;
for(i=0;i<272;i++)
{
for(j=0;j<40;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0x00;
}
for(j=40;j<80;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0x00; DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0x00;//red
}
for(j=80;j<120;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x00;//green
}
for(j=120;j<160;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0xff;//blue
}
for(j=160;j<200;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x0;//red+green
}
for(j=200;j<240;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0xff;//red+blue
}
for(j=240;j<280;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0xff;//green+blue
}
for(j=280;j<320;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0xff;
}
for(j=320;j<360;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x00;//green
}
for(j=360;j<400;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0xff;//blue
}
for(j=400;j<440;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x0;//red+green
}
for(j=440;j<480;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0xff;//red+blue
}
}
}
void RGB888_RGB565(unsigned char *src, int src_len, unsigned char *dst)
{
int i = 0;
int j = 0;
if (src_len % 3 != 0)
{
return;
}
for (i = 0; i < src_len; i += 3)
{
dst[j+1] = src[i+2] &0xf8; //B
dst[j+1] |= ((src[i+1]>>5) & 0x07); //GH
dst[j] = ((src[i+1]<<3) & 0xe0); //GL
dst[j] |= ((src[i]>>3) &0x1f); //R
j += 2;
}
}
unsigned char palette[3]={0xff,0xff,0x00};
void play_point(unsigned int x,unsigned int y,unsigned char pdispbuf)
{
pdispbuf[2x+1+y960] = 0x00;
pdispbuf[2x+0+y*960] = 0x1f ;
}
void osd(unsigned int x,unsigned int y,unsigned char *pdispbuf)
{
int i,j,k;
unsigned char temp;
if(x > 479)
x = 479;
if(y > 271)
y = 271;
play_point(x,y,pdispbuf);
}
void Mouse(int x,int y,unsigned char pdispbuf)
{
int i,j,k;
for (i=0;i<16;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<8;k++)
{
if( ((mouse[i2+j]>>(7-k)) & 0x1)!=0 )
{
play_point(x+8*j+k,y+i,pdispbuf);
}
}
}
}
}
#include <cdefBF533.h>
void Set_PLL(int pmsel,int pssel)
{
int new_PLL_CTL;
*pPLL_DIV = pssel;
asm(“ssync;”);
new_PLL_CTL = (pmsel & 0x3f) << 9;
*pSIC_IWR |= 0xffffffff;
if (new_PLL_CTL != *pPLL_CTL)
{
*pPLL_CTL = new_PLL_CTL;
asm(“ssync;”);
asm(“idle;”);
}
}
void Init_SDRAM(void)
{
*pEBIU_SDRRC = 0x00000817;
*pEBIU_SDBCTL = 0x00000013;
*pEBIU_SDGCTL = 0x0091998d;
ssync();
}
void Init_EBIU(void)
{
*pEBIU_AMBCTL0 = 0x7bb07bb0;
*pEBIU_AMBCTL1 = 0x7bb07bb0;
*pEBIU_AMGCTL = 0x000f;
}
void Init_Timers0(int dat)
{
*pTIMER0_CONFIG = 0x0019;
*pTIMER0_WIDTH = dat;
*pTIMER0_PERIOD = 2000;
}
void Enable_Timers0(void)
{
*pTIMER_ENABLE|= 0x0001;
asm(“ssync;”);
}
void Disable_Timers0(void)
{
*pTIMER_DISABLE |= 0x0001;
}
void delay(unsigned int tem)
{
int i;
for(i=0;i<tem;i++)
asm(“nop;”);
}
#include <cdefBF533.h>
extern unsigned char DisplayBuffer[272][1440] ;
extern unsigned char DisplayBuffer_565[272][960] ;
extern unsigned char TempBuffer_img[272][960] ;
extern unsigned char Inputdata[];
unsigned short ptx[9];
unsigned short pty[9];
unsigned short pax[3];
unsigned short pay[3];
void main(void)
{
int i;
unsigned int tem_x = 0,tem_y=0;
signed short dlXDiff0,dlXDiff1,dlXDiff2;
signed short dlYDiff0,dlYDiff1,dlYDiff2;
Set_PLL(16,4);
Init_EBIU();
Init_SDRAM();
Touch_Enable();
LCDBK_Disable();
SPIinit();
InitDMA();
InitPPI();
InitTimer();
PPI_TMR_DMA_Enable();
Init_Timers0(1999);//1~1999 控制背光亮度
Enable_Timers0();
LCD_Enable();
LCDBK_Enable();
memcpy(DisplayBuffer_565,TempBuffer_img,261120);
while(1)
{
for(i=0;i<9;i++)
{
ptx[i] = read_x();
pty[i] = read_y();
}
pax[0]=(ptx[0]+ptx[1]+ptx[2])/3;
pax[1]=(ptx[3]+ptx[4]+ptx[5])/3;
pax[2]=(ptx[6]+ptx[7]+ptx[8])/3;
dlXDiff0 = pax[ 0 ] - pax[ 1 ];
dlXDiff1 = pax[ 1 ] - pax[ 2 ];
dlXDiff2 = pax[ 2 ] - pax[ 0 ];
dlXDiff0 = dlXDiff0 > 0 ? dlXDiff0 : -dlXDiff0;
dlXDiff1 = dlXDiff1 > 0 ? dlXDiff1 : -dlXDiff1;
dlXDiff2 = dlXDiff2 > 0 ? dlXDiff2 : -dlXDiff2;
if ( dlXDiff0 < dlXDiff1 )
{
if ( dlXDiff2 < dlXDiff0 )
{
tem_x = ( ( pax[ 0 ] + pax[ 2 ] ) >> 1 ) ;
}
else
{
tem_x = ( ( pax[ 0 ] + pax[ 1 ] ) >> 1 );
}
}
else if ( dlXDiff2 < dlXDiff1 )
{
tem_x = ( ( pax[ 0 ] + pax[ 2 ] ) >> 1 ) ;
}
else
{
tem_x= ( ( pax[ 1 ] + pax[ 2 ] ) >> 1 ) ;
}
pay[0]=(pty[0]+pty[1]+pty[2])/3;
pay[1]=(pty[3]+pty[4]+pty[5])/3;
pay[2]=(pty[6]+pty[7]+pty[8])/3;
dlYDiff0 = pay[ 0 ] - pay[ 1 ];
dlYDiff1 = pay[ 1 ] - pay[ 2 ];
dlYDiff2 = pay[ 2 ] - pay[ 0 ];
dlYDiff0 = dlYDiff0 > 0 ? dlYDiff0 : -dlYDiff0;
dlYDiff1 = dlYDiff1 > 0 ? dlYDiff1 : -dlYDiff1;
dlYDiff2 = dlYDiff2 > 0 ? dlYDiff2 : -dlYDiff2;
if ( dlYDiff0 < dlYDiff1 )
{
if ( dlYDiff2 < dlYDiff0 )
{
tem_y = ( ( pay[ 0 ] + pay[ 2 ] ) >> 1 ) ;
}
else
{
tem_y = ( ( pay[ 0 ] + pay[ 1 ] ) >> 1 );
}
}
else if ( dlYDiff2 < dlYDiff1 )
{
tem_y = ( ( pay[ 0 ] + pay[ 2 ] ) >> 1 ) ;
}
else
{
tem_y= ( ( pay[ 1 ] + pay[ 2 ] ) >> 1 ) ;
}
if(tem_x !=0 )
{
if( tem_y != 0xfff)
{
tem_x = (tem_x-0xb0)*100/800;
tem_y = (tem_y-0x150) *100/1288;
memcpy(DisplayBuffer_565,TempBuffer_img,261120);
Mouse(tem_x,tem_y,DisplayBuffer_565);
}
}
}
}