AT91RM9200读取ADS7843触摸屏源代码(gliet
//--------------------------------------------------------------
typedef struct __xTouch_value
{
uint16 x;
uint16 y;
}_xTouch_value;
typedef struct __xTouch
{
boolean IRQ_Mod;
boolean down;
boolean up;
_xTouch_value
value;
}_xTouch;
//--------------------------------------------------------------
_xTouch xxTouch = { true,false,false,{0xffff,0xffff} };
_xTouch *xTouch = &xxTouch;
//--------------------------------------------------------------
//触摸屏
#define Touch_DCLK (uint32)(0x01<<11)/*PC11*/
#define Touch_CS (uint32)(0x01<<12)/*PC12*/
#define Touch_DIN (uint32)(0x01<<13)/*PC13*/
#define Touch_busy (uint32)(0x01<<14)/*PC14*/
#define Touch_DOUT (uint32)(0x01<<15)/*PC15*/
#define Touch_PIRQ (uint32)(0x01<<26)/*PA26*/
#define set_dclk() AT91F_PIO_SetOutput(AT91C_BASE_PIOC, Touch_DCLK)
#define clr_dclk() AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, Touch_DCLK)
#define set_din() AT91F_PIO_SetOutput(AT91C_BASE_PIOC, Touch_DIN)
#define clr_din() AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, Touch_DIN)
#define set_chcs() AT91F_PIO_SetOutput(AT91C_BASE_PIOC, Touch_CS)
#define clr_chcs() AT91F_PIO_ClearOutput(AT91C_BASE_PIOC, Touch_CS)
#define Set_InputMod() AT91C_BASE_PIOA->PIO_PER = Touch_PIRQ
#define Set_IRQMod() AT91C_BASE_PIOA->PIO_PDR = Touch_PIRQ
#define Get_TouIRQ() (AT91C_BASE_PIOA->PIO_PDSR & Touch_PIRQ)
//--------------------------------------------------------------
void Touch_write(uint8 data)//触屏传输速率最大125Kbits
{uint8 i;
for(i = 0;i < 8;i++)
{
clr_dclk(); //低电平改变数据数据
if(data & 0x80)set_din();
else clr_din();
data <<= 1;
set_dclk();
}
clr_dclk();
}
//--------------------------------------------------------------
uint16 Touch_read(void)//触屏传输速率最大125Kbits
{uint8 i;
uint16 result = 0;
for(i = 0;i < 12;i++)
{
result <<= 1;
set_dclk(); //仅有下降沿读取数据
clr_dclk();
if(AT91C_BASE_PIOC->PIO_PDSR & Touch_DOUT)result++;
}
return result;
}
//---------------------------------------------------------------
uint16 Get_X(void)
{
Touch_write(0x90);
return Touch_read();
}
uint16 Get_Y(void)
{
Touch_write(0xD0);
return Touch_read();
}
//---------------------------------------------------------------
void Touch_IRQ_proc(void)
{
AT91C_BASE_AIC->AIC_ICCR = ( 1 << AT91C_ID_IRQ1 );
AT91F_AIC_DisableIt(AT91C_BASE_AIC, AT91C_ID_IRQ1);
Set_InputMod();
xTouch->down = true;
xTouch->up = false;
xTouch->IRQ_Mod = false;
xxxdprintf("\r\nTouch按下\r\n");
}
//---------------------------------------------------------------
#define XTOUCH_DEBGU 1
#if XTOUCH_DEBGU
static char data[50];
#endif
void xTouchproc(void)
{
if(xTouch->down == true)
{
if( Get_TouIRQ() )
{//触屏抬起
{uint32 count;for(count = 0;count < 0xfff;count++);}
if( Get_TouIRQ() )
{
xTouch->down = false;
xTouch->up = true;
#if XTOUCH_DEBGU
xxxdprintf("\r\nTouch抬起\r\n");
#endif
}
}else
{uint16 x,y;OS_CPU_SR cpu_sr;
clr_chcs();
x = Get_X();
y = Get_Y();
OS_ENTER_CRITICAL();
xTouch->value.x = x;
xTouch->value.y = y;
OS_EXIT_CRITICAL();
set_chcs();
#if XTOUCH_DEBGU
sprintf(data,"x=%d,y=%d\r\n",
xTouch->value.x,xTouch->value.y);
xxxdprintf(data);
#endif
}
}
if(xTouch->up == true)
{
if(xTouch->IRQ_Mod == false)
{
xTouch->IRQ_Mod = true;
Set_IRQMod();
AT91C_BASE_AIC->AIC_ICCR =
( 1 << AT91C_ID_IRQ1 );
AT91F_AIC_EnableIt(AT91C_BASE_AIC,
AT91C_ID_IRQ1);
}
}
}
阅读(1037) | 评论(0) | 转发(0) |