|
|
View previous topic :: View next topic |
Author |
Message |
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
PIC18F27J53 - Need to run both spi and i2c - Help |
Posted: Mon May 13, 2019 4:55 am |
|
|
Hi,
Compiler ver - 5.081
Microcontroller - PIC18F27J53
Below i have written a program. I only get zero from the return data from
spi eeprom. My application requires both spi and i2c functions. need some help.
Thanks,
Jai
Code: |
#include <18F27J53.h>
#device adc=12;
#include <stdlib.h>
#fuses HSPLL,PLLEN,PLL5,NOWDT,NOIESO,NOFCMEN,SOSC_HIGH
#pin_select SCK2=PIN_C6
#pin_select SDI2=PIN_B1
#pin_select SDO2=PIN_C7
#use delay(clock=48000000)
#use spi (MASTER, SPI2, MODE=0, bits=8, STREAM=SRAM, FORCE_HW)
#use i2c(Master,Fast,sda=PIN_B5,scl=PIN_B4,force_hw)
#define MODE_CMD 0x01
#define WRITE_CMD 0x02
#define READ_CMD 0x03
#define CS1 PIN_B3
void write_byte(unsigned int32 address, byte val)
{
int8 dummy;
output_low(CS1); //select chip
spi_xfer(SRAM, WRITE_CMD, 8); //send write command
spi_xfer(SRAM, address, 24); //send address
dummy=spi_xfer(SRAM,val,8); //send byte and wait for it to send
output_high(CS1); //deselect
}
byte read_byte(unsigned int32 address)
{
int8 rval;
output_low(CS1); //select
spi_xfer(SRAM, READ_CMD, 8); //send read command
spi_xfer(SRAM, address, 24); //and address
rval=spi_xfer(SRAM, 0, 8); //clock dummy byte to get reply
output_high(CS1); //and deselect
return rval;
}
void main(void)
{
int data;
// setup_spi2(SPI_MASTER | spi_h_to_l | spi_clk_div_16);
output_low(PIN_A2); //make led low
write_byte((int32)5, 0xB5); //writing 0xB5 byte to location 5 sram
data = 0x00; //data is 0x00;
data = read_byte((int32)5); // reading from sram location 5
if (data == 0xB5 ) {output_high(PIN_A2);} //if data equal led on
While(1)
{
;
}
}
|
Last edited by jaikumar on Mon May 13, 2019 6:42 am; edited 3 times in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon May 13, 2019 5:09 am |
|
|
OK, I don't use that PIC but general comments.
1) confirm you can run a '1Hz LED' program. This will show PIC is running at correct speed and wired up right...
2) post your SPI 'device', link to datasheet. You could easily have the 'mode' wrong( there are 4 of them...).The only clue we have is the stream name as SRAM, but again we don't know what mode it requires.
3) add COMMENTS to every line ! While you know(for now..) what is supposed to happen,we don't, making it hard to debug.
4) assuming PIN_A2 has an LED, does it have a current limiting resistor? Also use a #DEFINE so instead of PINS_A2 , you have output_high(LED). Agian, it'll make the code a lot easier to understand. You can also change the pin function in the future by changing just one line of code.
5) as SPI is not working, delete all I2C code. this way you can concentrate on JUST the SPI code.
Jay |
|
|
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
|
Posted: Mon May 13, 2019 5:26 am |
|
|
Thanks temtronic for your reply.
The spi device is 23lc1024 and it is mode 0.
actually it was working when i had the spi device in default pins namely PIN_B5 SDI1,
PIN_B4 SCK1,
PIN_C7 SDO1.
I am trying to relocate these pins so that i can use the i2c adc on pin_B5 and
PIN_B4.
Have i set up the pps correctly.
Thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon May 13, 2019 7:25 am |
|
|
First thing. Don't specify 'bits=8' in your #use.
Only do this, if you are never going to send more than 8bits. You want to send 24bits, and are using a bit number in all the transactions, so leave
this at it's default.
Then make sure you are turning off any analog functions on the pins. A PPS
peripheral takes priority over all digital functions on the pin, but is overridden by any analog function. So CCP inputs and analog inputs
both override the PPS operation.
You don't need 'FORCE_HW', selecting a SPI port by name automatically
implies FORCE_HW.
Then the one that is probably the problem. Input levels.
RB1, has a Schmitt input buffer. Requires the incoming signal to go up to
0.8Vdd on a pin with 5v tolerance. This is high. You need to check that the
output from the EEPROM can pull the signal up to this level, or possibly
add a pull up resistor to this line to help it get to this level. The Voh of the
23lc1024, is only guaranteed to be Vcc-0.5v, which is only just high
enough....
At the high rate you will be sending the SPI, you need to look carefully
at the layout and termination of the lines.
Then also look carefully at the initialisation. Have you got /HOLD held high?.
I'd add PUT to the fuses. Otherwise there is a very real risk that the
code will start before the clock is actually stable. |
|
|
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
|
Posted: Mon May 13, 2019 11:49 pm |
|
|
Thanks Ttelmah.
I changed all those things you said. and moved PIN_B1 to some other pin and it worked thanks for your help once again. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue May 14, 2019 12:30 am |
|
|
Well done. |
|
|
|
|
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
|