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
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 6:25 am     Reply with quote

No,
You set the bit you have 'called' SSPSTATbitsCKE to '1', but you have
never shown us how you declare this.
No guarantees this is being correctly set.
L.T.



Joined: 24 Jul 2020
Posts: 62

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 6:30 am     Reply with quote

Ttelmah wrote:
No,
You set the bit you have 'called' SSPSTATbitsCKE to '1', but you have
never shown us how you declare this.
No guarantees this is being correctly set.


Did you look to pictures? I think, settings are true, I have a problem about data sending.

Code:

    #define RAMAdrDef(tipdef,addef,adresdef)   tipdef addef;#locate addef=adresdef
//TYPE DESCRIPTION

typedef signed char         sint8;
typedef signed long         sint16;
typedef signed long long   sint32;
typedef unsigned char      uint8;
typedef unsigned long      uint16;
typedef unsigned long long   uint32;
RAMAdrDef(aint8,mSSPSTAT,0x214)
//----- SSPSTAT Bits -----------------------------------------------------

#define SSPSTATbitsBF               mSSPSTAT.bit0
#define SSPSTATbitsUA               mSSPSTAT.bit1
#define SSPSTATbitsR_NOT_W          mSSPSTAT.bit2
#define SSPSTATbitsS                mSSPSTAT.bit3
#define SSPSTATbitsP                mSSPSTAT.bit4
#define SSPSTATbitsD_NOT_A          mSSPSTAT.bit5
#define SSPSTATbitsCKE              mSSPSTAT.bit6
#define SSPSTATbitsSMP              mSSPSTAT.bit7
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 6:50 am     Reply with quote

What you post won't compile, since you must have another set of stuff
defining the bits. It is also B&*%% stupid code. Use the compiler
functions, instead of this 'DIY' approach:
Code:

//TYPE DESCRIPTION

typedef signed char         sint8;
typedef signed long         sint16;
typedef signed int32        sint32;
typedef unsigned char      uint8;
typedef unsigned int16     uint16;
typedef unsigned lint32  uint32;
//Don't use terms like 'long', and 'long long', since these have
//different meanings on different compilers. Use the explicit sizes.

#byte mSSPSTAT = 0x214

//----- SSPSTAT Bits -----------------------------------------------------

#bit SSPSTATbitsBF =   mSSPSTAT.0
#bit SSPSTATbitsUA =   mSSPSTAT.1
#bit SSPSTATbitsR_NOT_W  =  mSSPSTAT.2
#bit SSPSTATbitsS   =  mSSPSTAT.3
#bit SSPSTATbitsP   =  mSSPSTAT.4
#bit SSPSTATbitsD_NOT_A   = mSSPSTAT.5
#bit SSPSTATbitsCKE =  mSSPSTAT.6
#bit SSPSTATbitsSMP =  mSSPSTAT.7


This then correctly compiles, and addresses what it is meant to do.
temtronic



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

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 7:14 am     Reply with quote

If you're going to the trouble of 'bit level' coding (NOT use the CCS functions), simply code it assembler. With about 35 instructions, PIC assembler is easy to learn and faster to code.
I started programming PICs 'preCCS', so almost 30 years ago. once I got PCM 2.534, I had to self teach myself 'C'. I use any CCS function I can as I know it'll work. Life is too short to be 'bit twiddling' in a high level language !

BTW, I'd still like to see how TI configures the part for SPI vs. UART mode.

Jay
L.T.



Joined: 24 Jul 2020
Posts: 62

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 8:45 am     Reply with quote

Ttelmah wrote:
What you post won't compile, since you must have another set of stuff
defining the bits. It is also B&*%% stupid code. Use the compiler
functions, instead of this 'DIY' approach:
Code:

//TYPE DESCRIPTION

typedef signed char         sint8;
typedef signed long         sint16;
typedef signed int32        sint32;
typedef unsigned char      uint8;
typedef unsigned int16     uint16;
typedef unsigned lint32  uint32;
//Don't use terms like 'long', and 'long long', since these have
//different meanings on different compilers. Use the explicit sizes.

#byte mSSPSTAT = 0x214

//----- SSPSTAT Bits -----------------------------------------------------

#bit SSPSTATbitsBF =   mSSPSTAT.0
#bit SSPSTATbitsUA =   mSSPSTAT.1
#bit SSPSTATbitsR_NOT_W  =  mSSPSTAT.2
#bit SSPSTATbitsS   =  mSSPSTAT.3
#bit SSPSTATbitsP   =  mSSPSTAT.4
#bit SSPSTATbitsD_NOT_A   = mSSPSTAT.5
#bit SSPSTATbitsCKE =  mSSPSTAT.6
#bit SSPSTATbitsSMP =  mSSPSTAT.7


This then correctly compiles, and addresses what it is meant to do.


I controlled all of these with watch screen. Everythinq is okay whatever I set with my description. Please give an idea of why the signal is this way, rather than getting stuck in the unnecessary point.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 4:29 pm     Reply with quote

L.T. wrote:

Actually data I sent is 10101010 10100000 01011111 01111111 01111111 01111111 01111111 01111111.

The problem occurs in the section I wrote in red. Actually it sends 10100000 data, I don't know why at the end of the data is 1, it should have remained 0.

https://imgur.com/jiH2cB5

And I caught another problem. It should have remained 1 at the end of the 01011111 data.

Why do you care what the data is after the 8 clocks are gone ?
There is no reason to care. In SPI mode 2, the data is clocked in on
the falling edge of clock.

Here is a link for the SPI modes. Note, mode 2 samples on the falling edge.
https://www.totalphase.com/support/articles/200349236-SPI-Background#modes

I have modified your scope diagram of "bad" data. I have put a red line
on the falling edges of clock for bytes 2 and 3 which you say are bad.
Notice those two bytes are 10100000 01011111 and that is desired data.
So I don't know what you are complaining about. There is nothing to
complain about.

Click on the thumbnail below to get a mid-size image. Then click on
the mid-size image to get the full size image. That's what you want.
L.T.



Joined: 24 Jul 2020
Posts: 62

View user's profile Send private message

PostPosted: Thu Aug 27, 2020 11:42 pm     Reply with quote

Because the data sent by the PGA is not true. It must change according with distance but it is sending same signal. When I set CKE and CKP as 0, then the data sent by the PIC is regular and the data sent by the PGA is according the distance but not same with using MSP. Because of this, I cant read the distance. I have to use mode 2 and all data must be regular as using MSP.
temtronic



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

View user's profile Send private message

PostPosted: Fri Aug 28, 2020 4:53 am     Reply with quote

comment..
SPI devices use one of 4 'modes'. Somewhere in the PGA device, TI will say which mode it uses. I presume it's mode '0' as you say it works (but not for distance). While I have only used a handful of SPI devices, none have ever been reprogrammable as to the mode they use. To be able to program the mode, it'd have to have at least 4 XOR gates, some EEPROM and a 'mode select engine' with some kind of 'lock' key, VDD sensing, auto reload, ...the more I think about it, it gets more complicated. You can't have a device changing it's mode for ANY reason. If that happens, it'll never communicate properly with the host.

The TI EVM works fine using their UART based software, your PIC based SPI doesn't. The device uses the same pins for both UART and SPI so 2 conclusions can be seen.
1. That the SPI based software is faulty. This can be either your main PIC program or the PGA setup 'header'. As I've said before, we need to see the header files, the ones TI uses to configure the device. It's quite possible it's NOT configuring the device properly for SPI communications.

2. Possible hardware failure. Again, this device uses the SAME pins for UART and SPI. There could be a flaw inside the chip, say a bridge between the SPI engine and the UART engine that allows data to mix.

3. You say it works but the 'distance' numbers are wrong. If the numbers are 100% for the demo TI programs, then this indicates the 'setup' or 'configuration' of the device when using the PIC program are wrong. This 'setup' is probably done in those 2 'header' files that I keep asking to see. Say that device has 20 or 30 'registers' that need to be configured. Just ONE bit of one register not set could easily cause the problem you're having.

The TI demo code is UART based, it works because they spent a lot of time and effort to make that work. I haven't seen a TI supplied SPI version of that code. The answer is in those 2 header files.
Well I've spent an hour and can't find ONE program from TI, even in their 'Software Devlopement Guide' that shows how to use SPI.... I'm thinking that part of the 'control logic' within the device is busted.

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 28, 2020 11:25 am     Reply with quote

Jay, these are very good points.
temtronic



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

View user's profile Send private message

PostPosted: Fri Aug 28, 2020 1:09 pm     Reply with quote

I have to admit I like 'challenges' like this post, but without the real hardware it is a PAIN to debug. Hate to be a 'broken record' but I'd still like to see TIs 'setup/configure' code to make the device a UART or SPI or OW device. I expect some kind of 'unlock' sequence then the 'config' parameters, update and store, then a 'lock' command. Otherwise some 'data' sent to it could reconfigure the device for ?? mode of operation.

Jay
L.T.



Joined: 24 Jul 2020
Posts: 62

View user's profile Send private message

PostPosted: Tue Sep 01, 2020 8:36 am     Reply with quote

I have a trouble about CCS C Compiler!!

I described pgau12 array as GLOBAL REGISTER. I debugged the data, I looked to watch screen and I dont like I saw. It didn't write values in register and when I looked to .sym docs, I saw register addresses are not same in the watch screen and in the .sym docs.

When I run the code with XC8 compiler, it works. CCS is trouble.

You can see follows.

WATCH SCREEN:
first address:
https://imgur.com/BgmA9Zd
last address:
https://imgur.com/mcdHthp

.SYM DOCS:
https://imgur.com/cg8SwJv

Global register definition:
https://imgur.com/GnehCLv
https://imgur.com/rCmK7CE

Data to be written to the Register:
https://imgur.com/UqOEsMj

Data written in the register:
"\x aa\x 30\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 0\x 88\x 88\x 88\x 88\x 88\x 88\x 0\x 12\x bd\x 23\x 84\x 21\x 8\x 42\x 0\x 0\x 0\x 75\x 1b\x 3f\x 23\x 0\x 0\x 0\x 12\x 10\x 1\x 0\x 0\x 0\x 0"

There is only a problem with this register and there is no difference with other registers! Please give a logical reason because CCS is starting to tire me a lot. I don't want to revert to XC8 as I have a delay problem with the XC8. Please don't suggest "you should not define registers yourself, use CCS ready commands". Other registers were defined in the same way, but this problem was not experienced in any of them. If you share your e-mail address, I can share my code with you.
temtronic



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

View user's profile Send private message

PostPosted: Tue Sep 01, 2020 12:28 pm     Reply with quote

If there's only ONE register that doesn't store data correctly, create/compile/post a small program that this happens.
If it's say 'register 0x13' then only write/read that register.
Post your code so we might be able to see what's happening.
L.T.



Joined: 24 Jul 2020
Posts: 62

View user's profile Send private message

PostPosted: Tue Sep 01, 2020 11:44 pm     Reply with quote

temtronic wrote:
If there's only ONE register that doesn't store data correctly, create/compile/post a small program that this happens.
If it's say 'register 0x13' then only write/read that register.
Post your code so we might be able to see what's happening.


I deleted unnecessary parts from code. But I get same result.

Main code (GetDistance.c):
Code:


#include <ussc.h>

#ifndef FoscDef
    #define FoscDef 24000000
#endif
#fuses RSTOSC_HFINTRC,LVP,DEBUG

#define userInputMode     
  BYTE commMode = 3;           
  BYTE fixedThr = 1;           
  BYTE xdcr = 1;               
  BYTE uartAddrUpdate = 0;     
  uint32 baudRate = 16;       
  void initPGA460();

void initPGA460() {
    pga460_initBoostXLPGA460(commMode, baudRate, uartAddrUpdate);
    if (fixedThr != 72){pga460_initThresholds(fixedThr);}
    if (xdcr != 72){pga460_defaultPGA460(xdcr);}
}
void setup() {   
   mTRISA.reg  =0b00000000;       //RA2=OUTPUT (BUZZER Pin)
   mTRISB.reg  =0b00110000;      //RB4=RB5=INPUT (MISO Pins)   RB6=RB7=OUTPUT   (SCLK Pins)
   mTRISC.reg  =0b00010000;      //RC0=RC4=RC7=OUTPUT (GV_ACTIVE and MOSI Pins)      
    mANSELA.reg =0b00000000;      //PORTA is completely digital
   mANSELB.reg =0b00000000;      //PORTB is completely digital
   mANSELC.reg =0b00000000;      //PORTC is completely digital
    mPORTA.reg  =0b00000000;      //Clear PORTA
   mPORTB.reg  =0b00000000;      //Clear PORTB
   mPORTC.reg  =0b00000000;      //Clear PORTC
   mSLRCONA.reg = 0x00;
   mSLRCONB.reg = 0x00;
   mSLRCONC.reg = 0x00;
    initPGA460();
}
void main(){
   setup();
   for(;;){          
   }
}


ussc.h file:
Code:

    #include "PGA460_SPI.h"
   #include "RegType.h"
    #define FoscDef 24000000
    #define F_CPU FoscDef
    #fuses RSTOSC_HFINTRC,LVP,DEBUG
    #use delay(clock=FoscDef)

   void pga460_initBoostXLPGA460(BYTE mode, uint32 baud, BYTE uartAddrUpdate);
   void pga460_defaultPGA460(BYTE xdcr);
   void pga460_initThresholds(BYTE thr);
   BYTE pga460_calcChecksum(BYTE cmd);
   void pga460_spiTransfer(BYTE* mosi, BYTE size);
   void pga460_spiMosiIdle(BYTE size);
   BYTE datareverse(BYTE* senddat, BYTE size);
   
      BYTE P1BL = 0x00;
      BYTE P2BL = 0x01;
      BYTE P1LO = 0x02;
      BYTE P2LO = 0x03;
      BYTE TNLM = 0x04;
      BYTE UMR = 0x05;
      BYTE TNLR = 0x06;
      BYTE TEDD = 0x07;
      BYTE SD = 0x08;
      BYTE SRR = 0x09;
      BYTE SRW = 0x0A;
      BYTE EEBR = 0x0B;
      BYTE EEBW = 0x0C;
      BYTE TVGBR = 0x0D;
      BYTE TVGBW = 0x0E;
      BYTE THRBR = 0x0F;
      BYTE THRBW = 0x10;
      BYTE BC_RW = 0x16;
      BYTE BC_EEBW = 0x17;
      BYTE BC_TVGBW = 0x18;
      BYTE BC_THRBW = 0x19;
   BYTE USER_DATA1 = 0x00;
   BYTE USER_DATA2 = 0x00;
   BYTE USER_DATA3 = 0x00;
   BYTE USER_DATA4 = 0x00;
   BYTE USER_DATA5 = 0x00;
   BYTE USER_DATA6 = 0x00;
   BYTE USER_DATA7 = 0x00;
   BYTE USER_DATA8 = 0x00;
   BYTE USER_DATA9 = 0x00;
   BYTE USER_DATA10 = 0x00;
   BYTE USER_DATA11 = 0x00;
   BYTE USER_DATA12 = 0x00;
   BYTE USER_DATA13 = 0x00;
   BYTE USER_DATA14 = 0x00;
   BYTE USER_DATA15 = 0x00;
   BYTE USER_DATA16 = 0x00;
   BYTE USER_DATA17 = 0x00;
   BYTE USER_DATA18 = 0x00;
   BYTE USER_DATA19 = 0x00;
   BYTE USER_DATA20 = 0x00;
   BYTE TVGAIN0 = 0xAF;
   BYTE TVGAIN1 = 0xFF;
   BYTE TVGAIN2 = 0xFF;
   BYTE TVGAIN3 = 0x2D;
   BYTE TVGAIN4 = 0x68;
   BYTE TVGAIN5 = 0x36;
   BYTE TVGAIN6 = 0xFC;
   BYTE INIT_GAIN = 0xC0;
   BYTE FREQUENCY  = 0x8C;
   BYTE DEADTIME = 0x00;
   BYTE PULSE_P1 = 0x01;
   BYTE PULSE_P2 = 0x12;
   BYTE CURR_LIM_P1 = 0x47;
   BYTE CURR_LIM_P2 = 0xFF;
   BYTE REC_LENGTH = 0x1C;
   BYTE FREQ_DIAG = 0x00;
   BYTE SAT_FDIAG_TH = 0xEE;
   BYTE FVOLT_DEC = 0x7C;
   BYTE DECPL_TEMP = 0x0A;
   BYTE DSP_SCALE = 0x00;
   BYTE TEMP_TRIM = 0x00;
   BYTE P1_GAIN_CTRL = 0x00;
   BYTE P2_GAIN_CTRL = 0x00;
   //BYTE EE_CRC = 0xFF;       NEVER USED
   //BYTE EE_CNTRL = 0x00;     NEVER USED
   BYTE P1_THR_0 = 0x88;
   BYTE P1_THR_1 = 0x88;
   BYTE P1_THR_2 = 0x88;
   BYTE P1_THR_3 = 0x88;
   BYTE P1_THR_4 = 0x88;
   BYTE P1_THR_5 = 0x88;
   BYTE P1_THR_6 = 0x84;
   BYTE P1_THR_7 = 0x21;
   BYTE P1_THR_8 = 0x08;
   BYTE P1_THR_9 = 0x42;
   BYTE P1_THR_10 = 0x10;
   BYTE P1_THR_11 = 0x80;
   BYTE P1_THR_12 = 0x80;
   BYTE P1_THR_13 = 0x80;
   BYTE P1_THR_14 = 0x80;
   BYTE P1_THR_15 = 0x80;
   BYTE P2_THR_0 = 0x88;
   BYTE P2_THR_1 = 0x88;
   BYTE P2_THR_2 = 0x88;
   BYTE P2_THR_3 = 0x88;
   BYTE P2_THR_4 = 0x88;
   BYTE P2_THR_5 = 0x88;
   BYTE P2_THR_6 = 0x84;
   BYTE P2_THR_7 = 0x21;
   BYTE P2_THR_8 = 0x08;
   BYTE P2_THR_9 = 0x42;
   BYTE P2_THR_10 = 0x10;
   BYTE P2_THR_11 = 0x80;
   BYTE P2_THR_12 = 0x80;
   BYTE P2_THR_13 = 0x80;
   BYTE P2_THR_14 = 0x80;
   BYTE P2_THR_15 = 0x80;
   BYTE ChecksumInput[44];       // data BYTE array for checksum calculator
   BYTE tempOrNoise = 0;          // data BYTE to determine if temp or noise measurement is to be performed
   BYTE syncBYTE = 0x55;       // data BYTE for Sync field set UART baud rate of PGA460
   BYTE regAddr = 0x00;       // data BYTE for Register Address
   BYTE regData = 0x00;       // data BYTE for Register Data
   BYTE uartAddr = 0;          // PGA460 UART device address (0-7). '0' is factory default address
   BYTE numObj = 1;          // number of objects to detect
   BYTE misoBuf[131];             // SPI MISO receive data buffer for all commands
   BYTE misoBuft[131];          // SPI MISO receive data buffer for transmit commands   
   
    pga_bufu12 pgau12;
   pga_bufu16 pgau16;


BYTE datareverse(BYTE* senddat, BYTE size)
{
   for(uint16 t=0; t<size; t++)
   {
      // // MSB --> LSB /////////////////////////////////////////////////
      senddat[t] = (((senddat[t] & 0b11110000)>>4) | ((senddat[t] & 0b00001111)<<4));
      senddat[t] = (((senddat[t] & 0b11001100)>>2) | ((senddat[t] & 0b00110011)<<2));
      senddat[t] = (((senddat[t] & 0b10101010)>>1) | ((senddat[t] & 0b01010101)<<1));
      ///////////////////////////////////////////////////////////////////      
   }
   return senddat;
}   

void pga460_initBoostXLPGA460(BYTE mode, uint32 baud, BYTE uartAddrUpdate)
{
   usscSPI_begin();               // start the SPI for BOOSTXL-PGA460 library
   return;
}

void pga460_defaultPGA460(BYTE xdcr)
{   
    memset(pgau12.pga_bufdizi12,0x00,sizeof(pga_bufs12));
   // Murata MA40H1SR
    USER_DATA1 = 0x00;
    USER_DATA2 = 0x00;
    USER_DATA3 = 0x00;
    USER_DATA4 = 0x00;
    USER_DATA5 = 0x00;
    USER_DATA6 = 0x00;
    USER_DATA7 = 0x00;
    USER_DATA8 = 0x00;
    USER_DATA9 = 0x00;
    USER_DATA10 = 0x00;
    USER_DATA11 = 0x00;
    USER_DATA12 = 0x00;
    USER_DATA13 = 0x00;
    USER_DATA14 = 0x00;
    USER_DATA15 = 0x00;
    USER_DATA16 = 0x00;
    USER_DATA17 = 0x00;
    USER_DATA18 = 0x00;
    USER_DATA19 = 0x00;
    USER_DATA20 = 0x00;
    TVGAIN0 = 0xAA;
    TVGAIN1 = 0xAA;
    TVGAIN2 = 0xAA;
    TVGAIN3 = 0x51;
    TVGAIN4 = 0x45;
    TVGAIN5 = 0x14;
    TVGAIN6 = 0x50;
    INIT_GAIN = 0x54;
    FREQUENCY  = 0x32;
    DEADTIME = 0xA0;
    PULSE_P1 = 0x08;
    PULSE_P2 = 0x10;
    CURR_LIM_P1 = 0x40;
    CURR_LIM_P2 = 0x40;
    REC_LENGTH = 0x19;
    FREQ_DIAG = 0x33;
    SAT_FDIAG_TH = 0xEE;
    FVOLT_DEC = 0x7C;
    DECPL_TEMP = 0x4F;
    DSP_SCALE = 0x00;
    TEMP_TRIM = 0x00;
    P1_GAIN_CTRL = 0x09;
    P2_GAIN_CTRL = 0x09;   

      pgau12.pgab12.s_syncBYTE=syncBYTE;
      pgau12.pgab12.s_EEBW=EEBW;
      pgau12.pgab12.s_USER_DATA1=USER_DATA1;
      pgau12.pgab12.s_USER_DATA2=USER_DATA2;
      pgau12.pgab12.s_USER_DATA3=USER_DATA3;
      pgau12.pgab12.s_USER_DATA4=USER_DATA4;
      pgau12.pgab12.s_USER_DATA5=USER_DATA5;
      pgau12.pgab12.s_USER_DATA6=USER_DATA6;
      pgau12.pgab12.s_USER_DATA7=USER_DATA7;
      pgau12.pgab12.s_USER_DATA8=USER_DATA8;
      pgau12.pgab12.s_USER_DATA9=USER_DATA9;
      pgau12.pgab12.s_USER_DATA10=USER_DATA10;
      pgau12.pgab12.s_USER_DATA11=USER_DATA11;
      pgau12.pgab12.s_USER_DATA12=USER_DATA12;
      pgau12.pgab12.s_USER_DATA13=USER_DATA13;
      pgau12.pgab12.s_USER_DATA14=USER_DATA14;
      pgau12.pgab12.s_USER_DATA15=USER_DATA15;
      pgau12.pgab12.s_USER_DATA16=USER_DATA16;
      pgau12.pgab12.s_USER_DATA17=USER_DATA17;
      pgau12.pgab12.s_USER_DATA18=USER_DATA18;
      pgau12.pgab12.s_USER_DATA19=USER_DATA19;
      pgau12.pgab12.s_USER_DATA20=USER_DATA20;
      pgau12.pgab12.s_TVGAIN0=TVGAIN0;
      pgau12.pgab12.s_TVGAIN1=TVGAIN1;
      pgau12.pgab12.s_TVGAIN2=TVGAIN2;
      pgau12.pgab12.s_TVGAIN3=TVGAIN3;
      pgau12.pgab12.s_TVGAIN4=TVGAIN4;
      pgau12.pgab12.s_TVGAIN5=TVGAIN5;
      pgau12.pgab12.s_TVGAIN6=TVGAIN6;
      pgau12.pgab12.s_INIT_GAIN=INIT_GAIN;
      pgau12.pgab12.s_FREQUENCY=FREQUENCY;
      pgau12.pgab12.s_DEADTIME=DEADTIME;
      pgau12.pgab12.s_PULSE_P1=PULSE_P1;
      pgau12.pgab12.s_PULSE_P2=PULSE_P2;
      pgau12.pgab12.s_CURR_LIM_P1=CURR_LIM_P1;
      pgau12.pgab12.s_CURR_LIM_P2=CURR_LIM_P2;
      pgau12.pgab12.s_REC_LENGTH=REC_LENGTH;
      pgau12.pgab12.s_FREQ_DIAG=FREQ_DIAG;
      pgau12.pgab12.s_SAT_FDIAG_TH=SAT_FDIAG_TH;
      pgau12.pgab12.s_FVOLT_DEC=FVOLT_DEC;
      pgau12.pgab12.s_DECPL_TEMP=DECPL_TEMP;
      pgau12.pgab12.s_DSP_SCALE=DSP_SCALE;
      pgau12.pgab12.s_TEMP_TRIM=TEMP_TRIM;
      pgau12.pgab12.s_P1_GAIN_CTRL=P1_GAIN_CTRL;
      pgau12.pgab12.s_P2_GAIN_CTRL=P2_GAIN_CTRL;
      pgau12.pgab12.s_pga460_calcChecksum=pga460_calcChecksum(EEBW);
        datareverse(pgau12.pga_bufdizi12, sizeof(pga_bufs12));
      pga460_spiTransfer(pgau12.pga_bufdizi12, sizeof(pga_bufs12));

      delay_ms(50);
      
      // Update targeted UART_ADDR to address defined in EEPROM bulk switch-case
      BYTE uartAddrUpdate = (PULSE_P2 >> 5) & 0x07;
      if (uartAddr != uartAddrUpdate)
      {
         // Update commands to account for new UART addr
           // Single Address
            P1BL = 0x00 + (uartAddrUpdate << 5);      
            P2BL = 0x01 + (uartAddrUpdate << 5);
            P1LO = 0x02 + (uartAddrUpdate << 5);
            P2LO = 0x03 + (uartAddrUpdate << 5);
            TNLM = 0x04 + (uartAddrUpdate << 5);
            UMR = 0x05 + (uartAddrUpdate << 5);
            TNLR = 0x06 + (uartAddrUpdate << 5);
            TEDD = 0x07 + (uartAddrUpdate << 5);
            SD = 0x08 + (uartAddrUpdate << 5);
            SRR = 0x09 + (uartAddrUpdate << 5);
            SRW = 0x0A + (uartAddrUpdate << 5);
            EEBR = 0x0B + (uartAddrUpdate << 5);
            EEBW = 0x0C + (uartAddrUpdate << 5);
            TVGBR = 0x0D + (uartAddrUpdate << 5);
            TVGBW = 0x0E + (uartAddrUpdate << 5);
            THRBR = 0x0F + (uartAddrUpdate << 5);
            THRBW = 0x10 + (uartAddrUpdate << 5);            
      }
      uartAddr = uartAddrUpdate;
   return;
}
       
void pga460_initThresholds(BYTE thr)
{
   switch (thr)
   {
      case 1: //50% Level (midcode) 128 of 255
         P1_THR_0 = 0x88;
         P1_THR_1 = 0x88;
         P1_THR_2 = 0x88;
         P1_THR_3 = 0x88;
         P1_THR_4 = 0x88;
         P1_THR_5 = 0x88;
         P1_THR_6 = 0x84;
         P1_THR_7 = 0x21;
         P1_THR_8 = 0x08;
         P1_THR_9 = 0x42;
         P1_THR_10 = 0x10;
         P1_THR_11 = 0x80;
         P1_THR_12 = 0x80;
         P1_THR_13 = 0x80;
         P1_THR_14 = 0x80;
         P1_THR_15 = 0x00;
         P2_THR_0 = 0x88;
         P2_THR_1 = 0x88;
         P2_THR_2 = 0x88;
         P2_THR_3 = 0x88;
         P2_THR_4 = 0x88;
         P2_THR_5 = 0x88;
         P2_THR_6 = 0x84;
         P2_THR_7 = 0x21;
         P2_THR_8 = 0x08;
         P2_THR_9 = 0x42;
         P2_THR_10 = 0x10;
         P2_THR_11 = 0x80;
         P2_THR_12 = 0x80;
         P2_THR_13 = 0x80;
         P2_THR_14 = 0x80;
         P2_THR_15 = 0x00;      
      break;
      default: break;
   }

        memset(pgau16.pga_bufdizi16,0x00,sizeof(pga_bufs16));
      pgau16.pgab16.s_syncBYTE=syncBYTE;
      pgau16.pgab16.s_THRBW=THRBW;
      pgau16.pgab16.s_P1_THR_0=P1_THR_0;
      pgau16.pgab16.s_P1_THR_1=P1_THR_1;
      pgau16.pgab16.s_P1_THR_2=P1_THR_2;
      pgau16.pgab16.s_P1_THR_3=P1_THR_3;
      pgau16.pgab16.s_P1_THR_4=P1_THR_4;
      pgau16.pgab16.s_P1_THR_5=P1_THR_5;
      pgau16.pgab16.s_P1_THR_6=P1_THR_6;
      pgau16.pgab16.s_P1_THR_7=P1_THR_7;
      pgau16.pgab16.s_P1_THR_8=P1_THR_8;
      pgau16.pgab16.s_P1_THR_9=P1_THR_9;
      pgau16.pgab16.s_P1_THR_10=P1_THR_10;
      pgau16.pgab16.s_P1_THR_11=P1_THR_11;
      pgau16.pgab16.s_P1_THR_12=P1_THR_12;
      pgau16.pgab16.s_P1_THR_13=P1_THR_13;
      pgau16.pgab16.s_P1_THR_14=P1_THR_14;
      pgau16.pgab16.s_P1_THR_15=P1_THR_15;
      pgau16.pgab16.s_P2_THR_0=P2_THR_0;
      pgau16.pgab16.s_P2_THR_1=P2_THR_1;
      pgau16.pgab16.s_P2_THR_2=P2_THR_2;
      pgau16.pgab16.s_P2_THR_3=P2_THR_3;
      pgau16.pgab16.s_P2_THR_4=P2_THR_4;
      pgau16.pgab16.s_P2_THR_5=P2_THR_5;
      pgau16.pgab16.s_P2_THR_6=P2_THR_6;
      pgau16.pgab16.s_P2_THR_7=P2_THR_7;
      pgau16.pgab16.s_P2_THR_8=P2_THR_8;
      pgau16.pgab16.s_P2_THR_9=P2_THR_9;
      pgau16.pgab16.s_P2_THR_10=P2_THR_10;
      pgau16.pgab16.s_P2_THR_11=P2_THR_11;
      pgau16.pgab16.s_P2_THR_12=P2_THR_12;
      pgau16.pgab16.s_P2_THR_13=P2_THR_13;
      pgau16.pgab16.s_P2_THR_14=P2_THR_14;
      pgau16.pgab16.s_P2_THR_15=P2_THR_15;
      pgau16.pgab16.s_pga460_calcChecksum=pga460_calcChecksum(THRBW);
      pgau16.pga_bufdizi16[sizeof(pga_bufs16)] = datareverse(pgau16.pga_bufdizi16, sizeof(pga_bufs16));
      pga460_spiTransfer(pgau16.pga_bufdizi16, sizeof(pga_bufs16));
   #endif
      //-------------------------------------------------------------------------------------------------
   
   delay_ms(100);
   return;
}
BYTE pga460_calcChecksum(BYTE cmd)
{
   int checksumLoops = 0;
   
   cmd = cmd & 0x001F; // zero-mask command address of cmd to select correct switch-case statement
   
   switch(cmd)
   {
      case 16 : //THRBW
      case 12 : //EEBW
      default: break;
   }

   if (ChecksumInput[0]<17) //only re-append command address for non-broadcast commands.
   {
      ChecksumInput[0] = ChecksumInput[0] + (uartAddr << 5);
   }
   
   uint16 carry = 0;

   for (int i = 0; i < checksumLoops; i++)
   {
      if ((ChecksumInput[i] + carry) < carry)
      {
         carry = carry + ChecksumInput[i] + 1;
      }
      else
      {
         carry = carry + ChecksumInput[i];
      }

      if (carry > 0xFF)
      {
        carry = carry - 255;
      }
   }
   
   carry = (~carry & 0x00FF);
   return carry;
}
void pga460_spiTransfer(uint8* mosi, uint8 size )
{
   memset(misoBuft, 0x00, sizeof(misoBuft)); // idle-low receive buffer data
   
   for (int i = 0; i<size; i++)
   {
      misoBuft[i] = usscSPI_transfer(mosi[i]);
   }

}
void pga460_spiMosiIdle(BYTE size)
{
   for (int i = 0; i<size; i++)
   {
      misoBuf[i] = usscSPI_transfer(0x7F); // 0xFE reversed as 0x7F
   }
   return;
}


RegType.h file:
Code:


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//BUF12////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct{
   uint8 s_syncBYTE;
   uint8 s_EEBW;
   uint8 s_USER_DATA1;
   uint8 s_USER_DATA2;
   uint8 s_USER_DATA3;
   uint8 s_USER_DATA4;
   uint8 s_USER_DATA5;
   uint8 s_USER_DATA6;
   uint8 s_USER_DATA7;
   uint8 s_USER_DATA8;
   uint8 s_USER_DATA9;
   uint8 s_USER_DATA10;
   uint8 s_USER_DATA11;
   uint8 s_USER_DATA12;
   uint8 s_USER_DATA13;
   uint8 s_USER_DATA14;
   uint8 s_USER_DATA15;
   uint8 s_USER_DATA16;
   uint8 s_USER_DATA17;
   uint8 s_USER_DATA18;
   uint8 s_USER_DATA19;
   uint8 s_USER_DATA20;
   uint8 s_TVGAIN0;
   uint8 s_TVGAIN1;
   uint8 s_TVGAIN2;
   uint8 s_TVGAIN3;
   uint8 s_TVGAIN4;
   uint8 s_TVGAIN5;
   uint8 s_TVGAIN6;
   uint8 s_INIT_GAIN;
   uint8 s_FREQUENCY;
   uint8 s_DEADTIME;
   uint8 s_PULSE_P1;
   uint8 s_PULSE_P2;
   uint8 s_CURR_LIM_P1;
   uint8 s_CURR_LIM_P2;
   uint8 s_REC_LENGTH;
   uint8 s_FREQ_DIAG;
   uint8 s_SAT_FDIAG_TH;
   uint8 s_FVOLT_DEC;
   uint8 s_DECPL_TEMP;
   uint8 s_DSP_SCALE;
   uint8 s_TEMP_TRIM;
   uint8 s_P1_GAIN_CTRL;
   uint8 s_P2_GAIN_CTRL;
   uint8 s_pga460_calcChecksum;
}pga_bufs12;

typedef union{
   pga_bufs12 pgab12;
   uint8 pga_bufdizi12[sizeof(pga_bufs12)];
}pga_bufu12;


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//BUF16////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct{
   uint8 s_syncBYTE;
   uint8 s_THRBW;
   uint8 s_P1_THR_0;
   uint8 s_P1_THR_1;
   uint8 s_P1_THR_2;
   uint8 s_P1_THR_3;
   uint8 s_P1_THR_4;
   uint8 s_P1_THR_5;
   uint8 s_P1_THR_6;
   uint8 s_P1_THR_7;
   uint8 s_P1_THR_8;
   uint8 s_P1_THR_9;
   uint8 s_P1_THR_10;
   uint8 s_P1_THR_11;
   uint8 s_P1_THR_12;
   uint8 s_P1_THR_13;
   uint8 s_P1_THR_14;
   uint8 s_P1_THR_15;
   uint8 s_P2_THR_0;
   uint8 s_P2_THR_1;
   uint8 s_P2_THR_2;
   uint8 s_P2_THR_3;
   uint8 s_P2_THR_4;
   uint8 s_P2_THR_5;
   uint8 s_P2_THR_6;
   uint8 s_P2_THR_7;
   uint8 s_P2_THR_8;
   uint8 s_P2_THR_9;
   uint8 s_P2_THR_10;
   uint8 s_P2_THR_11;
   uint8 s_P2_THR_12;
   uint8 s_P2_THR_13;
   uint8 s_P2_THR_14;
   uint8 s_P2_THR_15;
   uint8 s_pga460_calcChecksum;
}pga_bufs16;

typedef union{
   pga_bufs16 pgab16;
   uint8 pga_bufdizi16[sizeof(pga_bufs16)];
}pga_bufu16;


PGA460_SPI.h file:
Code:

#include "16F18346.h"
#include "PIC16F18346_PIN.h"
uint8 _regdata=0;
uint8 usscSPI_transfer(uint8 _data) {
   //SSP1CON1bitsWCOL = 0;
   mSSP1BUF.reg = _data;
   while(!SSP1STATbitsBF);
   _regdata = mSSP1BUF.reg;
   return _regdata;
}

void usscSPI_begin()
{
   //OSCILLATOR
   mOSCCON1.nosc=0;
   mOSCCON1.ndiv=0;
   //OSCFRQ
    OSCFRQbitsHFFRQ0 = 1;
    OSCFRQbitsHFFRQ1 = 0;
    OSCFRQbitsHFFRQ2 = 1;
    OSCFRQbitsHFFRQ3 = 0;
     
    mPPSLOCK.reg = 0x55;
    mPPSLOCK.reg = 0xAA;
    PPSLOCKbitsPPSLOCKED = 0x00;    //unlock PPS
     
    //INPUT
    mSSP1DATPPS.reg = 0x0C;     //RB4 -> MSSP1: SDI1 ---- Set pin RB4 as data input ----- PGA460_MISO_1 PIN12
    mSSP1CLKPPS.reg = 0X0E;     //RB6 -> MSSP1: SCK1 ---- Set pin RB6 as clock input ---- PGA460_SCK_1  PIN11;
   
    //OUTPUT
    mRB6PPS.reg = 0x18;             //RB6 -> MSSP1: SCK1 ---- Set pin RB6 as clock output --- PGA460_SCK_1  PIN11;
    mRC7PPS.reg = 0x19;             //RC7 -> MSSP1: SDO1 ---- Set pin RC7 as data output ---- PGA460_MOSI_1 PIN9;
   
    mPPSLOCK.reg = 0x55;
    mPPSLOCK.reg = 0xAA;
    PPSLOCKbitsPPSLOCKED = 0x01;    //lock PPS 
    SSP1CON1bitsSSPEN = 0;
   //Fosc/16
   SSPCON1bitsSSP1M0 = 1;
   SSPCON1bitsSSP1M1 = 0;
   SSPCON1bitsSSP1M2 = 0;
   SSPCON1bitsSSP1M3 = 0;
   //Mode 2
   SSPCON1bitsCKP = 1;
   SSPSTATbitsCKE = 1;
   SSPSTATbitsBF = 0;
   //High speed mode
   SSPSTATbitsSMP =0;
   mSSP1ADD.reg = 0x00;
    //mSSP1BUF.reg  = 0x00;
   //SSP1IF = 0;
   SSP1CON1bitsSSPEN = 1;
   
}


It cant write right data to pgau12 register and its addresses still are different .
temtronic



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

View user's profile Send private message

PostPosted: Wed Sep 02, 2020 4:28 am     Reply with quote

re:
Quote:
I deleted unnecessary parts from code. But I get same result.


That's WHY I wanted you to create a SMALL test program. The very,very MINIMUM to send and recevive data to the the ONE register that you say doesn't work properly.
99% of your program is NOT needed for the test. The test program should be 20-25 lines of code to simply send/receive/display/increment. If you send 0,1,2,3..., I expect to see ?,0,1,2,... pattern.

Jay
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
Page 7 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