sbit Conversion_Select at RC0_bit;
sbit Conversion_Select_Direction at TRISC0_bit;
sbit Chip_Select at RC1_bit;
sbit Chip_Select_Direction at TRISC1_bit;
sbit Busy_Select at RC2_bit;
sbit Busy_Select_Direction at TRISC2_bit;
// LCD module connections (pinout settings)
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
// LCD pin direction
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
unsigned short msb,lsb;
double ADCvalue;
double temp;
char txt[16];
void Init_Main()
{
Chip_Select=1; // CS pin is high at ADC
Chip_Select_Direction=0; // RC1 pin's direction is set as output
Conversion_Select=1; // CONV pin is high at ADC
Conversion_Select_Direction=0; // RC0 pin's direction is set as output
//Busy_Select=1; // BUSY pin is high at ADC
Busy_Select_Direction=1; // RC2 pin's direction is set as input
}
unsigned char SPI_Read1()
{
SSPBUF=0xff; /* Copy flush data in SSBUF */
while(!PIR1.SSPIF); /* Wait for complete 1 byte transmission */
PIR1.SSPIF=0; /* Clear SSPIF flag */
return(SSPBUF); /* Return received data.*/
}
float ADC_Output()
{
SPI1_Init();
SPI1_Write(0x20);
Chip_Select=1;
Conversion_Select=1; // Conversion pin is high at ADC
Conversion_Select=0;
Conversion_Select=1;
//SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
SPI1_Write(0x61);
msb=SPI_Read1(); //sending out zero as a dummy byte, to receive the answer (the register value) from chip
lsb=SPI_Read1(); //sending out zero as a dummy byte, to receive the answer (the register value) from chip
Chip_Select=1;
ADCvalue = (msb <<8) + (lsb& 0x00FF) ;
return (ADCvalue);
}
void main() {
ANSEL = ANSELH = 0; // All I/O pins are configured as digital
//PORTC=0;
Init_Main();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,5,"ADC test");
while(1)
{
temp=ADC_Output();
temp= ((ADCvalue*5)/65536);
//temp = ceil(temp * 100) / 100;
//IntToStr(ADCvalue,txt);
FloatToStr(temp,txt);
//temp=ADCvalue+0x30;
Lcd_Out(2,1,txt);
}
}
I have driven the Chip_Select=0, still its not working.Hi,
Mind to use CODE tags next time.
you never drive CHIP_SELECT = 0, thus the ADC never is active on the bus.
Klaus
I have tried with that option too, keeping the \CONV\ bit low till \BUSY\ gets high. But still, in the logical analyzer, it keeps showing that the \CONV\ and \BUSY\ remain low while \CS\ is always high.What 'control byte'? The timing diagram in your first post shows that the conversion is triggered by the \CONV\ line going low. That sets the \BUSY\ pin low until teh data is ready. You then start ther SPI exchange which triggers the SCK signal that will make the ADS8315 starts to shift out the value to the DATA pin (conneted to the MCUs SDI pin).
One 'guess' is that you are not leaving thre \CONV\ signal low for long enough. The data sheet says that it must be low for between 0.04 and 12uS. Personally I would go for the longer end of that range to be on the safe side. Have you read the paragraphs at the stop of Page 13 of the datasheet about kee ing the \CONV\ signal low for the entire conversion?
Susan
float ADC_Output()
{
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
Chip_Select=1; //Chip_Select is high
Conversion_Select=1; // Conversion pin is high at ADC
Delay_us(1);
Conversion_Select=0; // Conversion pin is low at ADC
Delay_us(19);
Chip_Select=0; // Chip_Select goes low
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
Chip_Select=0; // Chip_Select remains low
msb=SPI_Read1(); //sending out zero as a dummy byte, to receive the answer (the register value) from chip
lsb=SPI_Read1(); //sending out zero as a dummy byte, to receive the answer (the register value) from chip
Chip_Select=1;
Conversion_Select=1; // Conversion pin is high
ADCvalue = (msb <<8) + (lsb& 0x00FF) ;
return (ADCvalue);
}
If you can see the attached file, there it's been said that for 8bit transfer the control byte should be 20h and for 16bit transfer control byte should be 61h..I thought this is somehow related to sspcon1 reg..that's why I've initialise two times..Don't use floating point in these small(er) family MCUs - they don;t have the hardware and everything is done in the library software which is slow and takes up the limited space. Scaled integers are the way to do if you need fractional values from the (already integer) ADC value.
You start the conversion but why do you rely on the delay when you already have a line to the \BUSY\ signal - just ewait until that goers high again. However for that signal to be non-tristated, you will need \CS\ to be low (there is no harm in pulling that low at the start anyway).
Why are you initialising SPI1 twice - but with different clocks. The SPI is not used for the conversion but (as we can't see what the initialisation function does) it *might* cause some glitches that might interfere with the ADC. Initialise the SPI module as part of the startup of the app and then leave it alone after that.
You also duplicate the 'Chip_Select=0;' line which makes me thing that you are simply copying code from all over th eplace without erwally understnading what it is doing.
Be careful with making assumptions about implicit type conversions with embedded systems. You have 'msb' as unsigned short (8-bit). Expressions such as 'msb<<8' could well be performed in 8-bit arithmetic (which is the native size of these MCUs) - if you want the use 16-bit arithmetic then use explicit type casts. This is the source an MANY errors by beginners.
Susan
#Initialise the MSSP module to master mode ands whatever speed you like
CONV line = 1
CS line = 1
#loop
CS line = 0
CONV line = 0
while( BUSY==0);
topByte = SPI_read()
bottomByte = SPI_read()
CONV line = 1
CS line = 1
final value = ((uint16_t)topByte << 8) + bottomByte
#done
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?