|
|
View previous topic :: View next topic |
Author |
Message |
pdswar
Joined: 18 Jul 2006 Posts: 33 Location: Maryland, USA
|
PIC 18F6722 I/O SPI |
Posted: Thu Aug 03, 2006 9:52 am |
|
|
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
|
|
Posted: Thu Aug 03, 2006 10:37 am |
|
|
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
|
|
Posted: Thu Aug 03, 2006 11:20 am |
|
|
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
|
|
Posted: Thu Aug 03, 2006 1:51 pm |
|
|
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
|
|
Posted: Thu Aug 03, 2006 2:25 pm |
|
|
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
|
|
Posted: Tue Aug 08, 2006 9:46 am |
|
|
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
|
|
Posted: Tue Aug 08, 2006 10:07 am |
|
|
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
|
need help with sending char array over software driven SPI |
Posted: Mon Aug 14, 2006 12:27 pm |
|
|
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
|
|
Posted: Mon Aug 14, 2006 8:31 pm |
|
|
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
|
help with NKK IS01NCF programmable LCD |
Posted: Tue Aug 15, 2006 7:52 am |
|
|
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
|
help with NKK IS01NCF programmable LCD |
Posted: Tue Aug 15, 2006 7:56 am |
|
|
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
|
|
|
KamPutty Guest
|
|
Posted: Tue Aug 15, 2006 3:53 pm |
|
|
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* |
|
|
|
|
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
|