Hi,
Am trying to interface the scp1000 baro with the dspic30f4013.i found code on the website and tried to 'tweek' it without luck and am getting despondent as deadline draws closer...i have attached below the code,please may you help to get it working.
Kind regards
thabo
//CODE
#include <spi.h>
#include <math.h>
#include <stdio.h>
#include <uart.h>
#define FCY 7372000
#include <Libpic30.h>
#define __delay_ms(d) \
{ __delay32( (unsigned long) (d)*(FCY)/1000); }
#define __delay_us(d) \
{ __delay32( (unsigned long) (d)*(FCY)/1000000); }
#define SCK PORTFbits.RF6 //SCK This is clock input into SCP1000
#define SDI PORTFbits.RF2 //MOSI This is input into the SCP1000 from the PIC
#define SDO PORTFbits.RF3 //MISO This is output from SCP1000 into the PIC
#define CS PORTBbits.RB2 //CSB This is chip select input - when low, the SCP1000 is selected
unsigned char read_register(unsigned char register_name);
unsigned int read_register16(unsigned char register_name);
void write_register(unsigned char register_name, unsigned char register_value);
unsigned char spi_comm_software(unsigned char outgoing_byte);
unsigned int Write8(unsigned int add, unsigned int val);
unsigned int Read8(unsigned int add);
unsigned int Read16(unsigned int add);
unsigned int config1,config2;
unsigned int config_1,config_2;
void OpenSPI1(unsigned int config1, unsigned int config2);
void OpenUART1(unsigned int U1MODEvalue,unsigned int U1STAvalue, unsigned int ubrg);
/*_________________________________UART CONFIG______________________________________*/
config = UART_RX_INT_EN & UART_RX_INT_PR5 & UART_TX_INT_DIS & UART_TX_INT_PR0;
/* Configure UART1 module to transmit 8 bit data with one stopbit. Also Enable loopback mode */
U1MODEvalue = UART_EN & UART_IDLE_CON &
UART_DIS_WAKE & UART_EN_LOOPBACK &
UART_EN_ABAUD & UART_NO_PAR_8BIT &
UART_1STOPBIT;
U1STAvalue = UART_INT_TX_BUF_EMPTY &
UART_TX_PIN_NORMAL &
UART_TX_ENABLE & UART_INT_RX_BUF_FUL &
UART_ADR_DETECT_DIS &
UART_RX_OVERRUN_CLEAR;
ubrg = 1;
UARTinit()
{
OpenUART1(U1MODEvalue, U1STAvalue, ubrg);
ConfigIntUART1(config);
}
void scp1000_init()
{
TRISFbits.TRISF6 = 0; // just make this an output
TRISFbits.TRISF2 = 0; // just make this an output
TRISBbits.TRISB2 = 0; // just make this an output
TRISFbits.TRISF3 = 1; // just make this an input
TRISAbits.TRISA11 = 1; // just make this an input (dataready)
// TRISEbits.TRISE3 = 0; // just make this an output
// TRISEbits.TRISE4 = 0; // just make this an output
// PORTEbits.RE3 = 1;
// PORTEbits.RE4 = 1;
config1 = FRAME_ENABLE_OFF & FRAME_SYNC_INPUT & SPI_MODE16_OFF & SPI_SMP_OFF
& SPI_CKE_OFF & SLAVE_ENABLE_OFF & CLK_POL_ACTIVE_LOW & MASTER_ENABLE_ON
& SEC_PRESCAL_8_1 & PRI_PRESCAL_64_1;
config2 = SPI_ENABLE & SPI_IDLE_CON & SPI_RX_OVFLOW_CLR;
OpenSPI1(config1,config2);
__delay_ms(90);
Write8(0x02, 0x00); // indirect access to 0x00
__delay_us(100);
//Write8(0x01, 0x05); // high resolution to data write register
Write8(0x01, 0x0D); // high speed to data write register
__delay_us(100);
Write8(0x03, 0x02); // write data to 0x00
__delay_ms(50);
Write8(0x03, 0x0A); // start high resolution
Write8(0x03, 0x09); // start high speed
inline int scp1000_dataready()
{
return PORTAbits.RA11;
}
}
float scp1000_get_pressure()
{
unsigned int d1, d2;
unsigned long pressure;
//microcontroller_delay_us(1);
//Write8(0x03, 0x0A);
//microcontroller_delay_us(1);
d2 = Read8(0x1F);
d1 = Read16(0x20);
pressure = d2;
pressure <<= 16;
pressure |= d1;
return pressure / 4.0;
}
float scp1000_get_temperature()
{
unsigned int t = Read16(0x21);
return ((float)t) / 20.0;
}
unsigned int scp1000_get_status()
{
return Read8(0x07);
}
float scp1000_pressure_to_height(float pressure, float t)
{
//return 44330.0 * (1.0 - powf(pressure / 101325.0, 0.19));
//return logf(pressure / 101000.f) * (273.f + temperature) * (287.05f / 9.81f);
return logf(pressure / 101000.f) * (273.f + 20.f) * (-287.05f / 9.81f);
}
float scp1000_get_height()
{
return scp1000_pressure_to_height(scp1000_get_pressure(), scp1000_get_temperature());
}
unsigned int Read16(unsigned int register_name)
{
unsigned int i;
register_name <<= 2;
register_name &= 0b11111100; //Read command
//CS = 0;
//microcontroller_delay_us(10);
CS = 0;
i = SPI1BUF;
SPI1BUF = register_name; // address
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
i = SPI1BUF;
SPI1BUF = 0x00;
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
i = SPI1BUF;
i <<= 8;
SPI1BUF = 0x00;
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
unsigned int tmp = SPI1BUF;
CS = 1;
i |= tmp & 0xFF;
return i;
}
unsigned int Read8(unsigned int add)
{
unsigned int i;
add <<= 1;
add |= 0x00; // read
add <<= 1;
add |= 0x00;
while(SPI1STATbits.SPITBF) ;
i = SPI1BUF;
CS = 0;
__delay_us(10);
SPI1BUF = add;
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
__delay_us(1);
i = SPI1BUF;
SPI1BUF = 0x00;
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
__delay_us(1);
i = SPI1BUF;
CS = 1;
//PORTGbits.RG1 = 1;
return i;
}
unsigned int Write8(unsigned int add, unsigned int val)
{
int i;
add <<= 1;
add |= 0x01; // read
add <<= 1;
add |= 0x00;
i = SPI1BUF;
CS = 0;
__delay_us(10);
SPI1BUF = add;
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
i = SPI1BUF;
SPI1BUF = val;
while(SPI1STATbits.SPITBF) ;
while(! SPI1STATbits.SPIRBF) ;
CS = 1;
i = SPI1BUF;
return i;
}
int main()
{ UARTinit();
scp1000_init();//initialize SPI
while(1)
{
printf ("Temp: %f Height: %f Pressure: %f [Status %u]\n\r", scp1000_get_temperature(),scp1000_get_height(),scp1000_get_pressure(),scp1000_get_status());
__delay_ms(500);
}
return 0;
}