极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15422|回复: 0

红外控制+UNO与mega2560通过SPI通信+2.4寸TFT液晶显示屏

[复制链接]
发表于 2016-1-6 23:30:55 | 显示全部楼层 |阅读模式
       虽然进入这个论坛时间不长,却已经跟着各位大牛前辈们学了不少东西,不懂的问题,也有细心的网友给我解答,在此先谢谢各位了
       这次的“红外控制+UNO与mega2560通过SPI通信+2.4寸显示屏”并没有什么新东东,只是把这三样柔和到了一起,全当自己的一个学习笔记吧,当然,如果新手小白能够从中学到什么或者大牛老咖有什么可以优化的给我提出来,想必是极好的!

帖子内容多,为降低烦躁感和增加可读性,so设置下面四个主题任务目标、代码展示、截图显示、求助大神
       1、任务目标:遥控器按下上、下、左、右四个键,通过主设备(mega)上的红外接收装置,通过SPI与从设备(UNO)进行通信,最终在TFT显示屏上对应显示up、down、left、right(如 按下左键,显示left,再按一下上键,显示up)。
      2、 代码展示:
注:
SPI接线
      A--------------------B
(10) SS---------->(10) SS
(11) MOSI------->(11) MOSI
(12) MISO<-------(12) MISO
(13) SCLK------->(13) SCLK
TFT显示屏接线方式
请按照自己设备接即可
主设备(mega)代码:
/*1、2016.1.6,Written by TXJ * 2、SPI通信部分引用本论坛美女“shenhaiyu”的帖子,但是经测试从机代码不
*       完整,建议从原文处复制,当然,没有她的帖子,我就没办法进行下去
* 3、shenhaiyu的帖子地址:http://www.geek-workshop.com/thread-4781-1-1.html
* 4、SPI原文出处:http://www.gammon.com.au/forum/?id=10892
*/

#include <SPI.h>
#include <IRremote.h>
#include <IRremoteInt.h>


const int irReceiverPin = 11;  
IRrecv irrecv(irReceiverPin);
decode_results results;


void setup (void)
{
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(13,OUTPUT);            
  digitalWrite(SS, HIGH);         // 开始串口通讯
  SPI.begin ();                  // PI通讯开始
  SPI.setClockDivider(SPI_CLOCK_DIV8); //设置SPI串行通信的时钟。
}

void loop (void)
{
  int c;
    if (irrecv.decode(&results)) {
        Serial.print("irCode: ");
        Serial.println(results.value, DEC);
        Serial.print("bits: ");
        Serial.println(results.bits);  
        if(results.value==275196119){      //确认接收到“上”编码,此码是预先读出来的按键编码。
  digitalWrite(13,1);                     //点亮LED
   Serial.println("turn on LED");        //串口显示开灯


  // 片选为从机
  digitalWrite(SS, LOW);    // SS - pin 10
  // 发送字串
  for (const char * p = "up!\n" ; c = *p; p++) {
       SPI.transfer (c);
       Serial.print(c);
       }
   }
   if(results.value==275212439){      //确认接收到“下”编码,此码是预先读出来的按键编码。
     digitalWrite(13,0);                //点亮LED
     Serial.println("turn down LED"); //串口显示开灯


  // 片选为从机
  digitalWrite(SS, LOW);    // SS - pin 10
  // 发送字串
  for (const char * p = "down!\n" ; c = *p; p++) {
       SPI.transfer (c);
       Serial.print(c);
       }
    }
   if(results.value==275232839){      //确认接收到“左”的编码,此码是预先读出来的按键编码。
     digitalWrite(13,1);                //点亮LED
     Serial.println("turn on LED"); //串口显示开灯
  
  // 片选为从机
  digitalWrite(SS, LOW);    // SS - pin 10
  // 发送字串
  for (const char * p = "left!\n" ; c = *p; p++) {
       SPI.transfer (c);
       Serial.print(c);
       }
   }
   if(results.value==275220599){      //确认接收到“右”的编码,此码是预先读出来的按键编码。
     digitalWrite(13,0);                //点亮LED
     Serial.println("turn down LED"); //串口显示开灯

  // 片选为从机
  digitalWrite(SS, LOW);    // SS - pin 10
  // 发送字串
  for (const char * p = "right!\n" ; c = *p; p++) {
       SPI.transfer (c);
       Serial.print(c);
       }
   }
  // 取消从机
  digitalWrite(SS, HIGH);
  irrecv.resume();  
delay(600);


}
}

从设备(UNO)代码:
/*1、2016.1.6,Written by TXJ
* 2、下面不少是TFT显示屏的代码,我是从演示文件里提取出来的,
* 用自己显示屏的话直接替换相应代码即可,需要我的显示屏库文件
* 资料的留言即可
*/

// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
////Technical support:[email protected]

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPI.h>

// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
//   D0 connects to digital pin 8  (Notice these are
//   D1 connects to digital pin 9   NOT in order!)
//   D2 connects to digital pin 2
//   D3 connects to digital pin 3
//   D4 connects to digital pin 4
//   D5 connects to digital pin 5
//   D6 connects to digital pin 6
//   D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).

// Assign human-readable names to some common 16-bit color values:
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;

Adafruit_GFX_Button buttons[15];

//下面几行才是与SPI通信相关的
char ss;
char buf [100];
volatile byte pos;
volatile boolean process_it;

void setup()
{
Serial.begin(9600);
  tft.reset();
   uint16_t identifier = tft.readID();
  tft.begin(identifier);
SPCR |= bit (SPE);
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

  // 设置为接收状态
  SPCR |= _BV(SPE);

  // 准备接受中断
  pos = 0;   // 清空缓冲区
  process_it = false;

  // 开启中断
  SPI.attachInterrupt();
}

void loop()
{
    if (process_it)
  {
    buf [pos] = 0;  
    Serial.println (buf);
     tft.fillScreen(BLACK);
  tft.setCursor(40, 40);  
  tft.setTextColor(RED);  tft.setTextSize(2);
  tft.println(buf);
   tft.println("happy  new  years!!!");
    pos = 0;
    process_it = false;
  }

}

// SPI 中断程序
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // 从 SPI 数据寄存器获取数据
  if (pos < sizeof(buf))
  {
    buf [pos++] = c;
    if (c == '\n')
      process_it = true;
  }
}


    3截图显示:



     4求助大神
        请问,如何在两块板子之间传输数字,二进制或者十进制的,这个没找到资料,望大神指点一二
                 
















回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 13:27 , Processed in 0.100851 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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