• AVR Freaks

Helpful ReplySPI with PIC16F18345

Page: < 12345.. > >> Showing page 2 of 6
Author
DavidBLit
Super Member
  • Total Posts : 1579
  • Reward points : 0
  • Joined: 2012/02/18 13:08:48
  • Location: The Land of Confusion
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 07:45:50 (permalink)
+1 (1)
andre
blah, blah, blah

Don't hijack threads.

Yeah, "//Code and stuff".
#21
andre
Starting Member
  • Total Posts : 82
  • Reward points : 0
  • Joined: 2009/03/23 02:48:42
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 07:50:13 (permalink)
0
DavidBLit
andre
blah, blah, blah

Don't hijack threads.




oke agreed, sorry

-- breaking something that works is a lot easier than the other way around
-- using MPLAB 8.92 + Microstick II +  PIC32MX250F128B
#22
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 12:15:16 (permalink)
+1 (1)
ram1723
am i correct?

Close.


You moved the exchange routine to after the functions that call it, which means you MUST add a line to declare the function to the start of your code. That is why I had it before them.
I don't know why you felt the need to rename the function, or remove all the comments.
 
When you call the read and write functions, you are passing a variable called reg, but you never write any value to it.
Have you actually read the ADXL345 data sheet yet?
Particularly the page that describes all the registers in it.
Right now, you will only be accessing register 0x00, which is the ID register, which is read only.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#23
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 19:02:22 (permalink)
0
I will end this by EOD
#24
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 22:12:44 (permalink)
0
oid spi_write(unsigned char reg, unsigned char dat)
{
LATBbits.LATB6 = 1;
SSPBUF;
reg & = 0b00111111;//ensure WR and MB are both low
ss = 0;//assert SS pin
spi_xhng(reg); //write register, discard received data
spi_xhng(dat);
ss =1; // release SS
LATBbits.LATB6 = 0;
}
 
can you explain me "spi_xhng(reg); //write register, discard received data"
i have studied the datasheet. I am understanding the registers. here we are writing the address right? 
#25
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 22:39:19 (permalink)
+1 (1)
Yes. The first byte sent after asserting CS selects the register.
This is all documented in the datasheet. I'm reading the same datasheet you are...
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#26
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 22:55:11 (permalink)
0
That means when we write address and dummy data it gives nothing (from the wave forms  when WR/MB Low)
 
if we want to read the data from adxl we should write the address , discard the received data and write dummy data, save what we receive ? (R/W high).
 
am i in correct way?
#27
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 23:04:29 (permalink)
0
/* in main */
while (1)
{
spi_write(0x32 , 0);
// Add your application code
// printf("spi read\n\r");
//printf("%d\n\r" , spi_read(0x32));
//printf("%d\n\r" , spi_read(0x33));
printf("%d\n\r" , spi_read(0x32));
//spi_write(0x32 , 0);

__delay_ms(1);
}
/* end main */
unsigned char spi_read(unsigned char reg)
{
unsigned char temp;
SSPBUF; //dummy read to ensure BF is low
reg | = 0b10000000;//ensure RD/WR bit is high
__delay_ms(1);
ss= 0; //assert SS pin
__delay_ms(1);
LATBbits.LATB6 = 1;
spi_xhng(reg); //write register, discard received data
LATBbits.LATB6 = 0;
__delay_ms(1);
LATBbits.LATB6 = 1;
temp=spi_xhng(0); //write dummy data, and save what we receive
printf("%d\r\n" , temp); // printing dummy data
LATBbits.LATB6 = 0; // DEBUGGING
ss = 1;
__delay_ms(1);
return temp;
}
void spi_write(unsigned char reg, unsigned char dat)
{

SSPBUF;
reg & = 0b00111111;//ensure WR and MB are both low
ss = 0;//assert SS pin
LATBbits.LATB6 = 1;
spi_xhng(reg); //write register, discard received data
LATBbits.LATB6 = 0;
__delay_ms(1);
LATBbits.LATB6 = 1;
spi_xhng(dat);
LATBbits.LATB6 = 0;
ss =1; // release SS
__delay_ms(1);

}
unsigned char spi_xhng(unsigned char datout)
{
SSPBUF = datout;
while(!SSP1STATbits.BF) //wait for the exchange to complete
return SSPBUF;
}
 
i changed my code according to datasheet, still not getting 
#28
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 23:12:26 (permalink)
+1 (1)
ram1723
That means when we write address and dummy data it gives nothing (from the wave forms  when WR/MB Low)

You use strange wording. You are not writing "dummy data".
You write the register byte, then the data byte, and ignore the data coming back.
That is exactly what the "write_reg()" function I wrote for you does. I don't understand why you renamed it to "write_spi()".
 

if we want to read the data from adxl we should write the address , discard the received data and write dummy data, save what we receive ? (R/W high).

Yes, that is a fairly accurate description of what my "read_reg()" function does.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#29
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 23:22:42 (permalink)
0
what about the clock we are giving from the master (pic)
 
am i giving correct clock to the slave ?
#30
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 23:39:29 (permalink)
0
Do you have an oscilloscope?
Have you looked?
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#31
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/24 23:43:19 (permalink)
0
nope we do not have oscilloscope. My code is correct or wrong?
 
 
#32
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 03:19:40 (permalink)
0
Can you help me with the code?
#33
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 03:49:44 (permalink)
0
Lets look at your important settings:
[code]SSP1CON1bits.CKP = 1; // IDLE STATE FOR CLOCK IS A HIGH LEVEL
SSP1STATbits.CKE = 1; // CLOCK FROM ACTIVE TO IDLE
SSP1STATbits.SMP = 1; // SAMPLED AT THE END [code]

CKP is 1, so the clock idles high. That matches the diagrams on page 16 of the ADXL345 datasheet.
 
CKE is 1, so data out  changes when the clock changes from active to idle, so on the rising edge.
That is a problem, because the ADXL is sampling the data input on the rising edge, precisely when you are changing it.
You should set CKE = 0, so the data out changes half a clock cycle earlier.
Look at "FIGURE 29-6: SPI MODE WAVEFORM (MASTER MODE)" in the PIC datasheet to get a clearer idea of what the different combinations of CKP and CKE do.
 
SMP = 1, so the PIC will be sampling its data input (SDI) when the clock returns to idle (i.e. at the end of the clock cycle)
That is ok, as the ADXL updates its SDO pin on the falling edge of the clock.
 
So, leave CKP and SMP as they are, and change CKE to 0.
 
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#34
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 05:09:24 (permalink)
0
unsigned char spi_read(unsigned char reg)
{
unsigned char temp;
SSPBUF; //dummy read to ensure BF is low
ss = 0;
scl = 1;
reg |= 0b10000000; //ensure RD/WR bit is high
spi_xhng(reg); //write register, discard received data
scl = 0;

scl = 1;
temp = spi_xhng(0); //write dummy data, and save what we receive
scl = 0;
ss = 1;
return temp;
}
void spi_write(unsigned char reg, unsigned char dat)
{
SSPBUF; //dummy read to ensure BF is low
reg &= 0b00111111; //ensure WR and MB are both low
ss = 0;
scl = 1;
//write register, discard received data
spi_xhng(reg);
scl = 0;

scl = 1;
spi_xhng(dat);
scl = 0;

ss = 1;
}
unsigned char spi_xhng(unsigned char datout)
{
SSPBUF = datout;
while(!SSP1STATbits.BF); //wait for the exchange to complete
return SSPBUF;
}
i did as you said but still no response only '0' i am getting ...sad: sad
#35
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 05:32:59 (permalink)
0
what is "scl" ?
How do you have "ss" defined now?
 
You have not shown the code which is calling these functions.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#36
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 05:36:39 (permalink)
0
Sorry my bad  I just defined them 
#define ss LATCbits.LATC6
#define scl LATBbits.LATB6
 code is unchanged except the CKE
post edited by ram1723 - 2018/01/25 05:37:49
#37
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 05:44:50 (permalink)
0
What is "scl" for?
Is it connected to something, or are you just using it for debugging?
 
Have you tried printing what you read from register 0x00?
That is the one register that you know for sure what it will contain.
 
post edited by qɥb - 2018/01/25 05:48:37

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#38
ram1723
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 05:52:37 (permalink)
0
SCL is nothing but SCK
i.e for clock by master.
 
while (1)
{
spi_write(0x00 , 0);
// Add your application code
// printf("spi read\n\r");
//printf("%d\n\r" , spi_read(0x32));
//printf("%d\n\r" , spi_read(0x33));
printf("%d\n\r" , spi_read(0x00));
//spi_write(0x32 , 0);

__delay_ms(100);
}
 
when i did this i am getting '203' continuously
 
THESE are my connections
PIC                ADXL345
SCL/SCK(RB6) ---> SCL
SDI(RB4)                SDO
SDO(RC5)               SDI
SS(RC6)                  CS
post edited by ram1723 - 2018/01/25 06:18:56
#39
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3345
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: online
Re: SPI with PIC16F18345 2018/01/25 06:15:30 (permalink)
0
You don't set the clock, only csn (chip select not).

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#40
Page: < 12345.. > >> Showing page 2 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5