Giter Club home page Giter Club logo

raspberry-ili9340spi's Introduction

Raspberry-ili9340spi

ILI9340 SPI TFT Library for Raspberry Pi.
This may works with other boards like OrangePi/NanoPi.

You can show a chart to ILI9340/ILI9341/ILI9163C/ST7735 SPI TFT.
You can choose bmc2835 library/WiringPi library.

I tested these TFT.
1.44 inch 128x128 ST7735
1.44 inch 128x128 ILI9163C
1.8 inch 128x160 ST7735
2.2 inch 240x320 ILI9340
2.4 inch 240x320 ILI9341
2.4 inch 240x320 ILI9341

This project can be built with either:

  • Build using bcm2835 library
  • Build using Hardware SPI of the WiringPi library
  • Build using Software SPI of the WiringPi library

Wirering

TFT GPIO Header
VCC -- 3.3V *4
GND -- GND
CS -- Pin#24(SPI CS0) *2 *3
RES -- Pin#12 *1
D/C -- Pin#11 *1
MOSI -- Pin#19(SPI MOSI) *3
SCK -- Pin#23(SPI SCLK) *3
LED -- 3.3V *4
MISO -- N/C

(*1) You can change it to any pin by changing source.

(*2) You can use CS1 by specifying compilation flags.

(*3) For Software SPI, you can change it to any pin by changing source.

(*4) SPI TFTs require a lot of current.
If it is supplied from the Raspberry Pi's 3.3V pin, it may run out of current.
Use the 5V pin and the regulator to power it and it will be stable.
I used AMS1117.


Build using bcm2835 library

RPi Only, Very fast

Install bcm2835 library

wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.56.tar.gz
tar zxvf bcm2835-1.56.tar.gz
cd bcm2835-1.56
./configure
make
sudo make check
sudo make install

* This tool require 1.56 or later.
* Because this tool uses bcm2835_spi_write.

Using other GPIO

You can change GPIO to any pin by changing here.

#ifdef BCM
#include <bcm2835.h>
#define D_C 17  // BCM IO17=Pin#11
#define RES 18  // BCM IO18=Pin#12
#endif

Using SPI0

Use Pin#24 as ChipSelect.

cd $HOME   
git clone https://github.com/nopnop2002/Raspberry-ili9340spi
cd Raspberry-ili9340spi
make lib
cc -o demo demo.c fontx.c ili9340.c jpeg.a png.a -lbcm2835 -lm -DBCM
sudo ./demo

Using SPI1

Use Pin#26 as ChipSelect.

cd $HOME   
git clone https://github.com/nopnop2002/Raspberry-ili9340spi
cd Raspberry-ili9340spi
make lib
cc -o demo demo.c fontx.c ili9340.c jpeg.a png.a -lbcm2835 -lm -DBCM -DSPI1
sudo ./demo

SPI bus speed for bcm2835

By default it uses 7.8125MHz on Rpi2, 12.5MHz on RPI3.
Can be changed at compile time.

  • -DSPI_SPEED16 : 15.625MHz on Rpi2, 25MHz on RPI3.
  • -DSPI_SPEED32 : 31.25MHz on Rpi2, 50MHz on RPI3.

50MHz is an overclock.

SPI Bus speed comparison

7.8125MHz 15.625MHz 31.25MHz
ColorBarTest 245 160 107
ArrowTest 286 193 155
LineTest 595 397 294
CircleTest 558 373 282
RoundRectTest 560 371 273
DirectionTest 304 199 153
HorizontalTest 430 283 209
VerticalTest 438 290 214
FillRectTest 448 285 221
ColorTest 500 327 234
JPEGTest 1607 1076 816
PNGTest 1742 1231 974

Build using Hardware SPI of the WiringPi library

WiringPi library initializes GPIO in one of the following ways:

  • int wiringPiSetup (void);
  • int wiringPiSetupGpio (void);
  • int wiringPiSetupPhys (void);
  • int wiringPiSetupSys (void);

This project by default uses the wiringPiSetup() function to initialize GPIOs.
Then use the wiringPiSPISetup() function to initialize the SPI.
If you use it on a board other than the RPI board, you may need to change the WiringPi number.

#define D_C   0 // wPi IO00=Pin#11
#define RES   1 // wPi IO01=Pin#12

As far as I know, there are these WiringPi libraries.

  • WiringPi for OrangePi
  • WiringPi for BananaPi
  • WiringPi for NanoPi
  • WiringPi for Pine-64

If you want to initialize GPIO with wiringPiSetupGpio(), Use the -DGPIO compilation flag.
In this case, use the following GPIOs.

#define D_C  17 // BCM IO17=Pin#11
#define RES  18 // BCM IO18=Pin#12

Using SPI0

Use Pin#24 as ChipSelect.

git clone https://github.com/nopnop2002/Raspberry-ili9340spi
cd Raspberry-ili9340spi
make lib
cc -o demo demo.c fontx.c ili9340.c jpeg.a png.a -lwiringPi -lm -pthread -DWPI
sudo ./demo

Using SPI1

Use Pin#26 as ChipSelect.

git clone https://github.com/nopnop2002/Raspberry-ili9340spi
cd Raspberry-ili9340spi
make lib
cc -o demo demo.c fontx.c ili9340.c jpeg.a png.a -lwiringPi -lm -pthread -DWPI -DSPI1
sudo ./demo

Note for OrangePi
Opi have only 1 SPI.
OPi-PC has SPI0 on pin 24.
OPi-ZERO has SPI1 on pin 24.

SPI bus speed for WiringPi

By default it uses 8MHz on all Rpi.
Can be changed at compile time.

  • -DSPI_SPEED16 : 16MHz on all Rpi.
  • -DSPI_SPEED32 : 32MHz on all Rpi.

SPI Bus speed comparison

8MHz 16MHz 32MHz
ColorBarTest 208 116 93
ArrowTest 389 296 239
LineTest 1800 1637 1560
CircleTest 1630 1481 1393
RoundRectTest 1633 1490 1412
DirectionTest 461 354 315
HorizontalTest 878 723 663
VerticalTest 909 769 702
FillRectTest 454 246 148
ColorTest 558 234 159
JPEGTest 6292 5862 5640
PNGTest 6439 5989 5803

Build using Software SPI of the WiringPi library

WiringPi library initializes GPIO in one of the following ways:

  • int wiringPiSetup (void);
  • int wiringPiSetupGpio (void);
  • int wiringPiSetupPhys (void);
  • int wiringPiSetupSys (void);

This project by default uses the wiringPiSetup() function to initialize GPIOs.
Then use the wiringPiSPISetup() function to initialize the SPI.
If you use it on a board other than the RPI board, you may need to change the WiringPi number.

#define D_C   0 // wPi IO00=Pin#11
#define RES   1 // wPi IO01=Pin#12
#define MOSI 12 // wPi IO12=Pin#19
#define SCLK 14 // wPi IO14=Pin#23
#define CS   10 // wPi IO10=Pin#24

If you want to initialize GPIO with wiringPiSetupGpio(), Use the -DGPIO compilation flag.
In this case, use the following GPIOs.

#define D_C  17 // BCM IO17=Pin#11
#define RES  18 // BCM IO18=Pin#12
#define MOSI 10 // BCM IO10=Pin#19
#define SCLK 11 // BCM IO11=Pin#23
#define CS   24 // BCM IO24=Pin#24
git clone https://github.com/nopnop2002/Raspberry-ili9340spi
cd Raspberry-ili9340spi
make lib
cc -o demo demo.c fontx.c ili9340.c jpeg.a png.a -lwiringPi -lm -pthread -DWPI -DSOFT_SPI
sudo ./demo

TFT resolution and GRAM offset

TFT resolution is set to tft.conf.

If your TFT doesn't use a memory from 0th address in GRAM, It use GRAM offset which set to tft.conf.

#width=128 height=128
width=240 height=320
#width=240 height=400

#If TFT have GRAM offset
#offsetx=2
#offsety=1

ili9340-11 ili9340-12 ili9340-13 ili9340-14 ili9340-15 ili9340-16 ili9340-17 ili9340-18 ili9340-19 ili9340-20 ili9340-21

JPEG File
ili9340-JPEG
PNG File
ili9340-PNG


From left to right:
2.8" 240x320 ILI9341
2.4" 240x320 ILI9341
2.2" 240x320 ILI9340

ILI9341-A ILI9341-B ILI9341-C


From left to right:
2.2" 240x320 ILI9340
1.44" 128x128 ST7735
1.44" 128x128 ILI9163C
1.8" 128x160 ST7735

ili9163-1


XPT2046 Touch Screen

There is a TFT equipped with XPT2046.
XPT2046-3

A library of XPT2046 Touch Screen is included in this library.
I ported from here.

There is a TFT equipped with HR2046.
XPT2046 and HR2046 are very similar. But HR2046 does not work properly.
XPT2046-2

Wirering

TFT Rpi
VCC -- 3.3V
GND -- GND
CS -- Pin#24(SPI CS0)
RES -- Pin#12 (*1)
D/C -- Pin#11 (*1)
MOSI -- Pin#19(SPI MOSI) (*2)
SCK -- Pin#23(SPI SCLK) (*2)
LED -- 3.3V
MISO -- N/C
T_CLK -- Pin#23(SPI SCLK) (*2)
T_CS -- Pin#26(SPI CE1)
T_DIN -- Pin#19(SPI MOSI) (*2)
T_OUT -- Pin#21(SPI MISO) (*2)
T_IRQ -- Pin#22 (*1)

(*1) You can change any pin.

(*2) SPI is shared by TFT and XPT2046.


cc -o xpt xpt.c xpt2046.c -lbcm2835   
sudo ./xpt

If you touch screen, point will show.

Touch-11


cc -o touch touch.c fontx.c ili9340.c xpt2046.c -lbcm2835 -lm -DBCM
sudo ./touch

If you touch area, number will show.

Touch-12

raspberry-ili9340spi's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

raspberry-ili9340spi's Issues

PEN IRQ not responding

Values are not updating with touch. When I remove pin 14 (IRQ) on display side the values update but only X. Y seems to be stuck close to 32768 so all bits on likely.
Is it likely I have a fault touch panel or chip? Wired as per your example setup.

The screen works ok though.

Segmentation Fault

Hello,

I'm using a Pi Zero W with a generic ili9340 display. I've got it wired as per your instructions in the README.md. I followed the directions that you have labeled just for the Raspberry Pi and did not receive any errors. during setup/compiling. When everything was done I ran:

./demo

from the Raspberry-ili9340spi directory and received the following error:

Your TFT resolution is 240 x 320.
Your TFT offsetx is 0.
Your TFT offsety is 0.
Segmentation fault

I looked through the issues and saw a recommendation to someone to use bmc2835 1.6.4, so I used the only Raspberry Pi instructions to use 1.64 instead. I still receive the same error.

Thank you

Question: own fonts

Hi,
thx for this great work. I'm using your library with an ili9341 tft and Pi Zero. Is there a chance to use own fonts with your code? How can I convert other fonts?

Thx and Br

Hartmut

Compile error's with options -Wall -Werror

src/ili9340.c:659:25: error: & has lower precedence than ==; == will be evaluated first [-Werror,-Wparentheses]
        if (h == (ph-2) & _FONT_UNDER_LINE_)
            ~~~~~~~~~~~~^
src/ili9340.c:659:25: note: place parentheses around the '==' expression to silence this warning
        if (h == (ph-2) & _FONT_UNDER_LINE_)
                        ^
            (          )
src/ili9340.c:659:25: note: place parentheses around the & expression to evaluate it first
        if (h == (ph-2) & _FONT_UNDER_LINE_)
                        ^
                 (                         )
src/ili9340.c:661:25: error: & has lower precedence than ==; == will be evaluated first [-Werror,-Wparentheses]
        if (h == (ph-1) & _FONT_UNDER_LINE_)
            ~~~~~~~~~~~~^
src/ili9340.c:661:25: note: place parentheses around the '==' expression to silence this warning
        if (h == (ph-1) & _FONT_UNDER_LINE_)
                        ^
            (          )
src/ili9340.c:661:25: note: place parentheses around the & expression to evaluate it first
        if (h == (ph-1) & _FONT_UNDER_LINE_)
                        ^
                 (                         )
src/ili9340.c:626:14: error: variable 'yss' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
  } else if (_FONT_DIRECTION_ == 3) {
             ^~~~~~~~~~~~~~~~~~~~~
src/ili9340.c:641:8: note: uninitialized use occurs here
  yy = yss;
       ^~~
src/ili9340.c:626:10: note: remove the 'if' if its condition is always true
  } else if (_FONT_DIRECTION_ == 3) {
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
src/ili9340.c:593:20: note: initialize the variable 'yss' to silence this warning
  uint16_t xss, yss;
                   ^
                    = 0
src/ili9340.c:626:14: error: variable 'xss' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
  } else if (_FONT_DIRECTION_ == 3) {
             ^~~~~~~~~~~~~~~~~~~~~
src/ili9340.c:642:8: note: uninitialized use occurs here
  xx = xss;
       ^~~
src/ili9340.c:626:10: note: remove the 'if' if its condition is always true
  } else if (_FONT_DIRECTION_ == 3) {
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
src/ili9340.c:593:15: note: initialize the variable 'xss' to silence this warning
  uint16_t xss, yss;
              ^
               = 0
src/ili9340.c:702:35: error: passing 'uint8_t *' (aka 'unsigned char *') to parameter of type 'const char *' converts between pointers to integer types with different sign [-Werror,-Wpointer-sign]
  spos = String2SJIS(utfs, strlen(utfs), sjis, 64);
                                  ^~~~
/usr/include/string.h:82:28: note: passing argument to parameter '__s' here
size_t   strlen(const char *__s);
                            ^
src/ili9340.c:719:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
6 errors generated.

Unable to open SPI device: No such file or directory

Try to use it on Orange Pi Zero with WiringPi.

I connect TFT to OPI zero, to SPI 1

TFT                                OPI
SDO(MISO)  -------->   SPI1_MISO/PA16
SCK             -------->    SPI1_CLK/PA14
SDI(MOSI)    -------->   SPI1_MOSI/PA15
DC               -------->   PA12
RESET           -------->   PA11
CS                -------->   PA13
VCC, LED     -------->    3.3v
GND            -------->    GND

set in ili9340.c
#define D_C  12
#define RES  11
#define C_S  13

make
cc -o demo demo.c fontx.c ili9340.c -lwiringPi -lm -DWPI

in /boot/orangepiEnv.txt add:

overlays=spi-jedec-nor spi-spidev 
param_spidev_spi_bus=0

run ./demo and get:

Your TFT resolution is 240 x 320.
Your TFT offsetx    is 0.
Your TFT offsety    is 0.
Opening device /dev/spidev0.0
Unable to open SPI device: No such file or directory

Please, help: what I did wrong? Thanks!

Display image

How would I display a locally saved image (.bmp for example) to the display. I'm running it over software spi of wiringPi.

Compatability update: Adafruit 2.2" TFT with ili9340c

More of a notification for anyone looking to use the same board than an issue. I was able to successfully use your library to get this adafruit display to work with no modifications and by using the suggested pin-outs in the read-me. Pi hardware was a raspberry pi zero 2 w

GND to GND
Vin to 3.3v
D/C to GPIO 2
Rst to GPIO 3
LCD CS to GPIO 8
MOSI to GPIO 10
SCK to GPIO 11
BL to 3.3v

Do not connect the SDCD or MISO as these are for the SD card slot on the back of the board and will probably need modification to the code to get them to work.

License

Hello,

I could not find a license in the repository.
Am I allowed to use this library in my own projects and are you going to add a license?

SD card

Is there a way to display images from the built in SD card slot?

Direct control of the LED pin 8

Objective is to control the LED to on/off. I used the following code additions, with the appropriate additions to the .h file:

#define LED 18 // GPIO pin 12
void LED_on(void) {
bcm2835_gpio_fsel(LED,BCM2835_GPIO_FSEL_OUTP); // LED
bcm2835_gpio_write(LED, HIGH);
}

void LED_off(void) {
bcm2835_gpio_fsel(LED,BCM2835_GPIO_FSEL_OUTP); // LED
bcm2835_gpio_write(LED, LOW);
}

Those changes compile successfully, along with the addition of the calls to turn the LED on at the start of the main segment in demo.c and at the end to turn the LED off. I plead guilty to little or no knowledge of the BCM2834, so I just used your existing code for the RES function. The result is now a segmentation fault with those changes in place, whereas it runs normally with the original code.

Any suggestions??

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.