极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15792|回复: 7

新人请教Arduino的端口问题

[复制链接]
发表于 2013-3-5 15:16:12 | 显示全部楼层 |阅读模式
自己用Arduino Mega2560 驱动2.8"的液晶,8线方式,成功了,然后再驱动触摸屏,当时用的是2,3,4,5,6.

#define T_CLK 6
#define T_CS 5
#define T_DIN 4
#define T_DOUT 3
#define T_IRQ 2

可以识别,到触摸的信号,但是我换成

#define T_CLK 39
#define T_CS 40
#define T_DIN 38
#define T_DOUT 36
#define T_IRQ 37

就死活都不行,请问大家是否端口的选择上有什么需要注意的地方吗?
回复

使用道具 举报

发表于 2013-3-5 19:37:24 | 显示全部楼层
CLK CS 冒失是API 协议的接口,SPI 的端口是固定的CS 是可以换的 换CS 接口来接多个SPI 设备建议你去看看SPI协议  也顺便看下I2C
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-3-5 20:47:14 | 显示全部楼层
我看了,2560对于的SPI 应该是20(SCK),21(MOSI),22(MISO),我的代码没有应该是没有用SPI,是端口模拟的呀.
  1. /**********************************************
  2. Pay an attention!

  3. This code is designed for Arduino board.

  4. **********************************************/
  5. /**********************************************
  6. Define zone
  7. **********************************************/
  8.                                                                                                                                                                                                                            
  9. /*
  10. #define T_CLK 6
  11. #define T_CS 5
  12. #define T_DIN 4
  13. #define T_DOUT 3
  14. #define T_IRQ 2
  15. */

  16. #define T_CLK 46
  17. #define T_CS 45
  18. #define T_DIN 44
  19. #define T_DOUT 43
  20. #define T_IRQ 42

  21. #define X_CONST 240
  22. #define Y_CONST 320

  23. #define PREC_TOUCH_CONST 10

  24. #define PixSizeX        13.78
  25. #define PixOffsX        411

  26. #define PixSizeY        11.01
  27. #define PixOffsY        378


  28. /**********************************************
  29. Val Zone
  30. **********************************************/
  31. int TP_X,TP_Y;


  32. void Touch_Init(void)
  33. {
  34.         pinMode(T_CLK,  OUTPUT);
  35.     pinMode(T_CS,   OUTPUT);
  36.     pinMode(T_DIN,  OUTPUT);
  37.     pinMode(T_DOUT, INPUT);
  38.     pinMode(T_IRQ,  INPUT);

  39.         digitalWrite(T_CS,  HIGH);
  40.         digitalWrite(T_CLK, HIGH);
  41.         digitalWrite(T_DIN, HIGH);
  42.         digitalWrite(T_CLK, HIGH);
  43. }

  44. void Touch_WriteData(unsigned char data)
  45. {
  46.         unsigned char temp;
  47.         unsigned char nop;
  48.         unsigned char count;

  49.         temp=data;
  50.         digitalWrite(T_CLK,LOW);

  51.         for(count=0; count<8; count++)
  52.         {
  53.                 if(temp & 0x80)
  54.                         digitalWrite(T_DIN, HIGH);
  55.                 else
  56.                         digitalWrite(T_DIN, LOW);
  57.                 temp = temp << 1;
  58.                 digitalWrite(T_CLK, LOW);               
  59.                 nop++;
  60.                 digitalWrite(T_CLK, HIGH);
  61.                 nop++;
  62.         }
  63. }

  64. unsigned int Touch_ReadData()
  65. {
  66.         unsigned char nop;
  67.         unsigned int data = 0;
  68.         unsigned char count;
  69.         for(count=0; count<12; count++)
  70.         {
  71.                 data <<= 1;
  72.                 digitalWrite(T_CLK, HIGH);               
  73.                 nop++;
  74.                 digitalWrite(T_CLK, LOW);
  75.                 nop++;
  76.                 if (digitalRead(T_DOUT))
  77.                         data++;
  78.         }
  79.         return(data);
  80. }
  81. void Touch_Read()
  82. {
  83.         unsigned long tx=0;
  84.         unsigned long ty=0;

  85.         digitalWrite(T_CS,LOW);                    

  86.         for (int i=0; i<PREC_TOUCH_CONST; i++)
  87.         {
  88.                 Touch_WriteData(0x90);        
  89.                 digitalWrite(T_CLK,HIGH);
  90.                 digitalWrite(T_CLK,LOW);
  91.                 ty+=Touch_ReadData();

  92.                 Touch_WriteData(0xD0);      
  93.                 digitalWrite(T_CLK,HIGH);
  94.                 digitalWrite(T_CLK,LOW);
  95.                 tx+=Touch_ReadData();
  96.         }

  97.         digitalWrite(T_CS,HIGH);

  98.         TP_X=tx/PREC_TOUCH_CONST;
  99.         TP_Y=ty/PREC_TOUCH_CONST;
  100. }

  101. char Touch_DataAvailable()
  102. {
  103.   char avail;
  104.   pinMode(T_IRQ,  INPUT);
  105.   avail = !digitalRead(T_IRQ);
  106.   pinMode(T_IRQ,  OUTPUT);
  107.   return avail;
  108. }

  109. int Touch_GetX()
  110. {
  111.         int value;
  112.         value = ((TP_X-PixOffsX)/PixSizeX);
  113.         if (value < 0)
  114.                 value = 0;
  115.         return value;
  116. }
  117. int Touch_GetY()
  118. {
  119.         int value;
  120.         value = ((TP_Y-PixOffsY)/PixSizeY);
  121.         if (value < 0)
  122.                 value = 0;
  123.         return value;
  124. }
  125. /**********************************************
  126. Arduino functions zone
  127. **********************************************/
  128. void setup()
  129. {
  130.   Serial.begin(9600);
  131.   Serial.println("Begin init...");
  132.   Touch_Init();
  133.   Serial.println("Init Finieshed");
  134. }

  135. void loop()
  136. {
  137.     unsigned int  i,j;
  138.            while(Touch_DataAvailable() == 1)
  139.         {
  140.                 Touch_Read();
  141.                 i = Touch_GetX();
  142.                 j = Touch_GetY();
  143.                 //SetXY(i,i,j,j);
  144.                 //Write_Data(0xFFFF);
  145.                 Serial.print("Touch:X=");
  146.                 Serial.print(i,10);
  147.                 Serial.print(",Y=");
  148.                 Serial.println(j,10);
  149.         }
  150. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-3-5 21:46:08 | 显示全部楼层
bigmango 发表于 2013-3-5 20:47
我看了,2560对于的SPI 应该是20(SCK),21(MOSI),22(MISO),我的代码没有应该是没有用SPI,是端口模拟的呀.
/* ...

以下是官方2560 的接口信息  你说的都不对的
Input and Output

Each of the 54 digital pins on the Mega can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions:

Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX). Used to receive (RX) and transmit (TX) TTL serial data. Pins 0 and 1 are also connected to the corresponding pins of the ATmega16U2 USB-to-TTL Serial chip.

External Interrupts: 2 (interrupt 0), 3 (interrupt 1), 18 (interrupt 5), 19 (interrupt 4), 20 (interrupt 3), and 21 (interrupt 2). These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.

PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.

SPI: 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS). These pins support SPI communication using the SPI library. The SPI pins are also broken out on the ICSP header, which is physically compatible with the Uno, Duemilanove and Diecimila.

LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.

TWI: 20 (SDA) and 21 (SCL). Support TWI communication using the Wire library. Note that these pins are not in the same location as the TWI pins on the Duemilanove or Diecimila.


The Mega2560 has 16 analog inputs, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and analogReference() function.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-3-5 22:25:37 | 显示全部楼层
我看的是这个文档
http://arduino.cc/en/uploads/Main/arduino-mega2560_R3-schematic.pdf
我的是R3的版本.

但是,不管怎么样,2,3,4,5,6在这个程序中都不是SPI啊,只是普通的端口吧?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-3-6 17:26:45 | 显示全部楼层
已经弄好了,原来是IC坏了.换了一个TSC2046就可以了.

看来端口模拟的SPI和端口没有什么关系了.
回复 支持 反对

使用道具 举报

发表于 2013-3-8 00:56:25 | 显示全部楼层
应该是的,但是模拟的慢,硬件的快,
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-3-8 17:54:49 | 显示全部楼层
tuvas 发表于 2013-3-8 00:56
应该是的,但是模拟的慢,硬件的快,

看了源代码,确实是一个bit一个bit写的,不过感觉还可以,2.8"和3.2"的几秒可以刷一屏.
当然,自己直接用8bit,16bit的方式应该会快很多.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-4 14:16 , Processed in 0.111486 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表