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 support@ccsinfo.com

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
davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

PIF16F874 SPI with C
PostPosted: Fri Apr 30, 2010 3:12 pm     Reply with quote

I am new user with PIC.
Does anyone can show me how to send data to SPI port. (PIF16F874A with C)?
I want to send codes through SPI port (SDO) in sequence.
for example:
1st code: 0xdb,
2nd code: 0x06,
3rd code: 0xbf
all the source I can find online are in assembly language,

Thank you very much.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 30, 2010 3:59 pm     Reply with quote

If that is truly all you want to do, then this program will do it.
If you really want to create driver for an SPI peripheral chip, then
you need to post the manufacturer and part number of the chip.
Code:
#include <16F874A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

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

spi_write(0xDB);
spi_write(0x06);
spi_write(0xBF);

while(1);
davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

thanks
PostPosted: Sun May 02, 2010 12:54 pm     Reply with quote

Thanks.
I will try next week.
davidshang



Joined: 30 Apr 2010
Posts: 7

View user's profile Send private message

not working
PostPosted: Wed May 05, 2010 10:46 am     Reply with quote

Above code seems not working.
The code below is copied from text book, but still no data out from SDO (RC5).
Code:

#include "16F874A.h"
#use delay(clock=4000000)
#use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, ENABLE=PIN_A2, BITS=16)

void main()
{
   int Two=0xdb;
   setup_spi(spi_master);

   while(1)
   {
      Two=spi_read();
      spi_write(Two);
      delay_ms(100);
   }
   

Thanks.
Abdulla M.A.



Joined: 28 Mar 2010
Posts: 30
Location: Baghdad, Iraq

View user's profile Send private message Visit poster's website Yahoo Messenger

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

Hi,
Code:
int Two=0xdb;
// here you defined two=0xDB

Code:
while(1)
   {
        Two=spi_read();
            spi_write(Two);
      delay_ms(100);
   }
   


here you have a mistake, in this statement
Code:
Two=spi_read();

you started read an input from a slave, which did not connect to MCU
so, the value of Two will be always zero, not 0xDB.

Abdulla
_________________
"A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engineer is twice bigger than necessary."


Last edited by Abdulla M.A. on Wed May 05, 2010 11:26 am; edited 1 time in total
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