Go Down

Topic: uint64_t in Serial.print (Read 4 times) previous topic - next topic

Steve Spence - KK4HFJ

Nov 25, 2012, 01:04 pm Last Edit: Nov 25, 2012, 07:36 pm by Steve Spence - KK4HFJ Reason: 1
I'm working with the Card info sketch in the sd examples, and my 16gb sd card reports as 3gb. It's my understanding that this is a result of Serial.print only understanding uint32_t. I've tried editing print.h to understand long long int, but it's not taking.

What am I not understanding?

Code: [Select]
   size_t print(const __FlashStringHelper *);
   size_t print(const String &);
   size_t print(const char[]);
   size_t print(char);
   size_t print(unsigned char, int = DEC);
   size_t print(int, int = DEC);
   size_t print(unsigned int, int = DEC);
   size_t print(long, int = DEC);
//size_t print(long long, int = DEC);
   size_t print(unsigned long, int = DEC);
//size_t print(unsigned long long, int = DEC);
   size_t print(double, int = 2);
   size_t print(const Printable&);

   size_t println(const __FlashStringHelper *);
   size_t println(const String &s);
   size_t println(const char[]);
   size_t println(char);
   size_t println(unsigned char, int = DEC);
   size_t println(int, int = DEC);
   size_t println(unsigned int, int = DEC);
   size_t println(long, int = DEC);
//size_t println(long long, int = DEC);
   size_t println(unsigned long, int = DEC);
//size_t println(unsigned long long, int = DEC);
   size_t println(double, int = 2);
   size_t println(const Printable&);
   size_t println(void);
Steve Spence - ET/AT/R&D KK4HFJ


add this to print.cpp and the signatures to print.h - be aware long long is slow slow ;)
Code: [Select]

void Print::println(long long n, int base)
  print(n, base);

void Print::print(long long n, int base)
  if (n < 0) write('-');
  if (base == 0) write(n);
  else printLLNumber(n, base);

void Print::println(unsigned long long n, int base)
  print(n, base);

void Print::print(unsigned long long n, int base)
  if (base == 0) write(n);
  else printLLNumber(n, base);

void Print::printLLNumber(unsigned long long n, uint8_t base)
  unsigned char buf[16 * sizeof(long)]; // Assumes 8-bit chars.
  unsigned long long i = 0;

  if (n == 0) {

  while (n > 0) {
    buf[i++] = n % base;
    n /= base;

  for (; i > 0; i--)
    print((char) (buf[i - 1] < 10 ?
      '0' + buf[i - 1] :
      'A' + buf[i - 1] - 10));
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Steve Spence - KK4HFJ

I get

C:\Users\Steve\Programs\arduino-1.0.2\hardware\arduino\cores\arduino\nPrint.cpp:230: error: prototype for 'size_t Print::printNumber(long long unsigned int, uint8_t)' does not match any in class 'Print'
C:\Users\Steve\Programs\arduino-1.0.2\hardware\arduino\cores\arduino\/Print.h:38: error: candidate is: size_t Print::printNumber(long unsigned int, uint8_t)

When I run

Code: [Select]

 SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.

The circuit:
 * SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples

created  28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
// include the SD library:
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;    

void setup()
// Open serial communications and wait for port to open:
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only

 Serial.print("\nInitializing 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);     // change this to 53 on a mega

 // we'll use the initialization code from the utility libraries
 // since we're just testing if the card is working!
 if (!card.init(SPI_HALF_SPEED, chipSelect)) {
   Serial.println("initialization failed. Things to check:");
   Serial.println("* is a card is inserted?");
   Serial.println("* Is your wiring correct?");
   Serial.println("* did you change the chipSelect pin to match your shield or module?");
 } else {
  Serial.println("Wiring is correct and a card is present.");

 // print the type of card
 Serial.print("\nCard type: ");
 switch(card.type()) {
   case SD_CARD_TYPE_SD1:
   case SD_CARD_TYPE_SD2:

 // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
 if (!volume.init(card)) {
   Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");

 // print the type and size of the first FAT-type volume
 uint64_t volumesize;
 Serial.print("\nVolume type is FAT");
 Serial.println(volume.fatType(), DEC);
 volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
 volumesize *= volume.clusterCount();       // we'll have a lot of clusters
 // volumesize *= 512;                            // SD card blocks are always 512 bytes
 // Serial.print("Volume size (bytes): ");
 // Serial.println(volumesize);
 Serial.print("Volume size (Kbytes): ");
 // volumesize /= 1024;
 volumesize /= 2;
 Serial.print("Volume size (Mbytes): ");
 volumesize /= 1024;

 Serial.println("\nFiles found on the card (name, date and size in bytes): ");
 // list all files in the card with date and size
 root.ls(LS_R | LS_DATE | LS_SIZE);

void loop(void) {

Steve Spence - ET/AT/R&D KK4HFJ


Steve Spence - KK4HFJ

I attached print.h

Does it look correct?
Steve Spence - ET/AT/R&D KK4HFJ

Go Up