#define SCLK LATGbits.LATG6 //clock line
#define SDI LATGbits.LATG7 //SPI data input
#define SDO LATGbits.LATG8 // SPI data output
#define CS LATGbits.LATG9 // chip select
/// main starts here
int main()
{
double temp,result; // variable to store output
ADPCFG = 0; //ALL PINS in ANALOG form
TRISGbits.TRISG6 = 0; // SCK is an output
TRISGbits.TRISG7 = 1; // SDI is an input
TRISGbits.TRISG8 = 0; // SDO is an output
TRISGbits.TRISG9 = 0; // CHIP SELECT AS O/P
CS =1;
uart_init(); //Uart Initialization
SPI2_Init(); //SPI Initialization
while (1)
{
CS =0;
WriteSPI2(0x04EB); // Configure ADS1118
result = ReadSPI2(0x0000); // reading results from ADS1118
CS =1;
printf("%f\n",result);
Delay();
Delay();
}
}
/// SPI initialization
void SPI2_Init(void)
{
SPI2STATbits.SPIEN = 0; //initially disable pin
SPI2STATbits.SPISIDL = 0; //opertion mode idle
SPI2STATbits.SPIROV = 0; // Rx overflow flag
SPI2STATbits.SPITBF = 0; //Tx buf full status
SPI2STATbits.SPIRBF = 0; //Rx buf full status
SPI2CONbits.FRMEN = 0; //Framing support diabled
SPI2CONbits.SPIFSD = 0; //Frame Sync Pulse Direction Control on SSx pin bit 0-master,1-slave
SPI2CONbits.DISSDO = 0; //Disable SDOx pin bit,0=pin conreolled by module,1=Pin is controlled by associated port register
SPI2CONbits.MODE16 = 1; //Communication is word-wide (16 bits),0=Communication is word-wide (8 bits)
SPI2CONbits.SMP = 0; //SPI Data Input Sample Phase bit Master mode:0,1 Slave mode =0;
SPI2CONbits.CKE = 0; //
SPI2CONbits.SSEN = 1; //Slave Select Enable (Slave mode) bit
SPI2CONbits.CKP = 0; //Clock Polarity Select bit
SPI2CONbits.MSTEN = 1; //Master mode enabled
SPI2CONbits.SPRE = 7; //Primary prescalr 1:1
SPI2CONbits.PPRE = 3; //Secondary Prescalar 1:1
// IFS1bits.SPI2IF = 0; // Clear IF bit
// IPC6bits.SPI2IP = 6; // Assign interrupt priority
// IEC1bits.SPI2IE = 1; // Interrupt En
SPI2STATbits.SPIEN = 1;
printf("SPI2 init done\n");
}
void WriteSPI2(unsigned int data_out)
{
if (SPI2CONbits.MODE16) // word write
SPI2BUF = data_out;
else
SPI2BUF = data_out & 0xff; // byte write
}
unsigned int ReadSPI2()
{
/* Check for Receive buffer full status bit of status register*/
if (SPI2STATbits.SPIRBF)
{
SPI2STATbits.SPIROV = 0;
if (SPI2CONbits.MODE16)
return ( SPI2BUF ); /* return word read */
else
return (SPI2BUF & 0xff); /* return byte read */
}
return -1; /* RBF bit is not set return error*/
}
void Delay ( void )
{
int temp;
for( temp = 0; temp < 255; temp++ );
}
Can you explain why are you using UART also,along with SPI?uart_init(); //Uart Initialization
#include"SPI_2.h"
#include"uart.h"
volatile double temp,result;
int main()
{
ADPCFG = 1; //ALL PINS in ANALOG form
TRISGbits.TRISG6 = 0; // SCK is an output
TRISGbits.TRISG7 = 1; // SDI is an input
TRISGbits.TRISG8 = 0; // SDO is an output
TRISGbits.TRISG9 = 0; // CHIP SELECT AS O/P
CS =1;
uart_init(); //Uart Init
SPI2_Init(); //SPI INIT
while (1)
{
CS =0;
//Programmable gain amplifier configuration (011) ==> FS is ±1.024 V
//Data rate (111) = 860 Samples Per Second
//Data rate (000) = 8 SPS
WriteSPI2(0x0000);
WriteSPI2(0x060B); //06EB working with 0.2 volts difference
Delay();
Delay();
result = ReadSPI2();
temp = ReadSPI2();
CS =1;
Delay();
Delay();
printf("V %.5f\n",(double)result);
}
}
}
&FS is ±1.024 V
OUTPUT showing 2.5 v
Code C - [expand] 1 result = ReadSPI2(0x0000); // reading results from ADS1118
000 = FS is ±6.144 V(1)
001 = FS is ±4.096 V(1)
101 = FS is ±0.256 V
010 = FS is ±2.048 V (default)
110 = FS is ±0.256 V
011 = FS is ±1.024 V
000 = 8 SPS
100 = 128 SPS (default)
001 = 16 SPS
101 = 250 SPS
010 = 32 SPS
110 = 475 SPS
011 = 64 SPS
111 = 860 SPS
i used multiplication factor to calculate voltage//Mul factor = Operating voltage * 16bit resolution (2^16 =65536 )
digi_result= digi_result +((float)result * Mulfactor);
printf("V %.5f\n",(double)digi_result);
We can not know what input voltages you connect to your ADC.what is the suitable FS(PGA) configuration for my hardware??
//Mul factor = Operating voltage * 16bit resolution (2^16 =65536 )
Code C - [expand] 1//Mul factor = Operating voltage * 16bit resolution (2^16 =65536 )
include headers
#define OPV (6.144*2) //Peak to peak FS
#define bit16 (65536U) //2^16
#define MF (OPV / bit16)
int main()
{
ADPCFG = 1; //ALL PINS in ANALOG form
TRISGbits.TRISG6 = 0; // SCK is an output
TRISGbits.TRISG7 = 1; // SDI is an input
TRISGbits.TRISG8 = 0; // SDO is an output
TRISGbits.TRISG9 = 0; // CHIP SELECT AS O/P
CS =1;
uart_init(); //Uart Init
SPI2_Init(); //SPI INIT
while (1)
{
CS =0;
WriteSPI2(0x0000);
WriteSPI2(0x80EB); //04EB working
Delay();
Delay();
Delay();
result = ReadSPI2();
temp = ReadSPI2();
CS =1;
Delay();
Delay();
Delay();
Delay();
digi_result= (result * MF));
result =0;
printf("V %.3f\n",(float)digi_result);
digi_result=0;
}
}
}
Code C - [expand] 1 digi_result = (result * MF));
Code C - [expand] 1 digi_result = result * 6.144 * 2 / 65536;
did you really compiled this code, there is a extra ')' in the line
I mean the resolution can be reduced by multiplication after division
What kind error you are having linear or constant ?for example for input voltage 1 and 3 what are the output voltages?
How do you saying you are getting error? did you used any standard meters?
Code C - [expand] 1 2 3 4 if( result & 0x8000 ) digi_result = 0; else digi_result = result * 6.144 * 2 / 65536;
if( result & 0x8000 )
Code C - [expand] 1 2 3 4 5 6 result += 3087; if( result & 0x8000 ) digi_result = 0; else digi_result = result * 6.144 * 2 / 65536;
Are you using any diode on measuring circuit.
0.578 v sounds like diode or transistor base voltage...
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?