View previous topic :: View next topic |
Author |
Message |
davidshang
Joined: 30 Apr 2010 Posts: 7
|
PIF16F874 SPI with C |
Posted: Fri Apr 30, 2010 3:12 pm |
|
|
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
|
|
Posted: Fri Apr 30, 2010 3:59 pm |
|
|
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
|
thanks |
Posted: Sun May 02, 2010 12:54 pm |
|
|
Thanks.
I will try next week. |
|
|
davidshang
Joined: 30 Apr 2010 Posts: 7
|
not working |
Posted: Wed May 05, 2010 10:46 am |
|
|
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
|
|
Posted: Wed May 05, 2010 11:26 am |
|
|
Hi,
// 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
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
|
|
Posted: Wed May 05, 2010 11:26 am |
|
|
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
|
SDO and SDI |
Posted: Wed May 05, 2010 4:52 pm |
|
|
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
|
|
Posted: Thu May 06, 2010 2:46 am |
|
|
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
|
PIC Simulator |
Posted: Tue May 11, 2010 4:18 pm |
|
|
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
|
|
Posted: Tue May 11, 2010 4:35 pm |
|
|
I use hardware: PicDem2-Plus board and an oscilloscope. |
|
|
davidshang
Joined: 30 Apr 2010 Posts: 7
|
a |
Posted: Wed May 12, 2010 9:07 am |
|
|
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
|
|
Posted: Wed May 12, 2010 11:05 am |
|
|
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
|
Thanks. |
Posted: Wed May 12, 2010 1:04 pm |
|
|
Thanks. |
|
|
|