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

PIC 18F6722 I/O SPI

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



Joined: 18 Jul 2006
Posts: 33
Location: Maryland, USA

View user's profile Send private message

PIC 18F6722 I/O SPI
PostPosted: Thu Aug 03, 2006 9:52 am     Reply with quote

In my PIC 18F6722, the Pins RC3 (SCK1) and RD6 (SCK2) have already been used.

Is it possible to program any of the Pins in Port F to make it work as another Synchronous serial clock input/output for SPI mode.

I am using CCS PCH C compiler and MPLAB IDE to program my PIC MCU.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 03, 2006 10:37 am     Reply with quote

Yes it is possible. But it won't be using the built-in hardware anymore. You will need to bit-bang the port.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
pdswar



Joined: 18 Jul 2006
Posts: 33
Location: Maryland, USA

View user's profile Send private message

PostPosted: Thu Aug 03, 2006 11:20 am     Reply with quote

rwyoung, thank you very much for the info.

I was wondering if anyone here has any sample code that creates firmware driven SPI port using general I/O pins in PIC 18F6722.

Thank you for your time.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 03, 2006 1:51 pm     Reply with quote

Look in the examples and driver's directories of your compiler's install tree. Also consult both the PIC's data sheet (MSSP section) and your slave SPI device's datasheet. Between those two datasheets you should be able to descern the correct pattern to make things work.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 03, 2006 2:25 pm     Reply with quote

Specifically, look at any of the SPI eeprom drivers, such as 25640.C,
which is in c:\Program Files\Picc\Drivers

(Also, you don't need to start two threads on the same topic).
pdswar



Joined: 18 Jul 2006
Posts: 33
Location: Maryland, USA

View user's profile Send private message

PostPosted: Tue Aug 08, 2006 9:46 am     Reply with quote

PCM Programmer, thank you for replying. Sorry about creating multiple threads on same topic. I'll make sure it does not happen again.

I am trying to send a character array (5X7 pixels) to NKK Smartswitch LCD through firmware driven SPI port.

----------------------------------------------------------------------------
//The software driven SPI port F
//SPI Master without reciever function

//MASTER_SCK LCD_SCP // Connect to Master SCK (PIN_F1)
//MASTER_DO LCD_DIN // Connect to Master SDO (PIN_F0)
//MASTER_SELECT LCD_FLM // Connect to Master SS (PIN_F3)

void init_lcd(void)
{
char *ptr;

char letterT[7]={31 , 4 , 4 , 4 , 4 , 4 , 4 }; //letter T

ptr=&letterT[0]; //Assign address of first element to the pointer

set_tris_f(0x00); // Use all pins in Port F for outputs

spi_transfer(ptr); //Send address of first element of letterT[] array
}

-----------------------------------------------------------------------------------
void spi_transfer(char data)
{

char cmd[7];
int i,k;

//Initialization
output_low(LCD_SCP);
output_low(LCD_DIN);
output_low(LCD_LP);
output_low(LCD_FLM);


for(i = 0; i < 7; i++) //Transfer contents of data[] to cmd[]
{
cmd[i]=data++;
}

output_high(LCD_LP); //40 SCP pulses in five bursts between each LP signal
delay_us(500);
output_low(LCD_LP);

output_high(LCD_FLM); //First Line Marker

for(k=0; k<40; k++) //Each Row has 40 pixels
//40 SCP pulses in five bursts between each LP
{
output_high(LCD_SCP);
output_low(LCD_SCP);

if((k==7) || (k==15)|| (k==23) || (k==31) || (k==39))
delay_ms(1);

if (k==4) //First 4 pixels (0-3) are invisible
output_bit(LCD_DIN, shift_left(cmd,8,1));
}

output_low(LCD_FLM);
}

------------------------------------------------------------------------------------

-Is there anything wrong with shifting '1' to the left 8 times

output_bit(LCD_DIN, shift_left(cmd,8,1));

-Can anyone suggest me a better way to pass a character array to a
firmware driven SPI?

Thank you for your time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 08, 2006 10:07 am     Reply with quote

I didn't look at all your code in detail, but I do see an error.
You're passing a pointer to the spi_transfer() function, but in the
function declaration you haven't declared "data" as a pointer.
Also, within the program, you're using "data" as a pointer, but
you don't have the dereferencing operator in front of it (ie., an asterisk).
You need to add the asterisk in two places, as shown below:
Quote:
void spi_transfer(char *data) // Add asterisk
{

char cmd[7];
int i,k;

//Initialization
output_low(LCD_SCP);
output_low(LCD_DIN);
output_low(LCD_LP);
output_low(LCD_FLM);


for(i = 0; i < 7; i++)
{
cmd[i] = *data++; // Add asterisk
}


Web page that shows how to use a pointer to access a variable:
http://www.comp.lancs.ac.uk/computing/users/marash/SlidesCpp/slides/sld017.htm
pdswar



Joined: 18 Jul 2006
Posts: 33
Location: Maryland, USA

View user's profile Send private message

need help with sending char array over software driven SPI
PostPosted: Mon Aug 14, 2006 12:27 pm     Reply with quote

http://www.nkksmartswitch.com/Uploads/DrivingSSPart1and2.pdf

While using above article as a reference, I am using software driven SPI port to send 5X7 pixels characters to NKK's programmable LCD. I am having trouble sending character matrix over SPI since I do not have a clear idea on how character array gets converted into bits.

.........
char *ptr;
char letterT[7]={31,4,4,4,4,4,4};
/*
char letterT[7]=
{
31/*#####*/ ,
4 /* . . # . . */ ,
4 /* . . # . . */ ,
4 /* . . # . . */ ,
4 /* . . # . . */ ,
4 /* . . # . . */ ,
4 /* . . # . . */
};
*/


ptr=&letterT[0]; //Assign address of letterT[0] to the pointer

spi_transfer(ptr); //Send address of first element of letterT[] array

........


void spi_transfer(char *data)
{
char input[7];
int i,j,k;

for(i = 0; i < 7; i++)
{
input[i]=*data++; //Transfer elements of data[] to cmd[]

for(j=0; j<5; j++) // SPI Master data transfer;
//40 SCLK pulses in five bursts
{
for(k=0; k<8; k++)
{
output_high(SCLK);
output_low(SCLK);
}
delay_us(50);
}

if(bit_test(input[i],7))
output_high(SDO);
else
output_low(SDO);

}
}

What is wrong with code when I am trying to convert character array into binary bits?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 14, 2006 8:31 pm     Reply with quote

I don't think your output routine is correct. First, there doesn't appear
to be a real data sheet for the NKK smartswitch. There is just a little
four page brochure. It has a little fragment of a timing diagram on one
page, but there's no real description of what's necessary to drive the
device. I think that's left to the Kagan article. But the Kagan article
is itself fragmented. It says the shifting out is done in assembly
language, but it doesn't show the ASM code. I think the ASM code
that he refers to, may be on pages 15 and 16 of this document:
http://www.nkksmartswitch.com/pdf/PicSampleCode.pdf
It has a function called "shift", which consists of inline ASM code
to shift out 8 bits, and strobe the clock line for each bit.
Your posted code doesn't do that. You're strobing the clock line
8 times, while leaving the data stuck at 0. Then you set the data line
according to bit 7. That doesn't fit the required timing at all.
pdswar



Joined: 18 Jul 2006
Posts: 33
Location: Maryland, USA

View user's profile Send private message

help with NKK IS01NCF programmable LCD
PostPosted: Tue Aug 15, 2006 7:52 am     Reply with quote

PCM Programmer, thanks for replying. I am looking at the assembly code that you provided and trying to figure out how to embed/emulate ASM into my C-code.

Can you please look at these updated codes when you get a chance?

.............
char *ptr;
char letterT[7]={31 , 4 , 4 , 4 , 4 , 4 , 4 };

//char letterT[7]=
//{
//31/*##### = 2^(4)+2^(3)+2^(2)+2^(1)+2^(0)*/ ,
//4 /* . . # . . = 0 + 0 +2^(2)+ 0 +0 */ ,
//4 /* . . # . . = 0 + 0 +2^(2)+ 0 +0 */ ,
//4 /* . . # . . = 0 + 0 +2^(2)+ 0 +0 */ ,
//4 /* . . # . . = 0 + 0 +2^(2)+ 0 +0 */ ,
//4 /* . . # . . = 0 + 0 +2^(2)+ 0 +0 */ ,
//4 /* . . # . . = 0 + 0 +2^(2)+ 0 +0 */
//};

ptr=&letterT[0]; //Assign address of letterT[0] to the pointer

spi_transfer(ptr); //Send address of first element of letterT[] array

......................

void spi_transfer(char *data)
{
char input[7];
int i,j,k;

for(i = 0; i < 7; i++)
{
input[i]=*data++; //Transfer elements of data[] to input[]

// SPI Master data transfer;
//40 SCLK pulses in five bursts
for(j=0; j<5; j++)
{
for(k=0; k<8; k++)
{
if(k<5)
if(bit_test(input[i],(4-k))) //(4-k)decrements the bit index; LSB checked first
output_high(LCD_DIN);
else
output_low(LCD_DIN);


output_high(LCD_SCP);
output_low(LCD_SCP);
}
}
}
}
...............

Am I treating letterT[7] as a 5X7 pixels matrix in the codes that are highlighted above? Is there a better way to convert a char array into binary bits?
pdswar



Joined: 18 Jul 2006
Posts: 33
Location: Maryland, USA

View user's profile Send private message

help with NKK IS01NCF programmable LCD
PostPosted: Tue Aug 15, 2006 7:56 am     Reply with quote

I realized that signal names have not been consistent throughout this thread.

//The software driven SPI port F
//SPI Master without reciever function

//MASTER_SCLK LCD_SCP Connect to Master SCLK (PIN_F1)
//MASTER_DO LCD_DIN Connect to Master SDO (PIN_F0)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 15, 2006 11:30 am     Reply with quote

I found the code for the Kagan article. It's at this ftp site:
ftp://ftp.circuitcellar.com/pub/Circuit_Cellar/2002/145/
This is the full code for the "Driving the NKK SmartSwitch" pdf article.

This runs on a PSOC chip, which is not a PIC. It looks like he's
using hardware SPI. This is his first line in the MAIN.C file:
Code:

SPIM_1_Start(SPIM_MODE_2 | SPIM_LSB_FIRST);

He's using SPI master mode 2, and he's set it up to transmit the LSB
first. Of course, the PIC's SPI module transmits a byte "MSB first",
so a quick way of reversing the bit order within a byte by a software
algorithm must be used. Here are some threads with several methods:
http://www.ccsinfo.com/forum/viewtopic.php?t=23356
http://www.ccsinfo.com/forum/viewtopic.php?t=23364
http://forum.microchip.com/tm.aspx?m=50138

The timing should be checked with a logic analyzer. Here's a relatively
cheap one that runs on a PC (via a USB port), that we use:
http://www.pctestinstruments.com/
KamPutty
Guest







PostPosted: Tue Aug 15, 2006 3:53 pm     Reply with quote

pdswar wrote:
rwyoung, thank you very much for the info.

I was wondering if anyone here has any sample code that creates
firmware driven SPI port using general I/O pins in PIC 18F6722.

Thank you for your time.


Hi all,
I just pasted some of my spi code (pic18f4520 --> OLED) here:
http://forum.sparkfun.com/viewtopic.php?t=3808&start=15
It includes the bitbanging routines, 5x7 fonts, test code, etc...
Hope it helps...

~Kam (^8*
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