Sunday, November 13, 2016

PIC18F4550 With ST7735 TFT and ADC example

This topic shows an other example of interfacing PIC18F4550 microcontroller with 1.8" ST7735R SPI TFT display. ADC (Analog-to-Digital Converter) module is included in this example with 2 channels and the result is shown in the following video:

Related Topic:
Interfacing PIC18F4550 with 1.8" TFT display
PIC18F4550 With ST7735 TFT and ADC example circuit:
Our example circuit schematic is shown below.
PIC18F4550 with ST7735 TFT and 2 ADC channels circuit
PIC18F4550 With ST7735 TFT and ADC example CCS C code:
For this project we need a driver for the ST7735 TFT display. This driver can be found in the post at:
ST7735 SPI TFT Display Driver for CCS PIC C compiler
/* PIC18F4550 microcontroller with ST7735R SPI TFT and 2 ADC channels
   ST7735 TFT display driver for CCS PIC C compiler is required

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

#include <18F4550.h>
#device ADC = 8
#use delay(clock = 48000000)
#include <ST7735_TFT.c>
#use fast_io(D)

unsigned int8 i, j, k, pos_x0, pos_x, pos_y;
void main(){
  setup_adc_ports(AN0_TO_AN1);                        // Configure AN0 and AN1 as analog inputs
  setup_adc(ADC_CLOCK_DIV_64);                        // Set ADC conversion time to 64Tosc
  set_adc_channel(0);                                 // Select channel AN0
  pos_x0 = read_adc();
  pos_y = 0;
    i = read_adc();
    set_adc_channel(1);                               // Select channel AN1
    pos_x = i / 2;
    if(pos_x0 <= pos_x)
      for(j = pos_x0; j < pos_x + 1; j++)
        drawPixel(j, pos_y, ST7735_YELLOW);
      for(j = pos_x0; (j + 1) > pos_x; j--)
        drawPixel(j, pos_y, ST7735_YELLOW);
    pos_x0 = pos_x;
    k = read_adc();
    set_adc_channel(0);                               // Select channel AN0
    if(pos_y > _height){
      pos_y = 0;