s3c2440裸机触摸屏

s3c2440裸机触摸屏

1.0、触摸屏与LCD

       触摸屏与lcd是两个独立的屏,但是他们之间有对应关系,简单的说就是一个触摸屏的坐标点对应lcd的一个像素点,这样当我们按下触摸屏后,系统通过计算得到该地方的坐标,然后通过对应关系找到相应位置的lcd显示的内容。因为触摸屏和lcd是独立的,总是因为一些物理的原因会有一些位置上的改变,所以这种对应关系不是不变的,我们可以通过开机的时候的校验来确定这种关系,比如

LCD上每个点PD的坐标为[XD,YD],触摸屏上每个点PT的坐标为[XT,YT]。要实现触摸屏上的坐标转换为LCD上的坐标,需要下列公式进行转换:

XD=A×XT+B×YT+C

YD=D×XT+E×YT+F

这样我们只有确定了ABCDEF六个参数我们就可以确定这种对应关系了,其他的点就都可以对应了。这就是触摸屏校验的原理。

2.0、电阻触摸屏的原理

       通过X方向和Y方向的分开的电阻线,当你按下触摸屏的时候XY就有一个触点,然后通过计算在XY方向的分压值,在把电压值经过AD转换后即可得到xy的坐标。

3.0、触摸屏的流程

      触摸屏的流程我觉得是整个触摸屏比较重要的地方,其他的设置好寄存器之后读数据就可以了。流程中比较重要的是两个中断,触摸屏中断和ADC中断,其中触摸屏中断包括触摸屏按下中断和触摸屏松开中断。

初始化中断和寄存器——>等待触摸屏中断模式——>TC 中断(触摸屏按下中断)——>(进入xy自动转换模式)——>(ADC转换完成后)ADC中断——>(松开触摸屏)——>TC中断(松开触摸屏)——>等待触摸屏中断模式(循环)

代码比较简单,不过前面加入了串口的程序用于调试和输出信息copy

  1. /************************************************* 
  2. file name   Touchp 
  3. function    Touch Screen test  
  4. 硬件平台    mini2440开发板 
  5.         索尼X-35触摸屏 3.5寸 240 * 320 
  6. *************************************************/  
  7. #define GLOBAL_CLK      1  
  8.   
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11. #include "def.h"  
  12. #include "option.h"  
  13. #include "2440addr.h"  
  14. #include "2440lib.h"  
  15. #include "mmu.h"  
  16.   
  17. #include <stdarg.h>  
  18. #include <string.h>  
  19. #include <stdlib.h>  
  20. #include <stdio.h>  
  21. #include <ctype.h>  
  22.   
  23. #define baudrate 115200   
  24.   
  25. #define wait_down_int() (rADCTSC = 0xd3)  
  26. #define wait_up_int()   (rADCTSC = 0x1d3)  
  27. #define mode_auto_xy()  (rADCTSC = 0x0c)  
  28. #define Read_Adcdata0() (int)(rADCDAT0 & 0x3ff)  
  29. #define Read_Adcdata1() (int)(rADCDAT1 & 0x3ff)  
  30. #define PRSCVL(n)       ((n)<<6)  
  31.   
  32. static void delay(int times)  
  33. {  
  34.     int i = 1000 ;  
  35.     while(times--)  
  36.     {  
  37.         for(; i>0; --i)  
  38.             ;  
  39.     }  
  40. }  
  41.   
  42. /*********************************** 
  43. UART_int初始化led IO端口GPBCON5-8 
  44. 初始化GPBHCON为串口通信 
  45. 配置串口通信寄存器 
  46. 配置中断寄存器 
  47. ************************************/  
  48. void UART_int_init(void)  
  49. {  
  50.     /********configuration LED IO port**********/  
  51.     rGPBCON &= ~(0xff<<10) ;  
  52.     rGPBCON |= 0x55<<10 ;  
  53.       
  54.     /*******configuration GPHCON to UART*******/  
  55.     rGPHCON &= ~(0xf<<4) ;  
  56.     rGPHCON |=  0xa<<4 ;  
  57.       
  58.     /****configuration UART0 communication register******/  
  59.     rULCON0 = 0x03 ;                        //8-bits,1 stop bit, no parity  
  60.     rUCON0  = 0x05 ;  
  61.     rUBRDIV0= (int)(PCLK/baudrate/16) -1 ;  //configuration UART baudrate  
  62.       
  63.     /*****clean interrupt bit clea RX_INT******/  
  64.     rSUBSRCPND |= 0x1 ;  
  65.     rSRCPND |= 1<<28 ;  
  66.     rINTPND |= 1<<28 ;  
  67.   
  68.     /******open UART interrupt*********/  
  69.     rINTSUBMSK &= ~(0x1) ;  
  70.     rINTMSK &= ~(0x1<<28) ;     
  71. }  
  72.   
  73. //UART send byte  
  74. void UART_send_byte(char Tx_data)  
  75. {  
  76.     while(!(rUTRSTAT0&0x2)) ;//wait Tx empty  
  77.     if(Tx_data == '\n')     //Tx '\n'  
  78.     {  
  79.         rUTXH0 = 0x0d ;  
  80.         while(!(rUTRSTAT0&0x2)) ;  
  81.         rUTXH0 = 0x0a ;  
  82.     }  
  83.     else  
  84.     {  
  85.         rUTXH0 = Tx_data ;  
  86.     }  
  87. }  
  88. //UART send string  
  89. void UART_send_string(const char *str)   
  90. {  
  91.     while(*str)  
  92.     {  
  93.         UART_send_byte(*str) ;  
  94.         str++ ;  
  95.     }  
  96. }  
  97. //UART receive byte  
  98. void UART_receive_byte(void)   
  99. {  
  100.     char temp ;  
  101.       
  102.     while(!(rUTRSTAT0&0x1)) ;   //wait RX ready  
  103.       
  104.     temp = rURXH0 ;  
  105.     UART_send_byte(temp) ;  
  106.       
  107. }  
  108.   
  109. /******************************************* 
  110. 中断处理函数 
  111. 置1清除中断,注意顺序,先子中断后父中断 
  112. 点亮led灯 
  113. ********************************************/  
  114. void __irq UART0_interrupt(void)  
  115. {  
  116.     /******clean interrupt bit*************/  
  117.     rSUBSRCPND |= 0x1 ;  
  118.     rSRCPND |= 1<<28 ;  
  119.     rINTPND |= 1<<28 ;  
  120.       
  121.     //rGPBDAT &= ~(0xf<<5) ;  //lighten led  
  122.     UART_receive_byte();   
  123.     /* 
  124.     if(temp=='s'||temp=='S') 
  125.     { 
  126.         ***********Close interrupt************* 
  127.         rINTSUBMSK  |= (BIT_SUB_TC) ; 
  128.         rINTSUBMSK  |= (BIT_SUB_ADC) ; 
  129.         rINTMSK     |= (BIT_ADC) ; 
  130.         Uart_Printf("Touch Screen Test is Finished!!!\n") ; 
  131.     }    
  132.     */  
  133. }  
  134.   
  135. /**************【Touch Screen】****************/  
  136. static void Ts_Init(void)  
  137. {  
  138.     //使能预分频功能 设置A/D时钟 = PCLK/(49+1)  
  139.     rADCCON     = (1<<14)|PRSCVL(49);  
  140.     rADCDLY     = 50000 ;  
  141.     rADCUPDN    = 0x3 ; //按下 松开 都产生TC中断  
  142.           
  143.     /******clean interrupt bit*******/   
  144.     rSUBSRCPND  |= BIT_SUB_ADC ;   
  145.     rSUBSRCPND  |= BIT_SUB_TC ;   
  146.     rSRCPND     |= BIT_ADC ;  
  147.     rINTPND     |= BIT_ADC ;  
  148.   
  149.     /***********Open ADC TC interrupt*********/  
  150.     rINTSUBMSK  &= ~(BIT_SUB_TC) ;  
  151.     rINTSUBMSK  &= ~(BIT_SUB_ADC) ;  
  152.     rINTMSK     &= ~(BIT_ADC) ;  
  153.   
  154. }  
  155.   
  156. static void interrupt_Tc(void)  
  157. {  
  158.     if(rADCDAT0 & 0x8000)  
  159.     {  
  160.         Uart_Printf("Stylus Up \n\r") ;  
  161.         wait_down_int() ;   //wait interrupt mode  
  162.     }  
  163.     else  
  164.     {  
  165.         Uart_Printf("Stylus Down: ") ;  
  166.         mode_auto_xy() ;    //进入自动转换xy模式  
  167.           
  168.         rADCCON |= 1 ;  //start ADC   
  169.     }  
  170.     /******clean interrupt bit*************/   
  171.     rSUBSRCPND  |= BIT_SUB_TC ;   
  172.     rSRCPND     |= BIT_ADC ;  
  173.     rINTPND     |= BIT_ADC ;  
  174. }  
  175.   
  176. static void interrupt_Adc(void)  
  177. {  
  178.     Uart_Printf("xdata = %4d,  ydata = %4d\r\n", Read_Adcdata0(), Read_Adcdata1()) ;      
  179.       
  180.     wait_up_int() ; //wait 松开触摸屏 中断模式  
  181.       
  182.     /******clean interrupt bit*************/   
  183.     rSUBSRCPND  |= BIT_SUB_ADC ;   
  184.     rSRCPND     |= BIT_ADC ;  
  185.     rINTPND     |= BIT_ADC ;      
  186. }  
  187.   
  188. void __irq AdcTsinttrupt(void)  
  189. {  
  190.     rGPBDAT &= ~(0xf<<5) ;    //lighten led  
  191.       
  192.     if(rSUBSRCPND & BIT_SUB_TC)  
  193.     {     
  194.         UART_send_string("Tc interrupt\n") ;  
  195.         interrupt_Tc() ;      
  196.     }  
  197.   
  198.     if(rSUBSRCPND & BIT_SUB_ADC)  
  199.     {         
  200.         UART_send_string("Adc interrupt\n") ;  
  201.         interrupt_Adc() ;  
  202.     }     
  203. }  
  204.   
  205. static void Ts_test(void)  
  206. {  
  207.     Ts_Init() ;  
  208.     pISR_ADC    = (U32)AdcTsinttrupt ;  
  209.     wait_down_int() ;   //wait interrupt mode  
  210.     Uart_Printf("Touch Screen test,Please send 's'/'S' stop test\n") ;    
  211. }  
  212. /******************************************************************* 
  213. 串口UART是挂在PCLK总线上的,需要设置PCLK时钟,即需要设置2440时钟。 
  214. 首先需要设置PLLCON寄存器设置CPU时钟(FCLK),然后设置FCLK HCLK PCLK的 
  215. 分频比,确定好PCLK时钟。 
  216. ********************************************************************/  
  217. int Main(void)  
  218. {  
  219.     MMU_Init();  
  220.     ChangeMPllValue(127,2,1);       //405MHZ  
  221.     ChangeClockDivider(13,12);        //1:3:6     
  222.       
  223.     UART_int_init() ;  
  224.     pISR_UART0 = (U32)UART0_interrupt ;  
  225.     //delay(5000) ;  
  226.     Uart_SendString("\nUart initial finish!!\n") ;  
  227.     Ts_test();   
  228.     Uart_Printf("Touch Screen initial finish\n") ;  
  229.     while(1)   
  230.     {  
  231.           
  232.     }  
  233.     return 0 ;  
  234. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值