В целях самоуспокоения залил тестовый адафрутовский код, как ни в чем не бывало все корректно работает.
Отлично ! Пока ничего не трогайте !
На каком пине сейчас DC ?
dc - 8 пин ардуино или PB0 Atmega
SPI.h
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@arduino.cc>
* Copyright (c) 2014 by Paul Stoffregen <paul@pjrc.com> (Transaction API)
* Copyright (c) 2014 by Matthijs Kooijman <matthijs@stdin.nl> (SPISettings AVR)
* Copyright (c) 2014 by Andrew J. Kroll <xxxajk@gmail.com> (atomicity fixes)
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#ifndef _SPI_H_INCLUDED
#define _SPI_H_INCLUDED
#include <Arduino.h>
// SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
// usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
#define SPI_HAS_TRANSACTION 1
// SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
#define SPI_HAS_NOTUSINGINTERRUPT 1
// SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version.
// This way when there is a bug fix you can check this define to alert users
// of your code if it uses better version of this library.
// This also implies everything that SPI_HAS_TRANSACTION as documented above is
// available too.
#define SPI_ATOMIC_VERSION 1
// Uncomment this line to add detection of mismatched begin/end transactions.
// A mismatch occurs if other libraries fail to use SPI.endTransaction() for
// each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn
// on if any mismatch is ever detected.
//#define SPI_TRANSACTION_MISMATCH_LED 5
#ifndef LSBFIRST
#define LSBFIRST 0
#endif
#ifndef MSBFIRST
#define MSBFIRST 1
#endif
#define SPI_CLOCK_DIV4 0x00
#define SPI_CLOCK_DIV16 0x01
#define SPI_CLOCK_DIV64 0x02
#define SPI_CLOCK_DIV128 0x03
#define SPI_CLOCK_DIV2 0x04
#define SPI_CLOCK_DIV8 0x05
#define SPI_CLOCK_DIV32 0x06
#define SPI_MODE0 0x00
#define SPI_MODE1 0x04
#define SPI_MODE2 0x08
#define SPI_MODE3 0x0C
#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
// define SPI_AVR_EIMSK for AVR boards with external interrupt pins
#if defined(EIMSK)
#define SPI_AVR_EIMSK EIMSK
#elif defined(GICR)
#define SPI_AVR_EIMSK GICR
#elif defined(GIMSK)
#define SPI_AVR_EIMSK GIMSK
#endif
class SPISettings {
public:
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
if (__builtin_constant_p(clock)) {
init_AlwaysInline(clock, bitOrder, dataMode);
} else {
init_MightInline(clock, bitOrder, dataMode);
}
}
SPISettings() {
init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0);
}
private:
void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
init_AlwaysInline(clock, bitOrder, dataMode);
}
void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
__attribute__((__always_inline__)) {
// Clock settings are defined as follows. Note that this shows SPI2X
// inverted, so the bits form increasing numbers. Also note that
// fosc/64 appears twice
// SPR1 SPR0 ~SPI2X Freq
// 0 0 0 fosc/2
// 0 0 1 fosc/4
// 0 1 0 fosc/8
// 0 1 1 fosc/16
// 1 0 0 fosc/32
// 1 0 1 fosc/64
// 1 1 0 fosc/64
// 1 1 1 fosc/128
// We find the fastest clock that is less than or equal to the
// given clock rate. The clock divider that results in clock_setting
// is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the
// slowest (128 == 2 ^^ 7, so clock_div = 6).
uint8_t clockDiv;
// When the clock is known at compiletime, use this if-then-else
// cascade, which the compiler knows how to completely optimize
// away. When clock is not known, use a loop instead, which generates
// shorter code.
if (__builtin_constant_p(clock)) {
if (clock >= F_CPU / 2) {
clockDiv = 0;
} else if (clock >= F_CPU / 4) {
clockDiv = 1;
} else if (clock >= F_CPU / 8) {
clockDiv = 2;
} else if (clock >= F_CPU / 16) {
clockDiv = 3;
} else if (clock >= F_CPU / 32) {
clockDiv = 4;
} else if (clock >= F_CPU / 64) {
clockDiv = 5;
} else {
clockDiv = 6;
}
} else {
uint32_t clockSetting = F_CPU / 2;
clockDiv = 0;
while (clockDiv < 6 && clock < clockSetting) {
clockSetting /= 2;
clockDiv++;
}
}
// Compensate for the duplicate fosc/64
if (clockDiv == 6)
clockDiv = 7;
// Invert the SPI2X bit
clockDiv ^= 0x1;
// Pack into the SPISettings class
spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |
(dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK);
spsr = clockDiv & SPI_2XCLOCK_MASK;
}
uint8_t spcr;
uint8_t spsr;
friend class SPIClass;
};
class SPIClass {
public:
// Initialize the SPI library
static void begin();
// If SPI is used from within an interrupt, this function registers
// that interrupt with the SPI library, so beginTransaction() can
// prevent conflicts. The input interruptNumber is the number used
// with attachInterrupt. If SPI is used from a different interrupt
// (eg, a timer), interruptNumber should be 255.
static void usingInterrupt(uint8_t interruptNumber);
// And this does the opposite.
static void notUsingInterrupt(uint8_t interruptNumber);
// Note: the usingInterrupt and notUsingInterrupt functions should
// not to be called from ISR context or inside a transaction.
// For details see:
// https://github.com/arduino/Arduino/pull/2381
// https://github.com/arduino/Arduino/pull/2449
// Before using SPI.transfer() or asserting chip select pins,
// this function is used to gain exclusive access to the SPI bus
// and configure the correct settings.
inline static void beginTransaction(SPISettings settings) {
if (interruptMode > 0) {
uint8_t sreg = SREG;
noInterrupts();
#ifdef SPI_AVR_EIMSK
if (interruptMode == 1) {
interruptSave = SPI_AVR_EIMSK;
SPI_AVR_EIMSK &= ~interruptMask;
SREG = sreg;
} else
#endif
{
interruptSave = sreg;
}
}
#ifdef SPI_TRANSACTION_MISMATCH_LED
if (inTransactionFlag) {
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
}
inTransactionFlag = 1;
#endif
SPCR = settings.spcr;
SPSR = settings.spsr;
}
// Write to the SPI bus (MOSI pin) and also receive (MISO pin)
inline static uint8_t transfer(uint8_t data) {
Serial.println(digitalRead(8)?"data":"cmd");
Serial.println(data, HEX);
SPDR = data;
/*
* The following NOP introduces a small delay that can prevent the wait
* loop form iterating when running at the maximum speed. This gives
* about 10% more speed, even if it seems counter-intuitive. At lower
* speeds it is unnoticed.
*/
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ; // wait
return SPDR;
}
inline static uint16_t transfer16(uint16_t data) {
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
in.val = data;
Serial.println(digitalRead(8)?"data":"cmd");
Serial.println(data, HEX);
if (!(SPCR & _BV(DORD))) {
SPDR = in.msb;
asm volatile("nop"); // See transfer(uint8_t) function
while (!(SPSR & _BV(SPIF))) ;
out.msb = SPDR;
SPDR = in.lsb;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ;
out.lsb = SPDR;
} else {
SPDR = in.lsb;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ;
out.lsb = SPDR;
SPDR = in.msb;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ;
out.msb = SPDR;
}
return out.val;
}
inline static void transfer(void *buf, size_t count) {
if (count == 0) return;
uint8_t *p = (uint8_t *)buf;
Serial.println(digitalRead(8)?"data":"cmd");
Serial.println(*p, HEX);
SPDR = *p;
while (--count > 0) {
uint8_t out = *(p + 1);
while (!(SPSR & _BV(SPIF))) ;
uint8_t in = SPDR;
SPDR = out;
*p++ = in;
}
while (!(SPSR & _BV(SPIF))) ;
*p = SPDR;
}
// After performing a group of transfers and releasing the chip select
// signal, this function allows others to access the SPI bus
inline static void endTransaction(void) {
#ifdef SPI_TRANSACTION_MISMATCH_LED
if (!inTransactionFlag) {
pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH);
}
inTransactionFlag = 0;
#endif
if (interruptMode > 0) {
#ifdef SPI_AVR_EIMSK
uint8_t sreg = SREG;
#endif
noInterrupts();
#ifdef SPI_AVR_EIMSK
if (interruptMode == 1) {
SPI_AVR_EIMSK = interruptSave;
SREG = sreg;
} else
#endif
{
SREG = interruptSave;
}
}
}
// Disable the SPI bus
static void end();
// This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings.
inline static void setBitOrder(uint8_t bitOrder) {
if (bitOrder == LSBFIRST) SPCR |= _BV(DORD);
else SPCR &= ~(_BV(DORD));
}
// This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings.
inline static void setDataMode(uint8_t dataMode) {
SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode;
}
// This function is deprecated. New applications should use
// beginTransaction() to configure SPI settings.
inline static void setClockDivider(uint8_t clockDiv) {
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK);
}
// These undocumented functions should not be used. SPI.transfer()
// polls the hardware flag which is automatically cleared as the
// AVR responds to SPI's interrupt
inline static void attachInterrupt() { SPCR |= _BV(SPIE); }
inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); }
private:
static uint8_t initialized;
static uint8_t interruptMode; // 0=none, 1=mask, 2=global
static uint8_t interruptMask; // which interrupts to mask
static uint8_t interruptSave; // temp storage, to restore state
#ifdef SPI_TRANSACTION_MISMATCH_LED
static uint8_t inTransactionFlag;
#endif
};
extern SPIClass SPI;
#endif
Найдите SPI.h и переименуйте\сохраните\скопируйте его. А код вставьте в новый SPI.h
Где-нибудь в setup работающего примера добавьте Serial.begin(нужная скорость);
После компиляции и запуска весь вывод должен скопироваться в консоль.
Все сказанное проделал, я такое уже делал из кода, логировал пересылку команд и данных, вроде все соответствует. Текущий вывод таков:
Спойлер
cmd
1
cmd
36
data
0
cmd
3A
data
5
cmd
20
cmd
11
cmd
2A
data
0
data
0
data
1
data
17
cmd
2B
data
0
data
0
data
0
data
EF
cmd
2C
0
data
0
data
0
data
0
…
…
итд
По CASET и RASET, вижу что выводятся корректные данные разрешения экрана 0 0 1 17 и 0 0 0 EF (279 и 239 соответственно), но не стоит ли поменять местами ysize и xsize?
Command2(CASET, yoff, yoff + ysize - 1);
Command2(RASET, xoff, xoff + xsize - 1);
Теперь этот код:
Спойлер
//#define SoftSPI
#ifndef SoftSPI
#include <SPI.h>
#endif
int const dc = 0;
int const mosi = 3;
int const sck = 5;
int const cs = 1;
// Classic ATtiny port manipulations - assumes all pins in same port
#define PORT_TOGGLE(x) PINB = (x)
#define PORT_LOW(x) PORTB = PORTB & ~((x));
#define PORT_HIGH(x) PORTB = PORTB | ((x))
#define PORT_OUTPUT(x) DDRB = (x)
// Display parameters - uncomment the line for the one you want to use
// Adafruit 1.44" 128x128 display
// int const xsize = 128, ysize = 128, xoff = 2, yoff = 1, invert = 0, rotate = 3, bgr = 1;
// AliExpress 1.44" 128x128 display
// int const xsize = 128, ysize = 128, xoff = 2, yoff = 1, invert = 0, rotate = 3, bgr = 1;
// Adafruit 0.96" 160x80 display
// int const xsize = 160, ysize = 80, xoff = 0, yoff = 24, invert = 0, rotate = 6, bgr = 0;
// AliExpress 0.96" 160x80 display
// int const xsize = 160, ysize = 80, xoff = 1, yoff = 26, invert = 1, rotate = 0, bgr = 1;
// Adafruit 1.8" 160x128 display
int const xsize = 280, ysize = 240, xoff = 0, yoff = 0, invert = 0, rotate = 0, bgr = 0;
// AliExpress 1.8" 160x128 display (red PCB)
// int const xsize = 160, ysize = 128, xoff = 0, yoff = 0, invert = 0, rotate = 0, bgr = 1;
// AliExpress 1.8" 160x128 display (blue PCB)
// int const xsize = 160, ysize = 128, xoff = 0, yoff = 0, invert = 0, rotate = 6, bgr = 0;
// Adafruit 1.14" 240x135 display
// int const xsize = 240, ysize = 135, xoff = 40, yoff = 53, invert = 1, rotate = 6, bgr = 0;
// AliExpress 1.14" 240x135 display
// int const xsize = 240, ysize = 135, xoff = 40, yoff = 52, invert = 1, rotate = 0, bgr = 0;
// Adafruit 1.3" 240x240 display
// int const xsize = 240, ysize = 240, xoff = 0, yoff = 80, invert = 1, rotate = 5, bgr = 0;
// Adafruit 1.54" 240x240 display
// int const xsize = 240, ysize = 240, xoff = 0, yoff = 80, invert = 1, rotate = 5, bgr = 0;
// AliExpress 1.54" 240x240 display
// int const xsize = 240, ysize = 240, xoff = 0, yoff = 80, invert = 1, rotate = 5, bgr = 0;
// Adafruit 1.9" 320x170 display
// int const xsize = 320, ysize = 170, xoff = 0, yoff = 35, invert = 1, rotate = 0, bgr = 0;
// AliExpress 1.9" 320x170 display
// int const xsize = 320, ysize = 170, xoff = 0, yoff = 35, invert = 1, rotate = 0, bgr = 0;
// Adafruit 1.47" 320x172 rounded rectangle display
// int const xsize = 320, ysize = 172, xoff = 0, yoff = 34, invert = 1, rotate = 0, bgr = 0;
// AliExpress 1.47" 320x172 rounded rectangle display
// int const xsize = 320, ysize = 172, xoff = 0, yoff = 34, invert = 1, rotate = 0, bgr = 0;
// Adafruit 2.0" 320x240 display
// int const xsize = 320, ysize = 240, xoff = 0, yoff = 0, invert = 1, rotate = 6, bgr = 0;
// AliExpress 2.0" 320x240 display
// int const xsize = 320, ysize = 240, xoff = 0, yoff = 0, invert = 1, rotate = 0, bgr = 0;
// Adafruit 2.2" 320x240 display
// int const xsize = 320, ysize = 240, xoff = 0, yoff = 0, invert = 0, rotate = 4, bgr = 1;
// AliExpress 2.4" 320x240 display
// int const xsize = 320, ysize = 240, xoff = 0, yoff = 0, invert = 0, rotate = 2, bgr = 1;
// Character set for text - stored in program memory
const uint8_t CharMap[96][6] PROGMEM = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00 },
{ 0x00, 0x07, 0x00, 0x07, 0x00, 0x00 },
{ 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00 },
{ 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00 },
{ 0x23, 0x13, 0x08, 0x64, 0x62, 0x00 },
{ 0x36, 0x49, 0x56, 0x20, 0x50, 0x00 },
{ 0x00, 0x08, 0x07, 0x03, 0x00, 0x00 },
{ 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00 },
{ 0x00, 0x41, 0x22, 0x1C, 0x00, 0x00 },
{ 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00 },
{ 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00 },
{ 0x00, 0x80, 0x70, 0x30, 0x00, 0x00 },
{ 0x08, 0x08, 0x08, 0x08, 0x08, 0x00 },
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },
{ 0x20, 0x10, 0x08, 0x04, 0x02, 0x00 },
{ 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00 },
{ 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00 },
{ 0x72, 0x49, 0x49, 0x49, 0x46, 0x00 },
{ 0x21, 0x41, 0x49, 0x4D, 0x33, 0x00 },
{ 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00 },
{ 0x27, 0x45, 0x45, 0x45, 0x39, 0x00 },
{ 0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00 },
{ 0x41, 0x21, 0x11, 0x09, 0x07, 0x00 },
{ 0x36, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x46, 0x49, 0x49, 0x29, 0x1E, 0x00 },
{ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00 },
{ 0x00, 0x40, 0x34, 0x00, 0x00, 0x00 },
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x00 },
{ 0x00, 0x41, 0x22, 0x14, 0x08, 0x00 },
{ 0x02, 0x01, 0x59, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00 },
{ 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00 },
{ 0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00 },
{ 0x3E, 0x41, 0x41, 0x51, 0x73, 0x00 },
{ 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00 },
{ 0x00, 0x41, 0x7F, 0x41, 0x00, 0x00 },
{ 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00 },
{ 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00 },
{ 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00 },
{ 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00 },
{ 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00 },
{ 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00 },
{ 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00 },
{ 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00 },
{ 0x26, 0x49, 0x49, 0x49, 0x32, 0x00 },
{ 0x03, 0x01, 0x7F, 0x01, 0x03, 0x00 },
{ 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00 },
{ 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00 },
{ 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00 },
{ 0x63, 0x14, 0x08, 0x14, 0x63, 0x00 },
{ 0x03, 0x04, 0x78, 0x04, 0x03, 0x00 },
{ 0x61, 0x59, 0x49, 0x4D, 0x43, 0x00 },
{ 0x00, 0x7F, 0x41, 0x41, 0x41, 0x00 },
{ 0x02, 0x04, 0x08, 0x10, 0x20, 0x00 },
{ 0x00, 0x41, 0x41, 0x41, 0x7F, 0x00 },
{ 0x04, 0x02, 0x01, 0x02, 0x04, 0x00 },
{ 0x40, 0x40, 0x40, 0x40, 0x40, 0x00 },
{ 0x00, 0x03, 0x07, 0x08, 0x00, 0x00 },
{ 0x20, 0x54, 0x54, 0x78, 0x40, 0x00 },
{ 0x7F, 0x28, 0x44, 0x44, 0x38, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x28, 0x00 },
{ 0x38, 0x44, 0x44, 0x28, 0x7F, 0x00 },
{ 0x38, 0x54, 0x54, 0x54, 0x18, 0x00 },
{ 0x00, 0x08, 0x7E, 0x09, 0x02, 0x00 },
{ 0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00 },
{ 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00 },
{ 0x20, 0x40, 0x40, 0x3D, 0x00, 0x00 },
{ 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00 },
{ 0x00, 0x41, 0x7F, 0x40, 0x00, 0x00 },
{ 0x7C, 0x04, 0x78, 0x04, 0x78, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x78, 0x00 },
{ 0x38, 0x44, 0x44, 0x44, 0x38, 0x00 },
{ 0xFC, 0x18, 0x24, 0x24, 0x18, 0x00 },
{ 0x18, 0x24, 0x24, 0x18, 0xFC, 0x00 },
{ 0x7C, 0x08, 0x04, 0x04, 0x08, 0x00 },
{ 0x48, 0x54, 0x54, 0x54, 0x24, 0x00 },
{ 0x04, 0x04, 0x3F, 0x44, 0x24, 0x00 },
{ 0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00 },
{ 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00 },
{ 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00 },
{ 0x44, 0x28, 0x10, 0x28, 0x44, 0x00 },
{ 0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00 },
{ 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00 },
{ 0x00, 0x08, 0x36, 0x41, 0x00, 0x00 },
{ 0x00, 0x00, 0x77, 0x00, 0x00, 0x00 },
{ 0x00, 0x41, 0x36, 0x08, 0x00, 0x00 },
{ 0x00, 0x06, 0x09, 0x06, 0x00, 0x00 }, // degree symbol = '~'
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }
};
// TFT colour display **********************************************
int const CASET = 0x2A; // Define column address
int const RASET = 0x2B; // Define row address
int const RAMWR = 0x2C; // Write to display RAM
int const White = 0xFFFF;
int const Black = 0;
// Current plot position and colours
int xpos, ypos;
int fore = White;
int back = Black;
int scale = 1; // Text scale
// Send a byte to the display
void Data (uint8_t d) {
#ifndef SoftSPI
SPI.transfer(d);
#else
for (uint8_t bit = 0x80; bit; bit >>= 1) {
PORT_TOGGLE(1 << sck);
if (d & bit) PORT_HIGH(1 << mosi); else PORT_LOW(1 << mosi);
PORT_TOGGLE(1 << sck);
}
#endif
}
// Send a command to the display
void Command (uint8_t c) {
PORT_TOGGLE(1 << dc);
Data(c);
PORT_TOGGLE(1 << dc);
}
// Send a command followed by two data words
void Command2 (uint8_t c, uint16_t d1, uint16_t d2) {
PORT_TOGGLE(1 << dc);
Data(c);
PORT_TOGGLE(1 << dc);
Data(d1 >> 8); Data(d1); Data(d2 >> 8); Data(d2);
}
void InitDisplay () {
#ifndef SoftSPI
SPI.begin();
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
#endif
PORT_OUTPUT(1 << dc | 1 << cs | 1 << mosi | 1 << sck); // All outputs
PORT_HIGH(1 << dc | 1 << cs); // Outputs high
PORT_TOGGLE(1 << cs);
Command(0x01); // Software reset
delay(250); // delay 250 ms
Command(0x36); Data(rotate << 5 | bgr << 3); // Set orientation and rgb/bgr
Command(0x3A); Data(0x05); // Set color mode - 16-bit color
Command(0x20 + invert); // Invert
Command(0x11); // Out of sleep mode
delay(150);
PORT_TOGGLE(1 << cs);
}
void DisplayOn () {
PORT_TOGGLE(1 << cs);
Command(0x29); // Display on
delay(150);
PORT_TOGGLE(1 << cs);
}
void ClearDisplay () {
PORT_TOGGLE(1 << cs);
Command2(CASET, yoff, yoff + ysize - 1);
Command2(RASET, xoff, xoff + xsize - 1);
//Command(0x3A); Data(0x03); // 12-bit colour
Command(RAMWR); // Leaves mosi low
#ifndef SoftSPI
for (long i = 0; i < (long)xsize * ysize; i++) SPI.transfer16(0);
#else
for (int i = 0; i < xsize * 4; i++) {
for (int j = 0; j < ysize * 4; j++) {
PORT_TOGGLE(1 << sck);
PORT_TOGGLE(1 << sck);
}
}
#endif
//Command(0x3A); Data(0x05); // Back to 16-bit colour
PORT_TOGGLE(1 << cs);
}
unsigned int Colour (int r, int g, int b) {
return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3;
}
// Move current plot position to x,y
void MoveTo (int x, int y) {
xpos = x; ypos = y;
}
// Plot point at x,y
void PlotPoint (int x, int y) {
PORT_TOGGLE(1 << cs);
Command2(CASET, yoff + y, yoff + y);
Command2(RASET, xoff + x, xoff + x);
Command(RAMWR); Data(fore >> 8); Data(fore & 0xff);
PORT_TOGGLE(1 << cs);
}
// Draw a line to x,y
void DrawTo (int x, int y) {
int sx, sy, e2, err;
int dx = abs(x - xpos);
int dy = abs(y - ypos);
if (xpos < x) sx = 1; else sx = -1;
if (ypos < y) sy = 1; else sy = -1;
err = dx - dy;
for (;;) {
PlotPoint(xpos, ypos);
if (xpos == x && ypos == y) return;
e2 = err << 1;
if (e2 > -dy) {
err = err - dy;
xpos = xpos + sx;
}
if (e2 < dx) {
err = err + dx;
ypos = ypos + sy;
}
}
}
void FillRect (int w, int h) {
PORT_TOGGLE(1 << cs);
Command2(CASET, ypos + yoff, ypos + yoff + h - 1);
Command2(RASET, xpos + xoff, xpos + xoff + w - 1);
Command(RAMWR);
uint8_t hi = fore >> 8;
uint8_t lo = fore & 0xff;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Data(hi); Data(lo);
}
}
PORT_TOGGLE(1 << cs);
}
void DrawRect (int w, int h) {
int x1 = xpos, y1 = ypos;
FillRect(w - 1, 1); MoveTo(x1, y1 + 1);
FillRect(1, h - 1); MoveTo(x1 + 1, y1 + h - 1);
FillRect(w - 1, 1); MoveTo(x1 + w - 1, y1);
FillRect(1, h - 1);
xpos = x1; ypos = y1;
}
void FillCircle (int radius) {
int x1 = xpos, y1 = ypos, dx = 1, dy = 1;
int x = radius - 1, y = 0;
int err = dx - (radius << 1);
while (x >= y) {
MoveTo(x1 - x, y1 + y); FillRect(x << 1, 1);
MoveTo(x1 - y, y1 + x); FillRect(y << 1, 1);
MoveTo(x1 - y, y1 - x); FillRect(y << 1, 1);
MoveTo(x1 - x, y1 - y); FillRect(x << 1, 1);
if (err > 0) {
x = x - 1; dx = dx + 2;
err = err - (radius << 1) + dx;
} else {
y = y + 1; err = err + dy;
dy = dy + 2;
}
}
xpos = x1; ypos = y1;
}
void DrawCircle (int radius) {
int x1 = xpos, y1 = ypos, dx = 1, dy = 1;
int x = radius - 1, y = 0;
int err = dx - (radius << 1);
while (x >= y) {
PlotPoint(x1 - x, y1 + y); PlotPoint(x1 + x, y1 + y);
PlotPoint(x1 - y, y1 + x); PlotPoint(x1 + y, y1 + x);
PlotPoint(x1 - y, y1 - x); PlotPoint(x1 + y, y1 - x);
PlotPoint(x1 - x, y1 - y); PlotPoint(x1 + x, y1 - y);
if (err > 0) {
x = x - 1; dx = dx + 2;
err = err - (radius << 1) + dx;
} else {
y = y + 1; err = err + dy;
dy = dy + 2;
}
}
}
// Plot an ASCII character with bottom left corner at x,y
void PlotChar (char c) {
int colour;
PORT_TOGGLE(1 << cs);
Command2(CASET, yoff + ypos, yoff + ypos + 8 * scale - 1);
Command2(RASET, xoff + xpos, xoff + xpos + 6 * scale - 1);
Command(RAMWR);
for (int xx = 0; xx < 6; xx++) {
int bits = pgm_read_byte(&CharMap[c - 32][xx]);
for (int xr = 0; xr < scale; xr++) {
for (int yy = 0; yy < 8; yy++) {
if (bits >> (7 - yy) & 1) colour = fore; else colour = back;
for (int yr = 0; yr < scale; yr++) {
Data(colour >> 8); Data(colour & 0xFF);
}
}
}
}
PORT_TOGGLE(1 << cs);
xpos = xpos + 6 * scale;
}
// Plot text starting at the current plot position
void PlotText(PGM_P p) {
while (1) {
char c = pgm_read_byte(p++);
if (c == 0) return;
PlotChar(c);
}
}
void PlotInt(int n) {
bool lead = false;
for (int d = 10000; d > 0; d = d / 10) {
char j = (n / d) % 10;
if (j != 0 || lead || d == 1) {
PlotChar(j + '0');
lead = true;
}
}
}
void TestChart () {
int sf = fore;
fore = Colour(255, 0, 0);
DrawRect(xsize, ysize);
scale = 8;
MoveTo((xsize - 40) / 2, (ysize - 64) / 2); PlotChar('F');
scale = 1;
fore = sf;
}
// Demos **********************************************
void BarChart () {
int x0 = 0, y0 = 0, w = xsize, h = ysize, x1 = 15, y1 = 11;
MoveTo(x0 + (w - x1 - 90) / 2 + x1, y0 + h - 8); PlotText(PSTR("Sensor Readings"));
// Horizontal axis
int xinc = (w - x1) / 20;
MoveTo(x0 + x1, y0 + y1); DrawTo(x0 + w - 1, y0 + y1);
for (int i = 0; i <= 20; i = i + 4) {
int mark = x1 + i * xinc;
MoveTo(x0 + mark, y0 + y1); DrawTo(x0 + mark, y0 + y1 - 2);
// Draw histogram
if (i != 20) {
int bar = xinc * 4 / 3;
for (int b = 2; b >= 0; b--) {
fore = Colour(255, 127 * b, 0); // Red, Orange, Yellow
MoveTo(x0 + mark + bar * b - b + 1, y0 + y1 + 1); FillRect(bar, 5 + random(h - y1 - 20));
}
fore = White;
}
if (i > 9) MoveTo(x0 + mark - 7, y0 + y1 - 11); else MoveTo(x0 + mark - 3, y0 + y1 - 11);
PlotInt(i);
}
// Vertical axis
int yinc = (h - y1) / 20;
MoveTo(x0 + x1, y0 + y1); DrawTo(x0 + x1, y0 + h - 1);
for (int i = 0; i <= 20; i = i + 5) {
int mark = y1 + i * yinc;
MoveTo(x0 + x1, y0 + mark); DrawTo(x0 + x1 - 2, y0 + mark);
if (i > 9) MoveTo(x0 + x1 - 15, y0 + mark - 4); else MoveTo(x0 + x1 - 9, y0 + mark - 4);
PlotInt(i);
}
}
void Waterfall () {
int x0 = 0, y0 = 0, w = xsize, h = ysize, x1 = 15, y1 = 11;
int factor = 5160 / h * 10;
MoveTo(x0 + (w - x1 - 60) / 2 + x1, y0 + h - 8); PlotText(PSTR("Luminance"));
// Horizontal axis
int xinc = (w - x1 - 15) / 30;
MoveTo(x0 + x1, y0 + y1); DrawTo(x0 + x1 + xinc * 20, y0 + y1);
for (int i = 0; i <= 20; i = i + 5) {
int mark = x1 + i * xinc;
MoveTo(x0 + mark, y0 + y1); DrawTo(x0 + mark, y0 + y1 - 2);
if (i > 9) MoveTo(x0 + mark - 7, y0 + y1 - 11); else MoveTo(x0 + mark - 3, y0 + y1 - 11);
PlotInt(i);
}
// Vertical axis
int yinc = (h - y1) / 20;
MoveTo(x0 + x1, y0 + y1); DrawTo(x0 + x1, y0 + h - 1);
for (int i = 0; i <= 20; i = i + 5) {
int mark = y1 + i * yinc;
MoveTo(x0 + x1, y0 + mark); DrawTo(x0 + x1 - 2, y0 + mark);
if (i > 9) MoveTo(x0 + x1 - 15, y0 + mark - 4); else MoveTo(x0 + x1 - 9, y0 + mark - 4);
PlotInt(i);
}
// Diagonal axis
yinc = xinc / 2;
// MoveTo(x0+x1, y0+y1); DrawTo(x0+x1+10*xinc, y0+y1+10*xinc);
MoveTo(x0 + x1 + 20 * xinc, y0 + y1); DrawTo(x0 + x1 + 30 * xinc, y0 + y1 + 10 * xinc);
for (int i = 0; i <= 20; i = i + 5) {
MoveTo(x0 + x1 + 20 * xinc + i * xinc / 2, y0 + y1 + i * xinc / 2);
DrawTo(x0 + x1 + 20 * xinc + i * xinc / 2 + 3, y0 + y1 + i * xinc / 2);
MoveTo(x0 + x1 + 20 * xinc + i * xinc / 2 + 6, y0 + y1 + i * xinc / 2 - 4); PlotInt(i);
}
// Plot data
for (int y = 20; y >= 0; y--) {
for (int i = 0; i <= 20; i++) {
int fn0 = 180 - (i - 10) * (i - 10) - (y - 10) * (y - 10);
int fn1 = 180 - (i + 1 - 10) * (i + 1 - 10) - (y - 10) * (y - 10);
fore = Colour(255, 255, 0);
MoveTo(x0 + x1 + y * yinc + i * xinc, y0 + y1 + y * yinc + fn0 * fn0 / factor);
DrawTo(x0 + x1 + y * yinc + (i + 1)*xinc, y0 + y1 + y * yinc + fn1 * fn1 / factor);
fore = White;
}
}
}
// Setup **********************************************
void setup() {
InitDisplay();
ClearDisplay();
DisplayOn();
MoveTo(0, 0);
TestChart();
}
void loop () {
BarChart();
Waterfall();
for (;;);
}
Только вставьте Serial.begin(нужная скорость); в setup…
Он тоже плюнет в консоль весь вывод …
Все команды в принципе совпадают. Данные я подправил под разрешение вашего дисплея.
До одной команды не дошло - 0x29 Display on, но не думаю что она другая …
Вывод:
Спойлер
1
cmd
36
data
0
cmd
3A
data
5
cmd
20
cmd
11
cmd
2A
data
0
data
0
data
0
data
EF
cmd
2B
data
0
data
0
data
1
data
17
cmd
2C
data
0
data
0
data
0
data
0
data
0
…
…
Да, вывод в консоль очень замедляет код, я пока ни разу не дождался завершения) Но я тоже не вижу причин быть 0x29 другим)
Всё совпадает.
Возвращайте родной SPI.h.
Может на 3.3В попробовать?
С Adafruit же работает … и тут будет …
Дык к 3.3 и подключено, вот и резисторы плечи делителя стоят. Я уже и мультиметром все проверял, соответствует.
Командир, а выход из сна стоит после установки регистров, это никак не влияет на установку этих регистров?
Так и Adafruit начала стирать не включив дисплей.
Пока так, ведь и пинборд отстой, мой по крайней мере, на шинах питания потери напряжения до 0,6в, поэтому проводами напрямую законнектил. Потом разведу плату как разберусь с работой этого дисплея.
Что с родной SPI.h ?
Все вернул на место
Картинка появилась ?
неа((( Этому должно быть простое объяснение. А у вас нет случайно 7789 или 7735?