• AVR Freaks

Helpful ReplySPI with PIC16F18345

Page: < 12345.. > >> Showing page 4 of 6
Author
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/29 08:53:25 (permalink)
0
davekw7x
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. 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


Hey Dave sorry to say seriously I am at the deadline to complete the program. I tired of changing my code everytime that is why I followed brownt and qhb instructions and debugging . Finally out of curiosity I tried MCC code generator.

Initially I wrote my own SPI code studied the complete datasheet and there were minued difference between my previous code and the MCC code.

'//'A'lone Employee'
#61
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/29 23:21:23 (permalink)
0
Here i am getting only zero at Y-axis. i attached my output window
 
 
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// LATCbits.LATC0 = 1;
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
//set up ADXL345
//while(1)
// while(1)
while(0xe5 != D_ID)
{
Chip_Select = 0;
SPI1_Exchange8bit(0x80); //0x00 device id address 0x80 reading device ID
D_ID = SPI1_Exchange8bit(0x00); // reset value
Chip_Select = 1;
}

__delay_ms(1);

Chip_Select = 0;
SPI1_Exchange8bit(0x31); //0x31 device id
SPI1_Exchange8bit(0x0B); // reset value
Chip_Select = 1;

__delay_ms(1);

Chip_Select = 0;
SPI1_Exchange8bit(0x2D); //0x2D p_c
SPI1_Exchange8bit(0x00); // standby mode
Chip_Select = 1;

__delay_ms(1);


Chip_Select = 0;
SPI1_Exchange8bit(0x2D); //0x2D p_c
SPI1_Exchange8bit(0x08); // measure mode
Chip_Select = 1;

__delay_ms(1);

/* Chip_Select = 0;
SPI1_Exchange8bit(0xB8); //0x38 FIFO_CTL device id address 0x80 reading device ID
SPI1_Exchange8bit(0x50); // reset value
Chip_Select = 1;
*/
__delay_ms(1);


printf("FIFO %d\r\n" , FIFO);

while (1)
{

/*Chip_Select = 0;
SPI1_Exchange8bit(0xB9); //0x31 device id address 0x80 reading device ID
FIFO = SPI1_Exchange8bit(0x00); // reset value
Chip_Select = 1;
*/
printf("before D_ID %x FIFO : %x x : %d y : %d z : %d \r\n" ,D_ID , FIFO , x , y , z);

/*****X-AXIS*****/
Chip_Select = 0;
SPI1_Exchange8bit(0xB2); //0XB2 setup for reading data from low byte of x axis
msb = SPI1_Exchange8bit(0x00);
Chip_Select = 1;

Chip_Select = 0;
SPI1_Exchange8bit(0xB3); //0XB2 setup for reading data from low byte of x axis
lsb = SPI1_Exchange8bit(0x00);
Chip_Select = 1;
x = (( int )lsb >> 8 )| msb;
/*****X-AXIS*****/
/*****Y-AXIS*****/

Chip_Select = 0;
SPI1_Exchange8bit(0xB4); //0XB2 setup for reading data from low byte of x axis
msb = SPI1_Exchange8bit(0x00);
Chip_Select = 1;

Chip_Select = 0;
SPI1_Exchange8bit(0xB5); //0XB2 setup for reading data from low byte of x axis
lsb = SPI1_Exchange8bit(0x00);
Chip_Select = 1;
y = ((int )lsb) >> 8 | msb;
/*****Y-AXIS*****/
/*****Z-AXIS*****/
Chip_Select = 0;
SPI1_Exchange8bit(0xB6); //0XB2 setup for reading data from low byte of x axis
msb = SPI1_Exchange8bit(0x00);
Chip_Select = 1;

Chip_Select = 0;
SPI1_Exchange8bit(0xB7); //0XB2 setup for reading data from low byte of x axis
lsb = SPI1_Exchange8bit(0x00);
Chip_Select = 1;
z = ((int )lsb >> 8 )| msb;
/*****Z-AXIS*****/



printf(" D_ID %x FIFO : %d x : %d y : %d z : %d \r\n" ,D_ID , FIFO , x , y , z);

__delay_ms(5);



}
}
post edited by ram1723 - 2018/01/29 23:22:49

Attached Image(s)


'//'A'lone Employee'
#62
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/29 23:25:38 (permalink)
0
Have you tried rotating the device?
If it was perfectly horizontal, I would expect both X and Y to be zero, and only Z showing a reading.
 

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"
#63
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/29 23:28:05 (permalink)
0
Yep,  I tried only x&z values are changing
 
see my attached  output window when perfectly horizontal 
post edited by ram1723 - 2018/01/29 23:30:44

Attached Image(s)


'//'A'lone Employee'
#64
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/29 23:38:25 (permalink)
0
You are shifting your data the wrong way.
Change
x = (( int )lsb >> 8 )| msb;
to
x = (msb << 8) | lsb;
 
and the same for y and z.
You don't need any casting in that expression because of C's automatic "integer promotion".
 

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"
#65
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/29 23:48:32 (permalink)
0
 
y = (( int )lsb >> 8 )| msb;
to
y = (msb << 8) | lsb;
 
 




 
check my output image what are these values. Do i need to calculate the angle from these values?
post edited by ram1723 - 2018/01/29 23:51:51

Attached Image(s)


'//'A'lone Employee'
#66
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/30 00:02:18 (permalink)
+1 (1)
There should be application notes on the Analog Devices website showing you how to use the data.
 

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"
#67
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/30 03:21:24 (permalink)
0
strange that Y would be missing, but could be a SPI setup issue giving spurious results even though X and Z seem solid.
When moved along the X axis, does the X values change more than any other.
When tilted on the X axis, do both X and Z values change?
When tilted on the Y axis does only Z change?
When moved up and down along the Z axis, does Z change more than any other?
 
 
#68
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/30 03:41:59 (permalink)
0
Please check my complete code. I have to complete in this week.
 
sad: sadsad: sadsad: sad

'//'A'lone Employee'
#69
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/30 03:56:24 (permalink)
0
there is an issue while i am trying to attach my complete code.
post edited by ram1723 - 2018/01/30 03:58:09

'//'A'lone Employee'
#70
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/30 04:09:25 (permalink)
0
what happens if you read only the Y axis. Only have Y axis code. Remove X and Z code. Are there now Y values?
#71
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/30 04:12:46 (permalink)
0
brownt
what happens if you read only the Y axis. Only have Y axis code. Remove X and Z code. Are there now Y values?


yes they are... i am getting the values

'//'A'lone Employee'
#72
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/30 04:14:36 (permalink)
0
brownt
strange that Y would be missing, but could be a SPI setup issue giving spurious results even though X and Z seem solid.
 


did you check my configurations ? did you find any mistake?

'//'A'lone Employee'
#73
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/30 04:32:37 (permalink)
0
what I can see of it, yes. where is your SPI code. What is the SPI bus frequency?
#74
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/30 04:39:58 (permalink)
0
Fosc/4

'//'A'lone Employee'
#75
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/30 05:00:25 (permalink)
0
brownt
what I can see of it, yes. where is your SPI code. What is the SPI bus frequency?


0101 = SPI Slave mode,clock = SCK pin, SS pin control disabled, SS can be used as I/O pin
0100 = SPI Slave mode, clock = SCK pin, SS pin control enabled
0011 = SPI Master mode, clock = T2_match/2
0010 = SPI Master mode, clock = FOSC/64
0001 = SPI Master mode, clock = FOSC/16
0000 = SPI Master mode, clock = FOSC/4
 
please explain this

'//'A'lone Employee'
#76
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/30 05:17:58 (permalink)
0
I  am very interested in to know where i am enabling internal frequency. And what is the exact meaning of #define __XTAL__freq 10000000
When i generated it is default 1Mhz 
 

'//'A'lone Employee'
#77
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/30 05:45:42 (permalink)
0
Does SS wont activate until unless "0100 = SPI Slave mode, clock = SCK pin, SS pin control enabled" into SSP1CON1, i did not do that still i can read the device and values.
 
Lot of questions are running in my mindpink: pink
 
May be i should match my master clock with the slave clock frequency how to do that?????????
 

'//'A'lone Employee'
#78
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/02/02 22:27:54 (permalink)
0
I AM getting 0-255 when I  am moving 'x' to 90 degrees and 255-0 when it 90 to 180.
-255-0 from 180 to 270
0- -255 from 270 to 360
 
????????????????????????????????

'//'A'lone Employee'
#79
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/02/03 02:04:51 (permalink)
0
ram1723
I AM getting 0-255 when I  am moving 'x' to 90 degrees

Sounds reasonable

and 255-0 when it 90 to 180.

Also sounds reasonable.

-255-0 from 180 to 270

I would have expected 0 to -255.
Are you saying it instantly change from 0 to -255 when you rotated past 180 degrees?

0- -255 from 270 to 360

and I would have expected -255 to 0 here, you are back to where it was measuring zero before you started.



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"
#80
Page: < 12345.. > >> Showing page 4 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5