Helpful ReplyHot!SPI with PIC16F18345

Page: < 12345.. > >> Showing page 3 of 6
Author
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 06:18:17 (permalink) ☄ Helpfulby ram1723 2018/01/29 23:37:32
0
ram1723
SCL is nothing but SCK
i.e for clock by master.

What do you mean?
The SPI peripheral outputs a clock signal while it is doing a transfer.
Why are you switching RB6 around each transfer?
 

when i did this i am getting '203' continuously

Well it was pointless writing to register 00, that is a read-only register.
203 is 0b11001011
229 is 0b11100101
so it looks like you're data is shifted by one bit.
Try changing SMP from 1 to 0 so it samples the data half a clock cycle earlier.
 
I'm going to bed, so you're on your own now.
 
 

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"
#41
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/25 06:50:23 (permalink)
0
from the datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/40001795E.pdf page 360 states :
 
SSPOV <snip> In Master mode, the overflow bitis not set since each new reception (and transmission)is initiated by writing to the SSPBUF register
 
This would suggest that a dummy read is not per sé required in Master mode.
#42
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 06:55:44 (permalink)
0
yes, SSPBUF should be cleared by software
#43
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 06:56:54 (permalink)
0
andre
from the datasheet http://ww1.microchip.com/...eviceDoc/40001795E.pdf page 360 states :
 
SSPOV <snip> In Master mode, the overflow bitis not set since each new reception (and transmission)is initiated by writing to the SSPBUF register
 
This would suggest that a dummy read is not per sé required in Master mode.

As I mentioned in all my comments, the read is to clear the BF flag.
I never mentioned the overflow flag.

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"
#44
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/25 16:53:53 (permalink)
+1 (1)
 
 
http://ww1.microchip.com/downloads/en/DeviceDoc/40001795E.pdf
 
page 381 states :
 
When the application software is expecting to receive
valid data, the SSPxBUF should be read before the
next byte of data to transfer is written to the SSPxBUF.
#45
brownt
Super Member
  • Total Posts : 296
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 17:48:44 (permalink) ☄ Helpfulby ram1723 2018/01/29 23:36:23
+1 (1)
I would use the Microchip Code configurator too, or does it not support your micro.
 
 
 OpenSPI(SPI_FOSC_4, MODE_10, SMPMID );
//pretty sure MODE_10  and mid sample is correct but maybe MODE_11
 char values[6]; //used to hold data read from ADXL345
    
//set up ADXL345
    Chip_Select = 0;
      WriteSPI(0x2D);   //pwr_cntrl register
      WriteSPI(0x00);   //normal mode; 8hz readings
      Chip_Select = 1;
     
    Chip_Select = 0;
      WriteSPI(0x2D);     //pwr_cntrl register
      WriteSPI(0x16);     //sleep when inactive
      Chip_Select = 1;
     
     Chip_Select = 0;
      WriteSPI(0x1E);        //axis X offset register
      WriteSPI(0b000000011);     //offset
      Chip_Select = 1;
     
     Chip_Select = 0;
      WriteSPI(0x2D);   //pwr_cntrl register
      WriteSPI(0x08);   //start measuring
      Chip_Select = 1;
     
      Chip_Select = 0;
      WriteSPI(0x31);   //data format register
      WriteSPI(0b01001000);  //resolution, justification, range, SPI 4/3 wire mode
      Chip_Select = 1;
     
//read ADXL345
      while(1)
      {
      Chip_Select = 0;
      WriteSPI(0xB2);  // setup for reading data from low byte of x axis
      value[0]= ReadSPI();    
      Chip_Select = 1;
 
      Chip_Select = 0;
      WriteSPI(0xB3);  // setup for reading data from high byte of x axis
      value[1] = ReadSPI();    
      Chip_Select = 1;
 
       accelerationX= (((int)values[1])<< 8) | values[0];

      }


 
//the following is for the pic18F4680, so you will likely need to change PIR1, SSPSTAT etc to suit your micro
// but use MCC, so much easier.
 
signed char WriteSPI( unsigned char data_out )
{
  unsigned char TempVar;  
  TempVar = SSPBUF;           // Clears BF
  PIR1bits.SSPIF = 0;         // Clear interrupt flag
  SSPCONbits.WCOL = 0;                        //Clear any previous write collision
  SSPBUF = data_out;           // write byte to SSPBUF register
 
  if ( SSPCON & 0x80 )        // test if write collision occurred
   return ( -1 );              // if WCOL bit is set return negative #
  else
   //while( !SSPSTATbits.BF );  // wait until bus cycle complete
   while( !PIR1bits.SSPIF );  // wait until bus cycle complete  
  return ( 0 );                // if WCOL bit is not set return non-negative#
}

unsigned char ReadSPI( void )
{
  unsigned char TempVar;
  TempVar = SSPBUF;        // Clear BF
  PIR1bits.SSPIF = 0;      // Clear interrupt flag
  SSPBUF = 0x00;           // initiate bus cycle // for 3-wire spi, i changed this from 0x00 to 0xff and it seems to work now??
  //while ( !SSPSTATbits.BF );                  // wait until cycle complete
  while(!PIR1bits.SSPIF);  // wait until cycle complete
  return ( SSPBUF );       // return with byte read
}
 
 
#46
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 18:40:53 (permalink)
0
thank you for the code brownt I will try and let you know. I will use mcc code confg. in the meanwhile
PIR1bits.SSPIF = 0;      // Clear interrupt flag 
explain this why we need this when we are clearing SSPBUF
post edited by ram1723 - 2018/01/25 18:48:18
#47
brownt
Super Member
  • Total Posts : 296
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/25 19:35:10 (permalink)
0
SSP1IF: Synchronous Serial Port (MSSP) Interrupt Flag bit
1 = The Transmission/Reception/Bus Condition is complete (must be cleared in software)
0 = Waiting for the Transmission/Reception/Bus Condition in progress
 
So, I guess the flag lets you know when the bus is available.
I do not know what you mean by ‘SSPBUF is being cleared’, I cannot see that in the code.  The code writes and reads data to SSPBUF. At some point a dummy value such as 00 or FF needs to be written to SSPBUF, and then data is automatically read back from the device. That’s not a choice, but rather it is how the SPI protocol works.
 
I think you could use //while( !SSPSTATbits.BF ); instead of checking the interrupt flag, but I had some issues with it. Out of my depth with the code, which is mostly why I use MCC.
 
 
Also, things specific to the ADXL345 might be best found by Internet search for 'arduino + ADXL345'. The exact code would need to be modified, but it gives you a good idea of how to configure the registers and also connect the hardware for the ADXL345.
 
#48
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 23:31:10 (permalink)
+1 (1)
You can use either (that means "one of") BF or SSPIF to do your handshaking.
The code I have already given ram1723 does the same thing much more elegantly. They just need to get the SPI configuration correctly matching their device.
I would expect changing th eSMP setting will have done that.
I would NOT advise starting again from scratch doing it a totally different way, when it's almost sorted.
 
ram1723
explain this why we need this when we are clearing SSPBUF



you are not "clearing SSPBUF". You are sometimes reading SSPBUF to clear BF.
 

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"
#49
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/26 22:26:31 (permalink)
0
Please check my complete code. I changed my code as you said still i am reading zero only
post edited by ram1723 - 2018/01/30 03:33:41

'//'A'lone Employee'
#50
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/26 22:37:08 (permalink)
0
You're trying to do everything at once.
I think it was a big waste of effort changing to the MCC code, but now you have, have you tried just reading the ID register to see if you get the expected value?
The secret to debugging is taking small steps, trying to solve one problem at a time.
If you try to do it all at once, and it doesn't work, you have no idea WHICH step failed.
 
 

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"
#51
brownt
Super Member
  • Total Posts : 296
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/27 00:25:35 (permalink)
0
hmmm I see, there are a lot of things going on there. Looks like you have used QHB's SPI exchange function, my read and write functions, and used MCC for peripheral pin select of the SPI pins as well as for some other things.
 
Pick one and do that. Either use QHBs code, use my code, or best yet use MCC for everything. It won't be wasted time, assuming you continue with microchip you can use it again and again. It makes many things easy, and so a bit of time spent now learning to use it, would be a good investment.
 
MCC will give you something like the following->
 
void SPI1_Initialize(void)
{    //this will be based on what you chose in the MCC interface, such as MID_SAMPLE etc.
    SSP1STAT = 0x40;      
    SSP1CON1 = 0x21;   
    SSP1ADD = 0x00;
}

uint8_t SPI1_Exchange8bit(uint8_t data)
{  
    SSP1CON1bits.WCOL = 0;
    SSP1BUF = data;
    while(SSP1STATbits.BF == SPI_RX_IN_PROGRESS)
    {
    }
    return (SSP1BUF);
}
 
then you just go like this to read a register
SPI1_Exchange8bit(reg);
ReadValue = SPI1_Exchange8bit(0x00);
 
and like this to write to a register
SPI1_Exchange8bit(regTo WriteTo);
SPI1_Exchange8bit(valueToWrite);
#52
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/27 05:33:57 (permalink)
0
Now i am able to read the Device ID ('0XE5'   see ADXL345 datasheet page 25).  I need to configure the ADXL345 registers to get the proper output. I used MCC code generator and changed the PINmanager.c settings according to my requirement.  when i tried to read the data from 0xB2 i am getting only '0'. Do I need to change anything in the code to read other registers as well.
post edited by ram1723 - 2018/01/27 08:29:04

'//'A'lone Employee'
#53
brownt
Super Member
  • Total Posts : 296
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/27 22:47:31 (permalink)
0
So you can read register 0x00. Can you read any other register? Trying stepping through them one at a time and see what you get.
Are you using 3 or 4 wire mode (format register 0x31).
 
If the SPI settings are not correct you can get some incorrect things happening like being able to read some registers and not others, and reading incorrect values. Settings like the sample point and mode.
 
I can't remember why I used 0xB2 rather than 0x32.
0xB2 - 0x32 = 0x80, so it seems the last bit needs to be high. When you read register 0x00, did you read 0x00 or 0x80. I can't remember why i did this, maybe my SPI functions or settings were not correct. (It was before I discovered MCC). Try 0x32 for the low byte of the X axis as stated by the datahseet.
 
Anyway, start with register 0x00, read it in a loop, and increment it once each time, to see what you are reading from the other registers.
 
char reg = 0;
 
  while(1)
{
      Chip_Select = 0;
      SPI1_Exchange8bit(reg);
      value[0]= SPI1_Exchange8bit(0x00); 
      Chip_Select = 1;
     
//display or otherwise read the value
     __delay_ms(250); // time to see the read value
      reg++;
}
 
#54
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/27 23:02:07 (permalink)
0
I am using 4 wire SPI.
 
And coming to 0xB2 (0b10110010) and 0x(0b00110010) see here bit 7 represents the R/W operations thats it. 0x32 is x-axis MSB as per the datasheet.
 
A small change in your loop to read all the registers.
 
it should be SPI1_Exchange8bit(reg^(1<<7)); // 7th bit in every register address is zero.
 
 
still i am facing few problems while reading multiple bytes.
 
 
post edited by ram1723 - 2018/01/27 23:05:49

'//'A'lone Employee'
#55
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/27 23:04:59 (permalink)
0
brownt
 
 
I can't remember why i did this, maybe my SPI functions or settings were not correct. (It 
 


yes may be i changed pinmanger.c after mcc generated spi code 




'//'A'lone Employee'
#56
davekw7x
Entropy++
  • Total Posts : 1692
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Left Coast, USA
  • Status: offline
Re: SPI with PIC16F18345 2018/01/28 18:14:31 (permalink) ☄ Helpfulby ram1723 2018/01/29 23:36:54
+1 (1)
ram1723
Please check my complete code. I changed my code as you said still i am reading zero only



Well...

////NO!!! This sets 3-wire mode.  That won't work!
    //WriteSPI(0b01001000); //resolution, justification, range, SPI 4/3 wire mode
// Try this:
//
    WriteSPI(0b00001000); //resolution, justification, range, SPI 4 wire mode


Actually, for starters, the ONLY thing you need is to write 0x08 to register 0x2D to set the Measure bit in the POWER_CTL register.  I suggest you leave everything else at default values until you get things going.  (That works for me.)

Since you didn't make any PPS assignment for SDI1, I assume you are connecting MISO from the ADXL345 module to the default SDI1 pin on your PIC.  That works for me, but I usually make the PPS assignment explicitly as a reminder for documentation purposes.  Also, it's a bookmark in case I decide to change it some day.

If your connections are OK and your ADXL module is OK, this will work.  Although my project uses a PIC24 with a much more sophisticated SPI module, I have tested my ADXL345 with a PIC16F18346 using your code. (Other than the bogus 3-wire bit that you set in the DATA_FORMAT register that I flagged above.)

Actually, I had no problems with MCC-generated code for either chip.  In both cases I used multi-byte reads to get the X,Y,Z registers all together, but reading individual bytes also works.  Reading all of the X,Y,Z values in one fell swoop (or is it in one swell foop?) is recommended and is easy with the MCC-generated SPI1_Exchange8bitBuffer function.
 
[Edit]
Nothing to do with SPI, but it bugs me:
I don't know why the heck you changed your oscillator initialization, but you ended up with system freq = 4 MHz rather than 32 MHz.  That's why you had to kludge your UART BRG settings to get your desired bit rate.
Anyhow, if you change the code to give 32 MHz, then __delay_ms(1000) will give about one second between readings.  (Or you could leave your initialization function as is and change the definition of _XTAL_FREQ to 4000000 instead of 32000000.)
My point is that you still have some testing (and, maybe, some debugging) to go.  "Little things" like specifying incorrect system frequencies will haunt you.  Trust me: Been there, done that, have the scars to show for it!
Bottom line:
Whether I am using MCC or doing it manually, the formulas use _XTAL_FREQ to set up timers, bit rates,etc.  If _XTAL_FREQ isn't consistent with actual operating frequency, it's just one uncalled-for headache after another!
[/Edit]
 
 
Regards,
 
Dave
 
post edited by davekw7x - 2018/01/28 19:13:05

Sometimes I just can't help myself...
#57
ram1723
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2018/01/16 22:27:23
  • Location: 0
  • Status: offline
Re: SPI with PIC16F18345 2018/01/28 22:38:02 (permalink)
0
Hey Dave thank you for your suggestions. I already done that as you said. Configured as 4 wire SPI and used MCC code generator. Now I successfully read the device ID 0xE5

Then I put adxl in measure mode as you mentioned. Please forget about my attached program file. I am getting the values but getting last reading zero when I am reading multiple bytes at a time ?....

I tried as you said leaving other register in default mode but getting zero in last reading

'//'A'lone Employee'
#58
davekw7x
Entropy++
  • Total Posts : 1692
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Left Coast, USA
  • Status: offline
Re: SPI with PIC16F18345 2018/01/29 08:07:00 (permalink)
+1 (1)
ram1723
....Please forget about my attached program file....

I hate to repeat myself, but I ran the exact code from your post that I was quoting from (except for the one statement that I told you that I changed), and it gave me satisfactory printed values of the readings.  Later I added "\r\n" to the print statements to give reasonable looking output, but initially I made no other changes.  Really.  It worked.
 
[Edit]
I didn't tell you how I tested and observed "satisfactory" values.  Test methodology is important, so here goes...
 
For these tests my ADXL breakout board is just dangling from some jumpers connected to my Curiosity board with a PIC16F18345.  (My original tests were with '18346, but I repeated them for a '18345 to be same as yours.)
Now...
If the ADXL is placed on a perfectly horizontal surface, it gives an x value of 0.  If I twist it along the x-axis and/or the y-axis, values go from something near -256 to something near +256, as reported by the print statements in your program.  That's what I expected and that's what I saw on my PC running minicom at 19200 baud, connected through an RS232-to-Logic converter with Tx connected to the PIC RA2 pin:
 
x value : 46write blockedhello
write blockedhello
x value : -66write blockedhello
write blockedhello
x value : -61write blockedhello
write blockedhello
x value : -132write blockedhello
write blockedhello
x value : -204write blockedhello
write blockedhello
x value : -187write blockedhello
write blockedhello
x value : -184write blockedhello
write blockedhello
x value : 126write blockedhello
write blockedhello
x value : 248write blockedhello
write blockedhello
x value : 237write blockedhello
write blockedhello


This is with your "free" configuration and XC8 version 1.45
[/Edit]
 
I think you change stuff and change stuff and change stuff without letting us know what you are actually running so that it is impossible for anyone to give help.
 
I mean, there are a number of stylistic and other changes, minor and major, that I would make for my own project, but I wanted to keep things as close as possible to what you gave us to work with so that maybe, just maybe, we could help you you to get off of top dead center.
 
Sorry.
 
Regards,

Dave
post edited by davekw7x - 2018/01/29 09:34:46

Sometimes I just can't help myself...
#59
DavidBLit
Super Member
  • Total Posts : 1574
  • Reward points : 0
  • Joined: 2012/02/18 13:08:48
  • Location: The Land of Confusion
  • Status: offline
Re: SPI with PIC16F18345 2018/01/29 08:15:59 (permalink)
0
...which is why I watch too many of these threads lately with a combination of amusement, horror, and pity for you guys that are such good spoon-feeders.  Such a waste.  sad: sad

Yeah, "//Code and stuff".
#60
Page: < 12345.. > >> Showing page 3 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5