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

PIC18F4550 configuring SPI

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



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PIC18F4550 configuring SPI
PostPosted: Mon Jul 31, 2017 7:06 pm     Reply with quote

Hi,

I have a pic18f4550 with crystal 16Mhz, version of compiler is 5.015.
I want to drive ili9341 320x240 with spi.
The code it was for mplab-x but i have adapted it for ccs.

How do i configure spi ?

Code:

#include <18F4550.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HSPLL
#FUSES CPUDIV1
#FUSES PLL4   
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES VREGEN                   //USB voltage regulator enabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTC                   //Configuration registers not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads

//#use delay(crystal=16Mhz, clock=16Mhz)
#use delay(clock=48Mhz)



on file ili9341.h
Code:

#define TFT_CS             PIN_D0 //LATDbits.LATD0
//#define TRIS_CS            TRISDbits.TRISD0

#define TFT_RES            PIN_D1 //LATDbits.LATD1
//#define TRIS_RES           TRISDbits.TRISD1

#define TFT_DC             PIN_D2 //LATDbits.LATD2
//#define TRIS_DC            TRISDbits.TRISD2

......


The main.c
Code:

#include <main.h>
//#USE SPI (MASTER, CLK=PIN_B1, DI=PIN_B0, DO=PIN_C7, MODE=2, BITS=8, STREAM=SPI_1)

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#include "ili9341.h"
#include "ili9341.c"

void main()
{

   int i;
   char buffer1[20];
   
   setup_spi(SPI_MASTER | SPI_MODE_2| SPI_CLK_DIV_4 ); 
   TFT_Init();

   TFT_SetDotSize(1);
   TFT_RectangleRound(5, 115, 234, 314, 10, BLACK);
.......


I have tested mode0, mode1, mode2, mode3, but it don't work.
How to calculate SPI_CLK_DIV_X ?

Do i need to put line
Code:

#USE SPI (MASTER, CLK=PIN_B1, DI=PIN_B0, DO=PIN_C7, MODE=2, BITS=8, STREAM=SPI_1)


What is the difference with #use spi .., setup_spi and init_spi ?

Datasheet for ili9341 is here https://cdn-shop.adafruit.com/datasheets/ILI9341.pdf

My 2.4 tft spi 320x240 is http://www.winddeal.net/image/30209/30209-6.jpg

Example code for TFT_Init();
Code:

#include "ili9341.h"
//#use fast_io(d)
//#use fast_io(b)
//#use fast_io(c)

//==============================================================================
// Declaration of global variables.
//==============================================================================
char *font, *font2;
char width, height, letter_spacing, dot_size = 0, frame_memory = TFT_VERTICAL;
unsigned int8 tft_x = TFT_W - 1;
//==============================================================================
// This function initializes the driver ILI9341.
//==============================================================================
void TFT_Init(void){
set_tris_d(0x00);
set_tris_c(0x00);
set_tris_b(0x00);
//TRIS_CS = 0; TRIS_DC = 0; TRIS_RES = 0;

output_high(TFT_CS); output_low(TFT_DC);output_low(TFT_RES);
TFT_Reset();
TFT_WriteCommand(0xCB);   // Power control A (CBh)
TFT_WriteParameter(0x39);
TFT_WriteParameter(0x2C);
TFT_WriteParameter(0x00);
TFT_WriteParameter(0x34);
TFT_WriteParameter(0x02);
..........


reset code
Code:

void TFT_Reset(void){
output_high(TFT_CS);       
output_low(TFT_RES); delay_ms(50);
output_high(TFT_RES); delay_ms(150);
}


Thanks for your responses.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 31, 2017 9:43 pm     Reply with quote

Quote:
I want to drive ili9341 320x240 with spi.

According to the datasheet, the ili9341 can be jumpered to select 12
different interfaces. These can be i2c, SPI, and many parallel interfaces.
Do you know for certain that the SPI interface is selected ?

See page 26 of the data sheet, in this section:
7.1.1. MCU interface selection
https://cdn-shop.adafruit.com/datasheets/ILI9341.pdf

Can you post a link to the website where you bought the lcd ?
They may have some documentation that tells which interface
the lcd has selected.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Jul 31, 2017 11:48 pm     Reply with quote

As a comment, the ILI9341 data sheet shows three wire SPI with the clock idle low, and all data transfers being done with the data stable on the rising edge of the clock. This is mode0.
He is selecting mode2, which has the clock idling high and all data transfers on the falling edge....
He talks about MPLAB, but MPLAB is not a programming language, but an IDE. If he is translating from Microchip-C, then this will have different SPI setups, and he should look at these.

On #use,

have a look at:
<http://www.ccsinfo.com/forum/viewtopic.php?t=54162&highlight=spi+spixfer>
<http://www.ccsinfo.com/forum/viewtopic.php?t=52644&highlight=spi+spixfer>

There are many similar threads.
#USE is more flexible and more powerful, but must be used with spi_xfer. It can handle both software and hardware SPI. Setup_spi, is the older command to just handle the SPI hardware, with more limited abilities.

The unit supports a maximum SPI clock of 10MHz for a read, and 6.66MHz for a write). SPI_CLK_DIV_4, will be trying to clock at 12MHz. Too fast.
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Tue Aug 01, 2017 2:11 am     Reply with quote

PCM programmer wrote:
Quote:
I want to drive ili9341 320x240 with spi.

According to the datasheet, the ili9341 can be jumpered to select 12
different interfaces. These can be i2c, SPI, and many parallel interfaces.
Do you know for certain that the SPI interface is selected ?

See page 26 of the data sheet, in this section:
7.1.1. MCU interface selection
https://cdn-shop.adafruit.com/datasheets/ILI9341.pdf

Can you post a link to the website where you bought the lcd ?
They may have some documentation that tells which interface
the lcd has selected.


Interface is SPI, it work like a charm with arduino and the library adafruit ili9341. Tihis screen is only spi, other screen have uart/i2c/spi.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Aug 01, 2017 4:09 am     Reply with quote

Honestly you are starting from the wrong end.

First thing to do is get the SPI working with a minimum command.

Then translate the existing parallel mode ili9341 library:
<https://www.ccsinfo.com/forum/viewtopic.php?t=55040>

to use the SPI interface.

As have already pointed out, your SPI mode is wrong, and speed too fast for the chip.
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Tue Aug 01, 2017 5:14 am     Reply with quote

I have modified the code
Code:

 setup_spi(SPI_MASTER | SPI_MODE_0| SPI_CLK_DIV_4 );


and modified crystal to
Code:

#use delay(crystal=16Mhz, clock=16Mhz)


I do not know why it does not work because this code works on youtube, the only difference is that I replaced

that

Code:

void SPI1_Write(unsigned char data){
//SSP1CON1bits.WCOL = 0;
SSPBUF = data;
while(!SSPSTATbits.BF); 
data = SSPBUF;
}


by
Code:

spi_write(parameter);


what is the equivalent for ccs for code below using spi function
Code:

void SPI1_Close(void){
SSPCON1 &= 0xDF;
}

Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Aug 01, 2017 6:48 am     Reply with quote

You should change the fuse to HS, not HSPLL as well. Otherwise you may well still be clocking too fast.

setup_spi(SPI_DISABLED);

Is the command to switch the SPI off.
andromeda92



Joined: 21 Jul 2017
Posts: 16

View user's profile Send private message

PostPosted: Wed Aug 02, 2017 3:18 am     Reply with quote

thanks for the explanation
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