Pages

Sunday, November 13, 2016

ST7735 TFT Vertical Scrolling


This post shows how to make the ST7735 SPI TFT display vertically scrolling using PIC18F4550 microcontroller and CCS PIC C compiler.
The CCS C codes below are tested with versions 4.068 and 5.051.
For this project we need a driver for the ST7735 TFT display which can be found in the following post:
ST7735 SPI TFT Display Driver for CCS PIC C compiler
And the following post show how to interface ST7735 TFT display with PIC18F4550 microcontroller:
Interfacing PIC18F4550 with 1.8" TFT display
ST7735 TFT Vertical Scrolling with PIC18F4550 circuit:
The circuit schematic of all our examples below is as shown in the following image.
ST7735 SPI TFT vertical scrolling examples circuit
PIC18F4550 MCLR pin function is disabled in the code.
ST7735 TFT top to bottom and bottom to top vertical scrolling:
To make the TFT scrolls first we have to set the window that we would like to scroll, this is done using the following command:
setScrollDefinition(TFA, BFA, _scroll_direction);
Where TFA is top fixed area and BFA is bottom fixed area (in pixel)
_scroll_direction can be 0 or 1( 0 for to to bottom and 1 for bottom to top).
Example:
setScrollDefinition(40, 20, 1);
That makes the TFT display area from pixel 40 to pixel 140 (160 - 20) scrolls from bottom to top.
If TFA = BFA = 0 the whole screen is selected to scroll as shown in the example below.
Another command is required to make the TFT scrolls which is:
VerticalScroll(_vsp);
Where _vsp is an unsigned int8 number which controls the level of the scroll.
Example 2:
The following example shows how to make TFT scrolls from top to bottom:
Here TFT = BFA = 0.
/* PIC18F4550 microcontroller with ST7735R SPI TFT vertical scrolling example CCS C code
   ST7735 TFT display driver for CCS PIC C compiler is required
   http://ccspicc.blogspot.com/
   electronnote@gmail.com
*/

#define top_to_bottom  0
#define bottom_to_top  1
#define TFA  0                                        // Top Fixed Area 0 pixel
#define BFA  0                                        // Bottom Fixed Area 0 pixel

// TFT module connections
#define TFT_CS  PIN_D1
#define TFT_DC  PIN_D0
#define TFT_SPI_HARDWARE
// End TFT module connections

#include <18F4550.h>
#fuses NOMCLR HSPLL PLL2 CPUDIV1
#use delay(clock = 48000000)
#include <ST7735_TFT.c>
#use fast_io(D)

char *txt = "ST7735 TFT vertical  scrolling";
unsigned int8 scroll = 0;
void main(){
  TFT_BlackTab_Initialize();
  fillScreen(ST7735_BLACK);
  drawtext(0, 5, txt, ST7735_WHITE, ST7735_BLACK, 1);
  strcpy (txt, "Hello World!");
  drawtext(28, 100, txt, ST7735_CYAN, ST7735_BLACK, 1);
  setScrollDefinition(TFA, BFA, top_to_bottom);
  while(TRUE){
    VerticalScroll(scroll + TFA);
    scroll++;
    if(scroll >= (_height - TFA - BFA))
      scroll = 0;
    delay_ms(100);
  }
}
The following video shows the result:

If the line: setScrollDefinition(TFA, BFA, top_to_bottom); changed to:
setScrollDefinition(TFA, BFA, bottom to top);
then we will see the following result:

ST7735 TFT Vertical Scrolling example:
This is an other example for ST7735 TFT vertical scrolling.
In this example the scrolling direction is from bottom to top.
After every scrolling process the microcontroller deletes the data that may appear in the bottom of the screen by drawing a horizontal line.
/* PIC18F4550 microcontroller with ST7735R SPI TFT vertical scrolling example CCS C code
   ST7735 TFT display driver for CCS PIC C compiler is required
   http://ccspicc.blogspot.com/
   electronnote@gmail.com
*/

#define top_to_bottom  0
#define bottom_to_top  1
#define TFA  30                                       // Top Fixed Area 30 pixel
#define BFA  0                                        // Bottom Fixed Area 0 pixel

// TFT module connections
#define TFT_CS  PIN_D1
#define TFT_DC  PIN_D0
#define TFT_SPI_HARDWARE
// End TFT module connections

#include <18F4550.h>
#fuses NOMCLR HSPLL PLL2 CPUDIV1
#use delay(clock = 48000000)
#include <ST7735_TFT.c>
#use fast_io(D)

char *txt = "ST7735 TFT vertical  scrolling";
char *nbr = "  ";
unsigned int8 i, scroll = 0;
unsigned int16 number = 1;
void main(){
  TFT_BlackTab_Initialize();
  fillScreen(ST7735_BLACK);
  drawtext(0, 5, txt, ST7735_WHITE, ST7735_BLACK, 1);
  strcpy (txt, "Line:");
  for(i = 0; i < 13; i++){
  drawtext(0, i * 10 + 30, txt, ST7735_YELLOW, ST7735_BLACK, 1);
  sprintf(nbr,"%Lu",number);
  drawtext(40, i * 10 + 30, nbr, ST7735_GREEN, ST7735_BLACK, 1);
  number++;
  delay_ms(500);}
  setScrollDefinition(TFA, BFA, bottom_to_top);
  while(TRUE){
    for(i = 0; i < 10; i++){
      VerticalScroll(scroll + TFA);
      drawFastHLine(0, scroll + TFA, _width, ST7735_BLACK);            // Delete lines which may appear in bottom of TFT
      scroll++;
      delay_ms(100);
      if(scroll >= (_height - TFA))
        scroll = 0;
      }
      sprintf(nbr,"%Lu",number);
      if(scroll == 0){
        drawtext(0, 150, txt, ST7735_YELLOW, ST7735_BLACK, 1);
        drawtext(40, 150, nbr, ST7735_GREEN, ST7735_BLACK, 1);
      }
      else{
        drawtext(0, 20 + scroll, txt, ST7735_YELLOW, ST7735_BLACK, 1);
        drawtext(40, 20 + scroll, nbr, ST7735_GREEN, ST7735_BLACK, 1);
      }
    number++;
  }
}
Example video: