CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

tft st7796s problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ramses Chacon



Joined: 15 Dec 2025
Posts: 4

View user's profile Send private message

tft st7796s problems
PostPosted: Mon Dec 15, 2025 5:53 pm     Reply with quote

I am having significant trouble getting an ST7796S TFT display module (SPI interface) to work with a Microchip PIC18F45K22 microcontroller.

📝 The Problem
The code compiles successfully, but when I program the PIC, the display remains blank. The only thing that works is the LED backlight, which is powered directly from the source, indicating the display unit has power, but no data or commands are being processed.

Code:
#include <18F45K22.h>
#FUSES NOWDT, NOPROTECT, NOLVP, PUT, BROWNOUT, XT
#use delay(clock=20000000)

#define TFT_CS    PIN_A5
#define TFT_DC    PIN_A4
#define TFT_RST   PIN_A3
#use spi(MASTER, SPI1, MODE=0, BITS=8, BAUD=8000000, STREAM=TFTSPI)

#include "tft_st7796s.c"   // el código de arriba

void main(){
   tft_init();

   while(TRUE){
      fillScreen(Color565(255,0,0));   // rojo
      delay_ms(800);
      fillScreen(Color565(0,255,0));   // verde
      delay_ms(800);
      fillScreen(Color565(0,0,255));   // azul
      delay_ms(800);
   }
}


Code:

#define _width   480
#define _height  320

// Comandos ST7796S
#define CMD_SWRESET   0x01
#define CMD_SLPOUT    0x11
#define CMD_DISPON    0x29
#define CMD_MADCTL    0x36
#define CMD_COLMOD    0x3A
#define CMD_CASET     0x2A
#define CMD_RASET     0x2B
#define CMD_RAMWR     0x2C

void write_command(int8 cmd){
   output_low(TFT_CS);
   output_low(TFT_DC);
   spi_write(cmd);
   output_high(TFT_CS);
}

void write_data(int8 data){
   output_low(TFT_CS);
   output_high(TFT_DC);
   spi_write(data);
   output_high(TFT_CS);
}

void tft_init(void){
   output_high(TFT_RST); delay_ms(5);
   output_low(TFT_RST);  delay_ms(20);
   output_high(TFT_RST); delay_ms(150);

   write_command(CMD_SWRESET); delay_ms(150);
   write_command(CMD_SLPOUT);  delay_ms(150);
   write_command(CMD_COLMOD);  write_data(0x55); // RGB565
   write_command(CMD_MADCTL);  write_data(0x48); // rotación
   write_command(CMD_DISPON);  delay_ms(150);
}

void setAddrWindow(int16 x0, int16 y0, int16 x1, int16 y1){
   write_command(CMD_CASET);
   write_data(x0>>8); write_data(x0&0xFF);
   write_data(x1>>8); write_data(x1&0xFF);

   write_command(CMD_RASET);
   write_data(y0>>8); write_data(y0&0xFF);
   write_data(y1>>8); write_data(y1&0xFF);

   write_command(CMD_RAMWR);
}



// --- Definición de fillRect (igual que en ST7735 pero adaptado a 480x320) ---
void fillRect(int16 x, int16 y, int16 w, int16 h, int16 color){
   int16 x_end = x + w - 1;
   int16 y_end = y + h - 1;
   int32 n = (int32)w * (int32)h;
   int8 hi = color >> 8;
   int8 lo = color & 0xFF;

   setAddrWindow(x, y, x_end, y_end);

   output_low(TFT_CS);
   output_high(TFT_DC);
   while(n--){
      spi_write(hi);
      spi_write(lo);
   }
   output_high(TFT_CS);
}

// --- Definición de fillScreen (pantalla completa) ---
void fillScreen(int16 color){
   fillRect(0, 0, 480, 320, color);
}

int16 Color565(int8 r,int8 g,int8 b){
   return ((r&0xF8)<<8)|((g&0xFC)<<3)|(b>>3);
}
temtronic



Joined: 01 Jul 2010
Posts: 9606
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 15, 2025 6:36 pm     Reply with quote

hardware question is VDD 5 or 3 volts ??

The LCD is 3 volts, but the PIC can run at 5, so it is important to know what voltage the PIC and LCD are connected to.
Ramses Chacon



Joined: 15 Dec 2025
Posts: 4

View user's profile Send private message

PostPosted: Mon Dec 15, 2025 7:13 pm     Reply with quote

temtronic wrote:
hardware question is VDD 5 or 3 volts ??

The LCD is 3 volts, but the PIC can run at 5, so it is important to know what voltage the PIC and LCD are connected to.



The VCC converts 5V to 3.3V
Ttelmah



Joined: 11 Mar 2010
Posts: 20001

View user's profile Send private message

PostPosted: Tue Dec 16, 2025 2:33 am     Reply with quote

First thing, your clock setup is wrong. XT, is the medium power drive mode.
It is suitable for medium drive resonator, or a crystal below perhaps 8MHz.
Not 20MHz.
For a 20MHz crystal, HS needs to be selected.
Safer in the long run to just get rid of all the oscillator fuses, and tell the
clock setting what your are doing. So (for example):
#use delay(crystal=20000000)

Will automatically set the correct fuses.

Get your oscillator running and tested before anything else. Flash an LED
and check this flashes at the correct rate.

Now your post about Vcc, makes no sense. Vcc is just the supply volt6age
it does not 'convert' anything. If you mean that the display has a voltage
regulator on it that converts 5v to 3.3v, then this is your problem. You need
to understand that there are two 'parts' to mixing supplies. The supply
voltage itself, and the _signalling_ voltage. Now the ST7796S display
generates SPI supporting 3.3v signalling. The PIC if running at 5v, will not
accept the signals from this for it's inputs. Either the PIC needs to be
running at 3.3v as well, or there needs to be signal level translators on
the data connections.
Read the sticky at the top of the forum about 5v to 3.3v interfacing.
Ramses Chacon



Joined: 15 Dec 2025
Posts: 4

View user's profile Send private message

PostPosted: Tue Dec 16, 2025 9:39 am     Reply with quote

Ttelmah wrote:
First thing, your clock setup is wrong. XT, is the medium power drive mode.
It is suitable for medium drive resonator, or a crystal below perhaps 8MHz.
Not 20MHz.
For a 20MHz crystal, HS needs to be selected.
Safer in the long run to just get rid of all the oscillator fuses, and tell the
clock setting what your are doing. So (for example):
#use delay(crystal=20000000)

Will automatically set the correct fuses.

Get your oscillator running and tested before anything else. Flash an LED
and check this flashes at the correct rate.

Now your post about Vcc, makes no sense. Vcc is just the supply volt6age
it does not 'convert' anything. If you mean that the display has a voltage
regulator on it that converts 5v to 3.3v, then this is your problem. You need
to understand that there are two 'parts' to mixing supplies. The supply
voltage itself, and the _signalling_ voltage. Now the ST7796S display
generates SPI supporting 3.3v signalling. The PIC if running at 5v, will not
accept the signals from this for it's inputs. Either the PIC needs to be
running at 3.3v as well, or there needs to be signal level translators on
the data connections.
Read the sticky at the top of the forum about 5v to 3.3v interfacing.

But I have a problem because when I put hs the ccs c gives an error
Ttelmah



Joined: 11 Mar 2010
Posts: 20001

View user's profile Send private message

PostPosted: Tue Dec 16, 2025 10:30 am     Reply with quote

Double check how you are spelling crystal/
What compiler version do you have?????.

Code:

#include <18F45K22.h>
#FUSES NOWDT, NOPROTECT, NOLVP, PUT, BROWNOUT
#use delay(crystal=20MHz)


Compiled completely happily for me on any of the last 20 compilers.
Ramses Chacon



Joined: 15 Dec 2025
Posts: 4

View user's profile Send private message

PostPosted: Tue Dec 16, 2025 10:39 am     Reply with quote

Ttelmah wrote:
Double check how you are spelling crystal/
What compiler version do you have?????.

Code:

#include <18F45K22.h>
#FUSES NOWDT, NOPROTECT, NOLVP, PUT, BROWNOUT
#use delay(crystal=20MHz)


Compiled completely happily for me on any of the last 20 compilers.

Yes, it compiles that way, but if I use HS as you said, it doesn't work because the HS for this PIC is HSH.
Ttelmah



Joined: 11 Mar 2010
Posts: 20001

View user's profile Send private message

PostPosted: Tue Dec 16, 2025 11:22 pm     Reply with quote

I did _not_ say to use HS. I said to _remove all the clock fuses_
Do not use XT, HS, HSH etc. Let the compiler set the fuses. Otherwise
you risk getting them wrong.
HS is just the codename for the high speed crystal fuse. The actual
fuse needed depends on the chip.
temtronic



Joined: 01 Jul 2010
Posts: 9606
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 17, 2025 6:31 am     Reply with quote

my simple clock 'cheat', is to use the INTERNAL one. they are easy to setup, saves 2 pins, stable over room temperature. Unless you need precise timing they'll work 99.44% of the time.
Ttelmah



Joined: 11 Mar 2010
Posts: 20001

View user's profile Send private message

PostPosted: Wed Dec 17, 2025 8:24 am     Reply with quote

However his big problem is likely to be level translation, if he thinks:
The VCC converts 5V to 3.3V"..... Sad
temtronic



Joined: 01 Jul 2010
Posts: 9606
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 17, 2025 10:23 am     Reply with quote

have to wonder why not run on 3
Volts...seems to be the 'standard' for 99.44% of all peripherals and that PIC is good on 3 volts.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group