|
|
View previous topic :: View next topic |
Author |
Message |
asdf85
Joined: 03 Jan 2011 Posts: 34
|
Can't get SPI working |
Posted: Mon Jan 03, 2011 1:05 am |
|
|
Hi,
I've been trying to get the SPI communication working using the PIC16F873. I've browsed through the forum and nothing I try works.
I cant seem to get an output using the SPI_WRITE() command.
If i use the setup_spi() command, the microcontroller seems to stop working once it reaches the SPI_WRITE().
If i use the #USE SPI, the same thing happens unless i add the 'FORCE_HW', then it doesnt hang. However there is just no output even after i check the pin. I've tried it with and without the 'delay_ms' or 'delay_us'.
also, does the SCL and SDA pin require pull up resistors?
this is my code. May i know what im doing wrong? Im using compiler version 4.093
Thanks in advance
Code: |
#include "16F873.h"
#include <stdio.h>
#fuses XT,NOPROTECT,NOLVP,NOWDT,NODEBUG,NOBROWNOUT
#use delay(clock=4000000)
//#use SPI(MASTER,BITS=8,SAMPLE_RISE,FORCE_HW,SPI1)//,LOAD=pin_b7,DO=PIN_C5,CLK=PIN_C3)
#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_16);
while(1)
{
spi_write(0xAA);
spi_read(0);
delay_ms(100);
output_low(pin_c7);
spi_write(0xAA);
spi_read(0);
delay_ms(100);
output_high(pin_c7);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Mon Jan 03, 2011 3:45 am |
|
|
Start with something really simple.
Write a 'toggle a pin' program, and test it first on C3, and then on C5. Do both pins toggle OK. If not, you have found a hardware problem...
If both don't work, then oscillator problem, MCLR problem etc..
If one works not the other, 'shorted wire'....
Then re-arrange your code slightly. Fuses should be above includes like STDIO.
Basically layout should always be:
Processor include
Fuses
Clock settings
Serial (if used)
Then general includes
Are you sure whatever you are using to detect the pulse train, would see it?.
Best Wishes |
|
|
asdf85
Joined: 03 Jan 2011 Posts: 34
|
|
Posted: Mon Jan 03, 2011 4:13 am |
|
|
Hi Ttelmah,
I've tested toggling both pins. What I notice is that if I remove the #USE SPI, those 2 pins toggles OK. But if #USE SPI is present, both those pins don't toggle.
I've also re-arranged my code but it still doesn't work.
I've also tried adding an ENABLE=PIN_B6 to the #USE SPI. There is a correct output coming out of pin b6 when I tested it, but nothing from SDO and CLK.
I'm pretty sure I should be able to see the pulse from SDO & CLK if there is one.
Any other ideas?
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Mon Jan 03, 2011 5:07 am |
|
|
Not working, with #USE SPI present, is correct. The peripheral 'overrides' the pins. Means the peripheral _is_ being connected to the pins.
You say 'fairly sure'. With /16, on a 4MHz clock, the clock 'burst' will be eight cycles at 250KHz. Just 32uSec in duration. Doesn't take much to miss it.....
Try taking the division up to /64.
Best Wishes |
|
|
asdf85
Joined: 03 Jan 2011 Posts: 34
|
|
Posted: Mon Jan 03, 2011 9:43 pm |
|
|
Thanks for the info.
The actual problem lied with the MCU I was using. After replacing it with another one, it worked. |
|
|
|
|
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
|