硬件准备
ADSP-EDU-BF533:BF533开发板
AD-HP530ICE:ADI DSP仿真器
软件准备
Visual DSP++软件
硬件链接
硬件设计原理图
功能介绍
代码实现了读取触摸屏坐标,并将触摸屏坐标换算为液晶屏的显示坐标,将像素点显示到触摸坐标的位置,实现了通过触摸屏在液晶屏上进行简单的线条绘画。
代码设置了防触摸屏坐标误触发机制,通过算法尽量减少触摸屏野点。使触发坐标更准确。
运行代码,通过用手指或者其他光滑的物体在液晶屏上划动,可以看到液晶屏上显示出划动的轨迹。
代码使用说明
触摸屏野点算法:
为确保触摸屏显示坐标准确,通常采用野点处理算法,代码中每次采取 9 个坐标点,然后对其取平均值和差值,排除偏离实际坐标较大的坐标值,将准确的坐标输出。
触摸屏坐标与液晶屏坐标换算:
X 坐标换算: tem_x = (tem_x-0xb0)*100/763;
Y 坐标换算: tem_y = (tem_y-0x150) *100/1288;
如感觉坐标定位不准确,可适当修改上面的换算参数值。
在指定坐标显示一个点:
因液晶屏最终显示的数据格式为 RGB565,所以显示点的数据为 16bit ,由两个 8bit 的数据分别送入。
void play_point(unsigned int x,unsigned int y,unsigned char pdispbuf)
{
pdispbuf[2x+0+y960] = 0xff ;
pdispbuf[2x+1+y*960] = 0xff;
}
代码调试步骤
- 编译并运行代码
- 用手指在液晶屏上划动
- 观察液晶屏显示
- 液晶屏上会显示出手指划动的轨迹。
程序源码
#include <cdefBF533.h>
extern DisplayBuffer[][];
extern unsigned char DisplayBuffer_565[272][1440] ;
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][1440] ;
section(“sdram0_bank1”) unsigned char TempBuffer_img[272][1440] ;
section(“sdram0_bank1”) unsigned char Inputdata[391734];
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+0+y960] = 0xff ;
pdispbuf[2x+1+y*960] = 0xff;
}
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);
}
#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] ;
unsigned char DisplayBuffer_temp[272][1440] ;
extern unsigned char DisplayBuffer_565[272][960] ;
unsigned short ptx[9];
unsigned short pty[9];
unsigned short pax[3];
unsigned short pay[3];
extern unsigned char Inputdata[];
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();
LCDBK_Disable();
SPIinit();
InitDMA();
InitPPI();
InitTimer();
PPI_TMR_DMA_Enable();
Init_Timers0(1999);//1~1999 控制背光亮度
Enable_Timers0();
LCD_Enable();
LCDBK_Enable();
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)
tem_x = (tem_x-0xb0)*100/763;
if(tem_y!=0xfff)
tem_y = (tem_y-0x150) *100/1288;
osd(tem_x,tem_y,DisplayBuffer_565);
}
}