CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Can't read data with SPI using PIC16f18346
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



Joined: 01 Jul 2010
Posts: 7688
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 4:27 am     Reply with quote

I have a PDF, SLAU817.pdf from TI for the eveluation board. It shows the 'TEST' pin, 'TE' as pin #9. It is not connected however there is a 'pad' next to it. You'll have to solder a wire from it to VDD to get SPI mode to function.
As shipped, it's only designed for 'UART' communications, not SPI.
If you can't solder the PCB, I suggest using the UART communications. I assume TI and others have documentation and test code for this.
The document does say that there's a 3 bit 'address' needed for UART operation....
Ttelmah



Joined: 11 Mar 2010
Posts: 16429

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 6:52 am     Reply with quote

OK.
So you have the PIC running off 3.3v.
You have the PGA460 board running off what?.
Needs to be between 6 & 15v.
What clock rate are you running the PIC?.
Though the SPI is specified to 8MHz on this chip, there is a lot of dead
time needed between sending a command and reading the reply. You
need to pause for this.

Then there is a second huge issue. The data format is LSb first. SPI as
standard sends MSb first.

I have to ask why you are insisting on trying to use SPI, when the chip
is really designed for UART comms?. It actually implements sync or
async standard serial. The former is 'similar' to SPI, but not quite the same.
Key big difference is the bit ordering is reversed....


Last edited by Ttelmah on Wed Aug 12, 2020 6:58 am; edited 1 time in total
L.T.



Joined: 24 Jul 2020
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 6:58 am     Reply with quote

Thanks for reply again. I can try this solution you told but I tried this module with MSP430F5529, I used the same code. Only difference is changed some things for PIC. When I used module's pin (RX, TX, SCLK) with MSP's pin (SDI,SDO,SCLK) and it worked. I could succesful to measure the distance. I want to do this with PIC16F18346. I applied 3.3V supply voltage to both PIC and MSP. Still telling me I need to solder the test pin to Vdd? What will be the benefit of this?
Ttelmah



Joined: 11 Mar 2010
Posts: 16429

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 7:01 am     Reply with quote

No, the test pin needs to go up if you are using a 5v PIC.
You did not at that point tell us you were using a 3.3v PIC.

The most likely big issue is the bit order. The SPI interface sends the most
significant bit first. The USART sends least significant bit first. I's suspect
the code for the interface on the MSP is sending least significant bit
first....

The spi_xfer routines, can handle this for you. Specify LSB_FIRST in
#USE_SPI, and use spi_xfer instead of spi_read and spi_write.
L.T.



Joined: 24 Jul 2020
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 7:08 am     Reply with quote

Actually, I said the voltage before , I guess you didn't see.

Quote:
I used 3.3V power supply to PIC16F18346, 7.6V power supply to PGA460PSM-EVM. PGA460 communicates 3.3V SPI signals.


And I said the reason of use SPI.

Quote:
I want to use SPI because I will use 2 PGA module and I need 2 port SPI or UART. I have PIC16F18346 and it has 2 SPI. Therefore I have to use SPI protocol.


Oscillator frequency of PIC16F18346 is 16000000. And I used Fosc/16 for SPI. It is recommended clock division for PGA.

Since the PGA460 performs an SPI communication where the first LSB bit is sent, I provided transmission by converting from MSP to LSB in encoding.

Code:
    // MSB --> LSB /////////////////////////////////////////////////
   _data = (((_data & 0b11110000)>>4) | ((_data & 0b00001111)<<4));
   _data = (((_data & 0b11001100)>>2) | ((_data & 0b00110011)<<2));
   _data = (((_data & 0b10101010)>>1) | ((_data & 0b01010101)<<1));
   ////////////////////////////////////////////////////////////////
Ttelmah



Joined: 11 Mar 2010
Posts: 16429

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 7:53 am     Reply with quote

No, you hadn't said the voltage before I talked about the test pin. You did
immediately afterwards.
You said you were using 3.3v i/o, which could easily mean you were
trying to use a 3.3v spi device with a 5v PIC.

Two modules can be used with serial. The chip supports up to 8 on a
single UART. You just program a new address for the second chip.
L.T.



Joined: 24 Jul 2020
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 8:47 am     Reply with quote

Do you have any idea about my problem which is unsuccessful reading with SPI? I don't prefer work with UART for now. A true or false signal is coming from the slave. I can watch this signal on the oscilloscope from the PIC's MISO pin. Why is it not reading the signal coming to its pin, even if it receives the signal? It can't read anything. I shared with you my codes before. What do you think about problem?
PCM programmer



Joined: 06 Sep 2003
Posts: 21232

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 9:11 am     Reply with quote

Instead of doing it your way:
Quote:
mSSP1BUF.reg = _data;
while(!SSP1IF); //wait
uint8 flagg = SSP1IF;
mSSP1BUF.reg = 0b11111111;
while(!SSP1IF); //wait

_regdata=mSSP1BUF.reg;

Why don't you try it the CCS way:
Code:
 spi_write(_data); 
_regdata = spi_read(0xFF); 

See if it works better.

---------------

Also, can you post a schematic of your connections between the PIC
and the PGA460 ? Upload the schematic to an image hosting website
such as https://imgur.com/upload and then post a link to it here.
Ttelmah



Joined: 11 Mar 2010
Posts: 16429

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 11:15 am     Reply with quote

Thing is you have not posted any complete program that we can look at.
Lots of errors in what you do post (pre compiler directives posted as
'code' lines, which they are not), and things like int8's being rotated
left, which risks stuff going out the end of the register etc...
Also 'double' is pointless, the PIC does not support the double type.
As a partial example, since you don't tell us where your clock actually
comes from etc:
Code:

#include "16F18346.h"
#use delay(INTERNAL=16MHz)

#pin_select SDI1=PIN_B4
#pin_select SCK1IN=PIN_B6
#pin_select SCK1OUT=PIN_B6
#pin_select SDO1=PIN_C7

#use spi(SPI1, baud=1MHz, LSB_FIRST, MODE=1, stream=PGA)
#define BURST&LISTEN 0x0
#define READ_RESULT (0x5<<3)

//Now the data sheet says you need to program the device. See no sign
//of this being done.
//1. On PGA460 power up, the master configures the following:
//– EEPROM by using the EEPROM bulk write command
//– Time-varying gain by using the time-varying gain bulk write command
//– Threshold parameters by using the threshold bulk write command or by //independently writing to a
//particular parameter by using the register write command
//Where are the commands for this....
//How quickly the chip can be read depends on parameters that are set here

unsigned int16 time(void)
{
    //read the 16bit time reply
    union {
       unsigned int32 value;
       unsigned int8 bytes[4];
   } rval;
   int ctr;
   //Now it seems that you need to issue a listen, then wait for the
   //sampling interval and read the reply.
   spi_xfer(PGA,BURST&LISTEN,8);
   delay_ms(5); //this depends on config needs to be setup
   spi_xfer(PGA,READ_RESULT,8);
   for  (ctr=0;ctr<3;ctr++)
       rval.bytes[ctr]=spi_xfer(PGA, 0, 8);
   //Now if chip has replied, distance is low 16 bits of reply
   return ((int16)rval.value);
}

void main(void)
{
   unsigned int16 tval;
   delay_ms(500); //ensure everything has time to wake
   //Try just reading times
   while (TRUE)
   {
       tval=time();
       printf("time %08lX\n", tval);
       delay_ms(500);
   }
}

I see no sign in the code you post, of trying to setup the configuration,
or of triggering a reading.

In what you post, you need to clear the SSPIF, before you send the
byte and try to read the reply. Otherwise it'll already be set, and you will
just read back what is in the buffer, not the reply....
L.T.



Joined: 24 Jul 2020
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 11:06 pm     Reply with quote

PCM programmer wrote:
Instead of doing it your way:
Quote:
mSSP1BUF.reg = _data;
while(!SSP1IF); //wait
uint8 flagg = SSP1IF;
mSSP1BUF.reg = 0b11111111;
while(!SSP1IF); //wait

_regdata=mSSP1BUF.reg;

Why don't you try it the CCS way:
Code:
 spi_write(_data); 
_regdata = spi_read(0xFF); 

See if it works better.

---------------

Also, can you post a schematic of your connections between the PIC
and the PGA460 ? Upload the schematic to an image hosting website
such as https://imgur.com/upload and then post a link to it here.


I tried, nothing has changed. :(
PCM programmer



Joined: 06 Sep 2003
Posts: 21232

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 11:12 pm     Reply with quote

You almost certainly have multiple problems.

Did you try Ttelmah's code ?

Are you going post a link to your schematic.
L.T.



Joined: 24 Jul 2020
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 11:23 pm     Reply with quote

Ttelmah wrote:
Thing is you have not posted any complete program that we can look at.
Lots of errors in what you do post (pre compiler directives posted as
'code' lines, which they are not), and things like int8's being rotated
left, which risks stuff going out the end of the register etc...
Also 'double' is pointless, the PIC does not support the double type.
As a partial example, since you don't tell us where your clock actually
comes from etc:
Code:

#include "16F18346.h"
#use delay(INTERNAL=16MHz)

#pin_select SDI1=PIN_B4
#pin_select SCK1IN=PIN_B6
#pin_select SCK1OUT=PIN_B6
#pin_select SDO1=PIN_C7

#use spi(SPI1, baud=1MHz, LSB_FIRST, MODE=1, stream=PGA)
#define BURST&LISTEN 0x0
#define READ_RESULT (0x5<<3)

//Now the data sheet says you need to program the device. See no sign
//of this being done.
//1. On PGA460 power up, the master configures the following:
//– EEPROM by using the EEPROM bulk write command
//– Time-varying gain by using the time-varying gain bulk write command
//– Threshold parameters by using the threshold bulk write command or by //independently writing to a
//particular parameter by using the register write command
//Where are the commands for this....
//How quickly the chip can be read depends on parameters that are set here

unsigned int16 time(void)
{
    //read the 16bit time reply
    union {
       unsigned int32 value;
       unsigned int8 bytes[4];
   } rval;
   int ctr;
   //Now it seems that you need to issue a listen, then wait for the
   //sampling interval and read the reply.
   spi_xfer(PGA,BURST&LISTEN,8);
   delay_ms(5); //this depends on config needs to be setup
   spi_xfer(PGA,READ_RESULT,8);
   for  (ctr=0;ctr<3;ctr++)
       rval.bytes[ctr]=spi_xfer(PGA, 0, 8);
   //Now if chip has replied, distance is low 16 bits of reply
   return ((int16)rval.value);
}

void main(void)
{
   unsigned int16 tval;
   delay_ms(500); //ensure everything has time to wake
   //Try just reading times
   while (TRUE)
   {
       tval=time();
       printf("time %08lX\n", tval);
       delay_ms(500);
   }
}

I see no sign in the code you post, of trying to setup the configuration,
or of triggering a reading.

In what you post, you need to clear the SSPIF, before you send the
byte and try to read the reply. Otherwise it'll already be set, and you will
just read back what is in the buffer, not the reply....


You can download on this link the codes which is for MSP430. I convert only some things for PIC. : https://www.ti.com/technical-documents/mytilit/export-control?litId=SLAC741I&fileType=zip&ref_url=https%3A%2F%2Fwww.ti.com%2Ftool%2FPGA460PSM-EVM&ts=1597295728322

What is purpose of writing 0x5<<3 in READ_RESULT? Why was a 16-bit time planned? What variable is PGA written in spi_xfer (PGA, BURST & LISTEN, 8)? Why did we read the time? I could not understand anything.
L.T.



Joined: 24 Jul 2020
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 11:30 pm     Reply with quote

PCM programmer wrote:
You almost certainly have multiple problems.

Did you try Ttelmah's code ?

Are you going post a link to your schematic.


You can download here : https://imgur.com/a/lSpMS0N
Ttelmah



Joined: 11 Mar 2010
Posts: 16429

View user's profile Send private message

PostPosted: Thu Aug 13, 2020 12:20 am     Reply with quote

OK. I've made a 'silly' in what I posted, since I took the names from the
data sheet, and '&' is (of course) a C keyword. So this needs to change to AND
in the #define and where used later.
We can't see the TI documents. They require a login.
The point though is that you have to send the command to trigger a
pulse, then wait and then issue a read.
The read result command returns the time in the low 16 bits of the 32bit reply.

There are lots more issues looking at what you have posted. For instance,
you write to the port registers to clear the ports. Wrong. The PIC you are
using is one that has separate LAT registers. To clear the port it is these
that need to be written. Trying to access at the register level, requires you
to get the accesses right. Much easier to actually use the compiler
functions to do these things, which will access the correct registers....
Ttelmah



Joined: 11 Mar 2010
Posts: 16429

View user's profile Send private message

PostPosted: Thu Aug 13, 2020 1:28 am     Reply with quote

Looking further, there are so many issues, that you need to do a lot of work:

1) As I have already said, SPI has the bit order reversed compared to the
UART. Look in the code library, you are going to have to reverse the bit
order of every byte you send (and receive). Alternatively, use software SPI,
which will then allow the LSB_FIRST option. This is actually the neatest solution.
2) Then your SPI mode is wrong. Ti disagree with themselves on the mode
number needed, but in the forums, the general opinion is that mode=2 is
the one that works.
I'll have a play, and see if I can assemble something that may work.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Page 2 of 7

 
Jump to:  
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