add arduino sources

master
Peter Babič 4 years ago
parent e555d1e3cf
commit 0ebe9c4fd7
  1. 546
      arduino-sources/Bakalarka.cpp
  2. 88
      arduino-sources/Bakalarka.h
  3. 202
      arduino-sources/DS1302/DS1302.cpp
  4. 1388
      arduino-sources/MFRC522/MFRC522.cpp
  5. 329
      arduino-sources/MFRC522/MFRC522.h
  6. 334
      arduino-sources/PCD8544/PCD8544.cpp
  7. 78
      arduino-sources/PCD8544/PCD8544.h
  8. 24
      arduino-sources/Release/DS1302/subdir.mk
  9. 24
      arduino-sources/Release/MFRC522/subdir.mk
  10. 24
      arduino-sources/Release/PCD8544/subdir.mk
  11. 27
      arduino-sources/Release/SD/subdir.mk
  12. 30
      arduino-sources/Release/SD/utility/subdir.mk
  13. 27
      arduino-sources/Release/SDFAT16/subdir.mk
  14. 24
      arduino-sources/Release/SPI/subdir.mk
  15. 24
      arduino-sources/Release/SerialCommand/subdir.mk
  16. 27
      arduino-sources/Release/subdir.mk
  17. 38
      arduino-sources/SDFAT16/Fat16Config.h
  18. 208
      arduino-sources/SDFAT16/Fat16mainpage.h
  19. 418
      arduino-sources/SDFAT16/FatStructs.h
  20. 277
      arduino-sources/SDFAT16/SdCard.cpp
  21. 192
      arduino-sources/SDFAT16/SdCard.h
  22. 117
      arduino-sources/SDFAT16/SdInfo.h
  23. 66
      arduino-sources/SPI/SPI.cpp
  24. 70
      arduino-sources/SPI/SPI.h
  25. 94
      arduino-sources/SerialCommand/SerialCommand.h
  26. 118
      arduino-sources/prescaler.h
  27. 148
      arduino-sources/rotary.cpp
  28. 37
      arduino-sources/rotary.h

@ -0,0 +1,546 @@
#include "Bakalarka.h"
#include "prescaler.h"
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
//#include <avr/wdt.h>
#include "rotary.h"
Rotary encoder = Rotary(ENC_A, ENC_B);
#include "DS1302/DS1302.h"
DS1302 rtc(DS_RST, DS_IO, DS_SCK);
#include "SPI/SPI.h"
#include "MFRC522/MFRC522.h"
MFRC522 mfrc522(RFID_SS, RFID_RST);
#include "PCD8544/PCD8544.h"
PCD8544 lcd(LCD_DC, LCD_RST, LCD_SS);
#include <Fat16.h>
#include <Fat16util.h>
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
SdCard card;
Fat16 file;
#include "SerialCommand/SerialCommand.h"
SerialCommand SCmd;
volatile uint32_t lastDebounce;
volatile int8_t currentMenuItem = 0;
volatile boolean button = false, cursor = false, reading = false;
uint16_t lastRefresh;
static uint8_t storageIndex EEMEM;
Menu storage = {"Storage", list, 0};
Menu add_storage = {"New file", addStorage, 0};
Menu date_time = {"Date/time", frontPage, 0};
Menu exit_menu = {"Exit", frontPage, 0};
//Menu s1 = {"s1", list, 0};
//Menu s2 = {"s2", list, 0};
//Menu s3 = {"s3", list, 0};
//Menu s4 = {"s4", list, 0};
Menu mainMenu = { "", NULL, 4, { &storage, &add_storage, &date_time, &exit_menu} };
// bitmaps
const byte icon_usb[20] PROGMEM = {
0x08, 0x1C, 0x3E, 0x3E, 0x1C, 0x08, 0x0C, 0x0E, 0x0A, 0x1A,
0x3B, 0x6B, 0x4B, 0xE8, 0xE8, 0xE8, 0x08, 0x3E, 0x1C, 0x08
};
const byte icon_battery_100[21] PROGMEM = {
0xFF, 0x81, 0xBD, 0xBD, 0xBD, 0x81, 0xBD, 0xBD, 0xBD, 0x81,
0xBD, 0xBD, 0xBD, 0x81, 0xBD, 0xBD, 0xBD, 0x81, 0xFF, 0x3C, 0x3C
};
const byte icon_battery_75[21] PROGMEM = {
0xFF, 0x81, 0xBD, 0xBD, 0xBD, 0x81, 0xBD, 0xBD, 0xBD, 0x81,
0xBD, 0xBD, 0xBD, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF, 0x3C, 0x3C
};
const byte icon_battery_50[21] PROGMEM = {
0xFF, 0x81, 0xBD, 0xBD, 0xBD, 0x81, 0xBD, 0xBD, 0xBD, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF, 0x3C, 0x3C
};
const byte icon_battery_25[21] PROGMEM = {
0xFF, 0x81, 0xBD, 0xBD, 0xBD, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF, 0x3C, 0x3C
};
const byte icon_arrow[6] PROGMEM = {
0x00, 0xFE, 0x7C, 0x38, 0x10, 0x00
};
void setup(void) {
// There is 16HMz crystal used, halve it to reliable 8MHz@3V3
setClockPrescaler(CLOCK_PRESCALER_2);
// The fastest reliable baud rate is 38400
Serial.begin(38400);
lcd.begin();
lcd.clear();
// Rotary interrupt pin change enable
PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT8) | (1 << PCINT9);
// RFID host interrupt request
attachInterrupt(0, RFIDInterrupt, FALLING);
// Encoder button interrupt handle
attachInterrupt(1, debounceInterrupt, FALLING);
// Encoder button pull-up
pinMode(BTN, INPUT_PULLUP);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
if (!card.init(0, SD_SS))
error("card.init failed!");
if (!Fat16::init(&card))
error("Fat16::init failed!");
pinMode(SPKR, OUTPUT);
pinMode(VBAT_ADC_EN, OUTPUT);
// wdt_enable(WDTO_8S);
frontPage();
SCmd.addCommand("ls", ls);
SCmd.addCommand("touch", touch);
SCmd.addCommand("rm", rm);
// SCmd.addCommand("HELLO", SayHello); // Echos the string argument back
// SCmd.addCommand("P", process_command); // Converts two arguments to integers and echos them back
SCmd.addDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
sei();
}
void loop(void) {
// wdt_reset();
// tone(SPKR, 1000, 100);
// topBar();
SCmd.readSerial();
if (millis() - lastRefresh > 1000) {
frontPage();
lastRefresh = millis();
}
if (button) {
button = false;
navigateMenu(&mainMenu);
return;
// uint8_t menuLenght = sizeof(menuItems) / sizeof(menuItems[0]);
// if (currentMenuItem >= menuLenght)
// currentMenuItem = 0;
// else if (currentMenuItem < 0)
// currentMenuItem = menuLenght - 1;
// lcd.setCursor(0, 0);
// uint8_t i = 0, offset = 0;
// if (currentMenuItem >= PCD8544_LINES)
// offset = currentMenuItem - PCD8544_LINES + 1;
// for (i = offset; i < menuLenght && i < PCD8544_LINES + offset; i++) {
// if (i == (uint8_t)currentMenuItem)
// lcd.bitmap(icon_arrow, 1, 6);
// else
// lcd.print(' ');
// lcd.print(menuItems[i]);
// lcd.clearRestOfLine();
// }
}
// delay(1000);
// lcd.clear();
// topBar();
// delay(1000);
// if (Serial.available()) {
// Fat16::ls(LS_DATE | LS_SIZE);
// while(Serial.available())
// Serial.read();
// }
if (reading && mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
// // Look for new cards
// if ( ! mfrc522.PICC_IsNewCardPresent())
// return;
//
//
// // Select one of the cards
// if ( ! mfrc522.PICC_ReadCardSerial())
// return;
tone(SPKR, 5500, 70);
delay(1500);
reading = false;
if (file.open(eeprom_read_byte(&storageIndex), O_APPEND | O_WRITE)) {
Time t = rtc.time();
char buf[21];
snprintf(buf, sizeof buf, "%04d-%02d-%02d %02d:%02d:%02d;", t.yr, t.mon, t.day, t.hr, t.min, t.sec);
file.print(buf);
for (byte i = 0; i < mfrc522.uid.size; i++) {
file.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
file.print(mfrc522.uid.uidByte[i], HEX);
}
file.println(";");
file.close();
}
// lcd.setCursor(0, 1);
// for (byte i = 0; i < mfrc522.uid.size; i++) {
// lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
// lcd.print(mfrc522.uid.uidByte[i], HEX);
// }
// lcd.clearRestOfLine();
//// Dump debug info about the card. PICC_HaltA() is automatically called.
// mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
delay(100);
}
ISR(PCINT1_vect) {
cursor = encoder.process();
if (cursor == DIR_CW)
currentMenuItem++;
else if (cursor == DIR_CCW)
currentMenuItem--;
}
void debounceInterrupt(void) {
if ((long) (micros() - lastDebounce) >= (long)DEBOUNCE * 1000) {
button = true;
lastDebounce = micros();
}
}
void RFIDInterrupt(void) {
if (!reading)
reading = true;
}
void error_P(const char* str) {
lcd.setCursor(1, 4);
lcd.print(PSTR("ERROR"));
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode) {
PgmPrint("SD error: ");
Serial.println(card.errorCode, HEX);
}
while (1)
;
}
void navigateMenu(Menu *menu) {
if (menu->numSubmenus == 0) {
/* Execute the command */
lcd.clear();
menu->command();
return;
}
/* This is a menu, not a command, so show the menu and get the user's choice */
displayMenu(menu);
if (button) {
button = false;
menu = menu->submenu[currentMenuItem];
}
// menu = get_user_selection(menu);
navigateMenu(menu);
}
void displayMenu(Menu *menu) {
// uint8_t menuLenght = sizeof(menuItems) / sizeof(menuItems[0]);
if (currentMenuItem >= menu->numSubmenus)
currentMenuItem = 0;
else if (currentMenuItem < 0)
currentMenuItem = menu->numSubmenus - 1;
lcd.setCursor(0, 0);
uint8_t i = 0, offset = 0;
if (currentMenuItem >= PCD8544_LINES)
offset = currentMenuItem - PCD8544_LINES + 1;
for (i = offset; i < PCD8544_LINES + offset; i++) {
// clear rest of the menu screen if there are less items than LCD lines
if (i >= menu->numSubmenus) {
lcd.print(' ');
lcd.clearRestOfLine();
continue;
}
if (i == (uint8_t)currentMenuItem)
lcd.bitmap(icon_arrow, 1, 6);
else
lcd.print(' ');
lcd.print(menu->submenu[i]->title);
lcd.clearRestOfLine();
}
}
void list(void) {
cursor = DIR_CW;
while (!button) {
if (cursor) {
lcd.setCursor(0, 0);
dir_t d;
uint16_t i = 0;
boolean found = false;
uint8_t offset = 0;
if (currentMenuItem >= PCD8544_LINES)
offset = currentMenuItem - PCD8544_LINES + 1;
for (i = offset; Fat16::readDir(&d, &i, DIR_ATT_VOLUME_ID); i++) {
// if we are over the display, just seek the last file index
if (i >= (uint16_t)(PCD8544_LINES + offset))
continue;
if (i == (uint16_t) currentMenuItem) {
lcd.bitmap(icon_arrow, 1, 6);
found = true;
}
else
lcd.print(' ');
lcd.print(i);
lcd.print(':');
printDirName(d);
}
Serial.print("a: ");
if (!found) {
Serial.print("NOTfound, ");
if (cursor == DIR_CW) {
currentMenuItem++;
Serial.print("++");
} else if (cursor == DIR_CCW) {
currentMenuItem--;
Serial.print("--");
}
}
else
// this means that the screen will be refreshed
cursor = false;
if (currentMenuItem < 0) {
currentMenuItem = i - 1;
cursor = DIR_CCW;
Serial.print("i-1");
}
if ((uint16_t) currentMenuItem > i - 1) {
currentMenuItem = 0;
Serial.print("0");
cursor = DIR_CW;
}
Serial.print("; cmi: ");
Serial.print(currentMenuItem);
Serial.print(", li: ");
Serial.print(i);
Serial.print(", cursor: ");
Serial.print(cursor);
Serial.print(", button: ");
Serial.println(button);
}
}
Serial.println("Saved");
// save index of a file to store into to the EEPROM memory
eeprom_write_byte(&storageIndex, currentMenuItem);
button = false;
lcd.clear();
frontPage();
}
void addStorage(void) {
// file.writeError = false;
char buf[14];
for (unsigned short int i = 1; ; i++) {
snprintf(buf, sizeof buf, "file%d.csv", i);
if (file.open(buf, O_CREAT | O_WRITE | O_EXCL))
break;
}
file.close();
frontPage();
// snprintf(buf, sizeof buf, "FILE%d.CSV", 1);
}
void frontPage(void) {
// lcd.clear();
lcd.setCursor(0, 0);
// tone(SPKR, 5000, 50);
// USB plugged in
if (analogRead(STAT) < 512) {
lcd.bitmap(icon_usb, 1, 20);
}
// battery
else {
digitalWrite(VBAT_ADC_EN, LOW);
uint8_t vbat = (analogRead(VBAT_ADC) - 523) / 5;
if (vbat < 25)
lcd.bitmap(icon_battery_25, 1, 21);
else if (vbat < 50)
lcd.bitmap(icon_battery_50, 1, 21);
else if (vbat < 75)
lcd.bitmap(icon_battery_75, 1, 21);
else
lcd.bitmap(icon_battery_100, 1, 21);
digitalWrite(VBAT_ADC_EN, HIGH);
}
// Get the current time and date from the chip.
Time t = rtc.time();
char buf[24];
lcd.setCursor(9, 0);
snprintf(buf, sizeof(buf), "%02d:%02d", t.hr, t.min);
lcd.print(buf);
uint16_t i = eeprom_read_byte(&storageIndex);
dir_t d;
if (Fat16::readDir(&d, &i, DIR_ATT_VOLUME_ID)) {
lcd.setCursor(0, 2);
lcd.println("Saving to: ");
printDirName(d);
}
}
void printDirName(const dir_t& dir) {
uint8_t w = 0;
for (uint8_t i = 0; i < 11; i++) {
if (dir.name[i] == ' ')
continue;
if (i == 8) {
lcd.print('.');
w++;
}
lcd.print((char) dir.name[i]);
w++;
}
lcd.clearRestOfLine();
}
//
//void SayHello() {
// char *arg;
// arg = SCmd.next(); // Get the next argument from the SerialCommand object buffer
// if (arg != NULL) // As long as it existed, take it
// {
// Serial.print("Hello ");
// Serial.println(arg);
// } else {
// Serial.println("Hello, whoever you are");
// }
//}
//
//void process_command() {
// int aNumber;
// char *arg;
//
// Serial.println("We're in process_command");
// arg = SCmd.next();
// if (arg != NULL) {
// aNumber = atoi(arg); // Converts a char string to an integer
// Serial.print("First argument was: ");
// Serial.println(aNumber);
// } else {
// Serial.println("No arguments");
// }
//
// arg = SCmd.next();
// if (arg != NULL) {
// aNumber = atol(arg);
// Serial.print("Second argument was: ");
// Serial.println(aNumber);
// } else {
// Serial.println("No second argument");
// }
//
//}
void ls() {
Fat16::ls();
}
void touch() {
char *arg, fileName[14];
arg = SCmd.next();
snprintf(fileName, sizeof fileName, "%s.csv", arg);
PgmPrint("File '");
Serial.print(fileName);
if (!file.open(fileName, O_CREAT | O_WRITE))
PgmPrintln("' could not be created.");
else
PgmPrintln("' was created successfully");
}
void rm() {
char *arg, fileName[14];
arg = SCmd.next();
snprintf(fileName, sizeof fileName, "%s.csv", arg);
PgmPrint("File '");
Serial.print(fileName);
if (file.open(fileName, O_WRITE) && file.remove())
PgmPrintln("' was removed successfully");
else
PgmPrintln("' could not be removed.");
}
void mv() {
char *arg;
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized() {
PgmPrintln("Unrecognized command. Possibilities are: ");
PgmPrintln("ls\t\t\t- display files");
PgmPrintln("touch FILE\t\t- create file FILE.csv");
Serial.println();
}

@ -0,0 +1,88 @@
// Only modify this file to include
// - function definitions (prototypes)
// - include files
// - extern variable definitions
// In the appropriate section
#ifndef Bakalarka_H_
#define Bakalarka_H_
#include "Arduino.h"
//add your includes for the project Bakalarka here
#include "PCD8544/PCD8544.h"
#include <Fat16.h>
//end of add your includes here
#ifdef __cplusplus
extern "C" {
#endif
void loop();
void setup();
#ifdef __cplusplus
} // extern "C"
#endif
//add your function definitions for the project Bakalarka here
// pins on ATmega328
#define RFID_IRQ 2
#define BTN 3
#define RFID_RST 4
#define DS_SCK 5
#define DS_IO 6
#define DS_RST 7
#define LCD_SS 8
#define SD_SS 9
#define RFID_SS 10
#define ENC_A A0
#define ENC_B A1
#define LCD_DC A2
#define LCD_RST A3
#define SPKR A4
#define VBAT_ADC_EN A5
#define VBAT_ADC A6
#define STAT A7
// config
#define DEBOUNCE 200
// function macros
//#define error(s) error_P(PSTR(s)) // store error strings in flash to save RAM
#define MAX_SUBMENUS 4
typedef struct menu {
const char title[PCD8544_COLS];
void (*command)();
uint8_t numSubmenus;
struct menu *submenu[MAX_SUBMENUS];
} Menu;
// prototypes
void debounceInterrupt(void);
void RFIDInterrupt(void);
void error_P(const char* str);
void frontPage(void);
void list(void);
void navigateMenu(Menu *menu);
void displayMenu(Menu *menu);
void addStorage(void);
void printDirName(const dir_t& dir);
//void SayHello(void);
//void process_command(void);
void ls(void);
void touch(void);
void rm(void);
void unrecognized(void);
//Do not add code below this line
#endif /* Bakalarka_H_ */

@ -0,0 +1,202 @@
#include "Arduino.h"
#include "DS1302.h"
Time::Time(const uint16_t yr, const uint8_t mon, const uint8_t date,
const uint8_t hr, const uint8_t min, const uint8_t sec,
const Day day) {
this->yr = yr;
this->mon = mon;
this->date = date;
this->hr = hr;
this->min = min;
this->sec = sec;
this->day = day;
}
DS1302::DS1302(const uint8_t ce_pin, const uint8_t io_pin,
const uint8_t sclk_pin) {
ce_pin_ = ce_pin;
io_pin_ = io_pin;
sclk_pin_ = sclk_pin;
pinMode(ce_pin, OUTPUT);
pinMode(sclk_pin, OUTPUT);
}
void DS1302::writeOut(const uint8_t value) {
pinMode(io_pin_, OUTPUT);
shiftOut(io_pin_, sclk_pin_, LSBFIRST, value);
}
uint8_t DS1302::readIn() {
uint8_t input_value = 0;
uint8_t bit = 0;
pinMode(io_pin_, INPUT);
for (int i = 0; i < 8; ++i) {
bit = digitalRead(io_pin_);
input_value |= (bit << i);
digitalWrite(sclk_pin_, HIGH);
delayMicroseconds(1);
digitalWrite(sclk_pin_, LOW);
}
return input_value;
}
uint8_t DS1302::registerBcdToDec(const Register reg, const uint8_t high_bit) {
const uint8_t mask = (1 << (high_bit + 1)) - 1;
uint8_t val = readRegister(reg);
val &= mask;
val = (val & 15) + 10 * ((val & (15 << 4)) >> 4);
return val;
}
uint8_t DS1302::registerBcdToDec(const Register reg) {
return registerBcdToDec(reg, 7);
}
void DS1302::registerDecToBcd(const Register reg, uint8_t value,
const uint8_t high_bit) {
const uint8_t mask = (1 << (high_bit + 1)) - 1;
uint8_t regv = readRegister(reg);
// Convert value to bcd in place.
uint8_t tvalue = value / 10;
value = value % 10;
value |= (tvalue << 4);
// Replace high bits of value if needed.
value &= mask;
value |= (regv &= ~mask);
writeRegister(reg, value);
}
void DS1302::registerDecToBcd(const Register reg, const uint8_t value) {
registerDecToBcd(reg, value, 7);
}
uint8_t DS1302::readRegister(const Register reg) {
uint8_t cmd_byte = 129; // 1000 0001
uint8_t reg_value;
cmd_byte |= (reg << 1);
digitalWrite(sclk_pin_, LOW);
digitalWrite(ce_pin_, HIGH);
writeOut(cmd_byte);
reg_value = readIn();
digitalWrite(ce_pin_, LOW);
return reg_value;
}
void DS1302::writeRegister(const Register reg, const uint8_t value) {
uint8_t cmd_byte = (128 | (reg << 1));
digitalWrite(sclk_pin_, LOW);
digitalWrite(ce_pin_, HIGH);
writeOut(cmd_byte);
writeOut(value);
digitalWrite(ce_pin_, LOW);
}
void DS1302::writeProtect(const bool enable) {
writeRegister(kWriteProtectReg, (enable << 7));
}
void DS1302::halt(const bool enable) {
uint8_t sec = readRegister(kSecondReg);
sec &= ~(1 << 7);
sec |= (enable << 7);
writeRegister(kSecondReg, sec);
}
uint8_t DS1302::seconds() {
return registerBcdToDec(kSecondReg, 6);
}
uint8_t DS1302::minutes() {
return registerBcdToDec(kMinuteReg);
}
uint8_t DS1302::hour() {
uint8_t hr = readRegister(kHourReg);
uint8_t adj;
if (hr & 128) // 12-hour mode
adj = 12 * ((hr & 32) >> 5);
else // 24-hour mode
adj = 10 * ((hr & (32 + 16)) >> 4);
hr = (hr & 15) + adj;
return hr;
}
uint8_t DS1302::date() {
return registerBcdToDec(kDateReg, 5);
}
uint8_t DS1302::month() {
return registerBcdToDec(kMonthReg, 4);
}
Time::Day DS1302::day() {
return static_cast<Time::Day>(registerBcdToDec(kDayReg, 2));
}
uint16_t DS1302::year() {
return 2000 + registerBcdToDec(kYearReg);
}
Time DS1302::time() {
return Time(year(), month(), date(),
hour(), minutes(), seconds(),
day());
}
void DS1302::seconds(const uint8_t sec) {
registerDecToBcd(kSecondReg, sec, 6);
}
void DS1302::minutes(const uint8_t min) {
registerDecToBcd(kMinuteReg, min, 6);
}
void DS1302::hour(const uint8_t hr) {
writeRegister(kHourReg, 0); // set 24-hour mode
registerDecToBcd(kHourReg, hr, 5);
}
void DS1302::date(const uint8_t date) {
registerDecToBcd(kDateReg, date, 5);
}
void DS1302::month(const uint8_t mon) {
registerDecToBcd(kMonthReg, mon, 4);
}
void DS1302::day(const Time::Day day) {
registerDecToBcd(kDayReg, static_cast<int>(day), 2);
}
void DS1302::year(uint16_t yr) {
yr -= 2000;
registerDecToBcd(kYearReg, yr);
}
void DS1302::time(const Time t) {
seconds(t.sec);
minutes(t.min);
hour(t.hr);
date(t.date);
month(t.mon);
day(t.day);
year(t.yr);
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,329 @@
/**
* MFRC522.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Released into the public domain.
*
* Please read this file for an overview and then MFRC522.cpp for comments on the specific functions.
* Search for "mf-rc522" on ebay.com to purchase the MF-RC522 board.
*
* There are three hardware components involved:
* 1) The micro controller: An Arduino
* 2) The PCD (short for Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
* 3) The PICC (short for Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
*
* The microcontroller and card reader uses SPI for communication.
* The protocol is described in the MFRC522 datasheet: http://www.nxp.com/documents/data_sheet/MFRC522.pdf
*
* The card reader and the tags communicate using a 13.56MHz electromagnetic field.
* The protocol is defined in ISO/IEC 14443-3 Identification cards -- Contactless integrated circuit cards -- Proximity cards -- Part 3: Initialization and anticollision".
* A free version of the final draft can be found at http://wg8.de/wg8n1496_17n3613_Ballot_FCD14443-3.pdf
* Details are found in chapter 6, Type A Initialization and anticollision.
*
* If only the PICC UID is wanted, the above documents has all the needed information.
* To read and write from MIFARE PICCs, the MIFARE protocol is used after the PICC has been selected.
* The MIFARE Classic chips and protocol is described in the datasheets:
* 1K: http://www.nxp.com/documents/data_sheet/MF1S503x.pdf
* 4K: http://www.nxp.com/documents/data_sheet/MF1S703x.pdf
* Mini: http://www.idcardmarket.com/download/mifare_S20_datasheet.pdf
* The MIFARE Ultralight chip and protocol is described in the datasheets:
* Ultralight: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf
* Ultralight C: http://www.nxp.com/documents/short_data_sheet/MF0ICU2_SDS.pdf
*
* MIFARE Classic 1K (MF1S503x):
* Has 16 sectors * 4 blocks/sector * 16 bytes/block = 1024 bytes.
* The blocks are numbered 0-63.
* Block 3 in each sector is the Sector Trailer. See http://www.nxp.com/documents/data_sheet/MF1S503x.pdf sections 8.6 and 8.7:
* Bytes 0-5: Key A
* Bytes 6-8: Access Bits
* Bytes 9: User data
* Bytes 10-15: Key B (or user data)
* Block 0 is read only manufacturer data.
* To access a block, an authentication using a key from the block's sector must be performed first.
* Example: To read from block 10, first authenticate using a key from sector 3 (blocks 8-11).
* All keys are set to FFFFFFFFFFFFh at chip delivery.
* Warning: Please read section 8.7 "Memory Access". It includes this text: if the PICC detects a format violation the whole sector is irreversibly blocked.
* To use a block in "value block" mode (for Increment/Decrement operations) you need to change the sector trailer. Use PICC_SetAccessBits() to calculate the bit patterns.
* MIFARE Classic 4K (MF1S703x):
* Has (32 sectors * 4 blocks/sector + 8 sectors * 16 blocks/sector) * 16 bytes/block = 4096 bytes.
* The blocks are numbered 0-255.
* The last block in each sector is the Sector Trailer like above.
* MIFARE Classic Mini (MF1 IC S20):
* Has 5 sectors * 4 blocks/sector * 16 bytes/block = 320 bytes.
* The blocks are numbered 0-19.
* The last block in each sector is the Sector Trailer like above.
*
* MIFARE Ultralight (MF0ICU1):
* Has 16 pages of 4 bytes = 64 bytes.
* Pages 0 + 1 is used for the 7-byte UID.
* Page 2 contains the last chech digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
* Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
* Pages 4-15 are read/write unless blocked by the lock bytes in page 2.
* MIFARE Ultralight C (MF0ICU2):
* Has 48 pages of 4 bytes = 64 bytes.
* Pages 0 + 1 is used for the 7-byte UID.
* Page 2 contains the last chech digit for the UID, one byte manufacturer internal data, and the lock bytes (see http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf section 8.5.2)
* Page 3 is OTP, One Time Programmable bits. Once set to 1 they cannot revert to 0.
* Pages 4-39 are read/write unless blocked by the lock bytes in page 2.
* Page 40 Lock bytes
* Page 41 16 bit one way counter
* Pages 42-43 Authentication configuration
* Pages 44-47 Authentication key
*/
#ifndef MFRC522_h
#define MFRC522_h
#include <Arduino.h>
#include "../SPI/SPI.h"
class MFRC522 {
public:
// MFRC522 registers. Described in chapter 9 of the datasheet.
// When using SPI all addresses are shifted one bit left in the "SPI address byte" (section 8.1.2.3)
enum PCD_Register {
// Page 0: Command and status
// 0x00 // reserved for future use
CommandReg = 0x01 << 1, // starts and stops command execution
ComIEnReg = 0x02 << 1, // enable and disable interrupt request control bits
DivIEnReg = 0x03 << 1, // enable and disable interrupt request control bits
ComIrqReg = 0x04 << 1, // interrupt request bits
DivIrqReg = 0x05 << 1, // interrupt request bits
ErrorReg = 0x06 << 1, // error bits showing the error status of the last command executed
Status1Reg = 0x07 << 1, // communication status bits
Status2Reg = 0x08 << 1, // receiver and transmitter status bits
FIFODataReg = 0x09 << 1, // input and output of 64 byte FIFO buffer
FIFOLevelReg = 0x0A << 1, // number of bytes stored in the FIFO buffer
WaterLevelReg = 0x0B << 1, // level for FIFO underflow and overflow warning
ControlReg = 0x0C << 1, // miscellaneous control registers
BitFramingReg = 0x0D << 1, // adjustments for bit-oriented frames
CollReg = 0x0E << 1, // bit position of the first bit-collision detected on the RF interface
// 0x0F // reserved for future use
// Page 1:Command
// 0x10 // reserved for future use
ModeReg = 0x11 << 1, // defines general modes for transmitting and receiving
TxModeReg = 0x12 << 1, // defines transmission data rate and framing
RxModeReg = 0x13 << 1, // defines reception data rate and framing
TxControlReg = 0x14 << 1, // controls the logical behavior of the antenna driver pins TX1 and TX2
TxASKReg = 0x15 << 1, // controls the setting of the transmission modulation
TxSelReg = 0x16 << 1, // selects the internal sources for the antenna driver
RxSelReg = 0x17 << 1, // selects internal receiver settings
RxThresholdReg = 0x18 << 1, // selects thresholds for the bit decoder
DemodReg = 0x19 << 1, // defines demodulator settings
// 0x1A // reserved for future use
// 0x1B // reserved for future use
MfTxReg = 0x1C << 1, // controls some MIFARE communication transmit parameters
MfRxReg = 0x1D << 1, // controls some MIFARE communication receive parameters
// 0x1E // reserved for future use
SerialSpeedReg = 0x1F << 1, // selects the speed of the serial UART interface
// Page 2: Configuration
// 0x20 // reserved for future use
CRCResultRegH = 0x21 << 1, // shows the MSB and LSB values of the CRC calculation
CRCResultRegL = 0x22 << 1,
// 0x23 // reserved for future use
ModWidthReg = 0x24 << 1, // controls the ModWidth setting?
// 0x25 // reserved for future use
RFCfgReg = 0x26 << 1, // configures the receiver gain
GsNReg = 0x27 << 1, // selects the conductance of the antenna driver pins TX1 and TX2 for modulation
CWGsPReg = 0x28 << 1, // defines the conductance of the p-driver output during periods of no modulation
ModGsPReg = 0x29 << 1, // defines the conductance of the p-driver output during periods of modulation
TModeReg = 0x2A << 1, // defines settings for the internal timer
TPrescalerReg = 0x2B << 1, // the lower 8 bits of the TPrescaler value. The 4 high bits are in TModeReg.
TReloadRegH = 0x2C << 1, // defines the 16-bit timer reload value
TReloadRegL = 0x2D << 1,
TCounterValueRegH = 0x2E << 1, // shows the 16-bit timer value
TCounterValueRegL = 0x2F << 1,
// Page 3:Test Registers
// 0x30 // reserved for future use
TestSel1Reg = 0x31 << 1, // general test signal configuration
TestSel2Reg = 0x32 << 1, // general test signal configuration
TestPinEnReg = 0x33 << 1, // enables pin output driver on pins D1 to D7
TestPinValueReg = 0x34 << 1, // defines the values for D1 to D7 when it is used as an I/O bus
TestBusReg = 0x35 << 1, // shows the status of the internal test bus
AutoTestReg = 0x36 << 1, // controls the digital self test
VersionReg = 0x37 << 1, // shows the software version
AnalogTestReg = 0x38 << 1, // controls the pins AUX1 and AUX2
TestDAC1Reg = 0x39 << 1, // defines the test value for TestDAC1
TestDAC2Reg = 0x3A << 1, // defines the test value for TestDAC2
TestADCReg = 0x3B << 1 // shows the value of ADC I and Q channels
// 0x3C // reserved for production tests
// 0x3D // reserved for production tests
// 0x3E // reserved for production tests
// 0x3F // reserved for production tests
};
// MFRC522 comands. Described in chapter 10 of the datasheet.
enum PCD_Command {
PCD_Idle = 0x00, // no action, cancels current command execution
PCD_Mem = 0x01, // stores 25 bytes into the internal buffer
PCD_GenerateRandomID = 0x02, // generates a 10-byte random ID number
PCD_CalcCRC = 0x03, // activates the CRC coprocessor or performs a self test
PCD_Transmit = 0x04, // transmits data from the FIFO buffer
PCD_NoCmdChange = 0x07, // no command change, can be used to modify the CommandReg register bits without affecting the command, for example, the PowerDown bit
PCD_Receive = 0x08, // activates the receiver circuits
PCD_Transceive = 0x0C, // transmits data from FIFO buffer to antenna and automatically activates the receiver after transmission
PCD_MFAuthent = 0x0E, // performs the MIFARE standard authentication as a reader
PCD_SoftReset = 0x0F // resets the MFRC522
};
// Commands sent to the PICC.
enum PICC_Command {
// The commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4)
PICC_CMD_REQA = 0x26, // REQuest command, Type A. Invites PICCs in state IDLE to go to READY and prepare for anticollision or selection. 7 bit frame.
PICC_CMD_WUPA = 0x52, // Wake-UP command, Type A. Invites PICCs in state IDLE and HALT to go to READY(*) and prepare for anticollision or selection. 7 bit frame.
PICC_CMD_CT = 0x88, // Cascade Tag. Not really a command, but used during anti collision.
PICC_CMD_SEL_CL1 = 0x93, // Anti collision/Select, Cascade Level 1
PICC_CMD_SEL_CL2 = 0x95, // Anti collision/Select, Cascade Level 1
PICC_CMD_SEL_CL3 = 0x97, // Anti collision/Select, Cascade Level 1
PICC_CMD_HLTA = 0x50, // HaLT command, Type A. Instructs an ACTIVE PICC to go to state HALT.
// The commands used for MIFARE Classic (from http://www.nxp.com/documents/data_sheet/MF1S503x.pdf, Section 9)
// Use PCD_MFAuthent to authenticate access to a sector, then use these commands to read/write/modify the blocks on the sector.
// The read/write commands can also be used for MIFARE Ultralight.
PICC_CMD_MF_AUTH_KEY_A = 0x60, // Perform authentication with Key A
PICC_CMD_MF_AUTH_KEY_B = 0x61, // Perform authentication with Key B
PICC_CMD_MF_READ = 0x30, // Reads one 16 byte block from the authenticated sector of the PICC. Also used for MIFARE Ultralight.
PICC_CMD_MF_WRITE = 0xA0, // Writes one 16 byte block to the authenticated sector of the PICC. Called "COMPATIBILITY WRITE" for MIFARE Ultralight.
PICC_CMD_MF_DECREMENT = 0xC0, // Decrements the contents of a block and stores the result in the internal data register.
PICC_CMD_MF_INCREMENT = 0xC1, // Increments the contents of a block and stores the result in the internal data register.
PICC_CMD_MF_RESTORE = 0xC2, // Reads the contents of a block into the internal data register.
PICC_CMD_MF_TRANSFER = 0xB0, // Writes the contents of the internal data register to a block.
// The commands used for MIFARE Ultralight (from http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, Section 8.6)
// The PICC_CMD_MF_READ and PICC_CMD_MF_WRITE can also be used for MIFARE Ultralight.
PICC_CMD_UL_WRITE = 0xA2 // Writes one 4 byte page to the PICC.
};
// MIFARE constants that does not fit anywhere else
enum MIFARE_Misc {
MF_ACK = 0xA, // The MIFARE Classic uses a 4 bit ACK/NAK. Any other value than 0xA is NAK.
MF_KEY_SIZE = 6 // A Mifare Crypto1 key is 6 bytes.
};
// PICC types we can detect. Remember to update PICC_GetTypeName() if you add more.
enum PICC_Type {
PICC_TYPE_UNKNOWN = 0,
PICC_TYPE_ISO_14443_4 = 1, // PICC compliant with ISO/IEC 14443-4
PICC_TYPE_ISO_18092 = 2, // PICC compliant with ISO/IEC 18092 (NFC)
PICC_TYPE_MIFARE_MINI = 3, // MIFARE Classic protocol, 320 bytes
PICC_TYPE_MIFARE_1K = 4, // MIFARE Classic protocol, 1KB
PICC_TYPE_MIFARE_4K = 5, // MIFARE Classic protocol, 4KB
PICC_TYPE_MIFARE_UL = 6, // MIFARE Ultralight or Ultralight C
PICC_TYPE_MIFARE_PLUS = 7, // MIFARE Plus
PICC_TYPE_TNP3XXX = 8, // Only mentioned in NXP AN 10833 MIFARE Type Identification Procedure
PICC_TYPE_NOT_COMPLETE = 255 // SAK indicates UID is not complete.
};
// Return codes from the functions in this class. Remember to update GetStatusCodeName() if you add more.
enum StatusCode {
STATUS_OK = 1, // Success
STATUS_ERROR = 2, // Error in communication
STATUS_COLLISION = 3, // Collission detected
STATUS_TIMEOUT = 4, // Timeout in communication.
STATUS_NO_ROOM = 5, // A buffer is not big enough.
STATUS_INTERNAL_ERROR = 6, // Internal error in the code. Should not happen ;-)
STATUS_INVALID = 7, // Invalid argument.
STATUS_CRC_WRONG = 8, // The CRC_A does not match
STATUS_MIFARE_NACK = 9 // A MIFARE PICC responded with NAK.
};
// A struct used for passing the UID of a PICC.
typedef struct {
byte size; // Number of bytes in the UID. 4, 7 or 10.
byte uidByte[10];
byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
} Uid;
// A struct used for passing a MIFARE Crypto1 key
typedef struct {
byte keyByte[MF_KEY_SIZE];
} MIFARE_Key;
// Member variables
Uid uid; // Used by PICC_ReadCardSerial().
// Size of the MFRC522 FIFO
static const byte FIFO_SIZE = 64; // The FIFO is 64 bytes.
/////////////////////////////////////////////////////////////////////////////////////
// Functions for setting up the Arduino
/////////////////////////////////////////////////////////////////////////////////////
MFRC522(byte chipSelectPin, byte resetPowerDownPin);
void setSPIConfig();
/////////////////////////////////////////////////////////////////////////////////////
// Basic interface functions for communicating with the MFRC522
/////////////////////////////////////////////////////////////////////////////////////
void PCD_WriteRegister(byte reg, byte value);
void PCD_WriteRegister(byte reg, byte count, byte *values);
byte PCD_ReadRegister(byte reg);
void PCD_ReadRegister(byte reg, byte count, byte *values, byte rxAlign = 0);
void setBitMask(unsigned char reg, unsigned char mask);
void PCD_SetRegisterBitMask(byte reg, byte mask);
void PCD_ClearRegisterBitMask(byte reg, byte mask);
byte PCD_CalculateCRC(byte *data, byte length, byte *result);
/////////////////////////////////////////////////////////////////////////////////////
// Functions for manipulating the MFRC522
/////////////////////////////////////////////////////////////////////////////////////
void PCD_Init();
void PCD_Reset();
void PCD_AntennaOn();
/////////////////////////////////////////////////////////////////////////////////////
// Functions for communicating with PICCs
/////////////////////////////////////////////////////////////////////////////////////
byte PCD_TransceiveData(byte *sendData, byte sendLen, byte *backData, byte *backLen, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false);
byte PCD_CommunicateWithPICC(byte command, byte waitIRq, byte *sendData, byte sendLen, byte *backData = NULL, byte *backLen = NULL, byte *validBits = NULL, byte rxAlign = 0, bool checkCRC = false);
byte PICC_RequestA(byte *bufferATQA, byte *bufferSize);
byte PICC_WakeupA(byte *bufferATQA, byte *bufferSize);
byte PICC_REQA_or_WUPA( byte command, byte *bufferATQA, byte *bufferSize);
byte PICC_Select(Uid *uid, byte validBits = 0);
byte PICC_HaltA();
/////////////////////////////////////////////////////////////////////////////////////
// Functions for communicating with MIFARE PICCs
/////////////////////////////////////////////////////////////////////////////////////
byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
void PCD_StopCrypto1();
byte MIFARE_Read(byte blockAddr, byte *buffer, byte *bufferSize);
byte MIFARE_Write(byte blockAddr, byte *buffer, byte bufferSize);
byte MIFARE_Decrement(byte blockAddr, long delta);
byte MIFARE_Increment(byte blockAddr, long delta);
byte MIFARE_Restore(byte blockAddr);
byte MIFARE_Transfer(byte blockAddr);
byte MIFARE_Ultralight_Write(byte page, byte *buffer, byte bufferSize);
/////////////////////////////////////////////////////////////////////////////////////
// Support functions
/////////////////////////////////////////////////////////////////////////////////////
byte PCD_MIFARE_Transceive( byte *sendData, byte sendLen, bool acceptTimeout = false);
const char *GetStatusCodeName(byte code);
byte PICC_GetType(byte sak);
const char *PICC_GetTypeName(byte type);
void PICC_DumpToSerial(Uid *uid);
void PICC_DumpMifareClassicToSerial(Uid *uid, byte piccType, MIFARE_Key *key);
void PICC_DumpMifareClassicSectorToSerial(Uid *uid, MIFARE_Key *key, byte sector);
void PICC_DumpMifareUltralightToSerial();
void MIFARE_SetAccessBits(byte *accessBitBuffer, byte g0, byte g1, byte g2, byte g3);
/////////////////////////////////////////////////////////////////////////////////////
// Convenience functions - does not add extra functionality
/////////////////////////////////////////////////////////////////////////////////////
bool PICC_IsNewCardPresent();
bool PICC_ReadCardSerial();
/////////////////////////////////////////////////////////////////////////////////////
// User functions
/////////////////////////////////////////////////////////////////////////////////////
// void MFRC522::UID_to_lcd(Uid *uid);
private:
byte _chipSelectPin; // Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low)
byte _resetPowerDownPin; // Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low)
byte MIFARE_TwoStepHelper(byte command, byte blockAddr, long data);
};
#endif

@ -0,0 +1,334 @@
#include <stdint.h>
#include "PCD8544.h"
#include <pins_arduino.h>
#include <avr/pgmspace.h>
// LCD commands, Table 1, page 14
#define PCD8544_FUNCTION_SET (1<<5)
#define PCD8544_FUNCTION_PD (1<<2)
#define PCD8544_FUNCTION_V (1<<1)
#define PCD8544_FUNCTION_H (1<<0)
// Normal instructions, H = 0
#define PCD8544_DISPLAY_CONTROL (1<<3)
#define PCD8544_DISPLAY_CONTROL_D (1<<2)
#define PCD8544_DISPLAY_CONTROL_E (1<<0)
#define PCD8544_DISPLAY_CONTROL_BLANK 0
#define PCD8544_DISPLAY_CONTROL_NORMAL_MODE PCD8544_DISPLAY_CONTROL_D
#define PCD8544_DISPLAY_CONTROL_ALL_ON PCD8544_DISPLAY_CONTROL_E
#define PCD8544_DISPLAY_CONTROL_INVERSE (PCD8544_DISPLAY_CONTROL_D|PCD8544_DISPLAY_CONTROL_E)
#define PCD8544_SET_Y_ADDRESS (1<<6)
#define PCD8544_Y_ADRESS_MASK 0b111
#define PCD8544_SET_X_ADDRESS (1<<7)
#define PCD8544_X_ADRESS_MASK 0b01111111
// Extended instructions. H = 1
#define PCD8544_TEMP_CONTROL (1<<2)
#define PCD8544_TEMP_TC1 (1<<1)
#define PCD8544_TEMP_TC0 (1<<0)
#define PCD8544_BIAS (1<<4)
#define PCD8544_BIAS_BS2 (1<<2)
#define PCD8544_BIAS_BS1 (1<<1)
#define PCD8544_BIAS_BS0 (1<<0)
#define PCD8544_VOP (1<<7)
const unsigned char PROGMEM small_num[][4] = {
{0x0e,0x15,0x0e,0x00}, // 48, zero
{0x12,0x1f,0x10,0x00}, // 49, one
{0x12,0x19,0x16,0x00}, // 50, two
{0x11,0x15,0x0b,0x00}, // 51, three
{0x07,0x04,0x1f,0x00}, // 52, four
{0x17,0x15,0x09,0x00}, // 53, five
{0x0e,0x15,0x09,0x00}, // 54, six
{0x19,0x05,0x03,0x00}, // 55, seven
{0x1a,0x15,0x0b,0x00}, // 56, eight
{0x12,0x15,0x0e,0x00}, // 57, nine
{0x00,0x10,0x00,0x00}, // 46, period
};
const unsigned char PROGMEM font6x8 [][5] = {
{0x00,0x00,0x00,0x00,0x00,}, // ' ' 32
{0x00,0x00,0x5F,0x00,0x00,}, // '!' 33
{0x00,0x07,0x00,0x07,0x00,}, // '"' 34
{0x14,0x7F,0x14,0x7F,0x14,}, // '#' 35
{0x24,0x2A,0x7F,0x2A,0x12,}, // '$' 36
{0x23,0x13,0x08,0x64,0x62,}, // '%' 37
{0x36,0x49,0x55,0x22,0x50,}, // '&' 38
{0x00,0x05,0x03,0x00,0x00,}, // ''' 39
{0x00,0x1C,0x22,0x41,0x00,}, // '(' 40
{0x00,0x41,0x22,0x1C,0x00,}, // ')' 41
{0x14,0x08,0x3E,0x08,0x14,}, // '*' 42
{0x08,0x08,0x3E,0x08,0x08,}, // '+' 43
{0x00,0x50,0x30,0x00,0x00,}, // ',' 44
{0x08,0x08,0x08,0x08,0x08,}, // '-' 45
{0x00,0x60,0x60,0x00,0x00,}, // '.' 46
{0x20,0x10,0x08,0x04,0x02,}, // '/' 47
{0x3E,0x51,0x49,0x45,0x3E,}, // '0' 48
{0x00,0x42,0x7F,0x40,0x00,}, // '1' 49
{0x42,0x61,0x51,0x49,0x46,}, // '2' 50
{0x21,0x41,0x45,0x4B,0x31,}, // '3' 51
{0x18,0x14,0x12,0x7F,0x10,}, // '4' 52
{0x27,0x45,0x45,0x45,0x39,}, // '5' 53
{0x3C,0x4A,0x49,0x49,0x30,}, // '6' 54
{0x03,0x01,0x71,0x09,0x07,}, // '7' 55
{0x36,0x49,0x49,0x49,0x36,}, // '8' 56
{0x06,0x49,0x49,0x29,0x16,}, // '9' 57
{0x00,0x36,0x36,0x00,0x00,}, // ':' 58
{0x00,0x56,0x36,0x00,0x00,}, // ';' 59
{0x08,0x14,0x22,0x41,0x00,}, // '<' 60
{0x14,0x14,0x14,0x14,0x14,}, // '=' 61
{0x00,0x41,0x22,0x14,0x08,}, // '>' 62
{0x02,0x01,0x51,0x09,0x06,}, // '?' 63
{0x32,0x49,0x79,0x41,0x3E,}, // '@' 64
{0x7E,0x11,0x11,0x11,0x7E,}, // 'A' 65
{0x7F,0x49,0x49,0x49,0x36,}, // 'B' 66
{0x3E,0x41,0x41,0x41,0x22,}, // 'C' 67
{0x7F,0x41,0x41,0x22,0x1C,}, // 'D' 68
{0x7F,0x49,0x49,0x49,0x41,}, // 'E' 69
{0x7F,0x09,0x09,0x01,0x01,}, // 'F' 70
{0x3E,0x41,0x49,0x49,0x3A,}, // 'G' 71
{0x7F,0x08,0x08,0x08,0x7F,}, // 'H' 72
{0x00,0x41,0x7F,0x41,0x00,}, // 'I' 73
{0x20,0x41,0x41,0x3F,0x01,}, // 'J' 74
{0x7F,0x08,0x14,0x22,0x41,}, // 'K' 75
{0x7F,0x40,0x40,0x40,0x40,}, // 'L' 76
{0x7F,0x02,0x0C,0x02,0x7F,}, // 'M' 77
{0x7F,0x04,0x08,0x10,0x7F,}, // 'N' 78
{0x3E,0x41,0x41,0x41,0x3E,}, // 'O' 79
{0x7F,0x09,0x09,0x09,0x06,}, // 'P' 80
{0x3E,0x41,0x51,0x21,0x5E,}, // 'Q' 81
{0x7F,0x09,0x19,0x29,0x46,}, // 'R' 82
{0x26,0x49,0x49,0x49,0x32,}, // 'S' 83
{0x01,0x01,0x7F,0x01,0x01,}, // 'T' 84
{0x3F,0x40,0x40,0x40,0x3F,}, // 'U' 85
{0x1F,0x20,0x40,0x20,0x1F,}, // 'V' 86
{0x3F,0x40,0x38,0x40,0x3F,}, // 'W' 87
{0x63,0x14,0x08,0x14,0x63,}, // 'X' 88
{0x07,0x08,0x70,0x08,0x07,}, // 'Y' 89
{0x61,0x51,0x49,0x45,0x43,}, // 'Z' 90
{0x00,0x7F,0x41,0x41,0x00,}, // '[' 91
{0x02,0x04,0x08,0x10,0x20,}, // '\' 92
{0x00,0x41,0x41,0x7F,0x00,}, // ']' 93
{0x04,0x02,0x01,0x02,0x04,}, // '^' 94
{0x40,0x40,0x40,0x40,0x40,}, // '_' 95
{0x00,0x01,0x02,0x04,0x00,}, // '`' 96
{0x20,0x54,0x54,0x54,0x78,}, // 'a' 97
{0x7F,0x48,0x44,0x44,0x38,}, // 'b' 98
{0x38,0x44,0x44,0x44,0x20,}, // 'c' 99
{0x38,0x44,0x44,0x48,0x3F,}, // 'd' 100
{0x38,0x54,0x54,0x54,0x18,}, // 'e' 101
{0x08,0x7E,0x09,0x01,0x02,}, // 'f' 102
{0x0C,0x52,0x52,0x52,0x3E,}, // 'g' 103
{0x7F,0x08,0x04,0x04,0x78,}, // 'h' 104
{0x00,0x44,0x7D,0x40,0x00,}, // 'i' 105
{0x20,0x40,0x45,0x3C,0x00,}, // 'j' 106
{0x7F,0x10,0x28,0x44,0x00,}, // 'k' 107
{0x00,0x41,0x7F,0x40,0x00,}, // 'l' 108
{0x7C,0x04,0x18,0x04,0x78,}, // 'm' 109
{0x7C,0x08,0x04,0x04,0x78,}, // 'n' 110
{0x38,0x44,0x44,0x44,0x38,}, // 'o' 111
{0x7C,0x14,0x14,0x14,0x08,}, // 'p' 112
{0x08,0x14,0x14,0x18,0x7C,}, // 'q' 113
{0x7C,0x08,0x04,0x04,0x08,}, // 'r' 114
{0x48,0x54,0x54,0x54,0x20,}, // 's' 115
{0x04,0x3F,0x44,0x40,0x20,}, // 't' 116
{0x3C,0x40,0x40,0x20,0x7C,}, // 'u' 117
{0x1C,0x20,0x40,0x20,0x1C,}, // 'v' 118
{0x3C,0x40,0x30,0x40,0x3C,}, // 'w' 119
{0x44,0x28,0x10,0x28,0x44,}, // 'x' 120
{0x0C,0x50,0x50,0x50,0x3C,}, // 'y' 121
{0x44,0x64,0x54,0x4C,0x44,}, // 'z' 122
{0x00,0x08,0x36,0x41,0x00,}, // '{' 123
{0x00,0x00,0x7F,0x00,0x00,}, // '|' 124
{0x00,0x41,0x36,0x08,0x00,}, // '}' 125
{0x10,0x08,0x08,0x10,0x08,}, // '~' 126
{0x08,0x1C,0x2A,0x08,0x08,} // <- 127
};
PCD8544::PCD8544(uint8_t dc_pin, uint8_t reset_pin, uint8_t cs_pin, uint8_t hardware_spi)
{
dc = dc_pin;
cs = cs_pin;
reset = reset_pin;
hardware_spi_num = hardware_spi;
if (hardware_spi_num > 2)
hardware_spi_num = 2;
#ifndef MAPLE
sdin = MOSI;
sclk = SCK;
#else
sdin = 11; // Change to maple names
sclk = 13;
if (hardware_spi_num == 2) {
sdin = 32;
sclk = 34;
}
#endif
}
PCD8544::PCD8544(uint8_t dc_pin, uint8_t reset_pin, uint8_t cs_pin, uint8_t sdin_pin, uint8_t sclk_pin)
{
dc = dc_pin;
cs = cs_pin;
reset = reset_pin;
sdin = sdin_pin;
sclk = sclk_pin;
hardware_spi_num = 0;
}
void PCD8544::begin(void)
{
pinMode(cs, OUTPUT);
pinMode(reset, OUTPUT);
pinMode(dc, OUTPUT);
pinMode(sdin, OUTPUT);
pinMode(sclk, OUTPUT);
if (hardware_spi_num > 0) {
pinMode(SS, OUTPUT); // To ensure master mode
SPCR |= (1<<SPE) | (1<<MSTR);
}
digitalWrite(reset, LOW);
delay(1);
digitalWrite(reset, HIGH);
// Extenden instructions and !powerdown