|
|
View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
PIC18F25K50 SPI doesn't work |
Posted: Sat Apr 30, 2016 2:10 am |
|
|
Greetings! I'm experiencing problems with the spi interface of PIC18F25K50.
I want to read data from a thermocouple sensor. The sensor has only SDO and I've left the SDO of the controller unconnected. I've put mosfet level shifter. My hardware is properly set but the problem is the SPI interface doesn't generate any SCK pulses, so I can't read the driver.
Here is my code:
Code: |
#include <18F25K50.h>
#Fuses HSM,NOPLLEN,NOWDT,CPUDIV2
#use delay(CLOCK=12M,restart_wdt)
#define LED1 PIN_B5
#define Termo_SDI PIN_B0
#define Termo_SDO PIN_B3
#define Termo_SCK PIN_B1
#define Termo_CS1 PIN_B2
#define Termo_CS2 PIN_B4
#use spi(MASTER,DI=Termo_SDI, DO=Termo_SDO, CLK=Termo_SCK, BITS=8, MODE=0,LSB_FIRST, STREAM=THERMO)
unsigned int value;
void main()
{
delay_ms(100);
setup_spi(SPI_MASTER | SPI_CLK_DIV_64);
output_drive(Termo_CS1);
output_high(Termo_CS1);
delay_ms(5);
while(1)
{
output_low(Termo_CS1);
delay_ms(1);
value=spi_xfer(THERMO,255);
output_high(Termo_CS1);
delay_ms(1);
}
}
|
I think I've missed an adjustment?!
Why there aren't clock pulses on the SCK pin?
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Apr 30, 2016 3:26 am |
|
|
First, if a pin doesn't do what it should, try turning off all peripherals that might interfere. So:
setup_adc(NO_ANALOGS);
To ensure the ADC is not trying to use these pins.
Then turn off the ECCP, and CCP2, which both use some of these pins.
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
Then you are making a classic error. If you actually read the manuals, you will find that 'setup_spi', refers you to spi_read, and spi_write, while spi_xfer, only refers to #use spi.
Historically, the only CCS SPI functions supported the hardware only, and used setup_spi, spi_read, and spi_write. A few hundred compiler versions ago, a newer function was added, using #use spi, and spi_xfer. This is a separate function supporting both hardware and software SPI modes. The two versions are _not_ mixable. You should only ever use either setup_spi, with read and write, or spi_xfer with #use.
So:
Code: |
#use spi(MASTER,SPI1, BITS=8, MODE=0, LSB_FIRST, STREAM=THERMO, baud=xxxxx)
//specify a baud rate here. How fast can your sensor clock?.
//If you just specify 'SPI1', the code will automatically use the SPI1
//pins and port.
unsigned int value;
void main()
{
delay_ms(100);
//setup_spi(SPI_MASTER | SPI_CLK_DIV_64);
//Do not use this function with #use spi
output_drive(Termo_CS1);
output_high(Termo_CS1);
delay_ms(5);
while(1)
{
output_low(Termo_CS1);
delay_ms(1);
value=spi_xfer(THERMO,255);
output_high(Termo_CS1);
delay_ms(1);
}
}
|
Why are you sending '255'?.
If this doesn't work, then test that each pin involved does go up and down with an output_high/output_low function. If this still doesn't work, you have a hardware problem.
Also the obvious comment, you are sure the PIC is actually running?. Your clock settings are 'odd'. You are selecting HSM, which is rated for a crystal from 4 to 16MHz, but then clocking the chip off the oscillator/2, yet saying the CPU is being clocked at 12MHz, which would require a 24MHz crystal..... |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Sat Apr 30, 2016 4:04 am |
|
|
Ttelmah, there is a problem! I can't compile this:
Code: |
#use spi(MASTER, SPI1, baud=115200, BITS=8, MODE=0, LSB_FIRST, STREAM=THERMO)
|
The error is: Option invalid Not valid for H/W!
I'm using CCS v5.050 and MPLab v8.91!
I'm sending 255 because this value doesn't matter. I can send every number because the SDO pin of the microcontroller is not connected. I just need to send SCK pulses so I can push the driver buffer to the controller...
I've just tested the pin and ouput_low/high is working properly!
I've tested a lot of CPU settings so I may have made a mistake.
I'll set the oscillator to 48M
Code: |
#Fuses HSH,PLL4X,PLLEN,WDT,WDT8,CPUDIV2
#use delay(CLOCK=24M,restart_wdt)
|
because I want to use USB later. I'm not sure if I have to use CPUDIV2 setting.
The major problem now is the SCK.
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Apr 30, 2016 4:26 am |
|
|
The reason for your error, is that LSB_FIRST, can't be done by the hardware.
If you must use LSB_FIRST, then you have to use software SPI.
Why 115200?. SPI tends to use clock rates like 500Kbps, not values like this which are from UART based transmissions.
If your chip is synchronous requiring a rate like 115200, then using a USART might well be the better solution. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Sat Apr 30, 2016 5:05 am |
|
|
It's working! I set the baud rate to 500k!
Thanks a lot, Ttelmah! |
|
|
|
|
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
|