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

PIF16F874 SPI with C

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 05, 2010 11:26 am     Reply with quote

Try this simple program. You should see output on the SCK and SDO pins.
If you don't see output, it probably means there is something wrong with
your hardware. (Assuming you are using hardware, and not Proteus).
Code:

#include <16F874A.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000) 

#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)

//===============================
main(void)
{

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);

while(1)
  {
   spi_write(0x55);
   delay_us(100);
  }

davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

SDO and SDI
PostPosted: Wed May 05, 2010 4:52 pm     Reply with quote

By theory, I can get a serial signal 0x55 from RC5/SDO pin, and clock on RC3/SCK pin.
Do I need to connect SDO (RC5) to SDI (RC4) to form a loop.
I modified and can not get serial signal from SDO.
Code:

#include "16F874A.H"
#use delay(clock=4000000)
#use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, ENABLE=PIN_A2, BITS=16)
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP

#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)

//===============================
void main()
{

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
   while(1)
   {
   spi_write(0xff);
   delay_ms(300);
   }


Thanks.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 06, 2010 2:46 am     Reply with quote

Quote:
By theory, I can get a serial signal 0x55 from RC5/SDO pin, and clock on RC3/SCK pin.
By theory???
You are testing, right? So do you have a signal or not?
Are you testing with real hardware or in a simulator like Proteus?

Quote:
Do I need to connect SDO (RC5) to SDI (RC4) to form a loop.
Yes.

Quote:
I modified and can not get serial signal from SDO.
First of all you have to know that in the v4 compiler CCS introduced a new SPI software module. The new module has a lot of features but we suspect it to have a few bugs left in.
The new module is configured with the line '#use spi'. The old hardware SPI configuration is still present too, and is configured with setup_spi().
Only use one method. DON'T use both two lines in your program!!
I recommend you remove the '#use spi' line.

Always post your compiler version number.
davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

PIC Simulator
PostPosted: Tue May 11, 2010 4:18 pm     Reply with quote

I am using MPLAB IDE 5.20, CCS C Compiler 4.104, Oshonsoft PIC Simulator IDE 6.33, still not signal from SDO when run under PIC simulator.
What simulator do you run your .hex file?

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 11, 2010 4:35 pm     Reply with quote

I use hardware: PicDem2-Plus board and an oscilloscope.
davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

a
PostPosted: Wed May 12, 2010 9:07 am     Reply with quote

To PCM Programmer:
Could you do me a favor, could you compile your code for me (use MPLAB IDE with CCS)
Code:

#include <16F874A.h>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000) 

#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)

//===============================
main(void)
{

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);

while(1)
  {
   spi_write(0x55);
   delay_us(100);
  }

}

I want to know is my MPLAB IDE / CCS or Oshonsoft PIC Simulator IDE problem.
Thank you very much.

davidshang@hotmail.com
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 12, 2010 11:05 am     Reply with quote

I don't want to do that. I don't want to email things to other people.
I only want to participate in the forum.
davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

Thanks.
PostPosted: Wed May 12, 2010 1:04 pm     Reply with quote

Thanks.
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