touch.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "stm32f10x.h"
  2. #include "rtthread.h"
  3. #include "board.h"
  4. #if (LCD_VERSION == 2)
  5. /*
  6. MISO PA6
  7. MOSI PA7
  8. CLK PA5
  9. CS PC4
  10. */
  11. #define CS_0() GPIO_ResetBits(GPIOC,GPIO_Pin_4)
  12. #define CS_1() GPIO_SetBits(GPIOC,GPIO_Pin_4)
  13. /*
  14. 7 6 - 4 3 2 1-0
  15. s A2-A0 MODE SER/DFR PD1-PD0
  16. */
  17. #define TOUCH_MSR_Y 0x90 //读X轴坐标指令 addr:1
  18. #define TOUCH_MSR_X 0xD0 //读Y轴坐标指令 addr:3
  19. extern unsigned char SPI_WriteByte(unsigned char data);
  20. //SPI写数据
  21. static void WriteDataTo7843(unsigned char num)
  22. {
  23. SPI_WriteByte(num);
  24. }
  25. //触摸处理
  26. void Go_Touch(void)
  27. {
  28. unsigned int X;
  29. unsigned int Y;
  30. CS_0();
  31. WriteDataTo7843(TOUCH_MSR_X | 1); /* 发送读X坐标命令并关闭中断 */
  32. X = SPI_WriteByte(0x00)<<4; /* 读取第一字节MSB */
  33. X |= ((SPI_WriteByte(TOUCH_MSR_Y | 1)>>4)&0x0F );/* 读取第二字节 同时发送读Y轴坐标命令行*/
  34. Y = SPI_WriteByte(0x00)<<4; /* 读取第一字节MSB */
  35. Y |= ((SPI_WriteByte(1<<7)>>4)&0x0F ); /* 读取第二字节并重新打开中断 */
  36. CS_1();
  37. rt_kprintf("\r\nX: %04d Y: %04d",X,Y);
  38. }
  39. static void NVIC_Configuration(void)
  40. {
  41. NVIC_InitTypeDef NVIC_InitStructure;
  42. /* Enable the EXTI0 Interrupt */
  43. NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  44. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  45. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  46. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  47. NVIC_Init(&NVIC_InitStructure);
  48. }
  49. static void exti_int_config(void)
  50. {
  51. EXTI_InitTypeDef EXTI_InitStructure;
  52. /* PB1 touch INT */
  53. {
  54. GPIO_InitTypeDef GPIO_InitStructure;
  55. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  56. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  57. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  58. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  59. GPIO_Init(GPIOB,&GPIO_InitStructure);
  60. }
  61. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
  62. /* Configure EXTI */
  63. EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  64. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  65. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//Falling下降沿 Rising上升
  66. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  67. EXTI_Init(&EXTI_InitStructure);
  68. EXTI_ClearITPendingBit(EXTI_Line1);
  69. }
  70. #include <finsh.h>
  71. void touch_test(void)
  72. {
  73. SPI_InitTypeDef SPI_InitStructure;
  74. rt_kprintf("\r\ntouch testing....\r\n");
  75. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  76. /* Enable SPI_MASTER */
  77. SPI_Cmd(SPI1, DISABLE);
  78. /*------------------------ SPI1 configuration ------------------------*/
  79. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
  80. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  81. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  82. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  83. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  84. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  85. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;/* 72M/64=1.125M */
  86. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  87. SPI_InitStructure.SPI_CRCPolynomial = 7;
  88. SPI_I2S_DeInit(SPI1);
  89. SPI_Init(SPI1, &SPI_InitStructure);
  90. /* Enable SPI_MASTER */
  91. SPI_Cmd(SPI1, ENABLE);
  92. NVIC_Configuration();
  93. exti_int_config();
  94. /* PC4 touch CS */
  95. {
  96. GPIO_InitTypeDef GPIO_InitStructure;
  97. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  98. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  99. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  100. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  101. GPIO_Init(GPIOC,&GPIO_InitStructure);
  102. CS_1();
  103. }
  104. CS_0();
  105. WriteDataTo7843( 1<<7 ); /* 打开中断 */
  106. CS_1();
  107. }
  108. FINSH_FUNCTION_EXPORT(touch_test, touch_test)
  109. void EXTI1_IRQHandler(void)
  110. {
  111. EXTI_ClearITPendingBit(EXTI_Line1);
  112. Go_Touch();
  113. }
  114. #endif