SD卡和LED数码不能接在一个UNO R3上-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3858|回复: 0

[过期] SD卡和LED数码不能接在一个UNO R3上

[复制链接]
发表于 2013-10-22 21:24 | 显示全部楼层 |阅读模式

我用一个SD模块(SPI接口)和一个8位数码管模块连在一块UNO R3板子上,SD模块用D!!,D12,D13,D4
数码管模块用D5,D6,D7,并分别连上5v,我想让一个数据显示在数码管上,并且写到SD卡里去,可是现在出现单用数码管模块或者单用SD卡模块都可以,同时接好的话,就连SD卡的初始化都不行了,下面是我的程序
SD用的是官方库文件
/*
  SD card read/write

This example shows how to read and write data to and from an SD card file         
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe

This example code is in the public domain.
         
*/

#include <SD.h>

File myFile;
//Pin connected to ST_CP of 74HC595
int latchPin = 7;
//Pin connected to SH_CP of 74HC595
int clockPin = 6;
////Pin connected to DS of 74HC595
int dataPin = 5;
int tab[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f};
void ShowLED(int data)
{
  for(int i = 0; i < 8; i++)// display 0-7
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST,tab[7-i]);
      
    digitalWrite(latchPin, HIGH);

   
   
   
    delay(1000);
  }   
  while(1)//stop refresh the registers,then the numbers display steady
  {
    digitalWrite(latchPin, HIGH);
  }
}
void setup()
{
  
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while(1);
//   return;
  }
  Serial.println("initialization done.");
  
//set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  for(int i = 0; i < 8; i++)// shut off all the segments before being displayed
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST,0xff);   
    digitalWrite(latchPin, HIGH);
    delay(10);
  }

}

void loop()
{
  
  
         // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
   
    Serial.print("Writing to test.txt...");
    myFile.println("01234567");
    ShowLED(0);
    delay(3000);
        // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-4 16:07 , Processed in 0.078825 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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