 |
 |
View previous topic :: View next topic |
Author |
Message |
JerryR
Joined: 07 Feb 2008 Posts: 170
|
No SPI clocks |
Posted: Tue Jul 15, 2025 9:58 am |
|
|
Hi Group, thanks for your help on this-
I am using PCWH v5.093 to compile code for a PIC18F86J60. I am posting a small snippet of code below. I am using the hardware SPI1 to talk to two external SPI devices
The problem is that using either of the use_spi setups, results in no clocks being issue when attempting to write to a SPI device. The Data Out looks reasonable, however, but no clock pulses.
If I comment out both spi_setup statements, I can generate clock pulses on the clock pin (PIN_C3), so there's nothing in the hardware blocking the clock pin.
This is probably something very simple, but I just can't see it. Here's the code:
Code: |
#include <18F86J60.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#use delay(crystal=25000000)
#use spi (MASTER, DI=PIN_C4, DO= PIN_C5, CLK= PIN_C3, BITS=8, MODE=0)
//#use spi (MASTER, SPI1, BITS=8, MODE=0)
void main()
{
setup_adc_ports(AN0_TO_AN1, VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_0);
output_high(PIN_A3);
output_low(PIN_A3);
delay_us(10);
spi_write (0x50);
spi_write(0x00);
spi_write(0xFF);
delay_us(10);
output_high(PIN_A3);
while(TRUE)
{
}
}
|
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19898
|
|
Posted: Thu Jul 17, 2025 5:54 am |
|
|
Key thing. With #use SPI, you have to use spy_xfer, not spi_write.
You are mixing commands from setup_spi, with those from #use SPI.
You should also specify a clock rate. The default is as fast as possible.
If you look in the manual, under #use spi, you will have listed at the
bottom of the page, the commands that work with this. |
|
 |
JerryR
Joined: 07 Feb 2008 Posts: 170
|
|
Posted: Thu Jul 17, 2025 7:45 am |
|
|
Hello Ttelmah:
Thank you so much for your interest!
Yes, the current version of my code uses the correct method and calls, thanks for your clairification. I see from research you have been kind to mention this to quite a few in the group.
Should have posted this yesterday, but my problem with no clocks was solved. BAD LOGIC ANALYSER PROBE! I actually did have clocks using a scope.
Sorry to have raised the question to the group. Lessons here is don't always trust your equipment.
Best regards! |
|
 |
JerryR
Joined: 07 Feb 2008 Posts: 170
|
|
Posted: Sat Jul 19, 2025 11:37 am |
|
|
All:
I am trying to use "#USE_SPI" commands to read a MCP23S08 inputs. I can successfully set output to a MCP23S17 using SPI_XFER command, but can't seem to understand how to read inputs on a MCP23S08.
I have used the older method "SETUP_SPI" sucessfully with both MCP23S08 and MCP23S17, however can't seem to get my head around using SPI_XFER to read data. Result of attempting to read always generates clocks and transfers "0".
Any help is much appreciated
My SPI setup is:
#use spi(MASTER, SPI1, BITS=8, BAUD=100000, MODE=0)
And my code to read MCP23S08 is:
Code: |
#define IODIR 0x00
#define IPOL 0x01
#define GPINTEN 0x02
#define DEFVAL 0x03
#define INTCON 0x04
#define THERM_IOCON 0x05
#define GPPU 0x06
#define INTF 0x07
#define INTCAP 0x08
#define GPIO 0x09
#define OLAT 0x0A
/**************************************************************/
#define CONTROL_REG_VAL 0x40 //// these two defines are used here
#define CONTROL_REG_VAL_Read 0x41
#define GPIO_CONFIG_VAL 0x09 //// just to do very simple serial to parallel output transfer
void setup_MCP23S08(void){
int8 dummy;
output_low(THERM_CS);
delay_us(100);
dummy= spi_xfer(CONTROL_REG_VAL); // control register setup
dummy= spi_xfer (IODIR); // GPIO set to all inputs
dummy= spi_xfer (0xFF); // GPIO set to all inputs
delay_us(100);
output_high(THERM_CS);
}
//=============================================================================
int8 ReadFrom_Thermo_Exp(void)
{
int8 received_Therm_data;
int8 dummy;
output_low(THERM_CS);
delay_us(10);
dummy= spi_xfer(CONTROL_REG_VAL_Read);
dummy= spi_xfer(GPIO_CONFIG_VAL);
received_Therm_data = spi_xfer(0);
delay_us(10);
output_high(THERM_CS);
return received_Therm_data;
}
//=============================================================================
void WriteToExpanderPort(unsigned char data){
int8 dummy;
output_low(THERM_CS);
delay_us(10);
dummy= spi_xfer(CONTROL_REG_VAL); // control register setup
dummy= spi_xfer(GPIO_CONFIG_VAL); // GPIO configuration
dummy= spi_xfer(data); // value to I/O
delay_us(10);
output_high(THERM_CS);
}
//=============================================================================
| [/code] |
|
 |
|
|
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
|