scp1000 and dspic30f4013

Here is the place for all your dsPic or autopilot questions.

Moderator: lukasz

scp1000 and dspic30f4013

Postby thabo » Tue Sep 28, 2010 4:15 pm

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;


}
thabo
 
Posts: 6
Joined: Tue Sep 28, 2010 4:10 pm

Re: scp1000 and dspic30f4013

Postby Tom » Tue Sep 28, 2010 4:30 pm

Hi Thabo,

Unfortunately I have no time to debug you code.
You are welcome to use the gluonpilot code. You can download the source code from the SVN server and open the scp1000 example project: http://code.google.com/p/gluonpilot/sou ... 000_test.c

The hardware schematic can be found here: http://gluonpilot.com/wiki/Hardware_v0.1

Tom
User avatar
Tom
Site Admin
 
Posts: 1016
Joined: Fri Nov 13, 2009 6:27 pm
Location: Belgium

Re: scp1000 and dspic30f4013

Postby thabo » Tue Sep 28, 2010 4:37 pm

Hi Tom,

Thanks for speedy response. I will look at the hardware schematic and compare notes.

Kind Regards

Thabo
thabo
 
Posts: 6
Joined: Tue Sep 28, 2010 4:10 pm

Re: scp1000 and dspic30f4013

Postby thabo » Thu Sep 30, 2010 9:56 am

Hi Tom

I followed your schematic and implemented the code. All i get on realterm is "Temp" repeatedly,any suggestions?Also on building the project an error about not declaring a heap popped upon.After researching I declared a heap of 512bytes,did you perhaps encounter a similar problem?

Kind Regards

Thabo
thabo
 
Posts: 6
Joined: Tue Sep 28, 2010 4:10 pm

Re: scp1000 and dspic30f4013

Postby Tom » Thu Sep 30, 2010 7:57 pm

Yes, the heap is needed when you use FreeRTOS. Task stack space is allocated on the heap.
It is also needed when you use printf.
User avatar
Tom
Site Admin
 
Posts: 1016
Joined: Fri Nov 13, 2009 6:27 pm
Location: Belgium

Re: scp1000 and dspic30f4013

Postby thabo » Fri Oct 01, 2010 10:15 am

Hello,

How do you determine the correct size of the heap,the examples i found made no sense?

Regards

Thabo
thabo
 
Posts: 6
Joined: Tue Sep 28, 2010 4:10 pm

Re: scp1000 and dspic30f4013

Postby Tom » Fri Oct 01, 2010 6:54 pm

Depends which part needs the heap :-)

For FreeRTOS you need to look at how much stack you assign to every thread (see FreeRTOS website).

For printf I'm not sure... probably depends on the number of arguments you put in it.
User avatar
Tom
Site Admin
 
Posts: 1016
Joined: Fri Nov 13, 2009 6:27 pm
Location: Belgium

Re: scp1000 and dspic30f4013

Postby thabo » Sun Oct 03, 2010 3:42 pm

Ok,once again thanks a lot.I will look into that. I am a newbie at programming and the microchip world so if my questions seem 'strange' at times please bear with me.
thabo
 
Posts: 6
Joined: Tue Sep 28, 2010 4:10 pm

Re: scp1000 and dspic30f4013

Postby Mitch » Sun Oct 03, 2010 3:56 pm

For printf just assign 128 or 256. This is adequate for sending out basic debugging stuff. If it overflows you'll know because it will start doing weird things
User avatar
Mitch
 
Posts: 118
Joined: Sat Dec 05, 2009 1:59 pm
Location: Florida, USA

Re: scp1000 and dspic30f4013

Postby thabo » Mon Oct 11, 2010 1:47 pm

hi

had actually set it to 512!will reduce it though to 256,will this make a significant change>
thabo
 
Posts: 6
Joined: Tue Sep 28, 2010 4:10 pm


Return to Firmware

Who is online

Users browsing this forum: No registered users and 4 guests

cron